Back to index

cell-binutils  2.17cvs20070401
elf64-ppc.c
Go to the documentation of this file.
00001 /* PowerPC64-specific support for 64-bit ELF.
00002    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
00003    Free Software Foundation, Inc.
00004    Written by Linus Nordberg, Swox AB <info@swox.com>,
00005    based on elf32-ppc.c by Ian Lance Taylor.
00006    Largely rewritten by Alan Modra <amodra@bigpond.net.au>
00007 
00008    This file is part of BFD, the Binary File Descriptor library.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 2 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License along
00021    with this program; if not, write to the Free Software Foundation, Inc.,
00022    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00023 
00024 /* The 64-bit PowerPC ELF ABI may be found at
00025    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
00026    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
00027 
00028 #include <stdarg.h>
00029 #include "bfd.h"
00030 #include "sysdep.h"
00031 #include "bfdlink.h"
00032 #include "libbfd.h"
00033 #include "elf-bfd.h"
00034 #include "elf/ppc64.h"
00035 #include "elf64-ppc.h"
00036 
00037 static bfd_reloc_status_type ppc64_elf_ha_reloc
00038   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00039 static bfd_reloc_status_type ppc64_elf_branch_reloc
00040   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00041 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
00042   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00043 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
00044   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00045 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
00046   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00047 static bfd_reloc_status_type ppc64_elf_toc_reloc
00048   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00049 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
00050   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00051 static bfd_reloc_status_type ppc64_elf_toc64_reloc
00052   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00053 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
00054   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00055 static bfd_vma opd_entry_value
00056   (asection *, bfd_vma, asection **, bfd_vma *);
00057 
00058 #define TARGET_LITTLE_SYM   bfd_elf64_powerpcle_vec
00059 #define TARGET_LITTLE_NAME  "elf64-powerpcle"
00060 #define TARGET_BIG_SYM             bfd_elf64_powerpc_vec
00061 #define TARGET_BIG_NAME            "elf64-powerpc"
00062 #define ELF_ARCH            bfd_arch_powerpc
00063 #define ELF_MACHINE_CODE    EM_PPC64
00064 #define ELF_MAXPAGESIZE            0x10000
00065 #define ELF_COMMONPAGESIZE  0x1000
00066 #define elf_info_to_howto   ppc64_elf_info_to_howto
00067 
00068 #define elf_backend_want_got_sym 0
00069 #define elf_backend_want_plt_sym 0
00070 #define elf_backend_plt_alignment 3
00071 #define elf_backend_plt_not_loaded 1
00072 #define elf_backend_got_header_size 8
00073 #define elf_backend_can_gc_sections 1
00074 #define elf_backend_can_refcount 1
00075 #define elf_backend_rela_normal 1
00076 #define elf_backend_default_execstack 0
00077 
00078 #define bfd_elf64_mkobject               ppc64_elf_mkobject
00079 #define bfd_elf64_bfd_reloc_type_lookup         ppc64_elf_reloc_type_lookup
00080 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
00081 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
00082 #define bfd_elf64_new_section_hook       ppc64_elf_new_section_hook
00083 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
00084 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
00085 #define bfd_elf64_get_synthetic_symtab          ppc64_elf_get_synthetic_symtab
00086 
00087 #define elf_backend_object_p                    ppc64_elf_object_p
00088 #define elf_backend_grok_prstatus        ppc64_elf_grok_prstatus
00089 #define elf_backend_grok_psinfo                 ppc64_elf_grok_psinfo
00090 #define elf_backend_write_core_note             ppc64_elf_write_core_note
00091 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
00092 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
00093 #define elf_backend_add_symbol_hook             ppc64_elf_add_symbol_hook
00094 #define elf_backend_check_directives            ppc64_elf_check_directives
00095 #define elf_backend_as_needed_cleanup           ppc64_elf_as_needed_cleanup
00096 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
00097 #define elf_backend_check_relocs         ppc64_elf_check_relocs
00098 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
00099 #define elf_backend_gc_mark_hook         ppc64_elf_gc_mark_hook
00100 #define elf_backend_gc_sweep_hook        ppc64_elf_gc_sweep_hook
00101 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
00102 #define elf_backend_hide_symbol                 ppc64_elf_hide_symbol
00103 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
00104 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
00105 #define elf_backend_init_index_section          _bfd_elf_init_2_index_sections
00106 #define elf_backend_action_discarded            ppc64_elf_action_discarded
00107 #define elf_backend_relocate_section            ppc64_elf_relocate_section
00108 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
00109 #define elf_backend_reloc_type_class            ppc64_elf_reloc_type_class
00110 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
00111 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
00112 #define elf_backend_special_sections            ppc64_elf_special_sections
00113 
00114 /* The name of the dynamic interpreter.  This is put in the .interp
00115    section.  */
00116 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
00117 
00118 /* The size in bytes of an entry in the procedure linkage table.  */
00119 #define PLT_ENTRY_SIZE 24
00120 
00121 /* The initial size of the plt reserved for the dynamic linker.  */
00122 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
00123 
00124 /* TOC base pointers offset from start of TOC.  */
00125 #define TOC_BASE_OFF 0x8000
00126 
00127 /* Offset of tp and dtp pointers from start of TLS block.  */
00128 #define TP_OFFSET    0x7000
00129 #define DTP_OFFSET   0x8000
00130 
00131 /* .plt call stub instructions.  The normal stub is like this, but
00132    sometimes the .plt entry crosses a 64k boundary and we need to
00133    insert an addis to adjust r12.  */
00134 #define PLT_CALL_STUB_SIZE (7*4)
00135 #define ADDIS_R12_R2 0x3d820000    /* addis %r12,%r2,xxx@ha     */
00136 #define STD_R2_40R1  0xf8410028    /* std  %r2,40(%r1)       */
00137 #define LD_R11_0R12  0xe96c0000    /* ld   %r11,xxx+0@l(%r12)  */
00138 #define LD_R2_0R12   0xe84c0000    /* ld   %r2,xxx+8@l(%r12)   */
00139 #define MTCTR_R11    0x7d6903a6    /* mtctr %r11             */
00140                                    /* ld   %r11,xxx+16@l(%r12) */
00141 #define BCTR         0x4e800420    /* bctr                          */
00142 
00143 
00144 #define ADDIS_R12_R12       0x3d8c0000    /* addis %r12,%r12,off@ha  */
00145 #define ADDIS_R2_R2  0x3c420000    /* addis %r2,%r2,off@ha  */
00146 #define ADDI_R2_R2   0x38420000    /* addi  %r2,%r2,off@l   */
00147 
00148 #define LD_R2_40R1   0xe8410028    /* ld    %r2,40(%r1)     */
00149 
00150 /* glink call stub instructions.  We enter with the index in R0.  */
00151 #define GLINK_CALL_STUB_SIZE (16*4)
00152                                    /* 0:                       */
00153                                    /*  .quad plt0-1f           */
00154                                    /* __glink:                 */
00155 #define MFLR_R12     0x7d8802a6    /*  mflr %12                */
00156 #define BCL_20_31    0x429f0005    /*  bcl 20,31,1f            */
00157                                    /* 1:                       */
00158 #define MFLR_R11     0x7d6802a6    /*  mflr %11                */
00159 #define LD_R2_M16R11 0xe84bfff0    /*  ld %2,(0b-1b)(%11)             */
00160 #define MTLR_R12     0x7d8803a6    /*  mtlr %12                */
00161 #define ADD_R12_R2_R11      0x7d825a14    /*  add %12,%2,%11          */
00162                                    /*  ld %11,0(%12)           */
00163                                    /*  ld %2,8(%12)            */
00164                                    /*  mtctr %11               */
00165                                    /*  ld %11,16(%12)          */
00166                                    /*  bctr                    */
00167 
00168 /* Pad with this.  */
00169 #define NOP          0x60000000
00170 
00171 /* Some other nops.  */
00172 #define CROR_151515  0x4def7b82
00173 #define CROR_313131  0x4ffffb82
00174 
00175 /* .glink entries for the first 32k functions are two instructions.  */
00176 #define LI_R0_0             0x38000000    /* li    %r0,0              */
00177 #define B_DOT        0x48000000    /* b     .           */
00178 
00179 /* After that, we need two instructions to load the index, followed by
00180    a branch.  */
00181 #define LIS_R0_0     0x3c000000    /* lis   %r0,0              */
00182 #define ORI_R0_R0_0  0x60000000    /* ori  %r0,%r0,0    */
00183 
00184 /* Instructions used by the save and restore reg functions.  */
00185 #define STD_R0_0R1   0xf8010000    /* std   %r0,0(%r1)  */
00186 #define STD_R0_0R12  0xf80c0000    /* std   %r0,0(%r12) */
00187 #define LD_R0_0R1    0xe8010000    /* ld    %r0,0(%r1)  */
00188 #define LD_R0_0R12   0xe80c0000    /* ld    %r0,0(%r12) */
00189 #define STFD_FR0_0R1 0xd8010000    /* stfd  %fr0,0(%r1) */
00190 #define LFD_FR0_0R1  0xc8010000    /* lfd   %fr0,0(%r1) */
00191 #define LI_R12_0     0x39800000    /* li    %r12,0             */
00192 #define STVX_VR0_R12_R0     0x7c0c01ce    /* stvx  %v0,%r12,%r0       */
00193 #define LVX_VR0_R12_R0      0x7c0c00ce    /* lvx   %v0,%r12,%r0       */
00194 #define MTLR_R0             0x7c0803a6    /* mtlr  %r0         */
00195 #define BLR          0x4e800020    /* blr               */
00196 
00197 /* Since .opd is an array of descriptors and each entry will end up
00198    with identical R_PPC64_RELATIVE relocs, there is really no need to
00199    propagate .opd relocs;  The dynamic linker should be taught to
00200    relocate .opd without reloc entries.  */
00201 #ifndef NO_OPD_RELOCS
00202 #define NO_OPD_RELOCS 0
00203 #endif
00204 
00205 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
00206 
00207 /* Relocation HOWTO's.  */
00208 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
00209 
00210 static reloc_howto_type ppc64_elf_howto_raw[] = {
00211   /* This reloc does nothing.  */
00212   HOWTO (R_PPC64_NONE,             /* type */
00213         0,                  /* rightshift */
00214         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00215         32,                 /* bitsize */
00216         FALSE,                     /* pc_relative */
00217         0,                  /* bitpos */
00218         complain_overflow_dont, /* complain_on_overflow */
00219         bfd_elf_generic_reloc,     /* special_function */
00220         "R_PPC64_NONE",     /* name */
00221         FALSE,                     /* partial_inplace */
00222         0,                  /* src_mask */
00223         0,                  /* dst_mask */
00224         FALSE),             /* pcrel_offset */
00225 
00226   /* A standard 32 bit relocation.  */
00227   HOWTO (R_PPC64_ADDR32,    /* type */
00228         0,                  /* rightshift */
00229         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00230         32,                 /* bitsize */
00231         FALSE,                     /* pc_relative */
00232         0,                  /* bitpos */
00233         complain_overflow_bitfield, /* complain_on_overflow */
00234         bfd_elf_generic_reloc,     /* special_function */
00235         "R_PPC64_ADDR32",   /* name */
00236         FALSE,                     /* partial_inplace */
00237         0,                  /* src_mask */
00238         0xffffffff,         /* dst_mask */
00239         FALSE),             /* pcrel_offset */
00240 
00241   /* An absolute 26 bit branch; the lower two bits must be zero.
00242      FIXME: we don't check that, we just clear them.  */
00243   HOWTO (R_PPC64_ADDR24,    /* type */
00244         0,                  /* rightshift */
00245         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00246         26,                 /* bitsize */
00247         FALSE,                     /* pc_relative */
00248         0,                  /* bitpos */
00249         complain_overflow_bitfield, /* complain_on_overflow */
00250         bfd_elf_generic_reloc,     /* special_function */
00251         "R_PPC64_ADDR24",   /* name */
00252         FALSE,                     /* partial_inplace */
00253         0,                  /* src_mask */
00254         0x03fffffc,         /* dst_mask */
00255         FALSE),             /* pcrel_offset */
00256 
00257   /* A standard 16 bit relocation.  */
00258   HOWTO (R_PPC64_ADDR16,    /* type */
00259         0,                  /* rightshift */
00260         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00261         16,                 /* bitsize */
00262         FALSE,                     /* pc_relative */
00263         0,                  /* bitpos */
00264         complain_overflow_bitfield, /* complain_on_overflow */
00265         bfd_elf_generic_reloc,     /* special_function */
00266         "R_PPC64_ADDR16",   /* name */
00267         FALSE,                     /* partial_inplace */
00268         0,                  /* src_mask */
00269         0xffff,             /* dst_mask */
00270         FALSE),             /* pcrel_offset */
00271 
00272   /* A 16 bit relocation without overflow.  */
00273   HOWTO (R_PPC64_ADDR16_LO, /* type */
00274         0,                  /* rightshift */
00275         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00276         16,                 /* bitsize */
00277         FALSE,                     /* pc_relative */
00278         0,                  /* bitpos */
00279         complain_overflow_dont,/* complain_on_overflow */
00280         bfd_elf_generic_reloc,     /* special_function */
00281         "R_PPC64_ADDR16_LO",       /* name */
00282         FALSE,                     /* partial_inplace */
00283         0,                  /* src_mask */
00284         0xffff,             /* dst_mask */
00285         FALSE),             /* pcrel_offset */
00286 
00287   /* Bits 16-31 of an address.  */
00288   HOWTO (R_PPC64_ADDR16_HI, /* type */
00289         16,                 /* rightshift */
00290         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00291         16,                 /* bitsize */
00292         FALSE,                     /* pc_relative */
00293         0,                  /* bitpos */
00294         complain_overflow_dont, /* complain_on_overflow */
00295         bfd_elf_generic_reloc,     /* special_function */
00296         "R_PPC64_ADDR16_HI",       /* name */
00297         FALSE,                     /* partial_inplace */
00298         0,                  /* src_mask */
00299         0xffff,             /* dst_mask */
00300         FALSE),             /* pcrel_offset */
00301 
00302   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
00303      bits, treated as a signed number, is negative.  */
00304   HOWTO (R_PPC64_ADDR16_HA, /* type */
00305         16,                 /* rightshift */
00306         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00307         16,                 /* bitsize */
00308         FALSE,                     /* pc_relative */
00309         0,                  /* bitpos */
00310         complain_overflow_dont, /* complain_on_overflow */
00311         ppc64_elf_ha_reloc, /* special_function */
00312         "R_PPC64_ADDR16_HA",       /* name */
00313         FALSE,                     /* partial_inplace */
00314         0,                  /* src_mask */
00315         0xffff,             /* dst_mask */
00316         FALSE),             /* pcrel_offset */
00317 
00318   /* An absolute 16 bit branch; the lower two bits must be zero.
00319      FIXME: we don't check that, we just clear them.  */
00320   HOWTO (R_PPC64_ADDR14,    /* type */
00321         0,                  /* rightshift */
00322         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00323         16,                 /* bitsize */
00324         FALSE,                     /* pc_relative */
00325         0,                  /* bitpos */
00326         complain_overflow_bitfield, /* complain_on_overflow */
00327         ppc64_elf_branch_reloc, /* special_function */
00328         "R_PPC64_ADDR14",   /* name */
00329         FALSE,                     /* partial_inplace */
00330         0,                  /* src_mask */
00331         0x0000fffc,         /* dst_mask */
00332         FALSE),             /* pcrel_offset */
00333 
00334   /* An absolute 16 bit branch, for which bit 10 should be set to
00335      indicate that the branch is expected to be taken.  The lower two
00336      bits must be zero.  */
00337   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
00338         0,                  /* rightshift */
00339         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00340         16,                 /* bitsize */
00341         FALSE,                     /* pc_relative */
00342         0,                  /* bitpos */
00343         complain_overflow_bitfield, /* complain_on_overflow */
00344         ppc64_elf_brtaken_reloc, /* special_function */
00345         "R_PPC64_ADDR14_BRTAKEN",/* name */
00346         FALSE,                     /* partial_inplace */
00347         0,                  /* src_mask */
00348         0x0000fffc,         /* dst_mask */
00349         FALSE),             /* pcrel_offset */
00350 
00351   /* An absolute 16 bit branch, for which bit 10 should be set to
00352      indicate that the branch is not expected to be taken.  The lower
00353      two bits must be zero.  */
00354   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
00355         0,                  /* rightshift */
00356         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00357         16,                 /* bitsize */
00358         FALSE,                     /* pc_relative */
00359         0,                  /* bitpos */
00360         complain_overflow_bitfield, /* complain_on_overflow */
00361         ppc64_elf_brtaken_reloc, /* special_function */
00362         "R_PPC64_ADDR14_BRNTAKEN",/* name */
00363         FALSE,                     /* partial_inplace */
00364         0,                  /* src_mask */
00365         0x0000fffc,         /* dst_mask */
00366         FALSE),             /* pcrel_offset */
00367 
00368   /* A relative 26 bit branch; the lower two bits must be zero.  */
00369   HOWTO (R_PPC64_REL24,            /* type */
00370         0,                  /* rightshift */
00371         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00372         26,                 /* bitsize */
00373         TRUE,               /* pc_relative */
00374         0,                  /* bitpos */
00375         complain_overflow_signed, /* complain_on_overflow */
00376         ppc64_elf_branch_reloc, /* special_function */
00377         "R_PPC64_REL24",    /* name */
00378         FALSE,                     /* partial_inplace */
00379         0,                  /* src_mask */
00380         0x03fffffc,         /* dst_mask */
00381         TRUE),                     /* pcrel_offset */
00382 
00383   /* A relative 16 bit branch; the lower two bits must be zero.  */
00384   HOWTO (R_PPC64_REL14,            /* type */
00385         0,                  /* rightshift */
00386         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00387         16,                 /* bitsize */
00388         TRUE,               /* pc_relative */
00389         0,                  /* bitpos */
00390         complain_overflow_signed, /* complain_on_overflow */
00391         ppc64_elf_branch_reloc, /* special_function */
00392         "R_PPC64_REL14",    /* name */
00393         FALSE,                     /* partial_inplace */
00394         0,                  /* src_mask */
00395         0x0000fffc,         /* dst_mask */
00396         TRUE),                     /* pcrel_offset */
00397 
00398   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
00399      the branch is expected to be taken.  The lower two bits must be
00400      zero.  */
00401   HOWTO (R_PPC64_REL14_BRTAKEN,    /* type */
00402         0,                  /* rightshift */
00403         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00404         16,                 /* bitsize */
00405         TRUE,               /* pc_relative */
00406         0,                  /* bitpos */
00407         complain_overflow_signed, /* complain_on_overflow */
00408         ppc64_elf_brtaken_reloc, /* special_function */
00409         "R_PPC64_REL14_BRTAKEN", /* name */
00410         FALSE,                     /* partial_inplace */
00411         0,                  /* src_mask */
00412         0x0000fffc,         /* dst_mask */
00413         TRUE),                     /* pcrel_offset */
00414 
00415   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
00416      the branch is not expected to be taken.  The lower two bits must
00417      be zero.  */
00418   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
00419         0,                  /* rightshift */
00420         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00421         16,                 /* bitsize */
00422         TRUE,               /* pc_relative */
00423         0,                  /* bitpos */
00424         complain_overflow_signed, /* complain_on_overflow */
00425         ppc64_elf_brtaken_reloc, /* special_function */
00426         "R_PPC64_REL14_BRNTAKEN",/* name */
00427         FALSE,                     /* partial_inplace */
00428         0,                  /* src_mask */
00429         0x0000fffc,         /* dst_mask */
00430         TRUE),                     /* pcrel_offset */
00431 
00432   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
00433      symbol.  */
00434   HOWTO (R_PPC64_GOT16,            /* type */
00435         0,                  /* rightshift */
00436         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00437         16,                 /* bitsize */
00438         FALSE,                     /* pc_relative */
00439         0,                  /* bitpos */
00440         complain_overflow_signed, /* complain_on_overflow */
00441         ppc64_elf_unhandled_reloc, /* special_function */
00442         "R_PPC64_GOT16",    /* name */
00443         FALSE,                     /* partial_inplace */
00444         0,                  /* src_mask */
00445         0xffff,             /* dst_mask */
00446         FALSE),             /* pcrel_offset */
00447 
00448   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
00449      the symbol.  */
00450   HOWTO (R_PPC64_GOT16_LO,  /* type */
00451         0,                  /* rightshift */
00452         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00453         16,                 /* bitsize */
00454         FALSE,                     /* pc_relative */
00455         0,                  /* bitpos */
00456         complain_overflow_dont, /* complain_on_overflow */
00457         ppc64_elf_unhandled_reloc, /* special_function */
00458         "R_PPC64_GOT16_LO", /* name */
00459         FALSE,                     /* partial_inplace */
00460         0,                  /* src_mask */
00461         0xffff,             /* dst_mask */
00462         FALSE),             /* pcrel_offset */
00463 
00464   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
00465      the symbol.  */
00466   HOWTO (R_PPC64_GOT16_HI,  /* type */
00467         16,                 /* rightshift */
00468         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00469         16,                 /* bitsize */
00470         FALSE,                     /* pc_relative */
00471         0,                  /* bitpos */
00472         complain_overflow_dont,/* complain_on_overflow */
00473         ppc64_elf_unhandled_reloc, /* special_function */
00474         "R_PPC64_GOT16_HI", /* name */
00475         FALSE,                     /* partial_inplace */
00476         0,                  /* src_mask */
00477         0xffff,             /* dst_mask */
00478         FALSE),             /* pcrel_offset */
00479 
00480   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
00481      the symbol.  */
00482   HOWTO (R_PPC64_GOT16_HA,  /* type */
00483         16,                 /* rightshift */
00484         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00485         16,                 /* bitsize */
00486         FALSE,                     /* pc_relative */
00487         0,                  /* bitpos */
00488         complain_overflow_dont,/* complain_on_overflow */
00489         ppc64_elf_unhandled_reloc, /* special_function */
00490         "R_PPC64_GOT16_HA", /* name */
00491         FALSE,                     /* partial_inplace */
00492         0,                  /* src_mask */
00493         0xffff,             /* dst_mask */
00494         FALSE),             /* pcrel_offset */
00495 
00496   /* This is used only by the dynamic linker.  The symbol should exist
00497      both in the object being run and in some shared library.  The
00498      dynamic linker copies the data addressed by the symbol from the
00499      shared library into the object, because the object being
00500      run has to have the data at some particular address.  */
00501   HOWTO (R_PPC64_COPY,             /* type */
00502         0,                  /* rightshift */
00503         0,                  /* this one is variable size */
00504         0,                  /* bitsize */
00505         FALSE,                     /* pc_relative */
00506         0,                  /* bitpos */
00507         complain_overflow_dont, /* complain_on_overflow */
00508         ppc64_elf_unhandled_reloc, /* special_function */
00509         "R_PPC64_COPY",     /* name */
00510         FALSE,                     /* partial_inplace */
00511         0,                  /* src_mask */
00512         0,                  /* dst_mask */
00513         FALSE),             /* pcrel_offset */
00514 
00515   /* Like R_PPC64_ADDR64, but used when setting global offset table
00516      entries.  */
00517   HOWTO (R_PPC64_GLOB_DAT,  /* type */
00518         0,                  /* rightshift */
00519         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00520         64,                 /* bitsize */
00521         FALSE,                     /* pc_relative */
00522         0,                  /* bitpos */
00523         complain_overflow_dont, /* complain_on_overflow */
00524         ppc64_elf_unhandled_reloc,  /* special_function */
00525         "R_PPC64_GLOB_DAT", /* name */
00526         FALSE,                     /* partial_inplace */
00527         0,                  /* src_mask */
00528         ONES (64),          /* dst_mask */
00529         FALSE),             /* pcrel_offset */
00530 
00531   /* Created by the link editor.  Marks a procedure linkage table
00532      entry for a symbol.  */
00533   HOWTO (R_PPC64_JMP_SLOT,  /* type */
00534         0,                  /* rightshift */
00535         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00536         0,                  /* bitsize */
00537         FALSE,                     /* pc_relative */
00538         0,                  /* bitpos */
00539         complain_overflow_dont, /* complain_on_overflow */
00540         ppc64_elf_unhandled_reloc, /* special_function */
00541         "R_PPC64_JMP_SLOT", /* name */
00542         FALSE,                     /* partial_inplace */
00543         0,                  /* src_mask */
00544         0,                  /* dst_mask */
00545         FALSE),             /* pcrel_offset */
00546 
00547   /* Used only by the dynamic linker.  When the object is run, this
00548      doubleword64 is set to the load address of the object, plus the
00549      addend.  */
00550   HOWTO (R_PPC64_RELATIVE,  /* type */
00551         0,                  /* rightshift */
00552         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00553         64,                 /* bitsize */
00554         FALSE,                     /* pc_relative */
00555         0,                  /* bitpos */
00556         complain_overflow_dont, /* complain_on_overflow */
00557         bfd_elf_generic_reloc,     /* special_function */
00558         "R_PPC64_RELATIVE", /* name */
00559         FALSE,                     /* partial_inplace */
00560         0,                  /* src_mask */
00561         ONES (64),          /* dst_mask */
00562         FALSE),             /* pcrel_offset */
00563 
00564   /* Like R_PPC64_ADDR32, but may be unaligned.  */
00565   HOWTO (R_PPC64_UADDR32,   /* type */
00566         0,                  /* rightshift */
00567         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00568         32,                 /* bitsize */
00569         FALSE,                     /* pc_relative */
00570         0,                  /* bitpos */
00571         complain_overflow_bitfield, /* complain_on_overflow */
00572         bfd_elf_generic_reloc,     /* special_function */
00573         "R_PPC64_UADDR32",  /* name */
00574         FALSE,                     /* partial_inplace */
00575         0,                  /* src_mask */
00576         0xffffffff,         /* dst_mask */
00577         FALSE),             /* pcrel_offset */
00578 
00579   /* Like R_PPC64_ADDR16, but may be unaligned.  */
00580   HOWTO (R_PPC64_UADDR16,   /* type */
00581         0,                  /* rightshift */
00582         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00583         16,                 /* bitsize */
00584         FALSE,                     /* pc_relative */
00585         0,                  /* bitpos */
00586         complain_overflow_bitfield, /* complain_on_overflow */
00587         bfd_elf_generic_reloc,     /* special_function */
00588         "R_PPC64_UADDR16",  /* name */
00589         FALSE,                     /* partial_inplace */
00590         0,                  /* src_mask */
00591         0xffff,             /* dst_mask */
00592         FALSE),             /* pcrel_offset */
00593 
00594   /* 32-bit PC relative.  */
00595   HOWTO (R_PPC64_REL32,            /* type */
00596         0,                  /* rightshift */
00597         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00598         32,                 /* bitsize */
00599         TRUE,               /* pc_relative */
00600         0,                  /* bitpos */
00601         /* FIXME: Verify.  Was complain_overflow_bitfield.  */
00602         complain_overflow_signed, /* complain_on_overflow */
00603         bfd_elf_generic_reloc,     /* special_function */
00604         "R_PPC64_REL32",    /* name */
00605         FALSE,                     /* partial_inplace */
00606         0,                  /* src_mask */
00607         0xffffffff,         /* dst_mask */
00608         TRUE),                     /* pcrel_offset */
00609 
00610   /* 32-bit relocation to the symbol's procedure linkage table.  */
00611   HOWTO (R_PPC64_PLT32,            /* type */
00612         0,                  /* rightshift */
00613         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00614         32,                 /* bitsize */
00615         FALSE,                     /* pc_relative */
00616         0,                  /* bitpos */
00617         complain_overflow_bitfield, /* complain_on_overflow */
00618         ppc64_elf_unhandled_reloc, /* special_function */
00619         "R_PPC64_PLT32",    /* name */
00620         FALSE,                     /* partial_inplace */
00621         0,                  /* src_mask */
00622         0xffffffff,         /* dst_mask */
00623         FALSE),             /* pcrel_offset */
00624 
00625   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
00626      FIXME: R_PPC64_PLTREL32 not supported.  */
00627   HOWTO (R_PPC64_PLTREL32,  /* type */
00628         0,                  /* rightshift */
00629         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00630         32,                 /* bitsize */
00631         TRUE,               /* pc_relative */
00632         0,                  /* bitpos */
00633         complain_overflow_signed, /* complain_on_overflow */
00634         bfd_elf_generic_reloc,     /* special_function */
00635         "R_PPC64_PLTREL32", /* name */
00636         FALSE,                     /* partial_inplace */
00637         0,                  /* src_mask */
00638         0xffffffff,         /* dst_mask */
00639         TRUE),                     /* pcrel_offset */
00640 
00641   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
00642      the symbol.  */
00643   HOWTO (R_PPC64_PLT16_LO,  /* type */
00644         0,                  /* rightshift */
00645         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00646         16,                 /* bitsize */
00647         FALSE,                     /* pc_relative */
00648         0,                  /* bitpos */
00649         complain_overflow_dont, /* complain_on_overflow */
00650         ppc64_elf_unhandled_reloc, /* special_function */
00651         "R_PPC64_PLT16_LO", /* name */
00652         FALSE,                     /* partial_inplace */
00653         0,                  /* src_mask */
00654         0xffff,             /* dst_mask */
00655         FALSE),             /* pcrel_offset */
00656 
00657   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
00658      the symbol.  */
00659   HOWTO (R_PPC64_PLT16_HI,  /* type */
00660         16,                 /* rightshift */
00661         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00662         16,                 /* bitsize */
00663         FALSE,                     /* pc_relative */
00664         0,                  /* bitpos */
00665         complain_overflow_dont, /* complain_on_overflow */
00666         ppc64_elf_unhandled_reloc, /* special_function */
00667         "R_PPC64_PLT16_HI", /* name */
00668         FALSE,                     /* partial_inplace */
00669         0,                  /* src_mask */
00670         0xffff,             /* dst_mask */
00671         FALSE),             /* pcrel_offset */
00672 
00673   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
00674      the symbol.  */
00675   HOWTO (R_PPC64_PLT16_HA,  /* type */
00676         16,                 /* rightshift */
00677         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00678         16,                 /* bitsize */
00679         FALSE,                     /* pc_relative */
00680         0,                  /* bitpos */
00681         complain_overflow_dont, /* complain_on_overflow */
00682         ppc64_elf_unhandled_reloc, /* special_function */
00683         "R_PPC64_PLT16_HA", /* name */
00684         FALSE,                     /* partial_inplace */
00685         0,                  /* src_mask */
00686         0xffff,             /* dst_mask */
00687         FALSE),             /* pcrel_offset */
00688 
00689   /* 16-bit section relative relocation.  */
00690   HOWTO (R_PPC64_SECTOFF,   /* type */
00691         0,                  /* rightshift */
00692         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00693         16,                 /* bitsize */
00694         FALSE,                     /* pc_relative */
00695         0,                  /* bitpos */
00696         complain_overflow_bitfield, /* complain_on_overflow */
00697         ppc64_elf_sectoff_reloc, /* special_function */
00698         "R_PPC64_SECTOFF",  /* name */
00699         FALSE,                     /* partial_inplace */
00700         0,                  /* src_mask */
00701         0xffff,             /* dst_mask */
00702         FALSE),             /* pcrel_offset */
00703 
00704   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
00705   HOWTO (R_PPC64_SECTOFF_LO,       /* type */
00706         0,                  /* rightshift */
00707         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00708         16,                 /* bitsize */
00709         FALSE,                     /* pc_relative */
00710         0,                  /* bitpos */
00711         complain_overflow_dont, /* complain_on_overflow */
00712         ppc64_elf_sectoff_reloc, /* special_function */
00713         "R_PPC64_SECTOFF_LO",      /* name */
00714         FALSE,                     /* partial_inplace */
00715         0,                  /* src_mask */
00716         0xffff,             /* dst_mask */
00717         FALSE),             /* pcrel_offset */
00718 
00719   /* 16-bit upper half section relative relocation.  */
00720   HOWTO (R_PPC64_SECTOFF_HI,       /* type */
00721         16,                 /* rightshift */
00722         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00723         16,                 /* bitsize */
00724         FALSE,                     /* pc_relative */
00725         0,                  /* bitpos */
00726         complain_overflow_dont, /* complain_on_overflow */
00727         ppc64_elf_sectoff_reloc, /* special_function */
00728         "R_PPC64_SECTOFF_HI",      /* name */
00729         FALSE,                     /* partial_inplace */
00730         0,                  /* src_mask */
00731         0xffff,             /* dst_mask */
00732         FALSE),             /* pcrel_offset */
00733 
00734   /* 16-bit upper half adjusted section relative relocation.  */
00735   HOWTO (R_PPC64_SECTOFF_HA,       /* type */
00736         16,                 /* rightshift */
00737         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00738         16,                 /* bitsize */
00739         FALSE,                     /* pc_relative */
00740         0,                  /* bitpos */
00741         complain_overflow_dont, /* complain_on_overflow */
00742         ppc64_elf_sectoff_ha_reloc, /* special_function */
00743         "R_PPC64_SECTOFF_HA",      /* name */
00744         FALSE,                     /* partial_inplace */
00745         0,                  /* src_mask */
00746         0xffff,             /* dst_mask */
00747         FALSE),             /* pcrel_offset */
00748 
00749   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
00750   HOWTO (R_PPC64_REL30,            /* type */
00751         2,                  /* rightshift */
00752         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00753         30,                 /* bitsize */
00754         TRUE,               /* pc_relative */
00755         0,                  /* bitpos */
00756         complain_overflow_dont, /* complain_on_overflow */
00757         bfd_elf_generic_reloc, /* special_function */
00758         "R_PPC64_REL30",    /* name */
00759         FALSE,                     /* partial_inplace */
00760         0,                  /* src_mask */
00761         0xfffffffc,         /* dst_mask */
00762         TRUE),                     /* pcrel_offset */
00763 
00764   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
00765 
00766   /* A standard 64-bit relocation.  */
00767   HOWTO (R_PPC64_ADDR64,    /* type */
00768         0,                  /* rightshift */
00769         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00770         64,                 /* bitsize */
00771         FALSE,                     /* pc_relative */
00772         0,                  /* bitpos */
00773         complain_overflow_dont, /* complain_on_overflow */
00774         bfd_elf_generic_reloc,     /* special_function */
00775         "R_PPC64_ADDR64",   /* name */
00776         FALSE,                     /* partial_inplace */
00777         0,                  /* src_mask */
00778         ONES (64),          /* dst_mask */
00779         FALSE),             /* pcrel_offset */
00780 
00781   /* The bits 32-47 of an address.  */
00782   HOWTO (R_PPC64_ADDR16_HIGHER,    /* type */
00783         32,                 /* rightshift */
00784         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00785         16,                 /* bitsize */
00786         FALSE,                     /* pc_relative */
00787         0,                  /* bitpos */
00788         complain_overflow_dont, /* complain_on_overflow */
00789         bfd_elf_generic_reloc,     /* special_function */
00790         "R_PPC64_ADDR16_HIGHER", /* name */
00791         FALSE,                     /* partial_inplace */
00792         0,                  /* src_mask */
00793         0xffff,             /* dst_mask */
00794         FALSE),             /* pcrel_offset */
00795 
00796   /* The bits 32-47 of an address, plus 1 if the contents of the low
00797      16 bits, treated as a signed number, is negative.  */
00798   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
00799         32,                 /* rightshift */
00800         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00801         16,                 /* bitsize */
00802         FALSE,                     /* pc_relative */
00803         0,                  /* bitpos */
00804         complain_overflow_dont, /* complain_on_overflow */
00805         ppc64_elf_ha_reloc, /* special_function */
00806         "R_PPC64_ADDR16_HIGHERA", /* name */
00807         FALSE,                     /* partial_inplace */
00808         0,                  /* src_mask */
00809         0xffff,             /* dst_mask */
00810         FALSE),             /* pcrel_offset */
00811 
00812   /* The bits 48-63 of an address.  */
00813   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
00814         48,                 /* rightshift */
00815         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00816         16,                 /* bitsize */
00817         FALSE,                     /* pc_relative */
00818         0,                  /* bitpos */
00819         complain_overflow_dont, /* complain_on_overflow */
00820         bfd_elf_generic_reloc,     /* special_function */
00821         "R_PPC64_ADDR16_HIGHEST", /* name */
00822         FALSE,                     /* partial_inplace */
00823         0,                  /* src_mask */
00824         0xffff,             /* dst_mask */
00825         FALSE),             /* pcrel_offset */
00826 
00827   /* The bits 48-63 of an address, plus 1 if the contents of the low
00828      16 bits, treated as a signed number, is negative.  */
00829   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
00830         48,                 /* rightshift */
00831         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00832         16,                 /* bitsize */
00833         FALSE,                     /* pc_relative */
00834         0,                  /* bitpos */
00835         complain_overflow_dont, /* complain_on_overflow */
00836         ppc64_elf_ha_reloc, /* special_function */
00837         "R_PPC64_ADDR16_HIGHESTA", /* name */
00838         FALSE,                     /* partial_inplace */
00839         0,                  /* src_mask */
00840         0xffff,             /* dst_mask */
00841         FALSE),             /* pcrel_offset */
00842 
00843   /* Like ADDR64, but may be unaligned.  */
00844   HOWTO (R_PPC64_UADDR64,   /* type */
00845         0,                  /* rightshift */
00846         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00847         64,                 /* bitsize */
00848         FALSE,                     /* pc_relative */
00849         0,                  /* bitpos */
00850         complain_overflow_dont, /* complain_on_overflow */
00851         bfd_elf_generic_reloc,     /* special_function */
00852         "R_PPC64_UADDR64",  /* name */
00853         FALSE,                     /* partial_inplace */
00854         0,                  /* src_mask */
00855         ONES (64),          /* dst_mask */
00856         FALSE),             /* pcrel_offset */
00857 
00858   /* 64-bit relative relocation.  */
00859   HOWTO (R_PPC64_REL64,            /* type */
00860         0,                  /* rightshift */
00861         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00862         64,                 /* bitsize */
00863         TRUE,               /* pc_relative */
00864         0,                  /* bitpos */
00865         complain_overflow_dont, /* complain_on_overflow */
00866         bfd_elf_generic_reloc,     /* special_function */
00867         "R_PPC64_REL64",    /* name */
00868         FALSE,                     /* partial_inplace */
00869         0,                  /* src_mask */
00870         ONES (64),          /* dst_mask */
00871         TRUE),                     /* pcrel_offset */
00872 
00873   /* 64-bit relocation to the symbol's procedure linkage table.  */
00874   HOWTO (R_PPC64_PLT64,            /* type */
00875         0,                  /* rightshift */
00876         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00877         64,                 /* bitsize */
00878         FALSE,                     /* pc_relative */
00879         0,                  /* bitpos */
00880         complain_overflow_dont, /* complain_on_overflow */
00881         ppc64_elf_unhandled_reloc, /* special_function */
00882         "R_PPC64_PLT64",    /* name */
00883         FALSE,                     /* partial_inplace */
00884         0,                  /* src_mask */
00885         ONES (64),          /* dst_mask */
00886         FALSE),             /* pcrel_offset */
00887 
00888   /* 64-bit PC relative relocation to the symbol's procedure linkage
00889      table.  */
00890   /* FIXME: R_PPC64_PLTREL64 not supported.  */
00891   HOWTO (R_PPC64_PLTREL64,  /* type */
00892         0,                  /* rightshift */
00893         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00894         64,                 /* bitsize */
00895         TRUE,               /* pc_relative */
00896         0,                  /* bitpos */
00897         complain_overflow_dont, /* complain_on_overflow */
00898         ppc64_elf_unhandled_reloc, /* special_function */
00899         "R_PPC64_PLTREL64", /* name */
00900         FALSE,                     /* partial_inplace */
00901         0,                  /* src_mask */
00902         ONES (64),          /* dst_mask */
00903         TRUE),                     /* pcrel_offset */
00904 
00905   /* 16 bit TOC-relative relocation.  */
00906 
00907   /* R_PPC64_TOC16     47      half16*    S + A - .TOC.  */
00908   HOWTO (R_PPC64_TOC16,            /* type */
00909         0,                  /* rightshift */
00910         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00911         16,                 /* bitsize */
00912         FALSE,                     /* pc_relative */
00913         0,                  /* bitpos */
00914         complain_overflow_signed, /* complain_on_overflow */
00915         ppc64_elf_toc_reloc,       /* special_function */
00916         "R_PPC64_TOC16",    /* name */
00917         FALSE,                     /* partial_inplace */
00918         0,                  /* src_mask */
00919         0xffff,             /* dst_mask */
00920         FALSE),             /* pcrel_offset */
00921 
00922   /* 16 bit TOC-relative relocation without overflow.  */
00923 
00924   /* R_PPC64_TOC16_LO         48      half16      #lo (S + A - .TOC.)  */
00925   HOWTO (R_PPC64_TOC16_LO,  /* type */
00926         0,                  /* rightshift */
00927         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00928         16,                 /* bitsize */
00929         FALSE,                     /* pc_relative */
00930         0,                  /* bitpos */
00931         complain_overflow_dont, /* complain_on_overflow */
00932         ppc64_elf_toc_reloc,       /* special_function */
00933         "R_PPC64_TOC16_LO", /* name */
00934         FALSE,                     /* partial_inplace */
00935         0,                  /* src_mask */
00936         0xffff,             /* dst_mask */
00937         FALSE),             /* pcrel_offset */
00938 
00939   /* 16 bit TOC-relative relocation, high 16 bits.  */
00940 
00941   /* R_PPC64_TOC16_HI         49      half16      #hi (S + A - .TOC.)  */
00942   HOWTO (R_PPC64_TOC16_HI,  /* type */
00943         16,                 /* rightshift */
00944         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00945         16,                 /* bitsize */
00946         FALSE,                     /* pc_relative */
00947         0,                  /* bitpos */
00948         complain_overflow_dont, /* complain_on_overflow */
00949         ppc64_elf_toc_reloc,       /* special_function */
00950         "R_PPC64_TOC16_HI", /* name */
00951         FALSE,                     /* partial_inplace */
00952         0,                  /* src_mask */
00953         0xffff,             /* dst_mask */
00954         FALSE),             /* pcrel_offset */
00955 
00956   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
00957      contents of the low 16 bits, treated as a signed number, is
00958      negative.  */
00959 
00960   /* R_PPC64_TOC16_HA         50      half16      #ha (S + A - .TOC.)  */
00961   HOWTO (R_PPC64_TOC16_HA,  /* type */
00962         16,                 /* rightshift */
00963         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00964         16,                 /* bitsize */
00965         FALSE,                     /* pc_relative */
00966         0,                  /* bitpos */
00967         complain_overflow_dont, /* complain_on_overflow */
00968         ppc64_elf_toc_ha_reloc, /* special_function */
00969         "R_PPC64_TOC16_HA", /* name */
00970         FALSE,                     /* partial_inplace */
00971         0,                  /* src_mask */
00972         0xffff,             /* dst_mask */
00973         FALSE),             /* pcrel_offset */
00974 
00975   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
00976 
00977   /* R_PPC64_TOC              51      doubleword64       .TOC.  */
00978   HOWTO (R_PPC64_TOC,              /* type */
00979         0,                  /* rightshift */
00980         4,                  /* size (0=byte, 1=short, 2=long, 4=64 bits) */
00981         64,                 /* bitsize */
00982         FALSE,                     /* pc_relative */
00983         0,                  /* bitpos */
00984         complain_overflow_bitfield, /* complain_on_overflow */
00985         ppc64_elf_toc64_reloc,     /* special_function */
00986         "R_PPC64_TOC",             /* name */
00987         FALSE,                     /* partial_inplace */
00988         0,                  /* src_mask */
00989         ONES (64),          /* dst_mask */
00990         FALSE),             /* pcrel_offset */
00991 
00992   /* Like R_PPC64_GOT16, but also informs the link editor that the
00993      value to relocate may (!) refer to a PLT entry which the link
00994      editor (a) may replace with the symbol value.  If the link editor
00995      is unable to fully resolve the symbol, it may (b) create a PLT
00996      entry and store the address to the new PLT entry in the GOT.
00997      This permits lazy resolution of function symbols at run time.
00998      The link editor may also skip all of this and just (c) emit a
00999      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
01000   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
01001     HOWTO (R_PPC64_PLTGOT16,       /* type */
01002         0,                  /* rightshift */
01003         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01004         16,                 /* bitsize */
01005         FALSE,                     /* pc_relative */
01006         0,                  /* bitpos */
01007         complain_overflow_signed, /* complain_on_overflow */
01008         ppc64_elf_unhandled_reloc, /* special_function */
01009         "R_PPC64_PLTGOT16", /* name */
01010         FALSE,                     /* partial_inplace */
01011         0,                  /* src_mask */
01012         0xffff,             /* dst_mask */
01013         FALSE),             /* pcrel_offset */
01014 
01015   /* Like R_PPC64_PLTGOT16, but without overflow.  */
01016   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
01017   HOWTO (R_PPC64_PLTGOT16_LO,      /* type */
01018         0,                  /* rightshift */
01019         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01020         16,                 /* bitsize */
01021         FALSE,                     /* pc_relative */
01022         0,                  /* bitpos */
01023         complain_overflow_dont, /* complain_on_overflow */
01024         ppc64_elf_unhandled_reloc, /* special_function */
01025         "R_PPC64_PLTGOT16_LO",     /* name */
01026         FALSE,                     /* partial_inplace */
01027         0,                  /* src_mask */
01028         0xffff,             /* dst_mask */
01029         FALSE),             /* pcrel_offset */
01030 
01031   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
01032   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
01033   HOWTO (R_PPC64_PLTGOT16_HI,      /* type */
01034         16,                 /* rightshift */
01035         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01036         16,                 /* bitsize */
01037         FALSE,                     /* pc_relative */
01038         0,                  /* bitpos */
01039         complain_overflow_dont, /* complain_on_overflow */
01040         ppc64_elf_unhandled_reloc, /* special_function */
01041         "R_PPC64_PLTGOT16_HI",     /* name */
01042         FALSE,                     /* partial_inplace */
01043         0,                  /* src_mask */
01044         0xffff,             /* dst_mask */
01045         FALSE),             /* pcrel_offset */
01046 
01047   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
01048      1 if the contents of the low 16 bits, treated as a signed number,
01049      is negative.  */
01050   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
01051   HOWTO (R_PPC64_PLTGOT16_HA,      /* type */
01052         16,                 /* rightshift */
01053         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01054         16,                 /* bitsize */
01055         FALSE,                     /* pc_relative */
01056         0,                  /* bitpos */
01057         complain_overflow_dont,/* complain_on_overflow */
01058         ppc64_elf_unhandled_reloc, /* special_function */
01059         "R_PPC64_PLTGOT16_HA",     /* name */
01060         FALSE,                     /* partial_inplace */
01061         0,                  /* src_mask */
01062         0xffff,             /* dst_mask */
01063         FALSE),             /* pcrel_offset */
01064 
01065   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
01066   HOWTO (R_PPC64_ADDR16_DS, /* type */
01067         0,                  /* rightshift */
01068         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01069         16,                 /* bitsize */
01070         FALSE,                     /* pc_relative */
01071         0,                  /* bitpos */
01072         complain_overflow_bitfield, /* complain_on_overflow */
01073         bfd_elf_generic_reloc,     /* special_function */
01074         "R_PPC64_ADDR16_DS",       /* name */
01075         FALSE,                     /* partial_inplace */
01076         0,                  /* src_mask */
01077         0xfffc,             /* dst_mask */
01078         FALSE),             /* pcrel_offset */
01079 
01080   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
01081   HOWTO (R_PPC64_ADDR16_LO_DS,     /* type */
01082         0,                  /* rightshift */
01083         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01084         16,                 /* bitsize */
01085         FALSE,                     /* pc_relative */
01086         0,                  /* bitpos */
01087         complain_overflow_dont,/* complain_on_overflow */
01088         bfd_elf_generic_reloc,     /* special_function */
01089         "R_PPC64_ADDR16_LO_DS",/* name */
01090         FALSE,                     /* partial_inplace */
01091         0,                  /* src_mask */
01092         0xfffc,             /* dst_mask */
01093         FALSE),             /* pcrel_offset */
01094 
01095   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
01096   HOWTO (R_PPC64_GOT16_DS,  /* type */
01097         0,                  /* rightshift */
01098         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01099         16,                 /* bitsize */
01100         FALSE,                     /* pc_relative */
01101         0,                  /* bitpos */
01102         complain_overflow_signed, /* complain_on_overflow */
01103         ppc64_elf_unhandled_reloc, /* special_function */
01104         "R_PPC64_GOT16_DS", /* name */
01105         FALSE,                     /* partial_inplace */
01106         0,                  /* src_mask */
01107         0xfffc,             /* dst_mask */
01108         FALSE),             /* pcrel_offset */
01109 
01110   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
01111   HOWTO (R_PPC64_GOT16_LO_DS,      /* type */
01112         0,                  /* rightshift */
01113         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01114         16,                 /* bitsize */
01115         FALSE,                     /* pc_relative */
01116         0,                  /* bitpos */
01117         complain_overflow_dont, /* complain_on_overflow */
01118         ppc64_elf_unhandled_reloc, /* special_function */
01119         "R_PPC64_GOT16_LO_DS",     /* name */
01120         FALSE,                     /* partial_inplace */
01121         0,                  /* src_mask */
01122         0xfffc,             /* dst_mask */
01123         FALSE),             /* pcrel_offset */
01124 
01125   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
01126   HOWTO (R_PPC64_PLT16_LO_DS,      /* type */
01127         0,                  /* rightshift */
01128         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01129         16,                 /* bitsize */
01130         FALSE,                     /* pc_relative */
01131         0,                  /* bitpos */
01132         complain_overflow_dont, /* complain_on_overflow */
01133         ppc64_elf_unhandled_reloc, /* special_function */
01134         "R_PPC64_PLT16_LO_DS",     /* name */
01135         FALSE,                     /* partial_inplace */
01136         0,                  /* src_mask */
01137         0xfffc,             /* dst_mask */
01138         FALSE),             /* pcrel_offset */
01139 
01140   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
01141   HOWTO (R_PPC64_SECTOFF_DS,       /* type */
01142         0,                  /* rightshift */
01143         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01144         16,                 /* bitsize */
01145         FALSE,                     /* pc_relative */
01146         0,                  /* bitpos */
01147         complain_overflow_bitfield, /* complain_on_overflow */
01148         ppc64_elf_sectoff_reloc, /* special_function */
01149         "R_PPC64_SECTOFF_DS",      /* name */
01150         FALSE,                     /* partial_inplace */
01151         0,                  /* src_mask */
01152         0xfffc,             /* dst_mask */
01153         FALSE),             /* pcrel_offset */
01154 
01155   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
01156   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
01157         0,                  /* rightshift */
01158         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01159         16,                 /* bitsize */
01160         FALSE,                     /* pc_relative */
01161         0,                  /* bitpos */
01162         complain_overflow_dont, /* complain_on_overflow */
01163         ppc64_elf_sectoff_reloc, /* special_function */
01164         "R_PPC64_SECTOFF_LO_DS",/* name */
01165         FALSE,                     /* partial_inplace */
01166         0,                  /* src_mask */
01167         0xfffc,             /* dst_mask */
01168         FALSE),             /* pcrel_offset */
01169 
01170   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
01171   HOWTO (R_PPC64_TOC16_DS,  /* type */
01172         0,                  /* rightshift */
01173         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01174         16,                 /* bitsize */
01175         FALSE,                     /* pc_relative */
01176         0,                  /* bitpos */
01177         complain_overflow_signed, /* complain_on_overflow */
01178         ppc64_elf_toc_reloc,       /* special_function */
01179         "R_PPC64_TOC16_DS", /* name */
01180         FALSE,                     /* partial_inplace */
01181         0,                  /* src_mask */
01182         0xfffc,             /* dst_mask */
01183         FALSE),             /* pcrel_offset */
01184 
01185   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
01186   HOWTO (R_PPC64_TOC16_LO_DS,      /* type */
01187         0,                  /* rightshift */
01188         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01189         16,                 /* bitsize */
01190         FALSE,                     /* pc_relative */
01191         0,                  /* bitpos */
01192         complain_overflow_dont, /* complain_on_overflow */
01193         ppc64_elf_toc_reloc,       /* special_function */
01194         "R_PPC64_TOC16_LO_DS",     /* name */
01195         FALSE,                     /* partial_inplace */
01196         0,                  /* src_mask */
01197         0xfffc,             /* dst_mask */
01198         FALSE),             /* pcrel_offset */
01199 
01200   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
01201   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
01202   HOWTO (R_PPC64_PLTGOT16_DS,      /* type */
01203         0,                  /* rightshift */
01204         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01205         16,                 /* bitsize */
01206         FALSE,                     /* pc_relative */
01207         0,                  /* bitpos */
01208         complain_overflow_signed, /* complain_on_overflow */
01209         ppc64_elf_unhandled_reloc, /* special_function */
01210         "R_PPC64_PLTGOT16_DS",     /* name */
01211         FALSE,                     /* partial_inplace */
01212         0,                  /* src_mask */
01213         0xfffc,             /* dst_mask */
01214         FALSE),             /* pcrel_offset */
01215 
01216   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
01217   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
01218   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
01219         0,                  /* rightshift */
01220         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01221         16,                 /* bitsize */
01222         FALSE,                     /* pc_relative */
01223         0,                  /* bitpos */
01224         complain_overflow_dont, /* complain_on_overflow */
01225         ppc64_elf_unhandled_reloc, /* special_function */
01226         "R_PPC64_PLTGOT16_LO_DS",/* name */
01227         FALSE,                     /* partial_inplace */
01228         0,                  /* src_mask */
01229         0xfffc,             /* dst_mask */
01230         FALSE),             /* pcrel_offset */
01231 
01232   /* Marker reloc for TLS.  */
01233   HOWTO (R_PPC64_TLS,
01234         0,                  /* rightshift */
01235         2,                  /* size (0 = byte, 1 = short, 2 = long) */
01236         32,                 /* bitsize */
01237         FALSE,                     /* pc_relative */
01238         0,                  /* bitpos */
01239         complain_overflow_dont, /* complain_on_overflow */
01240         bfd_elf_generic_reloc,     /* special_function */
01241         "R_PPC64_TLS",             /* name */
01242         FALSE,                     /* partial_inplace */
01243         0,                  /* src_mask */
01244         0,                  /* dst_mask */
01245         FALSE),             /* pcrel_offset */
01246 
01247   /* Computes the load module index of the load module that contains the
01248      definition of its TLS sym.  */
01249   HOWTO (R_PPC64_DTPMOD64,
01250         0,                  /* rightshift */
01251         4,                  /* size (0 = byte, 1 = short, 2 = long) */
01252         64,                 /* bitsize */
01253         FALSE,                     /* pc_relative */
01254         0,                  /* bitpos */
01255         complain_overflow_dont, /* complain_on_overflow */
01256         ppc64_elf_unhandled_reloc, /* special_function */
01257         "R_PPC64_DTPMOD64", /* name */
01258         FALSE,                     /* partial_inplace */
01259         0,                  /* src_mask */
01260         ONES (64),          /* dst_mask */
01261         FALSE),             /* pcrel_offset */
01262 
01263   /* Computes a dtv-relative displacement, the difference between the value
01264      of sym+add and the base address of the thread-local storage block that
01265      contains the definition of sym, minus 0x8000.  */
01266   HOWTO (R_PPC64_DTPREL64,
01267         0,                  /* rightshift */
01268         4,                  /* size (0 = byte, 1 = short, 2 = long) */
01269         64,                 /* bitsize */
01270         FALSE,                     /* pc_relative */
01271         0,                  /* bitpos */
01272         complain_overflow_dont, /* complain_on_overflow */
01273         ppc64_elf_unhandled_reloc, /* special_function */
01274         "R_PPC64_DTPREL64", /* name */
01275         FALSE,                     /* partial_inplace */
01276         0,                  /* src_mask */
01277         ONES (64),          /* dst_mask */
01278         FALSE),             /* pcrel_offset */
01279 
01280   /* A 16 bit dtprel reloc.  */
01281   HOWTO (R_PPC64_DTPREL16,
01282         0,                  /* rightshift */
01283         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01284         16,                 /* bitsize */
01285         FALSE,                     /* pc_relative */
01286         0,                  /* bitpos */
01287         complain_overflow_signed, /* complain_on_overflow */
01288         ppc64_elf_unhandled_reloc, /* special_function */
01289         "R_PPC64_DTPREL16", /* name */
01290         FALSE,                     /* partial_inplace */
01291         0,                  /* src_mask */
01292         0xffff,             /* dst_mask */
01293         FALSE),             /* pcrel_offset */
01294 
01295   /* Like DTPREL16, but no overflow.  */
01296   HOWTO (R_PPC64_DTPREL16_LO,
01297         0,                  /* rightshift */
01298         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01299         16,                 /* bitsize */
01300         FALSE,                     /* pc_relative */
01301         0,                  /* bitpos */
01302         complain_overflow_dont, /* complain_on_overflow */
01303         ppc64_elf_unhandled_reloc, /* special_function */
01304         "R_PPC64_DTPREL16_LO",     /* name */
01305         FALSE,                     /* partial_inplace */
01306         0,                  /* src_mask */
01307         0xffff,             /* dst_mask */
01308         FALSE),             /* pcrel_offset */
01309 
01310   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
01311   HOWTO (R_PPC64_DTPREL16_HI,
01312         16,                 /* rightshift */
01313         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01314         16,                 /* bitsize */
01315         FALSE,                     /* pc_relative */
01316         0,                  /* bitpos */
01317         complain_overflow_dont, /* complain_on_overflow */
01318         ppc64_elf_unhandled_reloc, /* special_function */
01319         "R_PPC64_DTPREL16_HI",     /* name */
01320         FALSE,                     /* partial_inplace */
01321         0,                  /* src_mask */
01322         0xffff,             /* dst_mask */
01323         FALSE),             /* pcrel_offset */
01324 
01325   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
01326   HOWTO (R_PPC64_DTPREL16_HA,
01327         16,                 /* rightshift */
01328         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01329         16,                 /* bitsize */
01330         FALSE,                     /* pc_relative */
01331         0,                  /* bitpos */
01332         complain_overflow_dont, /* complain_on_overflow */
01333         ppc64_elf_unhandled_reloc, /* special_function */
01334         "R_PPC64_DTPREL16_HA",     /* name */
01335         FALSE,                     /* partial_inplace */
01336         0,                  /* src_mask */
01337         0xffff,             /* dst_mask */
01338         FALSE),             /* pcrel_offset */
01339 
01340   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
01341   HOWTO (R_PPC64_DTPREL16_HIGHER,
01342         32,                 /* rightshift */
01343         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01344         16,                 /* bitsize */
01345         FALSE,                     /* pc_relative */
01346         0,                  /* bitpos */
01347         complain_overflow_dont, /* complain_on_overflow */
01348         ppc64_elf_unhandled_reloc, /* special_function */
01349         "R_PPC64_DTPREL16_HIGHER", /* name */
01350         FALSE,                     /* partial_inplace */
01351         0,                  /* src_mask */
01352         0xffff,             /* dst_mask */
01353         FALSE),             /* pcrel_offset */
01354 
01355   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
01356   HOWTO (R_PPC64_DTPREL16_HIGHERA,
01357         32,                 /* rightshift */
01358         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01359         16,                 /* bitsize */
01360         FALSE,                     /* pc_relative */
01361         0,                  /* bitpos */
01362         complain_overflow_dont, /* complain_on_overflow */
01363         ppc64_elf_unhandled_reloc, /* special_function */
01364         "R_PPC64_DTPREL16_HIGHERA", /* name */
01365         FALSE,                     /* partial_inplace */
01366         0,                  /* src_mask */
01367         0xffff,             /* dst_mask */
01368         FALSE),             /* pcrel_offset */
01369 
01370   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
01371   HOWTO (R_PPC64_DTPREL16_HIGHEST,
01372         48,                 /* rightshift */
01373         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01374         16,                 /* bitsize */
01375         FALSE,                     /* pc_relative */
01376         0,                  /* bitpos */
01377         complain_overflow_dont, /* complain_on_overflow */
01378         ppc64_elf_unhandled_reloc, /* special_function */
01379         "R_PPC64_DTPREL16_HIGHEST", /* name */
01380         FALSE,                     /* partial_inplace */
01381         0,                  /* src_mask */
01382         0xffff,             /* dst_mask */
01383         FALSE),             /* pcrel_offset */
01384 
01385   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
01386   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
01387         48,                 /* rightshift */
01388         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01389         16,                 /* bitsize */
01390         FALSE,                     /* pc_relative */
01391         0,                  /* bitpos */
01392         complain_overflow_dont, /* complain_on_overflow */
01393         ppc64_elf_unhandled_reloc, /* special_function */
01394         "R_PPC64_DTPREL16_HIGHESTA", /* name */
01395         FALSE,                     /* partial_inplace */
01396         0,                  /* src_mask */
01397         0xffff,             /* dst_mask */
01398         FALSE),             /* pcrel_offset */
01399 
01400   /* Like DTPREL16, but for insns with a DS field.  */
01401   HOWTO (R_PPC64_DTPREL16_DS,
01402         0,                  /* rightshift */
01403         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01404         16,                 /* bitsize */
01405         FALSE,                     /* pc_relative */
01406         0,                  /* bitpos */
01407         complain_overflow_signed, /* complain_on_overflow */
01408         ppc64_elf_unhandled_reloc, /* special_function */
01409         "R_PPC64_DTPREL16_DS",     /* name */
01410         FALSE,                     /* partial_inplace */
01411         0,                  /* src_mask */
01412         0xfffc,             /* dst_mask */
01413         FALSE),             /* pcrel_offset */
01414 
01415   /* Like DTPREL16_DS, but no overflow.  */
01416   HOWTO (R_PPC64_DTPREL16_LO_DS,
01417         0,                  /* rightshift */
01418         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01419         16,                 /* bitsize */
01420         FALSE,                     /* pc_relative */
01421         0,                  /* bitpos */
01422         complain_overflow_dont, /* complain_on_overflow */
01423         ppc64_elf_unhandled_reloc, /* special_function */
01424         "R_PPC64_DTPREL16_LO_DS", /* name */
01425         FALSE,                     /* partial_inplace */
01426         0,                  /* src_mask */
01427         0xfffc,             /* dst_mask */
01428         FALSE),             /* pcrel_offset */
01429 
01430   /* Computes a tp-relative displacement, the difference between the value of
01431      sym+add and the value of the thread pointer (r13).  */
01432   HOWTO (R_PPC64_TPREL64,
01433         0,                  /* rightshift */
01434         4,                  /* size (0 = byte, 1 = short, 2 = long) */
01435         64,                 /* bitsize */
01436         FALSE,                     /* pc_relative */
01437         0,                  /* bitpos */
01438         complain_overflow_dont, /* complain_on_overflow */
01439         ppc64_elf_unhandled_reloc, /* special_function */
01440         "R_PPC64_TPREL64",  /* name */
01441         FALSE,                     /* partial_inplace */
01442         0,                  /* src_mask */
01443         ONES (64),          /* dst_mask */
01444         FALSE),             /* pcrel_offset */
01445 
01446   /* A 16 bit tprel reloc.  */
01447   HOWTO (R_PPC64_TPREL16,
01448         0,                  /* rightshift */
01449         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01450         16,                 /* bitsize */
01451         FALSE,                     /* pc_relative */
01452         0,                  /* bitpos */
01453         complain_overflow_signed, /* complain_on_overflow */
01454         ppc64_elf_unhandled_reloc, /* special_function */
01455         "R_PPC64_TPREL16",  /* name */
01456         FALSE,                     /* partial_inplace */
01457         0,                  /* src_mask */
01458         0xffff,             /* dst_mask */
01459         FALSE),             /* pcrel_offset */
01460 
01461   /* Like TPREL16, but no overflow.  */
01462   HOWTO (R_PPC64_TPREL16_LO,
01463         0,                  /* rightshift */
01464         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01465         16,                 /* bitsize */
01466         FALSE,                     /* pc_relative */
01467         0,                  /* bitpos */
01468         complain_overflow_dont, /* complain_on_overflow */
01469         ppc64_elf_unhandled_reloc, /* special_function */
01470         "R_PPC64_TPREL16_LO",      /* name */
01471         FALSE,                     /* partial_inplace */
01472         0,                  /* src_mask */
01473         0xffff,             /* dst_mask */
01474         FALSE),             /* pcrel_offset */
01475 
01476   /* Like TPREL16_LO, but next higher group of 16 bits.  */
01477   HOWTO (R_PPC64_TPREL16_HI,
01478         16,                 /* rightshift */
01479         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01480         16,                 /* bitsize */
01481         FALSE,                     /* pc_relative */
01482         0,                  /* bitpos */
01483         complain_overflow_dont, /* complain_on_overflow */
01484         ppc64_elf_unhandled_reloc, /* special_function */
01485         "R_PPC64_TPREL16_HI",      /* name */
01486         FALSE,                     /* partial_inplace */
01487         0,                  /* src_mask */
01488         0xffff,             /* dst_mask */
01489         FALSE),             /* pcrel_offset */
01490 
01491   /* Like TPREL16_HI, but adjust for low 16 bits.  */
01492   HOWTO (R_PPC64_TPREL16_HA,
01493         16,                 /* rightshift */
01494         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01495         16,                 /* bitsize */
01496         FALSE,                     /* pc_relative */
01497         0,                  /* bitpos */
01498         complain_overflow_dont, /* complain_on_overflow */
01499         ppc64_elf_unhandled_reloc, /* special_function */
01500         "R_PPC64_TPREL16_HA",      /* name */
01501         FALSE,                     /* partial_inplace */
01502         0,                  /* src_mask */
01503         0xffff,             /* dst_mask */
01504         FALSE),             /* pcrel_offset */
01505 
01506   /* Like TPREL16_HI, but next higher group of 16 bits.  */
01507   HOWTO (R_PPC64_TPREL16_HIGHER,
01508         32,                 /* rightshift */
01509         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01510         16,                 /* bitsize */
01511         FALSE,                     /* pc_relative */
01512         0,                  /* bitpos */
01513         complain_overflow_dont, /* complain_on_overflow */
01514         ppc64_elf_unhandled_reloc, /* special_function */
01515         "R_PPC64_TPREL16_HIGHER",  /* name */
01516         FALSE,                     /* partial_inplace */
01517         0,                  /* src_mask */
01518         0xffff,             /* dst_mask */
01519         FALSE),             /* pcrel_offset */
01520 
01521   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
01522   HOWTO (R_PPC64_TPREL16_HIGHERA,
01523         32,                 /* rightshift */
01524         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01525         16,                 /* bitsize */
01526         FALSE,                     /* pc_relative */
01527         0,                  /* bitpos */
01528         complain_overflow_dont, /* complain_on_overflow */
01529         ppc64_elf_unhandled_reloc, /* special_function */
01530         "R_PPC64_TPREL16_HIGHERA", /* name */
01531         FALSE,                     /* partial_inplace */
01532         0,                  /* src_mask */
01533         0xffff,             /* dst_mask */
01534         FALSE),             /* pcrel_offset */
01535 
01536   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
01537   HOWTO (R_PPC64_TPREL16_HIGHEST,
01538         48,                 /* rightshift */
01539         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01540         16,                 /* bitsize */
01541         FALSE,                     /* pc_relative */
01542         0,                  /* bitpos */
01543         complain_overflow_dont, /* complain_on_overflow */
01544         ppc64_elf_unhandled_reloc, /* special_function */
01545         "R_PPC64_TPREL16_HIGHEST", /* name */
01546         FALSE,                     /* partial_inplace */
01547         0,                  /* src_mask */
01548         0xffff,             /* dst_mask */
01549         FALSE),             /* pcrel_offset */
01550 
01551   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
01552   HOWTO (R_PPC64_TPREL16_HIGHESTA,
01553         48,                 /* rightshift */
01554         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01555         16,                 /* bitsize */
01556         FALSE,                     /* pc_relative */
01557         0,                  /* bitpos */
01558         complain_overflow_dont, /* complain_on_overflow */
01559         ppc64_elf_unhandled_reloc, /* special_function */
01560         "R_PPC64_TPREL16_HIGHESTA", /* name */
01561         FALSE,                     /* partial_inplace */
01562         0,                  /* src_mask */
01563         0xffff,             /* dst_mask */
01564         FALSE),             /* pcrel_offset */
01565 
01566   /* Like TPREL16, but for insns with a DS field.  */
01567   HOWTO (R_PPC64_TPREL16_DS,
01568         0,                  /* rightshift */
01569         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01570         16,                 /* bitsize */
01571         FALSE,                     /* pc_relative */
01572         0,                  /* bitpos */
01573         complain_overflow_signed, /* complain_on_overflow */
01574         ppc64_elf_unhandled_reloc, /* special_function */
01575         "R_PPC64_TPREL16_DS",      /* name */
01576         FALSE,                     /* partial_inplace */
01577         0,                  /* src_mask */
01578         0xfffc,             /* dst_mask */
01579         FALSE),             /* pcrel_offset */
01580 
01581   /* Like TPREL16_DS, but no overflow.  */
01582   HOWTO (R_PPC64_TPREL16_LO_DS,
01583         0,                  /* rightshift */
01584         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01585         16,                 /* bitsize */
01586         FALSE,                     /* pc_relative */
01587         0,                  /* bitpos */
01588         complain_overflow_dont, /* complain_on_overflow */
01589         ppc64_elf_unhandled_reloc, /* special_function */
01590         "R_PPC64_TPREL16_LO_DS", /* name */
01591         FALSE,                     /* partial_inplace */
01592         0,                  /* src_mask */
01593         0xfffc,             /* dst_mask */
01594         FALSE),             /* pcrel_offset */
01595 
01596   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
01597      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
01598      to the first entry relative to the TOC base (r2).  */
01599   HOWTO (R_PPC64_GOT_TLSGD16,
01600         0,                  /* rightshift */
01601         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01602         16,                 /* bitsize */
01603         FALSE,                     /* pc_relative */
01604         0,                  /* bitpos */
01605         complain_overflow_signed, /* complain_on_overflow */
01606         ppc64_elf_unhandled_reloc, /* special_function */
01607         "R_PPC64_GOT_TLSGD16",     /* name */
01608         FALSE,                     /* partial_inplace */
01609         0,                  /* src_mask */
01610         0xffff,             /* dst_mask */
01611         FALSE),             /* pcrel_offset */
01612 
01613   /* Like GOT_TLSGD16, but no overflow.  */
01614   HOWTO (R_PPC64_GOT_TLSGD16_LO,
01615         0,                  /* rightshift */
01616         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01617         16,                 /* bitsize */
01618         FALSE,                     /* pc_relative */
01619         0,                  /* bitpos */
01620         complain_overflow_dont, /* complain_on_overflow */
01621         ppc64_elf_unhandled_reloc, /* special_function */
01622         "R_PPC64_GOT_TLSGD16_LO", /* name */
01623         FALSE,                     /* partial_inplace */
01624         0,                  /* src_mask */
01625         0xffff,             /* dst_mask */
01626         FALSE),             /* pcrel_offset */
01627 
01628   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
01629   HOWTO (R_PPC64_GOT_TLSGD16_HI,
01630         16,                 /* rightshift */
01631         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01632         16,                 /* bitsize */
01633         FALSE,                     /* pc_relative */
01634         0,                  /* bitpos */
01635         complain_overflow_dont, /* complain_on_overflow */
01636         ppc64_elf_unhandled_reloc, /* special_function */
01637         "R_PPC64_GOT_TLSGD16_HI", /* name */
01638         FALSE,                     /* partial_inplace */
01639         0,                  /* src_mask */
01640         0xffff,             /* dst_mask */
01641         FALSE),             /* pcrel_offset */
01642 
01643   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
01644   HOWTO (R_PPC64_GOT_TLSGD16_HA,
01645         16,                 /* rightshift */
01646         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01647         16,                 /* bitsize */
01648         FALSE,                     /* pc_relative */
01649         0,                  /* bitpos */
01650         complain_overflow_dont, /* complain_on_overflow */
01651         ppc64_elf_unhandled_reloc, /* special_function */
01652         "R_PPC64_GOT_TLSGD16_HA", /* name */
01653         FALSE,                     /* partial_inplace */
01654         0,                  /* src_mask */
01655         0xffff,             /* dst_mask */
01656         FALSE),             /* pcrel_offset */
01657 
01658   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
01659      with values (sym+add)@dtpmod and zero, and computes the offset to the
01660      first entry relative to the TOC base (r2).  */
01661   HOWTO (R_PPC64_GOT_TLSLD16,
01662         0,                  /* rightshift */
01663         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01664         16,                 /* bitsize */
01665         FALSE,                     /* pc_relative */
01666         0,                  /* bitpos */
01667         complain_overflow_signed, /* complain_on_overflow */
01668         ppc64_elf_unhandled_reloc, /* special_function */
01669         "R_PPC64_GOT_TLSLD16",     /* name */
01670         FALSE,                     /* partial_inplace */
01671         0,                  /* src_mask */
01672         0xffff,             /* dst_mask */
01673         FALSE),             /* pcrel_offset */
01674 
01675   /* Like GOT_TLSLD16, but no overflow.  */
01676   HOWTO (R_PPC64_GOT_TLSLD16_LO,
01677         0,                  /* rightshift */
01678         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01679         16,                 /* bitsize */
01680         FALSE,                     /* pc_relative */
01681         0,                  /* bitpos */
01682         complain_overflow_dont, /* complain_on_overflow */
01683         ppc64_elf_unhandled_reloc, /* special_function */
01684         "R_PPC64_GOT_TLSLD16_LO", /* name */
01685         FALSE,                     /* partial_inplace */
01686         0,                  /* src_mask */
01687         0xffff,             /* dst_mask */
01688         FALSE),             /* pcrel_offset */
01689 
01690   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
01691   HOWTO (R_PPC64_GOT_TLSLD16_HI,
01692         16,                 /* rightshift */
01693         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01694         16,                 /* bitsize */
01695         FALSE,                     /* pc_relative */
01696         0,                  /* bitpos */
01697         complain_overflow_dont, /* complain_on_overflow */
01698         ppc64_elf_unhandled_reloc, /* special_function */
01699         "R_PPC64_GOT_TLSLD16_HI", /* name */
01700         FALSE,                     /* partial_inplace */
01701         0,                  /* src_mask */
01702         0xffff,             /* dst_mask */
01703         FALSE),             /* pcrel_offset */
01704 
01705   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
01706   HOWTO (R_PPC64_GOT_TLSLD16_HA,
01707         16,                 /* rightshift */
01708         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01709         16,                 /* bitsize */
01710         FALSE,                     /* pc_relative */
01711         0,                  /* bitpos */
01712         complain_overflow_dont, /* complain_on_overflow */
01713         ppc64_elf_unhandled_reloc, /* special_function */
01714         "R_PPC64_GOT_TLSLD16_HA", /* name */
01715         FALSE,                     /* partial_inplace */
01716         0,                  /* src_mask */
01717         0xffff,             /* dst_mask */
01718         FALSE),             /* pcrel_offset */
01719 
01720   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
01721      the offset to the entry relative to the TOC base (r2).  */
01722   HOWTO (R_PPC64_GOT_DTPREL16_DS,
01723         0,                  /* rightshift */
01724         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01725         16,                 /* bitsize */
01726         FALSE,                     /* pc_relative */
01727         0,                  /* bitpos */
01728         complain_overflow_signed, /* complain_on_overflow */
01729         ppc64_elf_unhandled_reloc, /* special_function */
01730         "R_PPC64_GOT_DTPREL16_DS", /* name */
01731         FALSE,                     /* partial_inplace */
01732         0,                  /* src_mask */
01733         0xfffc,             /* dst_mask */
01734         FALSE),             /* pcrel_offset */
01735 
01736   /* Like GOT_DTPREL16_DS, but no overflow.  */
01737   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
01738         0,                  /* rightshift */
01739         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01740         16,                 /* bitsize */
01741         FALSE,                     /* pc_relative */
01742         0,                  /* bitpos */
01743         complain_overflow_dont, /* complain_on_overflow */
01744         ppc64_elf_unhandled_reloc, /* special_function */
01745         "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
01746         FALSE,                     /* partial_inplace */
01747         0,                  /* src_mask */
01748         0xfffc,             /* dst_mask */
01749         FALSE),             /* pcrel_offset */
01750 
01751   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
01752   HOWTO (R_PPC64_GOT_DTPREL16_HI,
01753         16,                 /* rightshift */
01754         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01755         16,                 /* bitsize */
01756         FALSE,                     /* pc_relative */
01757         0,                  /* bitpos */
01758         complain_overflow_dont, /* complain_on_overflow */
01759         ppc64_elf_unhandled_reloc, /* special_function */
01760         "R_PPC64_GOT_DTPREL16_HI", /* name */
01761         FALSE,                     /* partial_inplace */
01762         0,                  /* src_mask */
01763         0xffff,             /* dst_mask */
01764         FALSE),             /* pcrel_offset */
01765 
01766   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
01767   HOWTO (R_PPC64_GOT_DTPREL16_HA,
01768         16,                 /* rightshift */
01769         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01770         16,                 /* bitsize */
01771         FALSE,                     /* pc_relative */
01772         0,                  /* bitpos */
01773         complain_overflow_dont, /* complain_on_overflow */
01774         ppc64_elf_unhandled_reloc, /* special_function */
01775         "R_PPC64_GOT_DTPREL16_HA", /* name */
01776         FALSE,                     /* partial_inplace */
01777         0,                  /* src_mask */
01778         0xffff,             /* dst_mask */
01779         FALSE),             /* pcrel_offset */
01780 
01781   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
01782      offset to the entry relative to the TOC base (r2).  */
01783   HOWTO (R_PPC64_GOT_TPREL16_DS,
01784         0,                  /* rightshift */
01785         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01786         16,                 /* bitsize */
01787         FALSE,                     /* pc_relative */
01788         0,                  /* bitpos */
01789         complain_overflow_signed, /* complain_on_overflow */
01790         ppc64_elf_unhandled_reloc, /* special_function */
01791         "R_PPC64_GOT_TPREL16_DS", /* name */
01792         FALSE,                     /* partial_inplace */
01793         0,                  /* src_mask */
01794         0xfffc,             /* dst_mask */
01795         FALSE),             /* pcrel_offset */
01796 
01797   /* Like GOT_TPREL16_DS, but no overflow.  */
01798   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
01799         0,                  /* rightshift */
01800         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01801         16,                 /* bitsize */
01802         FALSE,                     /* pc_relative */
01803         0,                  /* bitpos */
01804         complain_overflow_dont, /* complain_on_overflow */
01805         ppc64_elf_unhandled_reloc, /* special_function */
01806         "R_PPC64_GOT_TPREL16_LO_DS", /* name */
01807         FALSE,                     /* partial_inplace */
01808         0,                  /* src_mask */
01809         0xfffc,             /* dst_mask */
01810         FALSE),             /* pcrel_offset */
01811 
01812   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
01813   HOWTO (R_PPC64_GOT_TPREL16_HI,
01814         16,                 /* rightshift */
01815         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01816         16,                 /* bitsize */
01817         FALSE,                     /* pc_relative */
01818         0,                  /* bitpos */
01819         complain_overflow_dont, /* complain_on_overflow */
01820         ppc64_elf_unhandled_reloc, /* special_function */
01821         "R_PPC64_GOT_TPREL16_HI", /* name */
01822         FALSE,                     /* partial_inplace */
01823         0,                  /* src_mask */
01824         0xffff,             /* dst_mask */
01825         FALSE),             /* pcrel_offset */
01826 
01827   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
01828   HOWTO (R_PPC64_GOT_TPREL16_HA,
01829         16,                 /* rightshift */
01830         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01831         16,                 /* bitsize */
01832         FALSE,                     /* pc_relative */
01833         0,                  /* bitpos */
01834         complain_overflow_dont, /* complain_on_overflow */
01835         ppc64_elf_unhandled_reloc, /* special_function */
01836         "R_PPC64_GOT_TPREL16_HA", /* name */
01837         FALSE,                     /* partial_inplace */
01838         0,                  /* src_mask */
01839         0xffff,             /* dst_mask */
01840         FALSE),             /* pcrel_offset */
01841 
01842   /* GNU extension to record C++ vtable hierarchy.  */
01843   HOWTO (R_PPC64_GNU_VTINHERIT,    /* type */
01844         0,                  /* rightshift */
01845         0,                  /* size (0 = byte, 1 = short, 2 = long) */
01846         0,                  /* bitsize */
01847         FALSE,                     /* pc_relative */
01848         0,                  /* bitpos */
01849         complain_overflow_dont, /* complain_on_overflow */
01850         NULL,               /* special_function */
01851         "R_PPC64_GNU_VTINHERIT", /* name */
01852         FALSE,                     /* partial_inplace */
01853         0,                  /* src_mask */
01854         0,                  /* dst_mask */
01855         FALSE),             /* pcrel_offset */
01856 
01857   /* GNU extension to record C++ vtable member usage.  */
01858   HOWTO (R_PPC64_GNU_VTENTRY,      /* type */
01859         0,                  /* rightshift */
01860         0,                  /* size (0 = byte, 1 = short, 2 = long) */
01861         0,                  /* bitsize */
01862         FALSE,                     /* pc_relative */
01863         0,                  /* bitpos */
01864         complain_overflow_dont, /* complain_on_overflow */
01865         NULL,               /* special_function */
01866         "R_PPC64_GNU_VTENTRY",     /* name */
01867         FALSE,                     /* partial_inplace */
01868         0,                  /* src_mask */
01869         0,                  /* dst_mask */
01870         FALSE),             /* pcrel_offset */
01871 };
01872 
01873 
01874 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
01875    be done.  */
01876 
01877 static void
01878 ppc_howto_init (void)
01879 {
01880   unsigned int i, type;
01881 
01882   for (i = 0;
01883        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
01884        i++)
01885     {
01886       type = ppc64_elf_howto_raw[i].type;
01887       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
01888                        / sizeof (ppc64_elf_howto_table[0])));
01889       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
01890     }
01891 }
01892 
01893 static reloc_howto_type *
01894 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
01895                           bfd_reloc_code_real_type code)
01896 {
01897   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
01898 
01899   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
01900     /* Initialize howto table if needed.  */
01901     ppc_howto_init ();
01902 
01903   switch (code)
01904     {
01905     default:
01906       return NULL;
01907 
01908     case BFD_RELOC_NONE:                  r = R_PPC64_NONE;
01909       break;
01910     case BFD_RELOC_32:                           r = R_PPC64_ADDR32;
01911       break;
01912     case BFD_RELOC_PPC_BA26:                     r = R_PPC64_ADDR24;
01913       break;
01914     case BFD_RELOC_16:                           r = R_PPC64_ADDR16;
01915       break;
01916     case BFD_RELOC_LO16:                  r = R_PPC64_ADDR16_LO;
01917       break;
01918     case BFD_RELOC_HI16:                  r = R_PPC64_ADDR16_HI;
01919       break;
01920     case BFD_RELOC_HI16_S:                r = R_PPC64_ADDR16_HA;
01921       break;
01922     case BFD_RELOC_PPC_BA16:                     r = R_PPC64_ADDR14;
01923       break;
01924     case BFD_RELOC_PPC_BA16_BRTAKEN:             r = R_PPC64_ADDR14_BRTAKEN;
01925       break;
01926     case BFD_RELOC_PPC_BA16_BRNTAKEN:            r = R_PPC64_ADDR14_BRNTAKEN;
01927       break;
01928     case BFD_RELOC_PPC_B26:               r = R_PPC64_REL24;
01929       break;
01930     case BFD_RELOC_PPC_B16:               r = R_PPC64_REL14;
01931       break;
01932     case BFD_RELOC_PPC_B16_BRTAKEN:              r = R_PPC64_REL14_BRTAKEN;
01933       break;
01934     case BFD_RELOC_PPC_B16_BRNTAKEN:             r = R_PPC64_REL14_BRNTAKEN;
01935       break;
01936     case BFD_RELOC_16_GOTOFF:                    r = R_PPC64_GOT16;
01937       break;
01938     case BFD_RELOC_LO16_GOTOFF:                  r = R_PPC64_GOT16_LO;
01939       break;
01940     case BFD_RELOC_HI16_GOTOFF:                  r = R_PPC64_GOT16_HI;
01941       break;
01942     case BFD_RELOC_HI16_S_GOTOFF:         r = R_PPC64_GOT16_HA;
01943       break;
01944     case BFD_RELOC_PPC_COPY:                     r = R_PPC64_COPY;
01945       break;
01946     case BFD_RELOC_PPC_GLOB_DAT:          r = R_PPC64_GLOB_DAT;
01947       break;
01948     case BFD_RELOC_32_PCREL:                     r = R_PPC64_REL32;
01949       break;
01950     case BFD_RELOC_32_PLTOFF:                    r = R_PPC64_PLT32;
01951       break;
01952     case BFD_RELOC_32_PLT_PCREL:          r = R_PPC64_PLTREL32;
01953       break;
01954     case BFD_RELOC_LO16_PLTOFF:                  r = R_PPC64_PLT16_LO;
01955       break;
01956     case BFD_RELOC_HI16_PLTOFF:                  r = R_PPC64_PLT16_HI;
01957       break;
01958     case BFD_RELOC_HI16_S_PLTOFF:         r = R_PPC64_PLT16_HA;
01959       break;
01960     case BFD_RELOC_16_BASEREL:                   r = R_PPC64_SECTOFF;
01961       break;
01962     case BFD_RELOC_LO16_BASEREL:          r = R_PPC64_SECTOFF_LO;
01963       break;
01964     case BFD_RELOC_HI16_BASEREL:          r = R_PPC64_SECTOFF_HI;
01965       break;
01966     case BFD_RELOC_HI16_S_BASEREL:        r = R_PPC64_SECTOFF_HA;
01967       break;
01968     case BFD_RELOC_CTOR:                  r = R_PPC64_ADDR64;
01969       break;
01970     case BFD_RELOC_64:                           r = R_PPC64_ADDR64;
01971       break;
01972     case BFD_RELOC_PPC64_HIGHER:          r = R_PPC64_ADDR16_HIGHER;
01973       break;
01974     case BFD_RELOC_PPC64_HIGHER_S:        r = R_PPC64_ADDR16_HIGHERA;
01975       break;
01976     case BFD_RELOC_PPC64_HIGHEST:         r = R_PPC64_ADDR16_HIGHEST;
01977       break;
01978     case BFD_RELOC_PPC64_HIGHEST_S:              r = R_PPC64_ADDR16_HIGHESTA;
01979       break;
01980     case BFD_RELOC_64_PCREL:                     r = R_PPC64_REL64;
01981       break;
01982     case BFD_RELOC_64_PLTOFF:                    r = R_PPC64_PLT64;
01983       break;
01984     case BFD_RELOC_64_PLT_PCREL:          r = R_PPC64_PLTREL64;
01985       break;
01986     case BFD_RELOC_PPC_TOC16:                    r = R_PPC64_TOC16;
01987       break;
01988     case BFD_RELOC_PPC64_TOC16_LO:        r = R_PPC64_TOC16_LO;
01989       break;
01990     case BFD_RELOC_PPC64_TOC16_HI:        r = R_PPC64_TOC16_HI;
01991       break;
01992     case BFD_RELOC_PPC64_TOC16_HA:        r = R_PPC64_TOC16_HA;
01993       break;
01994     case BFD_RELOC_PPC64_TOC:                    r = R_PPC64_TOC;
01995       break;
01996     case BFD_RELOC_PPC64_PLTGOT16:        r = R_PPC64_PLTGOT16;
01997       break;
01998     case BFD_RELOC_PPC64_PLTGOT16_LO:            r = R_PPC64_PLTGOT16_LO;
01999       break;
02000     case BFD_RELOC_PPC64_PLTGOT16_HI:            r = R_PPC64_PLTGOT16_HI;
02001       break;
02002     case BFD_RELOC_PPC64_PLTGOT16_HA:            r = R_PPC64_PLTGOT16_HA;
02003       break;
02004     case BFD_RELOC_PPC64_ADDR16_DS:              r = R_PPC64_ADDR16_DS;
02005       break;
02006     case BFD_RELOC_PPC64_ADDR16_LO_DS:           r = R_PPC64_ADDR16_LO_DS;
02007       break;
02008     case BFD_RELOC_PPC64_GOT16_DS:        r = R_PPC64_GOT16_DS;
02009       break;
02010     case BFD_RELOC_PPC64_GOT16_LO_DS:            r = R_PPC64_GOT16_LO_DS;
02011       break;
02012     case BFD_RELOC_PPC64_PLT16_LO_DS:            r = R_PPC64_PLT16_LO_DS;
02013       break;
02014     case BFD_RELOC_PPC64_SECTOFF_DS:             r = R_PPC64_SECTOFF_DS;
02015       break;
02016     case BFD_RELOC_PPC64_SECTOFF_LO_DS:          r = R_PPC64_SECTOFF_LO_DS;
02017       break;
02018     case BFD_RELOC_PPC64_TOC16_DS:        r = R_PPC64_TOC16_DS;
02019       break;
02020     case BFD_RELOC_PPC64_TOC16_LO_DS:            r = R_PPC64_TOC16_LO_DS;
02021       break;
02022     case BFD_RELOC_PPC64_PLTGOT16_DS:            r = R_PPC64_PLTGOT16_DS;
02023       break;
02024     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:  r = R_PPC64_PLTGOT16_LO_DS;
02025       break;
02026     case BFD_RELOC_PPC_TLS:               r = R_PPC64_TLS;
02027       break;
02028     case BFD_RELOC_PPC_DTPMOD:                   r = R_PPC64_DTPMOD64;
02029       break;
02030     case BFD_RELOC_PPC_TPREL16:                  r = R_PPC64_TPREL16;
02031       break;
02032     case BFD_RELOC_PPC_TPREL16_LO:        r = R_PPC64_TPREL16_LO;
02033       break;
02034     case BFD_RELOC_PPC_TPREL16_HI:        r = R_PPC64_TPREL16_HI;
02035       break;
02036     case BFD_RELOC_PPC_TPREL16_HA:        r = R_PPC64_TPREL16_HA;
02037       break;
02038     case BFD_RELOC_PPC_TPREL:                    r = R_PPC64_TPREL64;
02039       break;
02040     case BFD_RELOC_PPC_DTPREL16:          r = R_PPC64_DTPREL16;
02041       break;
02042     case BFD_RELOC_PPC_DTPREL16_LO:              r = R_PPC64_DTPREL16_LO;
02043       break;
02044     case BFD_RELOC_PPC_DTPREL16_HI:              r = R_PPC64_DTPREL16_HI;
02045       break;
02046     case BFD_RELOC_PPC_DTPREL16_HA:              r = R_PPC64_DTPREL16_HA;
02047       break;
02048     case BFD_RELOC_PPC_DTPREL:                   r = R_PPC64_DTPREL64;
02049       break;
02050     case BFD_RELOC_PPC_GOT_TLSGD16:              r = R_PPC64_GOT_TLSGD16;
02051       break;
02052     case BFD_RELOC_PPC_GOT_TLSGD16_LO:           r = R_PPC64_GOT_TLSGD16_LO;
02053       break;
02054     case BFD_RELOC_PPC_GOT_TLSGD16_HI:           r = R_PPC64_GOT_TLSGD16_HI;
02055       break;
02056     case BFD_RELOC_PPC_GOT_TLSGD16_HA:           r = R_PPC64_GOT_TLSGD16_HA;
02057       break;
02058     case BFD_RELOC_PPC_GOT_TLSLD16:              r = R_PPC64_GOT_TLSLD16;
02059       break;
02060     case BFD_RELOC_PPC_GOT_TLSLD16_LO:           r = R_PPC64_GOT_TLSLD16_LO;
02061       break;
02062     case BFD_RELOC_PPC_GOT_TLSLD16_HI:           r = R_PPC64_GOT_TLSLD16_HI;
02063       break;
02064     case BFD_RELOC_PPC_GOT_TLSLD16_HA:           r = R_PPC64_GOT_TLSLD16_HA;
02065       break;
02066     case BFD_RELOC_PPC_GOT_TPREL16:              r = R_PPC64_GOT_TPREL16_DS;
02067       break;
02068     case BFD_RELOC_PPC_GOT_TPREL16_LO:           r = R_PPC64_GOT_TPREL16_LO_DS;
02069       break;
02070     case BFD_RELOC_PPC_GOT_TPREL16_HI:           r = R_PPC64_GOT_TPREL16_HI;
02071       break;
02072     case BFD_RELOC_PPC_GOT_TPREL16_HA:           r = R_PPC64_GOT_TPREL16_HA;
02073       break;
02074     case BFD_RELOC_PPC_GOT_DTPREL16:             r = R_PPC64_GOT_DTPREL16_DS;
02075       break;
02076     case BFD_RELOC_PPC_GOT_DTPREL16_LO:          r = R_PPC64_GOT_DTPREL16_LO_DS;
02077       break;
02078     case BFD_RELOC_PPC_GOT_DTPREL16_HI:          r = R_PPC64_GOT_DTPREL16_HI;
02079       break;
02080     case BFD_RELOC_PPC_GOT_DTPREL16_HA:          r = R_PPC64_GOT_DTPREL16_HA;
02081       break;
02082     case BFD_RELOC_PPC64_TPREL16_DS:             r = R_PPC64_TPREL16_DS;
02083       break;
02084     case BFD_RELOC_PPC64_TPREL16_LO_DS:          r = R_PPC64_TPREL16_LO_DS;
02085       break;
02086     case BFD_RELOC_PPC64_TPREL16_HIGHER:  r = R_PPC64_TPREL16_HIGHER;
02087       break;
02088     case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
02089       break;
02090     case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
02091       break;
02092     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:       r = R_PPC64_TPREL16_HIGHESTA;
02093       break;
02094     case BFD_RELOC_PPC64_DTPREL16_DS:            r = R_PPC64_DTPREL16_DS;
02095       break;
02096     case BFD_RELOC_PPC64_DTPREL16_LO_DS:  r = R_PPC64_DTPREL16_LO_DS;
02097       break;
02098     case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
02099       break;
02100     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:       r = R_PPC64_DTPREL16_HIGHERA;
02101       break;
02102     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:       r = R_PPC64_DTPREL16_HIGHEST;
02103       break;
02104     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:      r = R_PPC64_DTPREL16_HIGHESTA;
02105       break;
02106     case BFD_RELOC_VTABLE_INHERIT:        r = R_PPC64_GNU_VTINHERIT;
02107       break;
02108     case BFD_RELOC_VTABLE_ENTRY:          r = R_PPC64_GNU_VTENTRY;
02109       break;
02110     }
02111 
02112   return ppc64_elf_howto_table[r];
02113 };
02114 
02115 static reloc_howto_type *
02116 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
02117                           const char *r_name)
02118 {
02119   unsigned int i;
02120 
02121   for (i = 0;
02122        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
02123        i++)
02124     if (ppc64_elf_howto_raw[i].name != NULL
02125        && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
02126       return &ppc64_elf_howto_raw[i];
02127 
02128   return NULL;
02129 }
02130 
02131 /* Set the howto pointer for a PowerPC ELF reloc.  */
02132 
02133 static void
02134 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
02135                       Elf_Internal_Rela *dst)
02136 {
02137   unsigned int type;
02138 
02139   /* Initialize howto table if needed.  */
02140   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
02141     ppc_howto_init ();
02142 
02143   type = ELF64_R_TYPE (dst->r_info);
02144   if (type >= (sizeof (ppc64_elf_howto_table)
02145               / sizeof (ppc64_elf_howto_table[0])))
02146     {
02147       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
02148                           abfd, (int) type);
02149       type = R_PPC64_NONE;
02150     }
02151   cache_ptr->howto = ppc64_elf_howto_table[type];
02152 }
02153 
02154 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
02155 
02156 static bfd_reloc_status_type
02157 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02158                   void *data, asection *input_section,
02159                   bfd *output_bfd, char **error_message)
02160 {
02161   /* If this is a relocatable link (output_bfd test tells us), just
02162      call the generic function.  Any adjustment will be done at final
02163      link time.  */
02164   if (output_bfd != NULL)
02165     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02166                               input_section, output_bfd, error_message);
02167 
02168   /* Adjust the addend for sign extension of the low 16 bits.
02169      We won't actually be using the low 16 bits, so trashing them
02170      doesn't matter.  */
02171   reloc_entry->addend += 0x8000;
02172   return bfd_reloc_continue;
02173 }
02174 
02175 static bfd_reloc_status_type
02176 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02177                      void *data, asection *input_section,
02178                      bfd *output_bfd, char **error_message)
02179 {
02180   if (output_bfd != NULL)
02181     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02182                               input_section, output_bfd, error_message);
02183 
02184   if (strcmp (symbol->section->name, ".opd") == 0
02185       && (symbol->section->owner->flags & DYNAMIC) == 0)
02186     {
02187       bfd_vma dest = opd_entry_value (symbol->section,
02188                                   symbol->value + reloc_entry->addend,
02189                                   NULL, NULL);
02190       if (dest != (bfd_vma) -1)
02191        reloc_entry->addend = dest - (symbol->value
02192                                   + symbol->section->output_section->vma
02193                                   + symbol->section->output_offset);
02194     }
02195   return bfd_reloc_continue;
02196 }
02197 
02198 static bfd_reloc_status_type
02199 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02200                       void *data, asection *input_section,
02201                       bfd *output_bfd, char **error_message)
02202 {
02203   long insn;
02204   enum elf_ppc64_reloc_type r_type;
02205   bfd_size_type octets;
02206   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
02207   bfd_boolean is_power4 = FALSE;
02208 
02209   /* If this is a relocatable link (output_bfd test tells us), just
02210      call the generic function.  Any adjustment will be done at final
02211      link time.  */
02212   if (output_bfd != NULL)
02213     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02214                               input_section, output_bfd, error_message);
02215 
02216   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
02217   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
02218   insn &= ~(0x01 << 21);
02219   r_type = reloc_entry->howto->type;
02220   if (r_type == R_PPC64_ADDR14_BRTAKEN
02221       || r_type == R_PPC64_REL14_BRTAKEN)
02222     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
02223 
02224   if (is_power4)
02225     {
02226       /* Set 'a' bit.  This is 0b00010 in BO field for branch
02227         on CR(BI) insns (BO == 001at or 011at), and 0b01000
02228         for branch on CTR insns (BO == 1a00t or 1a01t).  */
02229       if ((insn & (0x14 << 21)) == (0x04 << 21))
02230        insn |= 0x02 << 21;
02231       else if ((insn & (0x14 << 21)) == (0x10 << 21))
02232        insn |= 0x08 << 21;
02233       else
02234        goto out;
02235     }
02236   else
02237     {
02238       bfd_vma target = 0;
02239       bfd_vma from;
02240 
02241       if (!bfd_is_com_section (symbol->section))
02242        target = symbol->value;
02243       target += symbol->section->output_section->vma;
02244       target += symbol->section->output_offset;
02245       target += reloc_entry->addend;
02246 
02247       from = (reloc_entry->address
02248              + input_section->output_offset
02249              + input_section->output_section->vma);
02250 
02251       /* Invert 'y' bit if not the default.  */
02252       if ((bfd_signed_vma) (target - from) < 0)
02253        insn ^= 0x01 << 21;
02254     }
02255   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
02256  out:
02257   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
02258                              input_section, output_bfd, error_message);
02259 }
02260 
02261 static bfd_reloc_status_type
02262 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02263                       void *data, asection *input_section,
02264                       bfd *output_bfd, char **error_message)
02265 {
02266   /* If this is a relocatable link (output_bfd test tells us), just
02267      call the generic function.  Any adjustment will be done at final
02268      link time.  */
02269   if (output_bfd != NULL)
02270     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02271                               input_section, output_bfd, error_message);
02272 
02273   /* Subtract the symbol section base address.  */
02274   reloc_entry->addend -= symbol->section->output_section->vma;
02275   return bfd_reloc_continue;
02276 }
02277 
02278 static bfd_reloc_status_type
02279 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02280                          void *data, asection *input_section,
02281                          bfd *output_bfd, char **error_message)
02282 {
02283   /* If this is a relocatable link (output_bfd test tells us), just
02284      call the generic function.  Any adjustment will be done at final
02285      link time.  */
02286   if (output_bfd != NULL)
02287     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02288                               input_section, output_bfd, error_message);
02289 
02290   /* Subtract the symbol section base address.  */
02291   reloc_entry->addend -= symbol->section->output_section->vma;
02292 
02293   /* Adjust the addend for sign extension of the low 16 bits.  */
02294   reloc_entry->addend += 0x8000;
02295   return bfd_reloc_continue;
02296 }
02297 
02298 static bfd_reloc_status_type
02299 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02300                    void *data, asection *input_section,
02301                    bfd *output_bfd, char **error_message)
02302 {
02303   bfd_vma TOCstart;
02304 
02305   /* If this is a relocatable link (output_bfd test tells us), just
02306      call the generic function.  Any adjustment will be done at final
02307      link time.  */
02308   if (output_bfd != NULL)
02309     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02310                               input_section, output_bfd, error_message);
02311 
02312   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
02313   if (TOCstart == 0)
02314     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
02315 
02316   /* Subtract the TOC base address.  */
02317   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
02318   return bfd_reloc_continue;
02319 }
02320 
02321 static bfd_reloc_status_type
02322 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02323                      void *data, asection *input_section,
02324                      bfd *output_bfd, char **error_message)
02325 {
02326   bfd_vma TOCstart;
02327 
02328   /* If this is a relocatable link (output_bfd test tells us), just
02329      call the generic function.  Any adjustment will be done at final
02330      link time.  */
02331   if (output_bfd != NULL)
02332     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02333                               input_section, output_bfd, error_message);
02334 
02335   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
02336   if (TOCstart == 0)
02337     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
02338 
02339   /* Subtract the TOC base address.  */
02340   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
02341 
02342   /* Adjust the addend for sign extension of the low 16 bits.  */
02343   reloc_entry->addend += 0x8000;
02344   return bfd_reloc_continue;
02345 }
02346 
02347 static bfd_reloc_status_type
02348 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02349                      void *data, asection *input_section,
02350                      bfd *output_bfd, char **error_message)
02351 {
02352   bfd_vma TOCstart;
02353   bfd_size_type octets;
02354 
02355   /* If this is a relocatable link (output_bfd test tells us), just
02356      call the generic function.  Any adjustment will be done at final
02357      link time.  */
02358   if (output_bfd != NULL)
02359     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02360                               input_section, output_bfd, error_message);
02361 
02362   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
02363   if (TOCstart == 0)
02364     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
02365 
02366   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
02367   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
02368   return bfd_reloc_ok;
02369 }
02370 
02371 static bfd_reloc_status_type
02372 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
02373                         void *data, asection *input_section,
02374                         bfd *output_bfd, char **error_message)
02375 {
02376   /* If this is a relocatable link (output_bfd test tells us), just
02377      call the generic function.  Any adjustment will be done at final
02378      link time.  */
02379   if (output_bfd != NULL)
02380     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
02381                               input_section, output_bfd, error_message);
02382 
02383   if (error_message != NULL)
02384     {
02385       static char buf[60];
02386       sprintf (buf, "generic linker can't handle %s",
02387               reloc_entry->howto->name);
02388       *error_message = buf;
02389     }
02390   return bfd_reloc_dangerous;
02391 }
02392 
02393 struct ppc64_elf_obj_tdata
02394 {
02395   struct elf_obj_tdata elf;
02396 
02397   /* Shortcuts to dynamic linker sections.  */
02398   asection *got;
02399   asection *relgot;
02400 
02401   /* Used during garbage collection.  We attach global symbols defined
02402      on removed .opd entries to this section so that the sym is removed.  */
02403   asection *deleted_section;
02404 
02405   /* TLS local dynamic got entry handling.  Suppose for multiple GOT
02406      sections means we potentially need one of these for each input bfd.  */
02407   union {
02408     bfd_signed_vma refcount;
02409     bfd_vma offset;
02410   } tlsld_got;
02411 
02412   /* A copy of relocs before they are modified for --emit-relocs.  */
02413   Elf_Internal_Rela *opd_relocs;
02414 };
02415 
02416 #define ppc64_elf_tdata(bfd) \
02417   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
02418 
02419 #define ppc64_tlsld_got(bfd) \
02420   (&ppc64_elf_tdata (bfd)->tlsld_got)
02421 
02422 /* Override the generic function because we store some extras.  */
02423 
02424 static bfd_boolean
02425 ppc64_elf_mkobject (bfd *abfd)
02426 {
02427   if (abfd->tdata.any == NULL)
02428     {
02429       bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
02430       abfd->tdata.any = bfd_zalloc (abfd, amt);
02431       if (abfd->tdata.any == NULL)
02432        return FALSE;
02433     }
02434   return bfd_elf_mkobject (abfd);
02435 }
02436 
02437 /* Return 1 if target is one of ours.  */
02438 
02439 static bfd_boolean
02440 is_ppc64_elf_target (const struct bfd_target *targ)
02441 {
02442   extern const bfd_target bfd_elf64_powerpc_vec;
02443   extern const bfd_target bfd_elf64_powerpcle_vec;
02444 
02445   return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
02446 }
02447 
02448 /* Fix bad default arch selected for a 64 bit input bfd when the
02449    default is 32 bit.  */
02450 
02451 static bfd_boolean
02452 ppc64_elf_object_p (bfd *abfd)
02453 {
02454   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
02455     {
02456       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
02457 
02458       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
02459        {
02460          /* Relies on arch after 32 bit default being 64 bit default.  */
02461          abfd->arch_info = abfd->arch_info->next;
02462          BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
02463        }
02464     }
02465   return TRUE;
02466 }
02467 
02468 /* Support for core dump NOTE sections.  */
02469 
02470 static bfd_boolean
02471 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
02472 {
02473   size_t offset, size;
02474 
02475   if (note->descsz != 504)
02476     return FALSE;
02477 
02478   /* pr_cursig */
02479   elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
02480 
02481   /* pr_pid */
02482   elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
02483 
02484   /* pr_reg */
02485   offset = 112;
02486   size = 384;
02487 
02488   /* Make a ".reg/999" section.  */
02489   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
02490                                      size, note->descpos + offset);
02491 }
02492 
02493 static bfd_boolean
02494 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
02495 {
02496   if (note->descsz != 136)
02497     return FALSE;
02498 
02499   elf_tdata (abfd)->core_program
02500     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
02501   elf_tdata (abfd)->core_command
02502     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
02503 
02504   return TRUE;
02505 }
02506 
02507 static char *
02508 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
02509                         ...)
02510 {
02511   switch (note_type)
02512     {
02513     default:
02514       return NULL;
02515 
02516     case NT_PRPSINFO:
02517       {
02518        char data[136];
02519        va_list ap;
02520 
02521        va_start (ap, note_type);
02522        memset (data, 0, 40);
02523        strncpy (data + 40, va_arg (ap, const char *), 16);
02524        strncpy (data + 56, va_arg (ap, const char *), 80);
02525        va_end (ap);
02526        return elfcore_write_note (abfd, buf, bufsiz,
02527                                "CORE", note_type, data, sizeof (data));
02528       }
02529 
02530     case NT_PRSTATUS:
02531       {
02532        char data[504];
02533        va_list ap;
02534        long pid;
02535        int cursig;
02536        const void *greg;
02537 
02538        va_start (ap, note_type);
02539        memset (data, 0, 112);
02540        pid = va_arg (ap, long);
02541        bfd_put_32 (abfd, pid, data + 32);
02542        cursig = va_arg (ap, int);
02543        bfd_put_16 (abfd, cursig, data + 12);
02544        greg = va_arg (ap, const void *);
02545        memcpy (data + 112, greg, 384);
02546        memset (data + 496, 0, 8);
02547        va_end (ap);
02548        return elfcore_write_note (abfd, buf, bufsiz,
02549                                "CORE", note_type, data, sizeof (data));
02550       }
02551     }
02552 }
02553 
02554 /* Merge backend specific data from an object file to the output
02555    object file when linking.  */
02556 
02557 static bfd_boolean
02558 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
02559 {
02560   /* Check if we have the same endianess.  */
02561   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
02562       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
02563       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
02564     {
02565       const char *msg;
02566 
02567       if (bfd_big_endian (ibfd))
02568        msg = _("%B: compiled for a big endian system "
02569               "and target is little endian");
02570       else
02571        msg = _("%B: compiled for a little endian system "
02572               "and target is big endian");
02573 
02574       (*_bfd_error_handler) (msg, ibfd);
02575 
02576       bfd_set_error (bfd_error_wrong_format);
02577       return FALSE;
02578     }
02579 
02580   return TRUE;
02581 }
02582 
02583 /* Add extra PPC sections.  */
02584 
02585 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
02586 {
02587   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
02588   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
02589   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
02590   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
02591   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
02592   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
02593   { NULL,                     0,  0, 0,            0 }
02594 };
02595 
02596 enum _ppc64_sec_type {
02597   sec_normal = 0,
02598   sec_opd = 1,
02599   sec_toc = 2
02600 };
02601 
02602 struct _ppc64_elf_section_data
02603 {
02604   struct bfd_elf_section_data elf;
02605 
02606   /* An array with one entry for each opd function descriptor.  */
02607   union
02608   {
02609     /* Points to the function code section for local opd entries.  */
02610     asection **opd_func_sec;
02611     /* After editing .opd, adjust references to opd local syms.  */
02612     long *opd_adjust;
02613 
02614     /* An array for toc sections, indexed by offset/8.
02615        Specifies the relocation symbol index used at a given toc offset.  */
02616     unsigned *t_symndx;
02617   } u;
02618 
02619   enum _ppc64_sec_type sec_type:2;
02620 
02621   /* Flag set when small branches are detected.  Used to
02622      select suitable defaults for the stub group size.  */
02623   unsigned int has_14bit_branch:1;
02624 };
02625 
02626 #define ppc64_elf_section_data(sec) \
02627   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
02628 
02629 static bfd_boolean
02630 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
02631 {
02632   if (!sec->used_by_bfd)
02633     {
02634       struct _ppc64_elf_section_data *sdata;
02635       bfd_size_type amt = sizeof (*sdata);
02636 
02637       sdata = bfd_zalloc (abfd, amt);
02638       if (sdata == NULL)
02639        return FALSE;
02640       sec->used_by_bfd = sdata;
02641     }
02642 
02643   return _bfd_elf_new_section_hook (abfd, sec);
02644 }
02645 
02646 static void *
02647 get_opd_info (asection * sec)
02648 {
02649   if (sec != NULL
02650       && ppc64_elf_section_data (sec) != NULL
02651       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
02652     return ppc64_elf_section_data (sec)->u.opd_adjust;
02653   return NULL;
02654 }
02655 
02656 /* Parameters for the qsort hook.  */
02657 static asection *synthetic_opd;
02658 static bfd_boolean synthetic_relocatable;
02659 
02660 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
02661 
02662 static int
02663 compare_symbols (const void *ap, const void *bp)
02664 {
02665   const asymbol *a = * (const asymbol **) ap;
02666   const asymbol *b = * (const asymbol **) bp;
02667 
02668   /* Section symbols first.  */
02669   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
02670     return -1;
02671   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
02672     return 1;
02673 
02674   /* then .opd symbols.  */
02675   if (a->section == synthetic_opd && b->section != synthetic_opd)
02676     return -1;
02677   if (a->section != synthetic_opd && b->section == synthetic_opd)
02678     return 1;
02679 
02680   /* then other code symbols.  */
02681   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
02682       == (SEC_CODE | SEC_ALLOC)
02683       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
02684         != (SEC_CODE | SEC_ALLOC))
02685     return -1;
02686 
02687   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
02688       != (SEC_CODE | SEC_ALLOC)
02689       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
02690         == (SEC_CODE | SEC_ALLOC))
02691     return 1;
02692 
02693   if (synthetic_relocatable)
02694     {
02695       if (a->section->id < b->section->id)
02696        return -1;
02697 
02698       if (a->section->id > b->section->id)
02699        return 1;
02700     }
02701 
02702   if (a->value + a->section->vma < b->value + b->section->vma)
02703     return -1;
02704 
02705   if (a->value + a->section->vma > b->value + b->section->vma)
02706     return 1;
02707 
02708   /* For syms with the same value, prefer strong dynamic global function
02709      syms over other syms.  */
02710   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
02711     return -1;
02712 
02713   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
02714     return 1;
02715 
02716   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
02717     return -1;
02718 
02719   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
02720     return 1;
02721 
02722   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
02723     return -1;
02724 
02725   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
02726     return 1;
02727 
02728   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
02729     return -1;
02730 
02731   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
02732     return 1;
02733 
02734   return 0;
02735 }
02736 
02737 /* Search SYMS for a symbol of the given VALUE.  */
02738 
02739 static asymbol *
02740 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
02741 {
02742   long mid;
02743 
02744   if (id == -1)
02745     {
02746       while (lo < hi)
02747        {
02748          mid = (lo + hi) >> 1;
02749          if (syms[mid]->value + syms[mid]->section->vma < value)
02750            lo = mid + 1;
02751          else if (syms[mid]->value + syms[mid]->section->vma > value)
02752            hi = mid;
02753          else
02754            return syms[mid];
02755        }
02756     }
02757   else
02758     {
02759       while (lo < hi)
02760        {
02761          mid = (lo + hi) >> 1;
02762          if (syms[mid]->section->id < id)
02763            lo = mid + 1;
02764          else if (syms[mid]->section->id > id)
02765            hi = mid;
02766          else if (syms[mid]->value < value)
02767            lo = mid + 1;
02768          else if (syms[mid]->value > value)
02769            hi = mid;
02770          else
02771            return syms[mid];
02772        }
02773     }
02774   return NULL;
02775 }
02776 
02777 /* Create synthetic symbols, effectively restoring "dot-symbol" function
02778    entry syms.  */
02779 
02780 static long
02781 ppc64_elf_get_synthetic_symtab (bfd *abfd,
02782                             long static_count, asymbol **static_syms,
02783                             long dyn_count, asymbol **dyn_syms,
02784                             asymbol **ret)
02785 {
02786   asymbol *s;
02787   long i;
02788   long count;
02789   char *names;
02790   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
02791   asection *opd;
02792   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
02793   asymbol **syms;
02794 
02795   *ret = NULL;
02796 
02797   opd = bfd_get_section_by_name (abfd, ".opd");
02798   if (opd == NULL)
02799     return 0;
02800 
02801   symcount = static_count;
02802   if (!relocatable)
02803     symcount += dyn_count;
02804   if (symcount == 0)
02805     return 0;
02806 
02807   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
02808   if (syms == NULL)
02809     return -1;
02810 
02811   if (!relocatable && static_count != 0 && dyn_count != 0)
02812     {
02813       /* Use both symbol tables.  */
02814       memcpy (syms, static_syms, static_count * sizeof (*syms));
02815       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
02816     }
02817   else if (!relocatable && static_count == 0)
02818     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
02819   else
02820     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
02821 
02822   synthetic_opd = opd;
02823   synthetic_relocatable = relocatable;
02824   qsort (syms, symcount, sizeof (*syms), compare_symbols);
02825 
02826   if (!relocatable && symcount > 1)
02827     {
02828       long j;
02829       /* Trim duplicate syms, since we may have merged the normal and
02830         dynamic symbols.  Actually, we only care about syms that have
02831         different values, so trim any with the same value.  */
02832       for (i = 1, j = 1; i < symcount; ++i)
02833        if (syms[i - 1]->value + syms[i - 1]->section->vma
02834            != syms[i]->value + syms[i]->section->vma)
02835          syms[j++] = syms[i];
02836       symcount = j;
02837     }
02838 
02839   i = 0;
02840   if (syms[i]->section == opd)
02841     ++i;
02842   codesecsym = i;
02843 
02844   for (; i < symcount; ++i)
02845     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
02846         != (SEC_CODE | SEC_ALLOC))
02847        || (syms[i]->flags & BSF_SECTION_SYM) == 0)
02848       break;
02849   codesecsymend = i;
02850 
02851   for (; i < symcount; ++i)
02852     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
02853       break;
02854   secsymend = i;
02855 
02856   for (; i < symcount; ++i)
02857     if (syms[i]->section != opd)
02858       break;
02859   opdsymend = i;
02860 
02861   for (; i < symcount; ++i)
02862     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
02863        != (SEC_CODE | SEC_ALLOC))
02864       break;
02865   symcount = i;
02866 
02867   count = 0;
02868   if (opdsymend == secsymend)
02869     goto done;
02870 
02871   if (relocatable)
02872     {
02873       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
02874       arelent *r;
02875       size_t size;
02876       long relcount;
02877 
02878       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
02879       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
02880       if (relcount == 0)
02881        goto done;
02882 
02883       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
02884        {
02885          count = -1;
02886          goto done;
02887        }
02888 
02889       size = 0;
02890       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
02891        {
02892          asymbol *sym;
02893 
02894          while (r < opd->relocation + relcount
02895                && r->address < syms[i]->value + opd->vma)
02896            ++r;
02897 
02898          if (r == opd->relocation + relcount)
02899            break;
02900 
02901          if (r->address != syms[i]->value + opd->vma)
02902            continue;
02903 
02904          if (r->howto->type != R_PPC64_ADDR64)
02905            continue;
02906 
02907          sym = *r->sym_ptr_ptr;
02908          if (!sym_exists_at (syms, opdsymend, symcount,
02909                            sym->section->id, sym->value + r->addend))
02910            {
02911              ++count;
02912              size += sizeof (asymbol);
02913              size += strlen (syms[i]->name) + 2;
02914            }
02915        }
02916 
02917       s = *ret = bfd_malloc (size);
02918       if (s == NULL)
02919        {
02920          count = -1;
02921          goto done;
02922        }
02923 
02924       names = (char *) (s + count);
02925 
02926       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
02927        {
02928          asymbol *sym;
02929 
02930          while (r < opd->relocation + relcount
02931                && r->address < syms[i]->value + opd->vma)
02932            ++r;
02933 
02934          if (r == opd->relocation + relcount)
02935            break;
02936 
02937          if (r->address != syms[i]->value + opd->vma)
02938            continue;
02939 
02940          if (r->howto->type != R_PPC64_ADDR64)
02941            continue;
02942 
02943          sym = *r->sym_ptr_ptr;
02944          if (!sym_exists_at (syms, opdsymend, symcount,
02945                            sym->section->id, sym->value + r->addend))
02946            {
02947              size_t len;
02948 
02949              *s = *syms[i];
02950              s->section = sym->section;
02951              s->value = sym->value + r->addend;
02952              s->name = names;
02953              *names++ = '.';
02954              len = strlen (syms[i]->name);
02955              memcpy (names, syms[i]->name, len + 1);
02956              names += len + 1;
02957              s++;
02958            }
02959        }
02960     }
02961   else
02962     {
02963       bfd_byte *contents;
02964       size_t size;
02965 
02966       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
02967        {
02968          if (contents)
02969            {
02970            free_contents_and_exit:
02971              free (contents);
02972            }
02973          count = -1;
02974          goto done;
02975        }
02976 
02977       size = 0;
02978       for (i = secsymend; i < opdsymend; ++i)
02979        {
02980          bfd_vma ent;
02981 
02982          ent = bfd_get_64 (abfd, contents + syms[i]->value);
02983          if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
02984            {
02985              ++count;
02986              size += sizeof (asymbol);
02987              size += strlen (syms[i]->name) + 2;
02988            }
02989        }
02990 
02991       s = *ret = bfd_malloc (size);
02992       if (s == NULL)
02993        goto free_contents_and_exit;
02994 
02995       names = (char *) (s + count);
02996 
02997       for (i = secsymend; i < opdsymend; ++i)
02998        {
02999          bfd_vma ent;
03000 
03001          ent = bfd_get_64 (abfd, contents + syms[i]->value);
03002          if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
03003            {
03004              long lo, hi;
03005              size_t len;
03006              asection *sec = abfd->sections;
03007 
03008              *s = *syms[i];
03009              lo = codesecsym;
03010              hi = codesecsymend;
03011              while (lo < hi)
03012               {
03013                 long mid = (lo + hi) >> 1;
03014                 if (syms[mid]->section->vma < ent)
03015                   lo = mid + 1;
03016                 else if (syms[mid]->section->vma > ent)
03017                   hi = mid;
03018                 else
03019                   {
03020                     sec = syms[mid]->section;
03021                     break;
03022                   }
03023               }
03024 
03025              if (lo >= hi && lo > codesecsym)
03026               sec = syms[lo - 1]->section;
03027 
03028              for (; sec != NULL; sec = sec->next)
03029               {
03030                 if (sec->vma > ent)
03031                   break;
03032                 if ((sec->flags & SEC_ALLOC) == 0
03033                     || (sec->flags & SEC_LOAD) == 0)
03034                   break;
03035                 if ((sec->flags & SEC_CODE) != 0)
03036                   s->section = sec;
03037               }
03038              s->value = ent - s->section->vma;
03039              s->name = names;
03040              *names++ = '.';
03041              len = strlen (syms[i]->name);
03042              memcpy (names, syms[i]->name, len + 1);
03043              names += len + 1;
03044              s++;
03045            }
03046        }
03047       free (contents);
03048     }
03049 
03050  done:
03051   free (syms);
03052   return count;
03053 }
03054 
03055 /* The following functions are specific to the ELF linker, while
03056    functions above are used generally.  Those named ppc64_elf_* are
03057    called by the main ELF linker code.  They appear in this file more
03058    or less in the order in which they are called.  eg.
03059    ppc64_elf_check_relocs is called early in the link process,
03060    ppc64_elf_finish_dynamic_sections is one of the last functions
03061    called.
03062 
03063    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
03064    functions have both a function code symbol and a function descriptor
03065    symbol.  A call to foo in a relocatable object file looks like:
03066 
03067    .          .text
03068    .   x:
03069    .          bl     .foo
03070    .          nop
03071 
03072    The function definition in another object file might be:
03073 
03074    .          .section .opd
03075    .   foo:   .quad  .foo
03076    .          .quad  .TOC.@tocbase
03077    .          .quad  0
03078    .
03079    .          .text
03080    .   .foo:  blr
03081 
03082    When the linker resolves the call during a static link, the branch
03083    unsurprisingly just goes to .foo and the .opd information is unused.
03084    If the function definition is in a shared library, things are a little
03085    different:  The call goes via a plt call stub, the opd information gets
03086    copied to the plt, and the linker patches the nop.
03087 
03088    .   x:
03089    .          bl     .foo_stub
03090    .          ld     2,40(1)
03091    .
03092    .
03093    .   .foo_stub:
03094    .          addis  12,2,Lfoo@toc@ha     # in practice, the call stub
03095    .          addi   12,12,Lfoo@toc@l     # is slightly optimized, but
03096    .          std    2,40(1)                     # this is the general idea
03097    .          ld     11,0(12)
03098    .          ld     2,8(12)
03099    .          mtctr  11
03100    .          ld     11,16(12)
03101    .          bctr
03102    .
03103    .          .section .plt
03104    .   Lfoo:  reloc (R_PPC64_JMP_SLOT, foo)
03105 
03106    The "reloc ()" notation is supposed to indicate that the linker emits
03107    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
03108    copying.
03109 
03110    What are the difficulties here?  Well, firstly, the relocations
03111    examined by the linker in check_relocs are against the function code
03112    sym .foo, while the dynamic relocation in the plt is emitted against
03113    the function descriptor symbol, foo.  Somewhere along the line, we need
03114    to carefully copy dynamic link information from one symbol to the other.
03115    Secondly, the generic part of the elf linker will make .foo a dynamic
03116    symbol as is normal for most other backends.  We need foo dynamic
03117    instead, at least for an application final link.  However, when
03118    creating a shared library containing foo, we need to have both symbols
03119    dynamic so that references to .foo are satisfied during the early
03120    stages of linking.  Otherwise the linker might decide to pull in a
03121    definition from some other object, eg. a static library.
03122 
03123    Update: As of August 2004, we support a new convention.  Function
03124    calls may use the function descriptor symbol, ie. "bl foo".  This
03125    behaves exactly as "bl .foo".  */
03126 
03127 /* The linker needs to keep track of the number of relocs that it
03128    decides to copy as dynamic relocs in check_relocs for each symbol.
03129    This is so that it can later discard them if they are found to be
03130    unnecessary.  We store the information in a field extending the
03131    regular ELF linker hash table.  */
03132 
03133 struct ppc_dyn_relocs
03134 {
03135   struct ppc_dyn_relocs *next;
03136 
03137   /* The input section of the reloc.  */
03138   asection *sec;
03139 
03140   /* Total number of relocs copied for the input section.  */
03141   bfd_size_type count;
03142 
03143   /* Number of pc-relative relocs copied for the input section.  */
03144   bfd_size_type pc_count;
03145 };
03146 
03147 /* Track GOT entries needed for a given symbol.  We might need more
03148    than one got entry per symbol.  */
03149 struct got_entry
03150 {
03151   struct got_entry *next;
03152 
03153   /* The symbol addend that we'll be placing in the GOT.  */
03154   bfd_vma addend;
03155 
03156   /* Unlike other ELF targets, we use separate GOT entries for the same
03157      symbol referenced from different input files.  This is to support
03158      automatic multiple TOC/GOT sections, where the TOC base can vary
03159      from one input file to another.
03160 
03161      Point to the BFD owning this GOT entry.  */
03162   bfd *owner;
03163 
03164   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
03165      TLS_TPREL or TLS_DTPREL for tls entries.  */
03166   char tls_type;
03167 
03168   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
03169   union
03170     {
03171       bfd_signed_vma refcount;
03172       bfd_vma offset;
03173     } got;
03174 };
03175 
03176 /* The same for PLT.  */
03177 struct plt_entry
03178 {
03179   struct plt_entry *next;
03180 
03181   bfd_vma addend;
03182 
03183   union
03184     {
03185       bfd_signed_vma refcount;
03186       bfd_vma offset;
03187     } plt;
03188 };
03189 
03190 /* Of those relocs that might be copied as dynamic relocs, this macro
03191    selects those that must be copied when linking a shared library,
03192    even when the symbol is local.  */
03193 
03194 #define MUST_BE_DYN_RELOC(RTYPE)          \
03195   ((RTYPE) != R_PPC64_REL32               \
03196    && (RTYPE) != R_PPC64_REL64                   \
03197    && (RTYPE) != R_PPC64_REL30)
03198 
03199 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
03200    copying dynamic variables from a shared lib into an app's dynbss
03201    section, and instead use a dynamic relocation to point into the
03202    shared lib.  With code that gcc generates, it's vital that this be
03203    enabled;  In the PowerPC64 ABI, the address of a function is actually
03204    the address of a function descriptor, which resides in the .opd
03205    section.  gcc uses the descriptor directly rather than going via the
03206    GOT as some other ABI's do, which means that initialized function
03207    pointers must reference the descriptor.  Thus, a function pointer
03208    initialized to the address of a function in a shared library will
03209    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
03210    redefines the function descriptor symbol to point to the copy.  This
03211    presents a problem as a plt entry for that function is also
03212    initialized from the function descriptor symbol and the copy reloc
03213    may not be initialized first.  */
03214 #define ELIMINATE_COPY_RELOCS 1
03215 
03216 /* Section name for stubs is the associated section name plus this
03217    string.  */
03218 #define STUB_SUFFIX ".stub"
03219 
03220 /* Linker stubs.
03221    ppc_stub_long_branch:
03222    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
03223    destination, but a 24 bit branch in a stub section will reach.
03224    .   b      dest
03225 
03226    ppc_stub_plt_branch:
03227    Similar to the above, but a 24 bit branch in the stub section won't
03228    reach its destination.
03229    .   addis  %r12,%r2,xxx@toc@ha
03230    .   ld     %r11,xxx@toc@l(%r12)
03231    .   mtctr  %r11
03232    .   bctr
03233 
03234    ppc_stub_plt_call:
03235    Used to call a function in a shared library.  If it so happens that
03236    the plt entry referenced crosses a 64k boundary, then an extra
03237    "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
03238    xxx+16 as appropriate.
03239    .   addis  %r12,%r2,xxx@toc@ha
03240    .   std    %r2,40(%r1)
03241    .   ld     %r11,xxx+0@toc@l(%r12)
03242    .   ld     %r2,xxx+8@toc@l(%r12)
03243    .   mtctr  %r11
03244    .   ld     %r11,xxx+16@toc@l(%r12)
03245    .   bctr
03246 
03247    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
03248    code to adjust the value and save r2 to support multiple toc sections.
03249    A ppc_stub_long_branch with an r2 offset looks like:
03250    .   std    %r2,40(%r1)
03251    .   addis  %r2,%r2,off@ha
03252    .   addi   %r2,%r2,off@l
03253    .   b      dest
03254 
03255    A ppc_stub_plt_branch with an r2 offset looks like:
03256    .   std    %r2,40(%r1)
03257    .   addis  %r12,%r2,xxx@toc@ha
03258    .   ld     %r11,xxx@toc@l(%r12)
03259    .   addis  %r2,%r2,off@ha
03260    .   addi   %r2,%r2,off@l
03261    .   mtctr  %r11
03262    .   bctr
03263 */
03264 
03265 enum ppc_stub_type {
03266   ppc_stub_none,
03267   ppc_stub_long_branch,
03268   ppc_stub_long_branch_r2off,
03269   ppc_stub_plt_branch,
03270   ppc_stub_plt_branch_r2off,
03271   ppc_stub_plt_call
03272 };
03273 
03274 struct ppc_stub_hash_entry {
03275 
03276   /* Base hash table entry structure.  */
03277   struct bfd_hash_entry root;
03278 
03279   enum ppc_stub_type stub_type;
03280 
03281   /* The stub section.  */
03282   asection *stub_sec;
03283 
03284   /* Offset within stub_sec of the beginning of this stub.  */
03285   bfd_vma stub_offset;
03286 
03287   /* Given the symbol's value and its section we can determine its final
03288      value when building the stubs (so the stub knows where to jump.  */
03289   bfd_vma target_value;
03290   asection *target_section;
03291 
03292   /* The symbol table entry, if any, that this was derived from.  */
03293   struct ppc_link_hash_entry *h;
03294 
03295   /* And the reloc addend that this was derived from.  */
03296   bfd_vma addend;
03297 
03298   /* Where this stub is being called from, or, in the case of combined
03299      stub sections, the first input section in the group.  */
03300   asection *id_sec;
03301 };
03302 
03303 struct ppc_branch_hash_entry {
03304 
03305   /* Base hash table entry structure.  */
03306   struct bfd_hash_entry root;
03307 
03308   /* Offset within branch lookup table.  */
03309   unsigned int offset;
03310 
03311   /* Generation marker.  */
03312   unsigned int iter;
03313 };
03314 
03315 struct ppc_link_hash_entry
03316 {
03317   struct elf_link_hash_entry elf;
03318 
03319   union {
03320     /* A pointer to the most recently used stub hash entry against this
03321        symbol.  */
03322     struct ppc_stub_hash_entry *stub_cache;
03323 
03324     /* A pointer to the next symbol starting with a '.'  */
03325     struct ppc_link_hash_entry *next_dot_sym;
03326   } u;
03327 
03328   /* Track dynamic relocs copied for this symbol.  */
03329   struct ppc_dyn_relocs *dyn_relocs;
03330 
03331   /* Link between function code and descriptor symbols.  */
03332   struct ppc_link_hash_entry *oh;
03333 
03334   /* Flag function code and descriptor symbols.  */
03335   unsigned int is_func:1;
03336   unsigned int is_func_descriptor:1;
03337   unsigned int fake:1;
03338 
03339   /* Whether global opd/toc sym has been adjusted or not.
03340      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
03341      should be set for all globals defined in any opd/toc section.  */
03342   unsigned int adjust_done:1;
03343 
03344   /* Set if we twiddled this symbol to weak at some stage.  */
03345   unsigned int was_undefined:1;
03346 
03347   /* Contexts in which symbol is used in the GOT (or TOC).
03348      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
03349      corresponding relocs are encountered during check_relocs.
03350      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
03351      indicate the corresponding GOT entry type is not needed.
03352      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
03353      a TPREL one.  We use a separate flag rather than setting TPREL
03354      just for convenience in distinguishing the two cases.  */
03355 #define TLS_GD               1     /* GD reloc. */
03356 #define TLS_LD               2     /* LD reloc. */
03357 #define TLS_TPREL     4     /* TPREL reloc, => IE. */
03358 #define TLS_DTPREL    8     /* DTPREL reloc, => LD. */
03359 #define TLS_TLS             16     /* Any TLS reloc.  */
03360 #define TLS_EXPLICIT 32     /* Marks TOC section TLS relocs. */
03361 #define TLS_TPRELGD  64     /* TPREL reloc resulting from GD->IE. */
03362   char tls_mask;
03363 };
03364 
03365 /* ppc64 ELF linker hash table.  */
03366 
03367 struct ppc_link_hash_table
03368 {
03369   struct elf_link_hash_table elf;
03370 
03371   /* The stub hash table.  */
03372   struct bfd_hash_table stub_hash_table;
03373 
03374   /* Another hash table for plt_branch stubs.  */
03375   struct bfd_hash_table branch_hash_table;
03376 
03377   /* Linker stub bfd.  */
03378   bfd *stub_bfd;
03379 
03380   /* Linker call-backs.  */
03381   asection * (*add_stub_section) (const char *, asection *);
03382   void (*layout_sections_again) (void);
03383 
03384   /* Array to keep track of which stub sections have been created, and
03385      information on stub grouping.  */
03386   struct map_stub {
03387     /* This is the section to which stubs in the group will be attached.  */
03388     asection *link_sec;
03389     /* The stub section.  */
03390     asection *stub_sec;
03391     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
03392     bfd_vma toc_off;
03393   } *stub_group;
03394 
03395   /* Temp used when calculating TOC pointers.  */
03396   bfd_vma toc_curr;
03397 
03398   /* Highest input section id.  */
03399   int top_id;
03400 
03401   /* Highest output section index.  */
03402   int top_index;
03403 
03404   /* Used when adding symbols.  */
03405   struct ppc_link_hash_entry *dot_syms;
03406 
03407   /* List of input sections for each output section.  */
03408   asection **input_list;
03409 
03410   /* Short-cuts to get to dynamic linker sections.  */
03411   asection *got;
03412   asection *plt;
03413   asection *relplt;
03414   asection *dynbss;
03415   asection *relbss;
03416   asection *glink;
03417   asection *sfpr;
03418   asection *brlt;
03419   asection *relbrlt;
03420 
03421   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
03422   struct ppc_link_hash_entry *tls_get_addr;
03423   struct ppc_link_hash_entry *tls_get_addr_fd;
03424 
03425   /* Statistics.  */
03426   unsigned long stub_count[ppc_stub_plt_call];
03427 
03428   /* Number of stubs against global syms.  */
03429   unsigned long stub_globals;
03430 
03431   /* Set if we should emit symbols for stubs.  */
03432   unsigned int emit_stub_syms:1;
03433 
03434   /* Support for multiple toc sections.  */
03435   unsigned int no_multi_toc:1;
03436   unsigned int multi_toc_needed:1;
03437 
03438   /* Set on error.  */
03439   unsigned int stub_error:1;
03440 
03441   /* Temp used by ppc64_elf_check_directives.  */
03442   unsigned int twiddled_syms:1;
03443 
03444   /* Incremented every time we size stubs.  */
03445   unsigned int stub_iteration;
03446 
03447   /* Small local sym to section mapping cache.  */
03448   struct sym_sec_cache sym_sec;
03449 };
03450 
03451 /* Rename some of the generic section flags to better document how they
03452    are used here.  */
03453 #define has_toc_reloc has_gp_reloc
03454 #define makes_toc_func_call need_finalize_relax
03455 #define call_check_in_progress reloc_done
03456 
03457 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
03458 
03459 #define ppc_hash_table(p) \
03460   ((struct ppc_link_hash_table *) ((p)->hash))
03461 
03462 #define ppc_stub_hash_lookup(table, string, create, copy) \
03463   ((struct ppc_stub_hash_entry *) \
03464    bfd_hash_lookup ((table), (string), (create), (copy)))
03465 
03466 #define ppc_branch_hash_lookup(table, string, create, copy) \
03467   ((struct ppc_branch_hash_entry *) \
03468    bfd_hash_lookup ((table), (string), (create), (copy)))
03469 
03470 /* Create an entry in the stub hash table.  */
03471 
03472 static struct bfd_hash_entry *
03473 stub_hash_newfunc (struct bfd_hash_entry *entry,
03474                  struct bfd_hash_table *table,
03475                  const char *string)
03476 {
03477   /* Allocate the structure if it has not already been allocated by a
03478      subclass.  */
03479   if (entry == NULL)
03480     {
03481       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
03482       if (entry == NULL)
03483        return entry;
03484     }
03485 
03486   /* Call the allocation method of the superclass.  */
03487   entry = bfd_hash_newfunc (entry, table, string);
03488   if (entry != NULL)
03489     {
03490       struct ppc_stub_hash_entry *eh;
03491 
03492       /* Initialize the local fields.  */
03493       eh = (struct ppc_stub_hash_entry *) entry;
03494       eh->stub_type = ppc_stub_none;
03495       eh->stub_sec = NULL;
03496       eh->stub_offset = 0;
03497       eh->target_value = 0;
03498       eh->target_section = NULL;
03499       eh->h = NULL;
03500       eh->id_sec = NULL;
03501     }
03502 
03503   return entry;
03504 }
03505 
03506 /* Create an entry in the branch hash table.  */
03507 
03508 static struct bfd_hash_entry *
03509 branch_hash_newfunc (struct bfd_hash_entry *entry,
03510                    struct bfd_hash_table *table,
03511                    const char *string)
03512 {
03513   /* Allocate the structure if it has not already been allocated by a
03514      subclass.  */
03515   if (entry == NULL)
03516     {
03517       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
03518       if (entry == NULL)
03519        return entry;
03520     }
03521 
03522   /* Call the allocation method of the superclass.  */
03523   entry = bfd_hash_newfunc (entry, table, string);
03524   if (entry != NULL)
03525     {
03526       struct ppc_branch_hash_entry *eh;
03527 
03528       /* Initialize the local fields.  */
03529       eh = (struct ppc_branch_hash_entry *) entry;
03530       eh->offset = 0;
03531       eh->iter = 0;
03532     }
03533 
03534   return entry;
03535 }
03536 
03537 /* Create an entry in a ppc64 ELF linker hash table.  */
03538 
03539 static struct bfd_hash_entry *
03540 link_hash_newfunc (struct bfd_hash_entry *entry,
03541                  struct bfd_hash_table *table,
03542                  const char *string)
03543 {
03544   /* Allocate the structure if it has not already been allocated by a
03545      subclass.  */
03546   if (entry == NULL)
03547     {
03548       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
03549       if (entry == NULL)
03550        return entry;
03551     }
03552 
03553   /* Call the allocation method of the superclass.  */
03554   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
03555   if (entry != NULL)
03556     {
03557       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
03558 
03559       memset (&eh->u.stub_cache, 0,
03560              (sizeof (struct ppc_link_hash_entry)
03561               - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
03562 
03563       /* When making function calls, old ABI code references function entry
03564         points (dot symbols), while new ABI code references the function
03565         descriptor symbol.  We need to make any combination of reference and
03566         definition work together, without breaking archive linking.
03567 
03568         For a defined function "foo" and an undefined call to "bar":
03569         An old object defines "foo" and ".foo", references ".bar" (possibly
03570         "bar" too).
03571         A new object defines "foo" and references "bar".
03572 
03573         A new object thus has no problem with its undefined symbols being
03574         satisfied by definitions in an old object.  On the other hand, the
03575         old object won't have ".bar" satisfied by a new object.
03576 
03577         Keep a list of newly added dot-symbols.  */
03578 
03579       if (string[0] == '.')
03580        {
03581          struct ppc_link_hash_table *htab;
03582 
03583          htab = (struct ppc_link_hash_table *) table;
03584          eh->u.next_dot_sym = htab->dot_syms;
03585          htab->dot_syms = eh;
03586        }
03587     }
03588 
03589   return entry;
03590 }
03591 
03592 /* Create a ppc64 ELF linker hash table.  */
03593 
03594 static struct bfd_link_hash_table *
03595 ppc64_elf_link_hash_table_create (bfd *abfd)
03596 {
03597   struct ppc_link_hash_table *htab;
03598   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
03599 
03600   htab = bfd_zmalloc (amt);
03601   if (htab == NULL)
03602     return NULL;
03603 
03604   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
03605                                   sizeof (struct ppc_link_hash_entry)))
03606     {
03607       free (htab);
03608       return NULL;
03609     }
03610 
03611   /* Init the stub hash table too.  */
03612   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
03613                          sizeof (struct ppc_stub_hash_entry)))
03614     return NULL;
03615 
03616   /* And the branch hash table.  */
03617   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
03618                          sizeof (struct ppc_branch_hash_entry)))
03619     return NULL;
03620 
03621   /* Initializing two fields of the union is just cosmetic.  We really
03622      only care about glist, but when compiled on a 32-bit host the
03623      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
03624      debugger inspection of these fields look nicer.  */
03625   htab->elf.init_got_refcount.refcount = 0;
03626   htab->elf.init_got_refcount.glist = NULL;
03627   htab->elf.init_plt_refcount.refcount = 0;
03628   htab->elf.init_plt_refcount.glist = NULL;
03629   htab->elf.init_got_offset.offset = 0;
03630   htab->elf.init_got_offset.glist = NULL;
03631   htab->elf.init_plt_offset.offset = 0;
03632   htab->elf.init_plt_offset.glist = NULL;
03633 
03634   return &htab->elf.root;
03635 }
03636 
03637 /* Free the derived linker hash table.  */
03638 
03639 static void
03640 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
03641 {
03642   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
03643 
03644   bfd_hash_table_free (&ret->stub_hash_table);
03645   bfd_hash_table_free (&ret->branch_hash_table);
03646   _bfd_generic_link_hash_table_free (hash);
03647 }
03648 
03649 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
03650 
03651 void
03652 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
03653 {
03654   struct ppc_link_hash_table *htab;
03655 
03656   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
03657 
03658 /* Always hook our dynamic sections into the first bfd, which is the
03659    linker created stub bfd.  This ensures that the GOT header is at
03660    the start of the output TOC section.  */
03661   htab = ppc_hash_table (info);
03662   htab->stub_bfd = abfd;
03663   htab->elf.dynobj = abfd;
03664 }
03665 
03666 /* Build a name for an entry in the stub hash table.  */
03667 
03668 static char *
03669 ppc_stub_name (const asection *input_section,
03670               const asection *sym_sec,
03671               const struct ppc_link_hash_entry *h,
03672               const Elf_Internal_Rela *rel)
03673 {
03674   char *stub_name;
03675   bfd_size_type len;
03676 
03677   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
03678      offsets from a sym as a branch target?  In fact, we could
03679      probably assume the addend is always zero.  */
03680   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
03681 
03682   if (h)
03683     {
03684       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
03685       stub_name = bfd_malloc (len);
03686       if (stub_name == NULL)
03687        return stub_name;
03688 
03689       sprintf (stub_name, "%08x.%s+%x",
03690               input_section->id & 0xffffffff,
03691               h->elf.root.root.string,
03692               (int) rel->r_addend & 0xffffffff);
03693     }
03694   else
03695     {
03696       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
03697       stub_name = bfd_malloc (len);
03698       if (stub_name == NULL)
03699        return stub_name;
03700 
03701       sprintf (stub_name, "%08x.%x:%x+%x",
03702               input_section->id & 0xffffffff,
03703               sym_sec->id & 0xffffffff,
03704               (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
03705               (int) rel->r_addend & 0xffffffff);
03706     }
03707   if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
03708     stub_name[len - 2] = 0;
03709   return stub_name;
03710 }
03711 
03712 /* Look up an entry in the stub hash.  Stub entries are cached because
03713    creating the stub name takes a bit of time.  */
03714 
03715 static struct ppc_stub_hash_entry *
03716 ppc_get_stub_entry (const asection *input_section,
03717                   const asection *sym_sec,
03718                   struct ppc_link_hash_entry *h,
03719                   const Elf_Internal_Rela *rel,
03720                   struct ppc_link_hash_table *htab)
03721 {
03722   struct ppc_stub_hash_entry *stub_entry;
03723   const asection *id_sec;
03724 
03725   /* If this input section is part of a group of sections sharing one
03726      stub section, then use the id of the first section in the group.
03727      Stub names need to include a section id, as there may well be
03728      more than one stub used to reach say, printf, and we need to
03729      distinguish between them.  */
03730   id_sec = htab->stub_group[input_section->id].link_sec;
03731 
03732   if (h != NULL && h->u.stub_cache != NULL
03733       && h->u.stub_cache->h == h
03734       && h->u.stub_cache->id_sec == id_sec)
03735     {
03736       stub_entry = h->u.stub_cache;
03737     }
03738   else
03739     {
03740       char *stub_name;
03741 
03742       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
03743       if (stub_name == NULL)
03744        return NULL;
03745 
03746       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
03747                                     stub_name, FALSE, FALSE);
03748       if (h != NULL)
03749        h->u.stub_cache = stub_entry;
03750 
03751       free (stub_name);
03752     }
03753 
03754   return stub_entry;
03755 }
03756 
03757 /* Add a new stub entry to the stub hash.  Not all fields of the new
03758    stub entry are initialised.  */
03759 
03760 static struct ppc_stub_hash_entry *
03761 ppc_add_stub (const char *stub_name,
03762              asection *section,
03763              struct ppc_link_hash_table *htab)
03764 {
03765   asection *link_sec;
03766   asection *stub_sec;
03767   struct ppc_stub_hash_entry *stub_entry;
03768 
03769   link_sec = htab->stub_group[section->id].link_sec;
03770   stub_sec = htab->stub_group[section->id].stub_sec;
03771   if (stub_sec == NULL)
03772     {
03773       stub_sec = htab->stub_group[link_sec->id].stub_sec;
03774       if (stub_sec == NULL)
03775        {
03776          size_t namelen;
03777          bfd_size_type len;
03778          char *s_name;
03779 
03780          namelen = strlen (link_sec->name);
03781          len = namelen + sizeof (STUB_SUFFIX);
03782          s_name = bfd_alloc (htab->stub_bfd, len);
03783          if (s_name == NULL)
03784            return NULL;
03785 
03786          memcpy (s_name, link_sec->name, namelen);
03787          memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
03788          stub_sec = (*htab->add_stub_section) (s_name, link_sec);
03789          if (stub_sec == NULL)
03790            return NULL;
03791          htab->stub_group[link_sec->id].stub_sec = stub_sec;
03792        }
03793       htab->stub_group[section->id].stub_sec = stub_sec;
03794     }
03795 
03796   /* Enter this entry into the linker stub hash table.  */
03797   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
03798                                  TRUE, FALSE);
03799   if (stub_entry == NULL)
03800     {
03801       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
03802                           section->owner, stub_name);
03803       return NULL;
03804     }
03805 
03806   stub_entry->stub_sec = stub_sec;
03807   stub_entry->stub_offset = 0;
03808   stub_entry->id_sec = link_sec;
03809   return stub_entry;
03810 }
03811 
03812 /* Create sections for linker generated code.  */
03813 
03814 static bfd_boolean
03815 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
03816 {
03817   struct ppc_link_hash_table *htab;
03818   flagword flags;
03819 
03820   htab = ppc_hash_table (info);
03821 
03822   /* Create .sfpr for code to save and restore fp regs.  */
03823   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
03824           | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
03825   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
03826                                              flags);
03827   if (htab->sfpr == NULL
03828       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
03829     return FALSE;
03830 
03831   /* Create .glink for lazy dynamic linking support.  */
03832   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
03833                                               flags);
03834   if (htab->glink == NULL
03835       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
03836     return FALSE;
03837 
03838   /* Create branch lookup table for plt_branch stubs.  */
03839   flags = (SEC_ALLOC | SEC_LOAD
03840           | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
03841   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
03842                                              flags);
03843   if (htab->brlt == NULL
03844       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
03845     return FALSE;
03846 
03847   if (!info->shared)
03848     return TRUE;
03849 
03850   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
03851           | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
03852   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
03853                                                 ".rela.branch_lt",
03854                                                 flags);
03855   if (!htab->relbrlt
03856       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
03857     return FALSE;
03858 
03859   return TRUE;
03860 }
03861 
03862 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
03863    not already done.  */
03864 
03865 static bfd_boolean
03866 create_got_section (bfd *abfd, struct bfd_link_info *info)
03867 {
03868   asection *got, *relgot;
03869   flagword flags;
03870   struct ppc_link_hash_table *htab = ppc_hash_table (info);
03871 
03872   if (!htab->got)
03873     {
03874       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
03875        return FALSE;
03876 
03877       htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
03878       if (!htab->got)
03879        abort ();
03880     }
03881 
03882   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
03883           | SEC_LINKER_CREATED);
03884 
03885   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
03886   if (!got
03887       || !bfd_set_section_alignment (abfd, got, 3))
03888     return FALSE;
03889 
03890   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
03891                                           flags | SEC_READONLY);
03892   if (!relgot
03893       || ! bfd_set_section_alignment (abfd, relgot, 3))
03894     return FALSE;
03895 
03896   ppc64_elf_tdata (abfd)->got = got;
03897   ppc64_elf_tdata (abfd)->relgot = relgot;
03898   return TRUE;
03899 }
03900 
03901 /* Create the dynamic sections, and set up shortcuts.  */
03902 
03903 static bfd_boolean
03904 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
03905 {
03906   struct ppc_link_hash_table *htab;
03907 
03908   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
03909     return FALSE;
03910 
03911   htab = ppc_hash_table (info);
03912   if (!htab->got)
03913     htab->got = bfd_get_section_by_name (dynobj, ".got");
03914   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
03915   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
03916   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
03917   if (!info->shared)
03918     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
03919 
03920   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
03921       || (!info->shared && !htab->relbss))
03922     abort ();
03923 
03924   return TRUE;
03925 }
03926 
03927 /* Merge PLT info on FROM with that on TO.  */
03928 
03929 static void
03930 move_plt_plist (struct ppc_link_hash_entry *from,
03931               struct ppc_link_hash_entry *to)
03932 {
03933   if (from->elf.plt.plist != NULL)
03934     {
03935       if (to->elf.plt.plist != NULL)
03936        {
03937          struct plt_entry **entp;
03938          struct plt_entry *ent;
03939 
03940          for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
03941            {
03942              struct plt_entry *dent;
03943 
03944              for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
03945               if (dent->addend == ent->addend)
03946                 {
03947                   dent->plt.refcount += ent->plt.refcount;
03948                   *entp = ent->next;
03949                   break;
03950                 }
03951              if (dent == NULL)
03952               entp = &ent->next;
03953            }
03954          *entp = to->elf.plt.plist;
03955        }
03956 
03957       to->elf.plt.plist = from->elf.plt.plist;
03958       from->elf.plt.plist = NULL;
03959     }
03960 }
03961 
03962 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
03963 
03964 static void
03965 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
03966                             struct elf_link_hash_entry *dir,
03967                             struct elf_link_hash_entry *ind)
03968 {
03969   struct ppc_link_hash_entry *edir, *eind;
03970 
03971   edir = (struct ppc_link_hash_entry *) dir;
03972   eind = (struct ppc_link_hash_entry *) ind;
03973 
03974   /* Copy over any dynamic relocs we may have on the indirect sym.  */
03975   if (eind->dyn_relocs != NULL)
03976     {
03977       if (edir->dyn_relocs != NULL)
03978        {
03979          struct ppc_dyn_relocs **pp;
03980          struct ppc_dyn_relocs *p;
03981 
03982          /* Add reloc counts against the indirect sym to the direct sym
03983             list.  Merge any entries against the same section.  */
03984          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
03985            {
03986              struct ppc_dyn_relocs *q;
03987 
03988              for (q = edir->dyn_relocs; q != NULL; q = q->next)
03989               if (q->sec == p->sec)
03990                 {
03991                   q->pc_count += p->pc_count;
03992                   q->count += p->count;
03993                   *pp = p->next;
03994                   break;
03995                 }
03996              if (q == NULL)
03997               pp = &p->next;
03998            }
03999          *pp = edir->dyn_relocs;
04000        }
04001 
04002       edir->dyn_relocs = eind->dyn_relocs;
04003       eind->dyn_relocs = NULL;
04004     }
04005 
04006   edir->is_func |= eind->is_func;
04007   edir->is_func_descriptor |= eind->is_func_descriptor;
04008   edir->tls_mask |= eind->tls_mask;
04009 
04010   /* If called to transfer flags for a weakdef during processing
04011      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
04012      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
04013   if (!(ELIMINATE_COPY_RELOCS
04014        && eind->elf.root.type != bfd_link_hash_indirect
04015        && edir->elf.dynamic_adjusted))
04016     edir->elf.non_got_ref |= eind->elf.non_got_ref;
04017 
04018   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
04019   edir->elf.ref_regular |= eind->elf.ref_regular;
04020   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
04021   edir->elf.needs_plt |= eind->elf.needs_plt;
04022 
04023   /* If we were called to copy over info for a weak sym, that's all.  */
04024   if (eind->elf.root.type != bfd_link_hash_indirect)
04025     return;
04026 
04027   /* Copy over got entries that we may have already seen to the
04028      symbol which just became indirect.  */
04029   if (eind->elf.got.glist != NULL)
04030     {
04031       if (edir->elf.got.glist != NULL)
04032        {
04033          struct got_entry **entp;
04034          struct got_entry *ent;
04035 
04036          for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
04037            {
04038              struct got_entry *dent;
04039 
04040              for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
04041               if (dent->addend == ent->addend
04042                   && dent->owner == ent->owner
04043                   && dent->tls_type == ent->tls_type)
04044                 {
04045                   dent->got.refcount += ent->got.refcount;
04046                   *entp = ent->next;
04047                   break;
04048                 }
04049              if (dent == NULL)
04050               entp = &ent->next;
04051            }
04052          *entp = edir->elf.got.glist;
04053        }
04054 
04055       edir->elf.got.glist = eind->elf.got.glist;
04056       eind->elf.got.glist = NULL;
04057     }
04058 
04059   /* And plt entries.  */
04060   move_plt_plist (eind, edir);
04061 
04062   if (eind->elf.dynindx != -1)
04063     {
04064       if (edir->elf.dynindx != -1)
04065        _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
04066                             edir->elf.dynstr_index);
04067       edir->elf.dynindx = eind->elf.dynindx;
04068       edir->elf.dynstr_index = eind->elf.dynstr_index;
04069       eind->elf.dynindx = -1;
04070       eind->elf.dynstr_index = 0;
04071     }
04072 }
04073 
04074 /* Find the function descriptor hash entry from the given function code
04075    hash entry FH.  Link the entries via their OH fields.  */
04076 
04077 static struct ppc_link_hash_entry *
04078 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
04079 {
04080   struct ppc_link_hash_entry *fdh = fh->oh;
04081 
04082   if (fdh == NULL)
04083     {
04084       const char *fd_name = fh->elf.root.root.string + 1;
04085 
04086       fdh = (struct ppc_link_hash_entry *)
04087        elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
04088       if (fdh != NULL)
04089        {
04090          fdh->is_func_descriptor = 1;
04091          fdh->oh = fh;
04092          fh->is_func = 1;
04093          fh->oh = fdh;
04094        }
04095     }
04096 
04097   return fdh;
04098 }
04099 
04100 /* Make a fake function descriptor sym for the code sym FH.  */
04101 
04102 static struct ppc_link_hash_entry *
04103 make_fdh (struct bfd_link_info *info,
04104          struct ppc_link_hash_entry *fh)
04105 {
04106   bfd *abfd;
04107   asymbol *newsym;
04108   struct bfd_link_hash_entry *bh;
04109   struct ppc_link_hash_entry *fdh;
04110 
04111   abfd = fh->elf.root.u.undef.abfd;
04112   newsym = bfd_make_empty_symbol (abfd);
04113   newsym->name = fh->elf.root.root.string + 1;
04114   newsym->section = bfd_und_section_ptr;
04115   newsym->value = 0;
04116   newsym->flags = BSF_WEAK;
04117 
04118   bh = NULL;
04119   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
04120                                     newsym->flags, newsym->section,
04121                                     newsym->value, NULL, FALSE, FALSE,
04122                                     &bh))
04123     return NULL;
04124 
04125   fdh = (struct ppc_link_hash_entry *) bh;
04126   fdh->elf.non_elf = 0;
04127   fdh->fake = 1;
04128   fdh->is_func_descriptor = 1;
04129   fdh->oh = fh;
04130   fh->is_func = 1;
04131   fh->oh = fdh;
04132   return fdh;
04133 }
04134 
04135 /* Fix function descriptor symbols defined in .opd sections to be
04136    function type.  */
04137 
04138 static bfd_boolean
04139 ppc64_elf_add_symbol_hook (bfd *ibfd ATTRIBUTE_UNUSED,
04140                         struct bfd_link_info *info ATTRIBUTE_UNUSED,
04141                         Elf_Internal_Sym *isym,
04142                         const char **name ATTRIBUTE_UNUSED,
04143                         flagword *flags ATTRIBUTE_UNUSED,
04144                         asection **sec,
04145                         bfd_vma *value ATTRIBUTE_UNUSED)
04146 {
04147   if (*sec != NULL
04148       && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
04149     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
04150 
04151   return TRUE;
04152 }
04153 
04154 /* This function makes an old ABI object reference to ".bar" cause the
04155    inclusion of a new ABI object archive that defines "bar".
04156    NAME is a symbol defined in an archive.  Return a symbol in the hash
04157    table that might be satisfied by the archive symbols.  */
04158 
04159 static struct elf_link_hash_entry *
04160 ppc64_elf_archive_symbol_lookup (bfd *abfd,
04161                              struct bfd_link_info *info,
04162                              const char *name)
04163 {
04164   struct elf_link_hash_entry *h;
04165   char *dot_name;
04166   size_t len;
04167 
04168   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
04169   if (h != NULL
04170       /* Don't return this sym if it is a fake function descriptor
04171         created by add_symbol_adjust.  */
04172       && !(h->root.type == bfd_link_hash_undefweak
04173           && ((struct ppc_link_hash_entry *) h)->fake))
04174     return h;
04175 
04176   if (name[0] == '.')
04177     return h;
04178 
04179   len = strlen (name);
04180   dot_name = bfd_alloc (abfd, len + 2);
04181   if (dot_name == NULL)
04182     return (struct elf_link_hash_entry *) 0 - 1;
04183   dot_name[0] = '.';
04184   memcpy (dot_name + 1, name, len + 1);
04185   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
04186   bfd_release (abfd, dot_name);
04187   return h;
04188 }
04189 
04190 /* This function satisfies all old ABI object references to ".bar" if a
04191    new ABI object defines "bar".  Well, at least, undefined dot symbols
04192    are made weak.  This stops later archive searches from including an
04193    object if we already have a function descriptor definition.  It also
04194    prevents the linker complaining about undefined symbols.
04195    We also check and correct mismatched symbol visibility here.  The
04196    most restrictive visibility of the function descriptor and the
04197    function entry symbol is used.  */
04198 
04199 static bfd_boolean
04200 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
04201 {
04202   struct ppc_link_hash_table *htab;
04203   struct ppc_link_hash_entry *fdh;
04204 
04205   if (eh->elf.root.type == bfd_link_hash_indirect)
04206     return TRUE;
04207 
04208   if (eh->elf.root.type == bfd_link_hash_warning)
04209     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
04210 
04211   if (eh->elf.root.root.string[0] != '.')
04212     abort ();
04213 
04214   htab = ppc_hash_table (info);
04215   fdh = get_fdh (eh, htab);
04216   if (fdh == NULL
04217       && !info->relocatable
04218       && (eh->elf.root.type == bfd_link_hash_undefined
04219          || eh->elf.root.type == bfd_link_hash_undefweak)
04220       && eh->elf.ref_regular)
04221     {
04222       /* Make an undefweak function descriptor sym, which is enough to
04223         pull in an --as-needed shared lib, but won't cause link
04224         errors.  Archives are handled elsewhere.  */
04225       fdh = make_fdh (info, eh);
04226       if (fdh == NULL)
04227        return FALSE;
04228       else
04229        fdh->elf.ref_regular = 1;
04230     }
04231   else if (fdh != NULL)
04232     {
04233       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
04234       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
04235       if (entry_vis < descr_vis)
04236        fdh->elf.other += entry_vis - descr_vis;
04237       else if (entry_vis > descr_vis)
04238        eh->elf.other += descr_vis - entry_vis;
04239 
04240       if ((fdh->elf.root.type == bfd_link_hash_defined
04241           || fdh->elf.root.type == bfd_link_hash_defweak)
04242          && eh->elf.root.type == bfd_link_hash_undefined)
04243        {
04244          eh->elf.root.type = bfd_link_hash_undefweak;
04245          eh->was_undefined = 1;
04246          htab->twiddled_syms = 1;
04247        }
04248     }
04249 
04250   return TRUE;
04251 }
04252 
04253 /* Process list of dot-symbols we made in link_hash_newfunc.  */
04254 
04255 static bfd_boolean
04256 ppc64_elf_check_directives (bfd *ibfd, struct bfd_link_info *info)
04257 {
04258   struct ppc_link_hash_table *htab;
04259   struct ppc_link_hash_entry **p, *eh;
04260 
04261   htab = ppc_hash_table (info);
04262   if (!is_ppc64_elf_target (htab->elf.root.creator))
04263     return TRUE;
04264 
04265   if (is_ppc64_elf_target (ibfd->xvec))
04266     {
04267       p = &htab->dot_syms;
04268       while ((eh = *p) != NULL)
04269        {
04270          *p = NULL;
04271          if (!add_symbol_adjust (eh, info))
04272            return FALSE;
04273          p = &eh->u.next_dot_sym;
04274        }
04275     }
04276 
04277   /* Clear the list for non-ppc64 input files.  */
04278   p = &htab->dot_syms;
04279   while ((eh = *p) != NULL)
04280     {
04281       *p = NULL;
04282       p = &eh->u.next_dot_sym;
04283     }
04284 
04285   /* We need to fix the undefs list for any syms we have twiddled to
04286      undef_weak.  */
04287   if (htab->twiddled_syms)
04288     {
04289       bfd_link_repair_undef_list (&htab->elf.root);
04290       htab->twiddled_syms = 0;
04291     }
04292   return TRUE;
04293 }
04294 
04295 /* Undo hash table changes when an --as-needed input file is determined
04296    not to be needed.  */
04297 
04298 static bfd_boolean
04299 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
04300                           struct bfd_link_info *info)
04301 {
04302   ppc_hash_table (info)->dot_syms = NULL;
04303   return TRUE;
04304 }
04305 
04306 static bfd_boolean
04307 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
04308                      unsigned long r_symndx, bfd_vma r_addend, int tls_type)
04309 {
04310   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
04311   char *local_got_tls_masks;
04312 
04313   if (local_got_ents == NULL)
04314     {
04315       bfd_size_type size = symtab_hdr->sh_info;
04316 
04317       size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
04318       local_got_ents = bfd_zalloc (abfd, size);
04319       if (local_got_ents == NULL)
04320        return FALSE;
04321       elf_local_got_ents (abfd) = local_got_ents;
04322     }
04323 
04324   if ((tls_type & TLS_EXPLICIT) == 0)
04325     {
04326       struct got_entry *ent;
04327 
04328       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
04329        if (ent->addend == r_addend
04330            && ent->owner == abfd
04331            && ent->tls_type == tls_type)
04332          break;
04333       if (ent == NULL)
04334        {
04335          bfd_size_type amt = sizeof (*ent);
04336          ent = bfd_alloc (abfd, amt);
04337          if (ent == NULL)
04338            return FALSE;
04339          ent->next = local_got_ents[r_symndx];
04340          ent->addend = r_addend;
04341          ent->owner = abfd;
04342          ent->tls_type = tls_type;
04343          ent->got.refcount = 0;
04344          local_got_ents[r_symndx] = ent;
04345        }
04346       ent->got.refcount += 1;
04347     }
04348 
04349   local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
04350   local_got_tls_masks[r_symndx] |= tls_type;
04351   return TRUE;
04352 }
04353 
04354 static bfd_boolean
04355 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
04356 {
04357   struct plt_entry *ent;
04358 
04359   for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
04360     if (ent->addend == addend)
04361       break;
04362   if (ent == NULL)
04363     {
04364       bfd_size_type amt = sizeof (*ent);
04365       ent = bfd_alloc (abfd, amt);
04366       if (ent == NULL)
04367        return FALSE;
04368       ent->next = eh->elf.plt.plist;
04369       ent->addend = addend;
04370       ent->plt.refcount = 0;
04371       eh->elf.plt.plist = ent;
04372     }
04373   ent->plt.refcount += 1;
04374   eh->elf.needs_plt = 1;
04375   if (eh->elf.root.root.string[0] == '.'
04376       && eh->elf.root.root.string[1] != '\0')
04377     eh->is_func = 1;
04378   return TRUE;
04379 }
04380 
04381 /* Look through the relocs for a section during the first phase, and
04382    calculate needed space in the global offset table, procedure
04383    linkage table, and dynamic reloc sections.  */
04384 
04385 static bfd_boolean
04386 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
04387                      asection *sec, const Elf_Internal_Rela *relocs)
04388 {
04389   struct ppc_link_hash_table *htab;
04390   Elf_Internal_Shdr *symtab_hdr;
04391   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
04392   const Elf_Internal_Rela *rel;
04393   const Elf_Internal_Rela *rel_end;
04394   asection *sreloc;
04395   asection **opd_sym_map;
04396 
04397   if (info->relocatable)
04398     return TRUE;
04399 
04400   /* Don't do anything special with non-loaded, non-alloced sections.
04401      In particular, any relocs in such sections should not affect GOT
04402      and PLT reference counting (ie. we don't allow them to create GOT
04403      or PLT entries), there's no possibility or desire to optimize TLS
04404      relocs, and there's not much point in propagating relocs to shared
04405      libs that the dynamic linker won't relocate.  */
04406   if ((sec->flags & SEC_ALLOC) == 0)
04407     return TRUE;
04408 
04409   htab = ppc_hash_table (info);
04410   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
04411 
04412   sym_hashes = elf_sym_hashes (abfd);
04413   sym_hashes_end = (sym_hashes
04414                   + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
04415                   - symtab_hdr->sh_info);
04416 
04417   sreloc = NULL;
04418   opd_sym_map = NULL;
04419   if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
04420     {
04421       /* Garbage collection needs some extra help with .opd sections.
04422         We don't want to necessarily keep everything referenced by
04423         relocs in .opd, as that would keep all functions.  Instead,
04424         if we reference an .opd symbol (a function descriptor), we
04425         want to keep the function code symbol's section.  This is
04426         easy for global symbols, but for local syms we need to keep
04427         information about the associated function section.  Later, if
04428         edit_opd deletes entries, we'll use this array to adjust
04429         local syms in .opd.  */
04430       union opd_info {
04431        asection *func_section;
04432        long entry_adjust;
04433       };
04434       bfd_size_type amt;
04435 
04436       amt = sec->size * sizeof (union opd_info) / 8;
04437       opd_sym_map = bfd_zalloc (abfd, amt);
04438       if (opd_sym_map == NULL)
04439        return FALSE;
04440       ppc64_elf_section_data (sec)->u.opd_func_sec = opd_sym_map;
04441       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
04442       ppc64_elf_section_data (sec)->sec_type = sec_opd;
04443     }
04444 
04445   if (htab->sfpr == NULL
04446       && !create_linkage_sections (htab->elf.dynobj, info))
04447     return FALSE;
04448 
04449   rel_end = relocs + sec->reloc_count;
04450   for (rel = relocs; rel < rel_end; rel++)
04451     {
04452       unsigned long r_symndx;
04453       struct elf_link_hash_entry *h;
04454       enum elf_ppc64_reloc_type r_type;
04455       int tls_type = 0;
04456       struct _ppc64_elf_section_data *ppc64_sec;
04457 
04458       r_symndx = ELF64_R_SYM (rel->r_info);
04459       if (r_symndx < symtab_hdr->sh_info)
04460        h = NULL;
04461       else
04462        {
04463          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
04464          while (h->root.type == bfd_link_hash_indirect
04465                || h->root.type == bfd_link_hash_warning)
04466            h = (struct elf_link_hash_entry *) h->root.u.i.link;
04467        }
04468 
04469       r_type = ELF64_R_TYPE (rel->r_info);
04470       switch (r_type)
04471        {
04472        case R_PPC64_GOT_TLSLD16:
04473        case R_PPC64_GOT_TLSLD16_LO:
04474        case R_PPC64_GOT_TLSLD16_HI:
04475        case R_PPC64_GOT_TLSLD16_HA:
04476          ppc64_tlsld_got (abfd)->refcount += 1;
04477          tls_type = TLS_TLS | TLS_LD;
04478          goto dogottls;
04479 
04480        case R_PPC64_GOT_TLSGD16:
04481        case R_PPC64_GOT_TLSGD16_LO:
04482        case R_PPC64_GOT_TLSGD16_HI:
04483        case R_PPC64_GOT_TLSGD16_HA:
04484          tls_type = TLS_TLS | TLS_GD;
04485          goto dogottls;
04486 
04487        case R_PPC64_GOT_TPREL16_DS:
04488        case R_PPC64_GOT_TPREL16_LO_DS:
04489        case R_PPC64_GOT_TPREL16_HI:
04490        case R_PPC64_GOT_TPREL16_HA:
04491          if (info->shared)
04492            info->flags |= DF_STATIC_TLS;
04493          tls_type = TLS_TLS | TLS_TPREL;
04494          goto dogottls;
04495 
04496        case R_PPC64_GOT_DTPREL16_DS:
04497        case R_PPC64_GOT_DTPREL16_LO_DS:
04498        case R_PPC64_GOT_DTPREL16_HI:
04499        case R_PPC64_GOT_DTPREL16_HA:
04500          tls_type = TLS_TLS | TLS_DTPREL;
04501        dogottls:
04502          sec->has_tls_reloc = 1;
04503          /* Fall thru */
04504 
04505        case R_PPC64_GOT16:
04506        case R_PPC64_GOT16_DS:
04507        case R_PPC64_GOT16_HA:
04508        case R_PPC64_GOT16_HI:
04509        case R_PPC64_GOT16_LO:
04510        case R_PPC64_GOT16_LO_DS:
04511          /* This symbol requires a global offset table entry.  */
04512          sec->has_toc_reloc = 1;
04513          if (ppc64_elf_tdata (abfd)->got == NULL
04514              && !create_got_section (abfd, info))
04515            return FALSE;
04516 
04517          if (h != NULL)
04518            {
04519              struct ppc_link_hash_entry *eh;
04520              struct got_entry *ent;
04521 
04522              eh = (struct ppc_link_hash_entry *) h;
04523              for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
04524               if (ent->addend == rel->r_addend
04525                   && ent->owner == abfd
04526                   && ent->tls_type == tls_type)
04527                 break;
04528              if (ent == NULL)
04529               {
04530                 bfd_size_type amt = sizeof (*ent);
04531                 ent = bfd_alloc (abfd, amt);
04532                 if (ent == NULL)
04533                   return FALSE;
04534                 ent->next = eh->elf.got.glist;
04535                 ent->addend = rel->r_addend;
04536                 ent->owner = abfd;
04537                 ent->tls_type = tls_type;
04538                 ent->got.refcount = 0;
04539                 eh->elf.got.glist = ent;
04540               }
04541              ent->got.refcount += 1;
04542              eh->tls_mask |= tls_type;
04543            }
04544          else
04545            /* This is a global offset table entry for a local symbol.  */
04546            if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
04547                                    rel->r_addend, tls_type))
04548              return FALSE;
04549          break;
04550 
04551        case R_PPC64_PLT16_HA:
04552        case R_PPC64_PLT16_HI:
04553        case R_PPC64_PLT16_LO:
04554        case R_PPC64_PLT32:
04555        case R_PPC64_PLT64:
04556          /* This symbol requires a procedure linkage table entry.  We
04557             actually build the entry in adjust_dynamic_symbol,
04558             because this might be a case of linking PIC code without
04559             linking in any dynamic objects, in which case we don't
04560             need to generate a procedure linkage table after all.  */
04561          if (h == NULL)
04562            {
04563              /* It does not make sense to have a procedure linkage
04564                table entry for a local symbol.  */
04565              bfd_set_error (bfd_error_bad_value);
04566              return FALSE;
04567            }
04568          else
04569            if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
04570                               rel->r_addend))
04571              return FALSE;
04572          break;
04573 
04574          /* The following relocations don't need to propagate the
04575             relocation if linking a shared object since they are
04576             section relative.  */
04577        case R_PPC64_SECTOFF:
04578        case R_PPC64_SECTOFF_LO:
04579        case R_PPC64_SECTOFF_HI:
04580        case R_PPC64_SECTOFF_HA:
04581        case R_PPC64_SECTOFF_DS:
04582        case R_PPC64_SECTOFF_LO_DS:
04583        case R_PPC64_DTPREL16:
04584        case R_PPC64_DTPREL16_LO:
04585        case R_PPC64_DTPREL16_HI:
04586        case R_PPC64_DTPREL16_HA:
04587        case R_PPC64_DTPREL16_DS:
04588        case R_PPC64_DTPREL16_LO_DS:
04589        case R_PPC64_DTPREL16_HIGHER:
04590        case R_PPC64_DTPREL16_HIGHERA:
04591        case R_PPC64_DTPREL16_HIGHEST:
04592        case R_PPC64_DTPREL16_HIGHESTA:
04593          break;
04594 
04595          /* Nor do these.  */
04596        case R_PPC64_TOC16:
04597        case R_PPC64_TOC16_LO:
04598        case R_PPC64_TOC16_HI:
04599        case R_PPC64_TOC16_HA:
04600        case R_PPC64_TOC16_DS:
04601        case R_PPC64_TOC16_LO_DS:
04602          sec->has_toc_reloc = 1;
04603          break;
04604 
04605          /* This relocation describes the C++ object vtable hierarchy.
04606             Reconstruct it for later use during GC.  */
04607        case R_PPC64_GNU_VTINHERIT:
04608          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
04609            return FALSE;
04610          break;
04611 
04612          /* This relocation describes which C++ vtable entries are actually
04613             used.  Record for later use during GC.  */
04614        case R_PPC64_GNU_VTENTRY:
04615          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
04616            return FALSE;
04617          break;
04618 
04619        case R_PPC64_REL14:
04620        case R_PPC64_REL14_BRTAKEN:
04621        case R_PPC64_REL14_BRNTAKEN:
04622          {
04623            asection *dest = NULL;
04624 
04625            /* Heuristic: If jumping outside our section, chances are
04626               we are going to need a stub.  */
04627            if (h != NULL)
04628              {
04629               /* If the sym is weak it may be overridden later, so
04630                  don't assume we know where a weak sym lives.  */
04631               if (h->root.type == bfd_link_hash_defined)
04632                 dest = h->root.u.def.section;
04633              }
04634            else
04635              dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
04636                                           sec, r_symndx);
04637            if (dest != sec)
04638              ppc64_elf_section_data (sec)->has_14bit_branch = 1;
04639          }
04640          /* Fall through.  */
04641 
04642        case R_PPC64_REL24:
04643          if (h != NULL)
04644            {
04645              /* We may need a .plt entry if the function this reloc
04646                refers to is in a shared lib.  */
04647              if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
04648                                 rel->r_addend))
04649               return FALSE;
04650              if (h == &htab->tls_get_addr->elf
04651                 || h == &htab->tls_get_addr_fd->elf)
04652               sec->has_tls_reloc = 1;
04653              else if (htab->tls_get_addr == NULL
04654                      && CONST_STRNEQ (h->root.root.string, ".__tls_get_addr")
04655                      && (h->root.root.string[15] == 0
04656                         || h->root.root.string[15] == '@'))
04657               {
04658                 htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
04659                 sec->has_tls_reloc = 1;
04660               }
04661              else if (htab->tls_get_addr_fd == NULL
04662                      && CONST_STRNEQ (h->root.root.string, "__tls_get_addr")
04663                      && (h->root.root.string[14] == 0
04664                         || h->root.root.string[14] == '@'))
04665               {
04666                 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
04667                 sec->has_tls_reloc = 1;
04668               }
04669            }
04670          break;
04671 
04672        case R_PPC64_TPREL64:
04673          tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
04674          if (info->shared)
04675            info->flags |= DF_STATIC_TLS;
04676          goto dotlstoc;
04677 
04678        case R_PPC64_DTPMOD64:
04679          if (rel + 1 < rel_end
04680              && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
04681              && rel[1].r_offset == rel->r_offset + 8)
04682            tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
04683          else
04684            tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
04685          goto dotlstoc;
04686 
04687        case R_PPC64_DTPREL64:
04688          tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
04689          if (rel != relocs
04690              && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
04691              && rel[-1].r_offset == rel->r_offset - 8)
04692            /* This is the second reloc of a dtpmod, dtprel pair.
04693               Don't mark with TLS_DTPREL.  */
04694            goto dodyn;
04695 
04696        dotlstoc:
04697          sec->has_tls_reloc = 1;
04698          if (h != NULL)
04699            {
04700              struct ppc_link_hash_entry *eh;
04701              eh = (struct ppc_link_hash_entry *) h;
04702              eh->tls_mask |= tls_type;
04703            }
04704          else
04705            if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
04706                                    rel->r_addend, tls_type))
04707              return FALSE;
04708 
04709          ppc64_sec = ppc64_elf_section_data (sec);
04710          if (ppc64_sec->sec_type != sec_toc)
04711            {
04712              /* One extra to simplify get_tls_mask.  */
04713              bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
04714              ppc64_sec->u.t_symndx = bfd_zalloc (abfd, amt);
04715              if (ppc64_sec->u.t_symndx == NULL)
04716               return FALSE;
04717              BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
04718              ppc64_sec->sec_type = sec_toc;
04719            }
04720          BFD_ASSERT (rel->r_offset % 8 == 0);
04721          ppc64_sec->u.t_symndx[rel->r_offset / 8] = r_symndx;
04722 
04723          /* Mark the second slot of a GD or LD entry.
04724             -1 to indicate GD and -2 to indicate LD.  */
04725          if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
04726            ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -1;
04727          else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
04728            ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -2;
04729          goto dodyn;
04730 
04731        case R_PPC64_TPREL16:
04732        case R_PPC64_TPREL16_LO:
04733        case R_PPC64_TPREL16_HI:
04734        case R_PPC64_TPREL16_HA:
04735        case R_PPC64_TPREL16_DS:
04736        case R_PPC64_TPREL16_LO_DS:
04737        case R_PPC64_TPREL16_HIGHER:
04738        case R_PPC64_TPREL16_HIGHERA:
04739        case R_PPC64_TPREL16_HIGHEST:
04740        case R_PPC64_TPREL16_HIGHESTA:
04741          if (info->shared)
04742            {
04743              info->flags |= DF_STATIC_TLS;
04744              goto dodyn;
04745            }
04746          break;
04747 
04748        case R_PPC64_ADDR64:
04749          if (opd_sym_map != NULL
04750              && rel + 1 < rel_end
04751              && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
04752            {
04753              if (h != NULL)
04754               {
04755                 if (h->root.root.string[0] == '.'
04756                     && h->root.root.string[1] != 0
04757                     && get_fdh ((struct ppc_link_hash_entry *) h, htab))
04758                   ;
04759                 else
04760                   ((struct ppc_link_hash_entry *) h)->is_func = 1;
04761               }
04762              else
04763               {
04764                 asection *s;
04765 
04766                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
04767                                            r_symndx);
04768                 if (s == NULL)
04769                   return FALSE;
04770                 else if (s != sec)
04771                   opd_sym_map[rel->r_offset / 8] = s;
04772               }
04773            }
04774          /* Fall through.  */
04775 
04776        case R_PPC64_REL30:
04777        case R_PPC64_REL32:
04778        case R_PPC64_REL64:
04779        case R_PPC64_ADDR14:
04780        case R_PPC64_ADDR14_BRNTAKEN:
04781        case R_PPC64_ADDR14_BRTAKEN:
04782        case R_PPC64_ADDR16:
04783        case R_PPC64_ADDR16_DS:
04784        case R_PPC64_ADDR16_HA:
04785        case R_PPC64_ADDR16_HI:
04786        case R_PPC64_ADDR16_HIGHER:
04787        case R_PPC64_ADDR16_HIGHERA:
04788        case R_PPC64_ADDR16_HIGHEST:
04789        case R_PPC64_ADDR16_HIGHESTA:
04790        case R_PPC64_ADDR16_LO:
04791        case R_PPC64_ADDR16_LO_DS:
04792        case R_PPC64_ADDR24:
04793        case R_PPC64_ADDR32:
04794        case R_PPC64_UADDR16:
04795        case R_PPC64_UADDR32:
04796        case R_PPC64_UADDR64:
04797        case R_PPC64_TOC:
04798          if (h != NULL && !info->shared)
04799            /* We may need a copy reloc.  */
04800            h->non_got_ref = 1;
04801 
04802          /* Don't propagate .opd relocs.  */
04803          if (NO_OPD_RELOCS && opd_sym_map != NULL)
04804            break;
04805 
04806          /* If we are creating a shared library, and this is a reloc
04807             against a global symbol, or a non PC relative reloc
04808             against a local symbol, then we need to copy the reloc
04809             into the shared library.  However, if we are linking with
04810             -Bsymbolic, we do not need to copy a reloc against a
04811             global symbol which is defined in an object we are
04812             including in the link (i.e., DEF_REGULAR is set).  At
04813             this point we have not seen all the input files, so it is
04814             possible that DEF_REGULAR is not set now but will be set
04815             later (it is never cleared).  In case of a weak definition,
04816             DEF_REGULAR may be cleared later by a strong definition in
04817             a shared library.  We account for that possibility below by
04818             storing information in the dyn_relocs field of the hash
04819             table entry.  A similar situation occurs when creating
04820             shared libraries and symbol visibility changes render the
04821             symbol local.
04822 
04823             If on the other hand, we are creating an executable, we
04824             may need to keep relocations for symbols satisfied by a
04825             dynamic library if we manage to avoid copy relocs for the
04826             symbol.  */
04827        dodyn:
04828          if ((info->shared
04829               && (MUST_BE_DYN_RELOC (r_type)
04830                  || (h != NULL
04831                      && (! info->symbolic
04832                         || h->root.type == bfd_link_hash_defweak
04833                         || !h->def_regular))))
04834              || (ELIMINATE_COPY_RELOCS
04835                 && !info->shared
04836                 && h != NULL
04837                 && (h->root.type == bfd_link_hash_defweak
04838                     || !h->def_regular)))
04839            {
04840              struct ppc_dyn_relocs *p;
04841              struct ppc_dyn_relocs **head;
04842 
04843              /* We must copy these reloc types into the output file.
04844                Create a reloc section in dynobj and make room for
04845                this reloc.  */
04846              if (sreloc == NULL)
04847               {
04848                 const char *name;
04849                 bfd *dynobj;
04850 
04851                 name = (bfd_elf_string_from_elf_section
04852                        (abfd,
04853                         elf_elfheader (abfd)->e_shstrndx,
04854                         elf_section_data (sec)->rel_hdr.sh_name));
04855                 if (name == NULL)
04856                   return FALSE;
04857 
04858                 if (! CONST_STRNEQ (name, ".rela")
04859                     || strcmp (bfd_get_section_name (abfd, sec),
04860                              name + 5) != 0)
04861                   {
04862                     (*_bfd_error_handler)
04863                      (_("%B: bad relocation section name `%s\'"),
04864                       abfd, name);
04865                     bfd_set_error (bfd_error_bad_value);
04866                   }
04867 
04868                 dynobj = htab->elf.dynobj;
04869                 sreloc = bfd_get_section_by_name (dynobj, name);
04870                 if (sreloc == NULL)
04871                   {
04872                     flagword flags;
04873 
04874                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
04875                             | SEC_IN_MEMORY | SEC_LINKER_CREATED
04876                             | SEC_ALLOC | SEC_LOAD);
04877                     sreloc = bfd_make_section_with_flags (dynobj,
04878                                                      name,
04879                                                      flags);
04880                     if (sreloc == NULL
04881                        || ! bfd_set_section_alignment (dynobj, sreloc, 3))
04882                      return FALSE;
04883                   }
04884                 elf_section_data (sec)->sreloc = sreloc;
04885               }
04886 
04887              /* If this is a global symbol, we count the number of
04888                relocations we need for this symbol.  */
04889              if (h != NULL)
04890               {
04891                 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
04892               }
04893              else
04894               {
04895                 /* Track dynamic relocs needed for local syms too.
04896                    We really need local syms available to do this
04897                    easily.  Oh well.  */
04898 
04899                 asection *s;
04900                 void *vpp;
04901 
04902                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
04903                                            sec, r_symndx);
04904                 if (s == NULL)
04905                   return FALSE;
04906 
04907                 vpp = &elf_section_data (s)->local_dynrel;
04908                 head = (struct ppc_dyn_relocs **) vpp;
04909               }
04910 
04911              p = *head;
04912              if (p == NULL || p->sec != sec)
04913               {
04914                 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
04915                 if (p == NULL)
04916                   return FALSE;
04917                 p->next = *head;
04918                 *head = p;
04919                 p->sec = sec;
04920                 p->count = 0;
04921                 p->pc_count = 0;
04922               }
04923 
04924              p->count += 1;
04925              if (!MUST_BE_DYN_RELOC (r_type))
04926               p->pc_count += 1;
04927            }
04928          break;
04929 
04930        default:
04931          break;
04932        }
04933     }
04934 
04935   return TRUE;
04936 }
04937 
04938 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
04939    of the code entry point, and its section.  */
04940 
04941 static bfd_vma
04942 opd_entry_value (asection *opd_sec,
04943                bfd_vma offset,
04944                asection **code_sec,
04945                bfd_vma *code_off)
04946 {
04947   bfd *opd_bfd = opd_sec->owner;
04948   Elf_Internal_Rela *relocs;
04949   Elf_Internal_Rela *lo, *hi, *look;
04950   bfd_vma val;
04951 
04952   /* No relocs implies we are linking a --just-symbols object.  */
04953   if (opd_sec->reloc_count == 0)
04954     {
04955       bfd_vma val;
04956 
04957       if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
04958        return (bfd_vma) -1;
04959 
04960       if (code_sec != NULL)
04961        {
04962          asection *sec, *likely = NULL;
04963          for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
04964            if (sec->vma <= val
04965               && (sec->flags & SEC_LOAD) != 0
04966               && (sec->flags & SEC_ALLOC) != 0)
04967              likely = sec;
04968          if (likely != NULL)
04969            {
04970              *code_sec = likely;
04971              if (code_off != NULL)
04972               *code_off = val - likely->vma;
04973            }
04974        }
04975       return val;
04976     }
04977 
04978   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
04979   if (relocs == NULL)
04980     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
04981 
04982   /* Go find the opd reloc at the sym address.  */
04983   lo = relocs;
04984   BFD_ASSERT (lo != NULL);
04985   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
04986   val = (bfd_vma) -1;
04987   while (lo < hi)
04988     {
04989       look = lo + (hi - lo) / 2;
04990       if (look->r_offset < offset)
04991        lo = look + 1;
04992       else if (look->r_offset > offset)
04993        hi = look;
04994       else
04995        {
04996          Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
04997          if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
04998              && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
04999            {
05000              unsigned long symndx = ELF64_R_SYM (look->r_info);
05001              asection *sec;
05002 
05003              if (symndx < symtab_hdr->sh_info)
05004               {
05005                 Elf_Internal_Sym *sym;
05006 
05007                 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
05008                 if (sym == NULL)
05009                   {
05010                     sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
05011                                             symtab_hdr->sh_info,
05012                                             0, NULL, NULL, NULL);
05013                     if (sym == NULL)
05014                      break;
05015                     symtab_hdr->contents = (bfd_byte *) sym;
05016                   }
05017 
05018                 sym += symndx;
05019                 val = sym->st_value;
05020                 sec = NULL;
05021                 if ((sym->st_shndx != SHN_UNDEF
05022                      && sym->st_shndx < SHN_LORESERVE)
05023                     || sym->st_shndx > SHN_HIRESERVE)
05024                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
05025                 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
05026               }
05027              else
05028               {
05029                 struct elf_link_hash_entry **sym_hashes;
05030                 struct elf_link_hash_entry *rh;
05031 
05032                 sym_hashes = elf_sym_hashes (opd_bfd);
05033                 rh = sym_hashes[symndx - symtab_hdr->sh_info];
05034                 while (rh->root.type == bfd_link_hash_indirect
05035                       || rh->root.type == bfd_link_hash_warning)
05036                   rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
05037                 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
05038                            || rh->root.type == bfd_link_hash_defweak);
05039                 val = rh->root.u.def.value;
05040                 sec = rh->root.u.def.section;
05041               }
05042              val += look->r_addend;
05043              if (code_off != NULL)
05044               *code_off = val;
05045              if (code_sec != NULL)
05046               *code_sec = sec;
05047              if (sec != NULL && sec->output_section != NULL)
05048               val += sec->output_section->vma + sec->output_offset;
05049            }
05050          break;
05051        }
05052     }
05053 
05054   return val;
05055 }
05056 
05057 /* Mark sections containing dynamically referenced symbols.  When
05058    building shared libraries, we must assume that any visible symbol is
05059    referenced.  */
05060 
05061 static bfd_boolean
05062 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
05063 {
05064   struct bfd_link_info *info = (struct bfd_link_info *) inf;
05065   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
05066 
05067   if (eh->elf.root.type == bfd_link_hash_warning)
05068     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
05069 
05070   /* Dynamic linking info is on the func descriptor sym.  */
05071   if (eh->oh != NULL
05072       && eh->oh->is_func_descriptor
05073       && (eh->oh->elf.root.type == bfd_link_hash_defined
05074          || eh->oh->elf.root.type == bfd_link_hash_defweak))
05075     eh = eh->oh;
05076 
05077   if ((eh->elf.root.type == bfd_link_hash_defined
05078        || eh->elf.root.type == bfd_link_hash_defweak)
05079       && (eh->elf.ref_dynamic
05080          || (!info->executable
05081              && eh->elf.def_regular
05082              && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
05083              && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
05084     {
05085       asection *code_sec;
05086 
05087       eh->elf.root.u.def.section->flags |= SEC_KEEP;
05088 
05089       /* Function descriptor syms cause the associated
05090         function code sym section to be marked.  */
05091       if (eh->is_func_descriptor
05092          && (eh->oh->elf.root.type == bfd_link_hash_defined
05093              || eh->oh->elf.root.type == bfd_link_hash_defweak))
05094        eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
05095       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
05096               && opd_entry_value (eh->elf.root.u.def.section,
05097                                eh->elf.root.u.def.value,
05098                                &code_sec, NULL) != (bfd_vma) -1)
05099        code_sec->flags |= SEC_KEEP;
05100     }
05101 
05102   return TRUE;
05103 }
05104 
05105 /* Return the section that should be marked against GC for a given
05106    relocation.  */
05107 
05108 static asection *
05109 ppc64_elf_gc_mark_hook (asection *sec,
05110                      struct bfd_link_info *info,
05111                      Elf_Internal_Rela *rel,
05112                      struct elf_link_hash_entry *h,
05113                      Elf_Internal_Sym *sym)
05114 {
05115   asection *rsec;
05116 
05117   /* First mark all our entry sym sections.  */
05118   if (info->gc_sym_list != NULL)
05119     {
05120       struct ppc_link_hash_table *htab = ppc_hash_table (info);
05121       struct bfd_sym_chain *sym = info->gc_sym_list;
05122 
05123       info->gc_sym_list = NULL;
05124       for (; sym != NULL; sym = sym->next)
05125        {
05126          struct ppc_link_hash_entry *eh;
05127 
05128          eh = (struct ppc_link_hash_entry *)
05129            elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
05130          if (eh == NULL)
05131            continue;
05132          if (eh->elf.root.type != bfd_link_hash_defined
05133              && eh->elf.root.type != bfd_link_hash_defweak)
05134            continue;
05135 
05136          if (eh->is_func_descriptor
05137              && (eh->oh->elf.root.type == bfd_link_hash_defined
05138                 || eh->oh->elf.root.type == bfd_link_hash_defweak))
05139            rsec = eh->oh->elf.root.u.def.section;
05140          else if (get_opd_info (eh->elf.root.u.def.section) != NULL
05141                  && opd_entry_value (eh->elf.root.u.def.section,
05142                                    eh->elf.root.u.def.value,
05143                                    &rsec, NULL) != (bfd_vma) -1)
05144            ;
05145          else
05146            continue;
05147 
05148          if (!rsec->gc_mark)
05149            _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
05150 
05151          rsec = eh->elf.root.u.def.section;
05152          if (!rsec->gc_mark)
05153            _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
05154        }
05155     }
05156 
05157   /* Syms return NULL if we're marking .opd, so we avoid marking all
05158      function sections, as all functions are referenced in .opd.  */
05159   rsec = NULL;
05160   if (get_opd_info (sec) != NULL)
05161     return rsec;
05162 
05163   if (h != NULL)
05164     {
05165       enum elf_ppc64_reloc_type r_type;
05166       struct ppc_link_hash_entry *eh;
05167 
05168       r_type = ELF64_R_TYPE (rel->r_info);
05169       switch (r_type)
05170        {
05171        case R_PPC64_GNU_VTINHERIT:
05172        case R_PPC64_GNU_VTENTRY:
05173          break;
05174 
05175        default:
05176          switch (h->root.type)
05177            {
05178            case bfd_link_hash_defined:
05179            case bfd_link_hash_defweak:
05180              eh = (struct ppc_link_hash_entry *) h;
05181              if (eh->oh != NULL
05182                 && eh->oh->is_func_descriptor
05183                 && (eh->oh->elf.root.type == bfd_link_hash_defined
05184                     || eh->oh->elf.root.type == bfd_link_hash_defweak))
05185               eh = eh->oh;
05186 
05187              /* Function descriptor syms cause the associated
05188                function code sym section to be marked.  */
05189              if (eh->is_func_descriptor
05190                 && (eh->oh->elf.root.type == bfd_link_hash_defined
05191                     || eh->oh->elf.root.type == bfd_link_hash_defweak))
05192               {
05193                 /* They also mark their opd section.  */
05194                 if (!eh->elf.root.u.def.section->gc_mark)
05195                   _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
05196                                   ppc64_elf_gc_mark_hook);
05197 
05198                 rsec = eh->oh->elf.root.u.def.section;
05199               }
05200              else if (get_opd_info (eh->elf.root.u.def.section) != NULL
05201                      && opd_entry_value (eh->elf.root.u.def.section,
05202                                       eh->elf.root.u.def.value,
05203                                       &rsec, NULL) != (bfd_vma) -1)
05204               {
05205                 if (!eh->elf.root.u.def.section->gc_mark)
05206                   _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
05207                                   ppc64_elf_gc_mark_hook);
05208               }
05209              else
05210               rsec = h->root.u.def.section;
05211              break;
05212 
05213            case bfd_link_hash_common:
05214              rsec = h->root.u.c.p->section;
05215              break;
05216 
05217            default:
05218              break;
05219            }
05220        }
05221     }
05222   else
05223     {
05224       asection **opd_sym_section;
05225 
05226       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
05227       opd_sym_section = get_opd_info (rsec);
05228       if (opd_sym_section != NULL)
05229        {
05230          if (!rsec->gc_mark)
05231            _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
05232 
05233          rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
05234        }
05235     }
05236 
05237   return rsec;
05238 }
05239 
05240 /* Update the .got, .plt. and dynamic reloc reference counts for the
05241    section being removed.  */
05242 
05243 static bfd_boolean
05244 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
05245                       asection *sec, const Elf_Internal_Rela *relocs)
05246 {
05247   struct ppc_link_hash_table *htab;
05248   Elf_Internal_Shdr *symtab_hdr;
05249   struct elf_link_hash_entry **sym_hashes;
05250   struct got_entry **local_got_ents;
05251   const Elf_Internal_Rela *rel, *relend;
05252 
05253   if ((sec->flags & SEC_ALLOC) == 0)
05254     return TRUE;
05255 
05256   elf_section_data (sec)->local_dynrel = NULL;
05257 
05258   htab = ppc_hash_table (info);
05259   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
05260   sym_hashes = elf_sym_hashes (abfd);
05261   local_got_ents = elf_local_got_ents (abfd);
05262 
05263   relend = relocs + sec->reloc_count;
05264   for (rel = relocs; rel < relend; rel++)
05265     {
05266       unsigned long r_symndx;
05267       enum elf_ppc64_reloc_type r_type;
05268       struct elf_link_hash_entry *h = NULL;
05269       char tls_type = 0;
05270 
05271       r_symndx = ELF64_R_SYM (rel->r_info);
05272       r_type = ELF64_R_TYPE (rel->r_info);
05273       if (r_symndx >= symtab_hdr->sh_info)
05274        {
05275          struct ppc_link_hash_entry *eh;
05276          struct ppc_dyn_relocs **pp;
05277          struct ppc_dyn_relocs *p;
05278 
05279          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
05280          while (h->root.type == bfd_link_hash_indirect
05281                || h->root.type == bfd_link_hash_warning)
05282            h = (struct elf_link_hash_entry *) h->root.u.i.link;
05283          eh = (struct ppc_link_hash_entry *) h;
05284 
05285          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
05286            if (p->sec == sec)
05287              {
05288               /* Everything must go for SEC.  */
05289               *pp = p->next;
05290               break;
05291              }
05292        }
05293 
05294       switch (r_type)
05295        {
05296        case R_PPC64_GOT_TLSLD16:
05297        case R_PPC64_GOT_TLSLD16_LO:
05298        case R_PPC64_GOT_TLSLD16_HI:
05299        case R_PPC64_GOT_TLSLD16_HA:
05300          ppc64_tlsld_got (abfd)->refcount -= 1;
05301          tls_type = TLS_TLS | TLS_LD;
05302          goto dogot;
05303 
05304        case R_PPC64_GOT_TLSGD16:
05305        case R_PPC64_GOT_TLSGD16_LO:
05306        case R_PPC64_GOT_TLSGD16_HI:
05307        case R_PPC64_GOT_TLSGD16_HA:
05308          tls_type = TLS_TLS | TLS_GD;
05309          goto dogot;
05310 
05311        case R_PPC64_GOT_TPREL16_DS:
05312        case R_PPC64_GOT_TPREL16_LO_DS:
05313        case R_PPC64_GOT_TPREL16_HI:
05314        case R_PPC64_GOT_TPREL16_HA:
05315          tls_type = TLS_TLS | TLS_TPREL;
05316          goto dogot;
05317 
05318        case R_PPC64_GOT_DTPREL16_DS:
05319        case R_PPC64_GOT_DTPREL16_LO_DS:
05320        case R_PPC64_GOT_DTPREL16_HI:
05321        case R_PPC64_GOT_DTPREL16_HA:
05322          tls_type = TLS_TLS | TLS_DTPREL;
05323          goto dogot;
05324 
05325        case R_PPC64_GOT16:
05326        case R_PPC64_GOT16_DS:
05327        case R_PPC64_GOT16_HA:
05328        case R_PPC64_GOT16_HI:
05329        case R_PPC64_GOT16_LO:
05330        case R_PPC64_GOT16_LO_DS:
05331        dogot:
05332          {
05333            struct got_entry *ent;
05334 
05335            if (h != NULL)
05336              ent = h->got.glist;
05337            else
05338              ent = local_got_ents[r_symndx];
05339 
05340            for (; ent != NULL; ent = ent->next)
05341              if (ent->addend == rel->r_addend
05342                 && ent->owner == abfd
05343                 && ent->tls_type == tls_type)
05344               break;
05345            if (ent == NULL)
05346              abort ();
05347            if (ent->got.refcount > 0)
05348              ent->got.refcount -= 1;
05349          }
05350          break;
05351 
05352        case R_PPC64_PLT16_HA:
05353        case R_PPC64_PLT16_HI:
05354        case R_PPC64_PLT16_LO:
05355        case R_PPC64_PLT32:
05356        case R_PPC64_PLT64:
05357        case R_PPC64_REL14:
05358        case R_PPC64_REL14_BRNTAKEN:
05359        case R_PPC64_REL14_BRTAKEN:
05360        case R_PPC64_REL24:
05361          if (h != NULL)
05362            {
05363              struct plt_entry *ent;
05364 
05365              for (ent = h->plt.plist; ent != NULL; ent = ent->next)
05366               if (ent->addend == rel->r_addend)
05367                 break;
05368              if (ent == NULL)
05369               abort ();
05370              if (ent->plt.refcount > 0)
05371               ent->plt.refcount -= 1;
05372            }
05373          break;
05374 
05375        default:
05376          break;
05377        }
05378     }
05379   return TRUE;
05380 }
05381 
05382 /* The maximum size of .sfpr.  */
05383 #define SFPR_MAX (218*4)
05384 
05385 struct sfpr_def_parms
05386 {
05387   const char name[12];
05388   unsigned char lo, hi;
05389   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
05390   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
05391 };
05392 
05393 /* Auto-generate _save*, _rest* functions in .sfpr.  */
05394 
05395 static unsigned int
05396 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
05397 {
05398   struct ppc_link_hash_table *htab = ppc_hash_table (info);
05399   unsigned int i;
05400   size_t len = strlen (parm->name);
05401   bfd_boolean writing = FALSE;
05402   char sym[16];
05403 
05404   memcpy (sym, parm->name, len);
05405   sym[len + 2] = 0;
05406 
05407   for (i = parm->lo; i <= parm->hi; i++)
05408     {
05409       struct elf_link_hash_entry *h;
05410 
05411       sym[len + 0] = i / 10 + '0';
05412       sym[len + 1] = i % 10 + '0';
05413       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
05414       if (h != NULL
05415          && !h->def_regular)
05416        {
05417          h->root.type = bfd_link_hash_defined;
05418          h->root.u.def.section = htab->sfpr;
05419          h->root.u.def.value = htab->sfpr->size;
05420          h->type = STT_FUNC;
05421          h->def_regular = 1;
05422          _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
05423          writing = TRUE;
05424          if (htab->sfpr->contents == NULL)
05425            {
05426              htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
05427              if (htab->sfpr->contents == NULL)
05428               return FALSE;
05429            }
05430        }
05431       if (writing)
05432        {
05433          bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
05434          if (i != parm->hi)
05435            p = (*parm->write_ent) (htab->elf.dynobj, p, i);
05436          else
05437            p = (*parm->write_tail) (htab->elf.dynobj, p, i);
05438          htab->sfpr->size = p - htab->sfpr->contents;
05439        }
05440     }
05441 
05442   return TRUE;
05443 }
05444 
05445 static bfd_byte *
05446 savegpr0 (bfd *abfd, bfd_byte *p, int r)
05447 {
05448   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
05449   return p + 4;
05450 }
05451 
05452 static bfd_byte *
05453 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
05454 {
05455   p = savegpr0 (abfd, p, r);
05456   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
05457   p = p + 4;
05458   bfd_put_32 (abfd, BLR, p);
05459   return p + 4;
05460 }
05461 
05462 static bfd_byte *
05463 restgpr0 (bfd *abfd, bfd_byte *p, int r)
05464 {
05465   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
05466   return p + 4;
05467 }
05468 
05469 static bfd_byte *
05470 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
05471 {
05472   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
05473   p = p + 4;
05474   p = restgpr0 (abfd, p, r);
05475   bfd_put_32 (abfd, MTLR_R0, p);
05476   p = p + 4;
05477   if (r == 29)
05478     {
05479       p = restgpr0 (abfd, p, 30);
05480       p = restgpr0 (abfd, p, 31);
05481     }
05482   bfd_put_32 (abfd, BLR, p);
05483   return p + 4;
05484 }
05485 
05486 static bfd_byte *
05487 savegpr1 (bfd *abfd, bfd_byte *p, int r)
05488 {
05489   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
05490   return p + 4;
05491 }
05492 
05493 static bfd_byte *
05494 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
05495 {
05496   p = savegpr1 (abfd, p, r);
05497   bfd_put_32 (abfd, BLR, p);
05498   return p + 4;
05499 }
05500 
05501 static bfd_byte *
05502 restgpr1 (bfd *abfd, bfd_byte *p, int r)
05503 {
05504   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
05505   return p + 4;
05506 }
05507 
05508 static bfd_byte *
05509 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
05510 {
05511   p = restgpr1 (abfd, p, r);
05512   bfd_put_32 (abfd, BLR, p);
05513   return p + 4;
05514 }
05515 
05516 static bfd_byte *
05517 savefpr (bfd *abfd, bfd_byte *p, int r)
05518 {
05519   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
05520   return p + 4;
05521 }
05522 
05523 static bfd_byte *
05524 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
05525 {
05526   p = savefpr (abfd, p, r);
05527   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
05528   p = p + 4;
05529   bfd_put_32 (abfd, BLR, p);
05530   return p + 4;
05531 }
05532 
05533 static bfd_byte *
05534 restfpr (bfd *abfd, bfd_byte *p, int r)
05535 {
05536   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
05537   return p + 4;
05538 }
05539 
05540 static bfd_byte *
05541 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
05542 {
05543   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
05544   p = p + 4;
05545   p = restfpr (abfd, p, r);
05546   bfd_put_32 (abfd, MTLR_R0, p);
05547   p = p + 4;
05548   if (r == 29)
05549     {
05550       p = restfpr (abfd, p, 30);
05551       p = restfpr (abfd, p, 31);
05552     }
05553   bfd_put_32 (abfd, BLR, p);
05554   return p + 4;
05555 }
05556 
05557 static bfd_byte *
05558 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
05559 {
05560   p = savefpr (abfd, p, r);
05561   bfd_put_32 (abfd, BLR, p);
05562   return p + 4;
05563 }
05564 
05565 static bfd_byte *
05566 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
05567 {
05568   p = restfpr (abfd, p, r);
05569   bfd_put_32 (abfd, BLR, p);
05570   return p + 4;
05571 }
05572 
05573 static bfd_byte *
05574 savevr (bfd *abfd, bfd_byte *p, int r)
05575 {
05576   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
05577   p = p + 4;
05578   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
05579   return p + 4;
05580 }
05581 
05582 static bfd_byte *
05583 savevr_tail (bfd *abfd, bfd_byte *p, int r)
05584 {
05585   p = savevr (abfd, p, r);
05586   bfd_put_32 (abfd, BLR, p);
05587   return p + 4;
05588 }
05589 
05590 static bfd_byte *
05591 restvr (bfd *abfd, bfd_byte *p, int r)
05592 {
05593   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
05594   p = p + 4;
05595   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
05596   return p + 4;
05597 }
05598 
05599 static bfd_byte *
05600 restvr_tail (bfd *abfd, bfd_byte *p, int r)
05601 {
05602   p = restvr (abfd, p, r);
05603   bfd_put_32 (abfd, BLR, p);
05604   return p + 4;
05605 }
05606 
05607 /* Called via elf_link_hash_traverse to transfer dynamic linking
05608    information on function code symbol entries to their corresponding
05609    function descriptor symbol entries.  */
05610 
05611 static bfd_boolean
05612 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
05613 {
05614   struct bfd_link_info *info;
05615   struct ppc_link_hash_table *htab;
05616   struct plt_entry *ent;
05617   struct ppc_link_hash_entry *fh;
05618   struct ppc_link_hash_entry *fdh;
05619   bfd_boolean force_local;
05620 
05621   fh = (struct ppc_link_hash_entry *) h;
05622   if (fh->elf.root.type == bfd_link_hash_indirect)
05623     return TRUE;
05624 
05625   if (fh->elf.root.type == bfd_link_hash_warning)
05626     fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
05627 
05628   info = inf;
05629   htab = ppc_hash_table (info);
05630 
05631   /* Resolve undefined references to dot-symbols as the value
05632      in the function descriptor, if we have one in a regular object.
05633      This is to satisfy cases like ".quad .foo".  Calls to functions
05634      in dynamic objects are handled elsewhere.  */
05635   if (fh->elf.root.type == bfd_link_hash_undefweak
05636       && fh->was_undefined
05637       && (fh->oh->elf.root.type == bfd_link_hash_defined
05638          || fh->oh->elf.root.type == bfd_link_hash_defweak)
05639       && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
05640       && opd_entry_value (fh->oh->elf.root.u.def.section,
05641                        fh->oh->elf.root.u.def.value,
05642                        &fh->elf.root.u.def.section,
05643                        &fh->elf.root.u.def.value) != (bfd_vma) -1)
05644     {
05645       fh->elf.root.type = fh->oh->elf.root.type;
05646       fh->elf.forced_local = 1;
05647     }
05648 
05649   /* If this is a function code symbol, transfer dynamic linking
05650      information to the function descriptor symbol.  */
05651   if (!fh->is_func)
05652     return TRUE;
05653 
05654   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
05655     if (ent->plt.refcount > 0)
05656       break;
05657   if (ent == NULL
05658       || fh->elf.root.root.string[0] != '.'
05659       || fh->elf.root.root.string[1] == '\0')
05660     return TRUE;
05661 
05662   /* Find the corresponding function descriptor symbol.  Create it
05663      as undefined if necessary.  */
05664 
05665   fdh = get_fdh (fh, htab);
05666   if (fdh != NULL)
05667     while (fdh->elf.root.type == bfd_link_hash_indirect
05668           || fdh->elf.root.type == bfd_link_hash_warning)
05669       fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
05670 
05671   if (fdh == NULL
05672       && info->shared
05673       && (fh->elf.root.type == bfd_link_hash_undefined
05674          || fh->elf.root.type == bfd_link_hash_undefweak))
05675     {
05676       fdh = make_fdh (info, fh);
05677       if (fdh == NULL)
05678        return FALSE;
05679     }
05680 
05681   /* Fake function descriptors are made undefweak.  If the function
05682      code symbol is strong undefined, make the fake sym the same.
05683      If the function code symbol is defined, then force the fake
05684      descriptor local;  We can't support overriding of symbols in a
05685      shared library on a fake descriptor.  */
05686 
05687   if (fdh != NULL
05688       && fdh->fake
05689       && fdh->elf.root.type == bfd_link_hash_undefweak)
05690     {
05691       if (fh->elf.root.type == bfd_link_hash_undefined)
05692        {
05693          fdh->elf.root.type = bfd_link_hash_undefined;
05694          bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
05695        }
05696       else if (fh->elf.root.type == bfd_link_hash_defined
05697               || fh->elf.root.type == bfd_link_hash_defweak)
05698        {
05699          _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
05700        }
05701     }
05702 
05703   if (fdh != NULL
05704       && !fdh->elf.forced_local
05705       && (info->shared
05706          || fdh->elf.def_dynamic
05707          || fdh->elf.ref_dynamic
05708          || (fdh->elf.root.type == bfd_link_hash_undefweak
05709              && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
05710     {
05711       if (fdh->elf.dynindx == -1)
05712        if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
05713          return FALSE;
05714       fdh->elf.ref_regular |= fh->elf.ref_regular;
05715       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
05716       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
05717       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
05718       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
05719        {
05720          move_plt_plist (fh, fdh);
05721          fdh->elf.needs_plt = 1;
05722        }
05723       fdh->is_func_descriptor = 1;
05724       fdh->oh = fh;
05725       fh->oh = fdh;
05726     }
05727 
05728   /* Now that the info is on the function descriptor, clear the
05729      function code sym info.  Any function code syms for which we
05730      don't have a definition in a regular file, we force local.
05731      This prevents a shared library from exporting syms that have
05732      been imported from another library.  Function code syms that
05733      are really in the library we must leave global to prevent the
05734      linker dragging in a definition from a static library.  */
05735   force_local = (!fh->elf.def_regular
05736                || fdh == NULL
05737                || !fdh->elf.def_regular
05738                || fdh->elf.forced_local);
05739   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
05740 
05741   return TRUE;
05742 }
05743 
05744 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
05745    this hook to a) provide some gcc support functions, and b) transfer
05746    dynamic linking information gathered so far on function code symbol
05747    entries, to their corresponding function descriptor symbol entries.  */
05748 
05749 static bfd_boolean
05750 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
05751                          struct bfd_link_info *info)
05752 {
05753   struct ppc_link_hash_table *htab;
05754   unsigned int i;
05755   const struct sfpr_def_parms funcs[] =
05756     {
05757       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
05758       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
05759       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
05760       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
05761       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
05762       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
05763       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
05764       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
05765       { "._savef", 14, 31, savefpr, savefpr1_tail },
05766       { "._restf", 14, 31, restfpr, restfpr1_tail },
05767       { "_savevr_", 20, 31, savevr, savevr_tail },
05768       { "_restvr_", 20, 31, restvr, restvr_tail }
05769     };
05770 
05771   htab = ppc_hash_table (info);
05772   if (htab->sfpr == NULL)
05773     /* We don't have any relocs.  */
05774     return TRUE;
05775 
05776   /* Provide any missing _save* and _rest* functions.  */
05777   htab->sfpr->size = 0;
05778   for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
05779     if (!sfpr_define (info, &funcs[i]))
05780       return FALSE;
05781 
05782   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
05783 
05784   if (htab->sfpr->size == 0)
05785     htab->sfpr->flags |= SEC_EXCLUDE;
05786 
05787   return TRUE;
05788 }
05789 
05790 /* Adjust a symbol defined by a dynamic object and referenced by a
05791    regular object.  The current definition is in some section of the
05792    dynamic object, but we're not including those sections.  We have to
05793    change the definition to something the rest of the link can
05794    understand.  */
05795 
05796 static bfd_boolean
05797 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
05798                              struct elf_link_hash_entry *h)
05799 {
05800   struct ppc_link_hash_table *htab;
05801   asection *s;
05802   unsigned int power_of_two;
05803 
05804   htab = ppc_hash_table (info);
05805 
05806   /* Deal with function syms.  */
05807   if (h->type == STT_FUNC
05808       || h->needs_plt)
05809     {
05810       /* Clear procedure linkage table information for any symbol that
05811         won't need a .plt entry.  */
05812       struct plt_entry *ent;
05813       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
05814        if (ent->plt.refcount > 0)
05815          break;
05816       if (ent == NULL
05817          || SYMBOL_CALLS_LOCAL (info, h)
05818          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
05819              && h->root.type == bfd_link_hash_undefweak))
05820        {
05821          h->plt.plist = NULL;
05822          h->needs_plt = 0;
05823        }
05824     }
05825   else
05826     h->plt.plist = NULL;
05827 
05828   /* If this is a weak symbol, and there is a real definition, the
05829      processor independent code will have arranged for us to see the
05830      real definition first, and we can just use the same value.  */
05831   if (h->u.weakdef != NULL)
05832     {
05833       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
05834                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
05835       h->root.u.def.section = h->u.weakdef->root.u.def.section;
05836       h->root.u.def.value = h->u.weakdef->root.u.def.value;
05837       if (ELIMINATE_COPY_RELOCS)
05838        h->non_got_ref = h->u.weakdef->non_got_ref;
05839       return TRUE;
05840     }
05841 
05842   /* If we are creating a shared library, we must presume that the
05843      only references to the symbol are via the global offset table.
05844      For such cases we need not do anything here; the relocations will
05845      be handled correctly by relocate_section.  */
05846   if (info->shared)
05847     return TRUE;
05848 
05849   /* If there are no references to this symbol that do not use the
05850      GOT, we don't need to generate a copy reloc.  */
05851   if (!h->non_got_ref)
05852     return TRUE;
05853 
05854   if (ELIMINATE_COPY_RELOCS)
05855     {
05856       struct ppc_link_hash_entry * eh;
05857       struct ppc_dyn_relocs *p;
05858 
05859       eh = (struct ppc_link_hash_entry *) h;
05860       for (p = eh->dyn_relocs; p != NULL; p = p->next)
05861        {
05862          s = p->sec->output_section;
05863          if (s != NULL && (s->flags & SEC_READONLY) != 0)
05864            break;
05865        }
05866 
05867       /* If we didn't find any dynamic relocs in read-only sections, then
05868         we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
05869       if (p == NULL)
05870        {
05871          h->non_got_ref = 0;
05872          return TRUE;
05873        }
05874     }
05875 
05876   if (h->plt.plist != NULL)
05877     {
05878       /* We should never get here, but unfortunately there are versions
05879         of gcc out there that improperly (for this ABI) put initialized
05880         function pointers, vtable refs and suchlike in read-only
05881         sections.  Allow them to proceed, but warn that this might
05882         break at runtime.  */
05883       (*_bfd_error_handler)
05884        (_("copy reloc against `%s' requires lazy plt linking; "
05885           "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
05886         h->root.root.string);
05887     }
05888 
05889   /* This is a reference to a symbol defined by a dynamic object which
05890      is not a function.  */
05891 
05892   if (h->size == 0)
05893     {
05894       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
05895                           h->root.root.string);
05896       return TRUE;
05897     }
05898 
05899   /* We must allocate the symbol in our .dynbss section, which will
05900      become part of the .bss section of the executable.  There will be
05901      an entry for this symbol in the .dynsym section.  The dynamic
05902      object will contain position independent code, so all references
05903      from the dynamic object to this symbol will go through the global
05904      offset table.  The dynamic linker will use the .dynsym entry to
05905      determine the address it must put in the global offset table, so
05906      both the dynamic object and the regular object will refer to the
05907      same memory location for the variable.  */
05908 
05909   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
05910      to copy the initial value out of the dynamic object and into the
05911      runtime process image.  We need to remember the offset into the
05912      .rela.bss section we are going to use.  */
05913   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
05914     {
05915       htab->relbss->size += sizeof (Elf64_External_Rela);
05916       h->needs_copy = 1;
05917     }
05918 
05919   /* We need to figure out the alignment required for this symbol.  I
05920      have no idea how ELF linkers handle this.  */
05921   power_of_two = bfd_log2 (h->size);
05922   if (power_of_two > 4)
05923     power_of_two = 4;
05924 
05925   /* Apply the required alignment.  */
05926   s = htab->dynbss;
05927   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
05928   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
05929     {
05930       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
05931        return FALSE;
05932     }
05933 
05934   /* Define the symbol as being at this point in the section.  */
05935   h->root.u.def.section = s;
05936   h->root.u.def.value = s->size;
05937 
05938   /* Increment the section size to make room for the symbol.  */
05939   s->size += h->size;
05940 
05941   return TRUE;
05942 }
05943 
05944 /* If given a function descriptor symbol, hide both the function code
05945    sym and the descriptor.  */
05946 static void
05947 ppc64_elf_hide_symbol (struct bfd_link_info *info,
05948                      struct elf_link_hash_entry *h,
05949                      bfd_boolean force_local)
05950 {
05951   struct ppc_link_hash_entry *eh;
05952   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
05953 
05954   eh = (struct ppc_link_hash_entry *) h;
05955   if (eh->is_func_descriptor)
05956     {
05957       struct ppc_link_hash_entry *fh = eh->oh;
05958 
05959       if (fh == NULL)
05960        {
05961          const char *p, *q;
05962          struct ppc_link_hash_table *htab;
05963          char save;
05964 
05965          /* We aren't supposed to use alloca in BFD because on
05966             systems which do not have alloca the version in libiberty
05967             calls xmalloc, which might cause the program to crash
05968             when it runs out of memory.  This function doesn't have a
05969             return status, so there's no way to gracefully return an
05970             error.  So cheat.  We know that string[-1] can be safely
05971             accessed;  It's either a string in an ELF string table,
05972             or allocated in an objalloc structure.  */
05973 
05974          p = eh->elf.root.root.string - 1;
05975          save = *p;
05976          *(char *) p = '.';
05977          htab = ppc_hash_table (info);
05978          fh = (struct ppc_link_hash_entry *)
05979            elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
05980          *(char *) p = save;
05981 
05982          /* Unfortunately, if it so happens that the string we were
05983             looking for was allocated immediately before this string,
05984             then we overwrote the string terminator.  That's the only
05985             reason the lookup should fail.  */
05986          if (fh == NULL)
05987            {
05988              q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
05989              while (q >= eh->elf.root.root.string && *q == *p)
05990               --q, --p;
05991              if (q < eh->elf.root.root.string && *p == '.')
05992               fh = (struct ppc_link_hash_entry *)
05993                 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
05994            }
05995          if (fh != NULL)
05996            {
05997              eh->oh = fh;
05998              fh->oh = eh;
05999            }
06000        }
06001       if (fh != NULL)
06002        _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
06003     }
06004 }
06005 
06006 static bfd_boolean
06007 get_sym_h (struct elf_link_hash_entry **hp,
06008           Elf_Internal_Sym **symp,
06009           asection **symsecp,
06010           char **tls_maskp,
06011           Elf_Internal_Sym **locsymsp,
06012           unsigned long r_symndx,
06013           bfd *ibfd)
06014 {
06015   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
06016 
06017   if (r_symndx >= symtab_hdr->sh_info)
06018     {
06019       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
06020       struct elf_link_hash_entry *h;
06021 
06022       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
06023       while (h->root.type == bfd_link_hash_indirect
06024             || h->root.type == bfd_link_hash_warning)
06025        h = (struct elf_link_hash_entry *) h->root.u.i.link;
06026 
06027       if (hp != NULL)
06028        *hp = h;
06029 
06030       if (symp != NULL)
06031        *symp = NULL;
06032 
06033       if (symsecp != NULL)
06034        {
06035          asection *symsec = NULL;
06036          if (h->root.type == bfd_link_hash_defined
06037              || h->root.type == bfd_link_hash_defweak)
06038            symsec = h->root.u.def.section;
06039          *symsecp = symsec;
06040        }
06041 
06042       if (tls_maskp != NULL)
06043        {
06044          struct ppc_link_hash_entry *eh;
06045 
06046          eh = (struct ppc_link_hash_entry *) h;
06047          *tls_maskp = &eh->tls_mask;
06048        }
06049     }
06050   else
06051     {
06052       Elf_Internal_Sym *sym;
06053       Elf_Internal_Sym *locsyms = *locsymsp;
06054 
06055       if (locsyms == NULL)
06056        {
06057          locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
06058          if (locsyms == NULL)
06059            locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
06060                                        symtab_hdr->sh_info,
06061                                        0, NULL, NULL, NULL);
06062          if (locsyms == NULL)
06063            return FALSE;
06064          *locsymsp = locsyms;
06065        }
06066       sym = locsyms + r_symndx;
06067 
06068       if (hp != NULL)
06069        *hp = NULL;
06070 
06071       if (symp != NULL)
06072        *symp = sym;
06073 
06074       if (symsecp != NULL)
06075        {
06076          asection *symsec = NULL;
06077          if ((sym->st_shndx != SHN_UNDEF
06078               && sym->st_shndx < SHN_LORESERVE)
06079              || sym->st_shndx > SHN_HIRESERVE)
06080            symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
06081          *symsecp = symsec;
06082        }
06083 
06084       if (tls_maskp != NULL)
06085        {
06086          struct got_entry **lgot_ents;
06087          char *tls_mask;
06088 
06089          tls_mask = NULL;
06090          lgot_ents = elf_local_got_ents (ibfd);
06091          if (lgot_ents != NULL)
06092            {
06093              char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
06094              tls_mask = &lgot_masks[r_symndx];
06095            }
06096          *tls_maskp = tls_mask;
06097        }
06098     }
06099   return TRUE;
06100 }
06101 
06102 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
06103    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
06104    type suitable for optimization, and 1 otherwise.  */
06105 
06106 static int
06107 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
06108              Elf_Internal_Sym **locsymsp,
06109              const Elf_Internal_Rela *rel, bfd *ibfd)
06110 {
06111   unsigned long r_symndx;
06112   int next_r;
06113   struct elf_link_hash_entry *h;
06114   Elf_Internal_Sym *sym;
06115   asection *sec;
06116   bfd_vma off;
06117 
06118   r_symndx = ELF64_R_SYM (rel->r_info);
06119   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
06120     return 0;
06121 
06122   if ((*tls_maskp != NULL && **tls_maskp != 0)
06123       || sec == NULL
06124       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
06125     return 1;
06126 
06127   /* Look inside a TOC section too.  */
06128   if (h != NULL)
06129     {
06130       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
06131       off = h->root.u.def.value;
06132     }
06133   else
06134     off = sym->st_value;
06135   off += rel->r_addend;
06136   BFD_ASSERT (off % 8 == 0);
06137   r_symndx = ppc64_elf_section_data (sec)->u.t_symndx[off / 8];
06138   next_r = ppc64_elf_section_data (sec)->u.t_symndx[off / 8 + 1];
06139   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
06140     return 0;
06141   if (toc_symndx != NULL)
06142     *toc_symndx = r_symndx;
06143   if ((h == NULL
06144        || ((h->root.type == bfd_link_hash_defined
06145            || h->root.type == bfd_link_hash_defweak)
06146           && !h->def_dynamic))
06147       && (next_r == -1 || next_r == -2))
06148     return 1 - next_r;
06149   return 1;
06150 }
06151 
06152 /* Adjust all global syms defined in opd sections.  In gcc generated
06153    code for the old ABI, these will already have been done.  */
06154 
06155 static bfd_boolean
06156 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
06157 {
06158   struct ppc_link_hash_entry *eh;
06159   asection *sym_sec;
06160   long *opd_adjust;
06161 
06162   if (h->root.type == bfd_link_hash_indirect)
06163     return TRUE;
06164 
06165   if (h->root.type == bfd_link_hash_warning)
06166     h = (struct elf_link_hash_entry *) h->root.u.i.link;
06167 
06168   if (h->root.type != bfd_link_hash_defined
06169       && h->root.type != bfd_link_hash_defweak)
06170     return TRUE;
06171 
06172   eh = (struct ppc_link_hash_entry *) h;
06173   if (eh->adjust_done)
06174     return TRUE;
06175 
06176   sym_sec = eh->elf.root.u.def.section;
06177   opd_adjust = get_opd_info (sym_sec);
06178   if (opd_adjust != NULL)
06179     {
06180       long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
06181       if (adjust == -1)
06182        {
06183          /* This entry has been deleted.  */
06184          asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
06185          if (dsec == NULL)
06186            {
06187              for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
06188               if (elf_discarded_section (dsec))
06189                 {
06190                   ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
06191                   break;
06192                 }
06193            }
06194          eh->elf.root.u.def.value = 0;
06195          eh->elf.root.u.def.section = dsec;
06196        }
06197       else
06198        eh->elf.root.u.def.value += adjust;
06199       eh->adjust_done = 1;
06200     }
06201   return TRUE;
06202 }
06203 
06204 /* Handles decrementing dynamic reloc counts for the reloc specified by
06205    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
06206    have already been determined.  */
06207 
06208 static bfd_boolean
06209 dec_dynrel_count (bfd_vma r_info,
06210                 asection *sec,
06211                 struct bfd_link_info *info,
06212                 Elf_Internal_Sym **local_syms,
06213                 struct elf_link_hash_entry *h,
06214                 asection *sym_sec)
06215 {
06216   enum elf_ppc64_reloc_type r_type;
06217   struct ppc_dyn_relocs *p;
06218   struct ppc_dyn_relocs **pp;
06219 
06220   /* Can this reloc be dynamic?  This switch, and later tests here
06221      should be kept in sync with the code in check_relocs.  */
06222   r_type = ELF64_R_TYPE (r_info);
06223   switch (r_type)
06224     {
06225     default:
06226       return TRUE;
06227 
06228     case R_PPC64_TPREL16:
06229     case R_PPC64_TPREL16_LO:
06230     case R_PPC64_TPREL16_HI:
06231     case R_PPC64_TPREL16_HA:
06232     case R_PPC64_TPREL16_DS:
06233     case R_PPC64_TPREL16_LO_DS:
06234     case R_PPC64_TPREL16_HIGHER:
06235     case R_PPC64_TPREL16_HIGHERA:
06236     case R_PPC64_TPREL16_HIGHEST:
06237     case R_PPC64_TPREL16_HIGHESTA:
06238       if (!info->shared)
06239        return TRUE;
06240 
06241     case R_PPC64_TPREL64:
06242     case R_PPC64_DTPMOD64:
06243     case R_PPC64_DTPREL64:
06244     case R_PPC64_ADDR64:
06245     case R_PPC64_REL30:
06246     case R_PPC64_REL32:
06247     case R_PPC64_REL64:
06248     case R_PPC64_ADDR14:
06249     case R_PPC64_ADDR14_BRNTAKEN:
06250     case R_PPC64_ADDR14_BRTAKEN:
06251     case R_PPC64_ADDR16:
06252     case R_PPC64_ADDR16_DS:
06253     case R_PPC64_ADDR16_HA:
06254     case R_PPC64_ADDR16_HI:
06255     case R_PPC64_ADDR16_HIGHER:
06256     case R_PPC64_ADDR16_HIGHERA:
06257     case R_PPC64_ADDR16_HIGHEST:
06258     case R_PPC64_ADDR16_HIGHESTA:
06259     case R_PPC64_ADDR16_LO:
06260     case R_PPC64_ADDR16_LO_DS:
06261     case R_PPC64_ADDR24:
06262     case R_PPC64_ADDR32:
06263     case R_PPC64_UADDR16:
06264     case R_PPC64_UADDR32:
06265     case R_PPC64_UADDR64:
06266     case R_PPC64_TOC:
06267       break;
06268     }
06269 
06270   if (local_syms != NULL)
06271     {
06272       unsigned long r_symndx;
06273       Elf_Internal_Sym *sym;
06274       bfd *ibfd = sec->owner;
06275 
06276       r_symndx = ELF64_R_SYM (r_info);
06277       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
06278        return FALSE;
06279     }
06280 
06281   if ((info->shared
06282        && (MUST_BE_DYN_RELOC (r_type)
06283           || (h != NULL
06284               && (!info->symbolic
06285                  || h->root.type == bfd_link_hash_defweak
06286                  || !h->def_regular))))
06287       || (ELIMINATE_COPY_RELOCS
06288          && !info->shared
06289          && h != NULL
06290          && (h->root.type == bfd_link_hash_defweak
06291              || !h->def_regular)))
06292     ;
06293   else
06294     return TRUE;
06295 
06296   if (h != NULL)
06297     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
06298   else
06299     {
06300       if (sym_sec != NULL)
06301        {
06302          void *vpp = &elf_section_data (sym_sec)->local_dynrel;
06303          pp = (struct ppc_dyn_relocs **) vpp;
06304        }
06305       else
06306        {
06307          void *vpp = &elf_section_data (sec)->local_dynrel;
06308          pp = (struct ppc_dyn_relocs **) vpp;
06309        }
06310 
06311       /* elf_gc_sweep may have already removed all dyn relocs associated
06312         with local syms for a given section.  Don't report a dynreloc
06313         miscount.  */
06314       if (*pp == NULL)
06315        return TRUE;
06316     }
06317 
06318   while ((p = *pp) != NULL)
06319     {
06320       if (p->sec == sec)
06321        {
06322          if (!MUST_BE_DYN_RELOC (r_type))
06323            p->pc_count -= 1;
06324          p->count -= 1;
06325          if (p->count == 0)
06326            *pp = p->next;
06327          return TRUE;
06328        }
06329       pp = &p->next;
06330     }
06331 
06332   (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
06333                         sec->owner, sec);
06334   bfd_set_error (bfd_error_bad_value);
06335   return FALSE;
06336 }
06337 
06338 /* Remove unused Official Procedure Descriptor entries.  Currently we
06339    only remove those associated with functions in discarded link-once
06340    sections, or weakly defined functions that have been overridden.  It
06341    would be possible to remove many more entries for statically linked
06342    applications.  */
06343 
06344 bfd_boolean
06345 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
06346                   bfd_boolean no_opd_opt,
06347                   bfd_boolean non_overlapping)
06348 {
06349   bfd *ibfd;
06350   bfd_boolean some_edited = FALSE;
06351   asection *need_pad = NULL;
06352 
06353   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
06354     {
06355       asection *sec;
06356       Elf_Internal_Rela *relstart, *rel, *relend;
06357       Elf_Internal_Shdr *symtab_hdr;
06358       Elf_Internal_Sym *local_syms;
06359       struct elf_link_hash_entry **sym_hashes;
06360       bfd_vma offset;
06361       bfd_size_type amt;
06362       long *opd_adjust;
06363       bfd_boolean need_edit, add_aux_fields;
06364       bfd_size_type cnt_16b = 0;
06365 
06366       sec = bfd_get_section_by_name (ibfd, ".opd");
06367       if (sec == NULL || sec->size == 0)
06368        continue;
06369 
06370       amt = sec->size * sizeof (long) / 8;
06371       opd_adjust = get_opd_info (sec);
06372       if (opd_adjust == NULL)
06373        {
06374          /* check_relocs hasn't been called.  Must be a ld -r link
06375             or --just-symbols object.   */
06376          opd_adjust = bfd_alloc (obfd, amt);
06377          if (opd_adjust == NULL)
06378            return FALSE;
06379          ppc64_elf_section_data (sec)->u.opd_adjust = opd_adjust;
06380          BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
06381          ppc64_elf_section_data (sec)->sec_type = sec_opd;
06382        }
06383       memset (opd_adjust, 0, amt);
06384 
06385       if (no_opd_opt)
06386        continue;
06387 
06388       if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
06389        continue;
06390 
06391       if (sec->output_section == bfd_abs_section_ptr)
06392        continue;
06393 
06394       /* Look through the section relocs.  */
06395       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
06396        continue;
06397 
06398       local_syms = NULL;
06399       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
06400       sym_hashes = elf_sym_hashes (ibfd);
06401 
06402       /* Read the relocations.  */
06403       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
06404                                        info->keep_memory);
06405       if (relstart == NULL)
06406        return FALSE;
06407 
06408       /* First run through the relocs to check they are sane, and to
06409         determine whether we need to edit this opd section.  */
06410       need_edit = FALSE;
06411       need_pad = sec;
06412       offset = 0;
06413       relend = relstart + sec->reloc_count;
06414       for (rel = relstart; rel < relend; )
06415        {
06416          enum elf_ppc64_reloc_type r_type;
06417          unsigned long r_symndx;
06418          asection *sym_sec;
06419          struct elf_link_hash_entry *h;
06420          Elf_Internal_Sym *sym;
06421 
06422          /* .opd contains a regular array of 16 or 24 byte entries.  We're
06423             only interested in the reloc pointing to a function entry
06424             point.  */
06425          if (rel->r_offset != offset
06426              || rel + 1 >= relend
06427              || (rel + 1)->r_offset != offset + 8)
06428            {
06429              /* If someone messes with .opd alignment then after a
06430                "ld -r" we might have padding in the middle of .opd.
06431                Also, there's nothing to prevent someone putting
06432                something silly in .opd with the assembler.  No .opd
06433                optimization for them!  */
06434            broken_opd:
06435              (*_bfd_error_handler)
06436               (_("%B: .opd is not a regular array of opd entries"), ibfd);
06437              need_edit = FALSE;
06438              break;
06439            }
06440 
06441          if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
06442              || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
06443            {
06444              (*_bfd_error_handler)
06445               (_("%B: unexpected reloc type %u in .opd section"),
06446                ibfd, r_type);
06447              need_edit = FALSE;
06448              break;
06449            }
06450 
06451          r_symndx = ELF64_R_SYM (rel->r_info);
06452          if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
06453                        r_symndx, ibfd))
06454            goto error_ret;
06455 
06456          if (sym_sec == NULL || sym_sec->owner == NULL)
06457            {
06458              const char *sym_name;
06459              if (h != NULL)
06460               sym_name = h->root.root.string;
06461              else
06462               sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
06463                                         sym_sec);
06464 
06465              (*_bfd_error_handler)
06466               (_("%B: undefined sym `%s' in .opd section"),
06467                ibfd, sym_name);
06468              need_edit = FALSE;
06469              break;
06470            }
06471 
06472          /* opd entries are always for functions defined in the
06473             current input bfd.  If the symbol isn't defined in the
06474             input bfd, then we won't be using the function in this
06475             bfd;  It must be defined in a linkonce section in another
06476             bfd, or is weak.  It's also possible that we are
06477             discarding the function due to a linker script /DISCARD/,
06478             which we test for via the output_section.  */
06479          if (sym_sec->owner != ibfd
06480              || sym_sec->output_section == bfd_abs_section_ptr)
06481            need_edit = TRUE;
06482 
06483          rel += 2;
06484          if (rel == relend
06485              || (rel + 1 == relend && rel->r_offset == offset + 16))
06486            {
06487              if (sec->size == offset + 24)
06488               {
06489                 need_pad = NULL;
06490                 break;
06491               }
06492              if (rel == relend && sec->size == offset + 16)
06493               {
06494                 cnt_16b++;
06495                 break;
06496               }
06497              goto broken_opd;
06498            }
06499 
06500          if (rel->r_offset == offset + 24)
06501            offset += 24;
06502          else if (rel->r_offset != offset + 16)
06503            goto broken_opd;
06504          else if (rel + 1 < relend
06505                  && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
06506                  && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
06507            {
06508              offset += 16;
06509              cnt_16b++;
06510            }
06511          else if (rel + 2 < relend
06512                  && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
06513                  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
06514            {
06515              offset += 24;
06516              rel += 1;
06517            }
06518          else
06519            goto broken_opd;
06520        }
06521 
06522       add_aux_fields = non_overlapping && cnt_16b > 0;
06523 
06524       if (need_edit || add_aux_fields)
06525        {
06526          Elf_Internal_Rela *write_rel;
06527          bfd_byte *rptr, *wptr;
06528          bfd_byte *new_contents = NULL;
06529          bfd_boolean skip;
06530          long opd_ent_size;
06531 
06532          /* This seems a waste of time as input .opd sections are all
06533             zeros as generated by gcc, but I suppose there's no reason
06534             this will always be so.  We might start putting something in
06535             the third word of .opd entries.  */
06536          if ((sec->flags & SEC_IN_MEMORY) == 0)
06537            {
06538              bfd_byte *loc;
06539              if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
06540               {
06541                 if (loc != NULL)
06542                   free (loc);
06543               error_ret:
06544                 if (local_syms != NULL
06545                     && symtab_hdr->contents != (unsigned char *) local_syms)
06546                   free (local_syms);
06547                 if (elf_section_data (sec)->relocs != relstart)
06548                   free (relstart);
06549                 return FALSE;
06550               }
06551              sec->contents = loc;
06552              sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
06553            }
06554 
06555          elf_section_data (sec)->relocs = relstart;
06556 
06557          new_contents = sec->contents;
06558          if (add_aux_fields)
06559            {
06560              new_contents = bfd_malloc (sec->size + cnt_16b * 8);
06561              if (new_contents == NULL)
06562               return FALSE;
06563              need_pad = FALSE;
06564            }
06565          wptr = new_contents;
06566          rptr = sec->contents;
06567 
06568          write_rel = relstart;
06569          skip = FALSE;
06570          offset = 0;
06571          opd_ent_size = 0;
06572          for (rel = relstart; rel < relend; rel++)
06573            {
06574              unsigned long r_symndx;
06575              asection *sym_sec;
06576              struct elf_link_hash_entry *h;
06577              Elf_Internal_Sym *sym;
06578 
06579              r_symndx = ELF64_R_SYM (rel->r_info);
06580              if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
06581                            r_symndx, ibfd))
06582               goto error_ret;
06583 
06584              if (rel->r_offset == offset)
06585               {
06586                 struct ppc_link_hash_entry *fdh = NULL;
06587 
06588                 /* See if the .opd entry is full 24 byte or
06589                    16 byte (with fd_aux entry overlapped with next
06590                    fd_func).  */
06591                 opd_ent_size = 24;
06592                 if ((rel + 2 == relend && sec->size == offset + 16)
06593                     || (rel + 3 < relend
06594                        && rel[2].r_offset == offset + 16
06595                        && rel[3].r_offset == offset + 24
06596                        && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
06597                        && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
06598                   opd_ent_size = 16;
06599 
06600                 if (h != NULL
06601                     && h->root.root.string[0] == '.')
06602                   {
06603                     fdh = get_fdh ((struct ppc_link_hash_entry *) h,
06604                                  ppc_hash_table (info));
06605                     if (fdh != NULL
06606                        && fdh->elf.root.type != bfd_link_hash_defined
06607                        && fdh->elf.root.type != bfd_link_hash_defweak)
06608                      fdh = NULL;
06609                   }
06610 
06611                 skip = (sym_sec->owner != ibfd
06612                        || sym_sec->output_section == bfd_abs_section_ptr);
06613                 if (skip)
06614                   {
06615                     if (fdh != NULL && sym_sec->owner == ibfd)
06616                      {
06617                        /* Arrange for the function descriptor sym
06618                           to be dropped.  */
06619                        fdh->elf.root.u.def.value = 0;
06620                        fdh->elf.root.u.def.section = sym_sec;
06621                      }
06622                     opd_adjust[rel->r_offset / 8] = -1;
06623                   }
06624                 else
06625                   {
06626                     /* We'll be keeping this opd entry.  */
06627 
06628                     if (fdh != NULL)
06629                      {
06630                        /* Redefine the function descriptor symbol to
06631                           this location in the opd section.  It is
06632                           necessary to update the value here rather
06633                           than using an array of adjustments as we do
06634                           for local symbols, because various places
06635                           in the generic ELF code use the value
06636                           stored in u.def.value.  */
06637                        fdh->elf.root.u.def.value = wptr - new_contents;
06638                        fdh->adjust_done = 1;
06639                      }
06640 
06641                     /* Local syms are a bit tricky.  We could
06642                       tweak them as they can be cached, but
06643                       we'd need to look through the local syms
06644                       for the function descriptor sym which we
06645                       don't have at the moment.  So keep an
06646                       array of adjustments.  */
06647                     opd_adjust[rel->r_offset / 8]
06648                      = (wptr - new_contents) - (rptr - sec->contents);
06649 
06650                     if (wptr != rptr)
06651                      memcpy (wptr, rptr, opd_ent_size);
06652                     wptr += opd_ent_size;
06653                     if (add_aux_fields && opd_ent_size == 16)
06654                      {
06655                        memset (wptr, '\0', 8);
06656                        wptr += 8;
06657                      }
06658                   }
06659                 rptr += opd_ent_size;
06660                 offset += opd_ent_size;
06661               }
06662 
06663              if (skip)
06664               {
06665                 if (!NO_OPD_RELOCS
06666                     && !info->relocatable
06667                     && !dec_dynrel_count (rel->r_info, sec, info,
06668                                        NULL, h, sym_sec))
06669                   goto error_ret;
06670               }
06671              else
06672               {
06673                 /* We need to adjust any reloc offsets to point to the
06674                    new opd entries.  While we're at it, we may as well
06675                    remove redundant relocs.  */
06676                 rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
06677                 if (write_rel != rel)
06678                   memcpy (write_rel, rel, sizeof (*rel));
06679                 ++write_rel;
06680               }
06681            }
06682 
06683          sec->size = wptr - new_contents;
06684          sec->reloc_count = write_rel - relstart;
06685          if (add_aux_fields)
06686            {
06687              free (sec->contents);
06688              sec->contents = new_contents;
06689            }
06690 
06691          /* Fudge the header size too, as this is used later in
06692             elf_bfd_final_link if we are emitting relocs.  */
06693          elf_section_data (sec)->rel_hdr.sh_size
06694            = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
06695          BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
06696          some_edited = TRUE;
06697        }
06698       else if (elf_section_data (sec)->relocs != relstart)
06699        free (relstart);
06700 
06701       if (local_syms != NULL
06702          && symtab_hdr->contents != (unsigned char *) local_syms)
06703        {
06704          if (!info->keep_memory)
06705            free (local_syms);
06706          else
06707            symtab_hdr->contents = (unsigned char *) local_syms;
06708        }
06709     }
06710 
06711   if (some_edited)
06712     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
06713 
06714   /* If we are doing a final link and the last .opd entry is just 16 byte
06715      long, add a 8 byte padding after it.  */
06716   if (need_pad != NULL && !info->relocatable)
06717     {
06718       bfd_byte *p;
06719 
06720       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
06721        {
06722          BFD_ASSERT (need_pad->size > 0);
06723 
06724          p = bfd_malloc (need_pad->size + 8);
06725          if (p == NULL)
06726            return FALSE;
06727 
06728          if (! bfd_get_section_contents (need_pad->owner, need_pad,
06729                                      p, 0, need_pad->size))
06730            return FALSE;
06731 
06732          need_pad->contents = p;
06733          need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
06734        }
06735       else
06736        {
06737          p = bfd_realloc (need_pad->contents, need_pad->size + 8);
06738          if (p == NULL)
06739            return FALSE;
06740 
06741          need_pad->contents = p;
06742        }
06743 
06744       memset (need_pad->contents + need_pad->size, 0, 8);
06745       need_pad->size += 8;
06746     }
06747 
06748   return TRUE;
06749 }
06750 
06751 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
06752 
06753 asection *
06754 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
06755 {
06756   struct ppc_link_hash_table *htab;
06757 
06758   htab = ppc_hash_table (info);
06759   if (htab->tls_get_addr != NULL)
06760     {
06761       struct ppc_link_hash_entry *h = htab->tls_get_addr;
06762 
06763       while (h->elf.root.type == bfd_link_hash_indirect
06764             || h->elf.root.type == bfd_link_hash_warning)
06765        h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
06766 
06767       htab->tls_get_addr = h;
06768 
06769       if (htab->tls_get_addr_fd == NULL
06770          && h->oh != NULL
06771          && h->oh->is_func_descriptor
06772          && (h->oh->elf.root.type == bfd_link_hash_defined
06773              || h->oh->elf.root.type == bfd_link_hash_defweak))
06774        htab->tls_get_addr_fd = h->oh;
06775     }
06776 
06777   if (htab->tls_get_addr_fd != NULL)
06778     {
06779       struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
06780 
06781       while (h->elf.root.type == bfd_link_hash_indirect
06782             || h->elf.root.type == bfd_link_hash_warning)
06783        h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
06784 
06785       htab->tls_get_addr_fd = h;
06786     }
06787 
06788   return _bfd_elf_tls_setup (obfd, info);
06789 }
06790 
06791 /* Run through all the TLS relocs looking for optimization
06792    opportunities.  The linker has been hacked (see ppc64elf.em) to do
06793    a preliminary section layout so that we know the TLS segment
06794    offsets.  We can't optimize earlier because some optimizations need
06795    to know the tp offset, and we need to optimize before allocating
06796    dynamic relocations.  */
06797 
06798 bfd_boolean
06799 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
06800 {
06801   bfd *ibfd;
06802   asection *sec;
06803   struct ppc_link_hash_table *htab;
06804 
06805   if (info->relocatable || info->shared)
06806     return TRUE;
06807 
06808   htab = ppc_hash_table (info);
06809   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
06810     {
06811       Elf_Internal_Sym *locsyms = NULL;
06812       asection *toc = bfd_get_section_by_name (ibfd, ".toc");
06813       unsigned char *toc_ref = NULL;
06814 
06815       /* Look at all the sections for this file, with TOC last.  */
06816       for (sec = (ibfd->sections == toc && toc && toc->next ? toc->next
06817                 : ibfd->sections);
06818           sec != NULL;
06819           sec = (sec == toc ? NULL
06820                 : sec->next == NULL ? toc
06821                 : sec->next == toc && toc->next ? toc->next
06822                 : sec->next))
06823        if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
06824          {
06825            Elf_Internal_Rela *relstart, *rel, *relend;
06826            int expecting_tls_get_addr;
06827            long toc_ref_index = 0;
06828 
06829            /* Read the relocations.  */
06830            relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
06831                                             info->keep_memory);
06832            if (relstart == NULL)
06833              return FALSE;
06834 
06835            expecting_tls_get_addr = 0;
06836            relend = relstart + sec->reloc_count;
06837            for (rel = relstart; rel < relend; rel++)
06838              {
06839               enum elf_ppc64_reloc_type r_type;
06840               unsigned long r_symndx;
06841               struct elf_link_hash_entry *h;
06842               Elf_Internal_Sym *sym;
06843               asection *sym_sec;
06844               char *tls_mask;
06845               char tls_set, tls_clear, tls_type = 0;
06846               bfd_vma value;
06847               bfd_boolean ok_tprel, is_local;
06848 
06849               r_symndx = ELF64_R_SYM (rel->r_info);
06850               if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
06851                             r_symndx, ibfd))
06852                 {
06853                 err_free_rel:
06854                   if (elf_section_data (sec)->relocs != relstart)
06855                     free (relstart);
06856                   if (toc_ref != NULL)
06857                     free (toc_ref);
06858                   if (locsyms != NULL
06859                      && (elf_tdata (ibfd)->symtab_hdr.contents
06860                          != (unsigned char *) locsyms))
06861                     free (locsyms);
06862                   return FALSE;
06863                 }
06864 
06865               if (h != NULL)
06866                 {
06867                   if (h->root.type != bfd_link_hash_defined
06868                      && h->root.type != bfd_link_hash_defweak)
06869                     continue;
06870                   value = h->root.u.def.value;
06871                 }
06872               else
06873                 /* Symbols referenced by TLS relocs must be of type
06874                    STT_TLS.  So no need for .opd local sym adjust.  */
06875                 value = sym->st_value;
06876 
06877               ok_tprel = FALSE;
06878               is_local = FALSE;
06879               if (h == NULL
06880                   || !h->def_dynamic)
06881                 {
06882                   is_local = TRUE;
06883                   value += sym_sec->output_offset;
06884                   value += sym_sec->output_section->vma;
06885                   value -= htab->elf.tls_sec->vma;
06886                   ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
06887                             < (bfd_vma) 1 << 32);
06888                 }
06889 
06890               r_type = ELF64_R_TYPE (rel->r_info);
06891               switch (r_type)
06892                 {
06893                 case R_PPC64_GOT_TLSLD16:
06894                 case R_PPC64_GOT_TLSLD16_LO:
06895                 case R_PPC64_GOT_TLSLD16_HI:
06896                 case R_PPC64_GOT_TLSLD16_HA:
06897                   /* These relocs should never be against a symbol
06898                      defined in a shared lib.  Leave them alone if
06899                      that turns out to be the case.  */
06900                   ppc64_tlsld_got (ibfd)->refcount -= 1;
06901                   if (!is_local)
06902                     continue;
06903 
06904                   /* LD -> LE */
06905                   tls_set = 0;
06906                   tls_clear = TLS_LD;
06907                   tls_type = TLS_TLS | TLS_LD;
06908                   expecting_tls_get_addr = 1;
06909                   break;
06910 
06911                 case R_PPC64_GOT_TLSGD16:
06912                 case R_PPC64_GOT_TLSGD16_LO:
06913                 case R_PPC64_GOT_TLSGD16_HI:
06914                 case R_PPC64_GOT_TLSGD16_HA:
06915                   if (ok_tprel)
06916                     /* GD -> LE */
06917                     tls_set = 0;
06918                   else
06919                     /* GD -> IE */
06920                     tls_set = TLS_TLS | TLS_TPRELGD;
06921                   tls_clear = TLS_GD;
06922                   tls_type = TLS_TLS | TLS_GD;
06923                   expecting_tls_get_addr = 1;
06924                   break;
06925 
06926                 case R_PPC64_GOT_TPREL16_DS:
06927                 case R_PPC64_GOT_TPREL16_LO_DS:
06928                 case R_PPC64_GOT_TPREL16_HI:
06929                 case R_PPC64_GOT_TPREL16_HA:
06930                   expecting_tls_get_addr = 0;
06931                   if (ok_tprel)
06932                     {
06933                      /* IE -> LE */
06934                      tls_set = 0;
06935                      tls_clear = TLS_TPREL;
06936                      tls_type = TLS_TLS | TLS_TPREL;
06937                      break;
06938                     }
06939                   else
06940                     continue;
06941 
06942                 case R_PPC64_REL14:
06943                 case R_PPC64_REL14_BRTAKEN:
06944                 case R_PPC64_REL14_BRNTAKEN:
06945                 case R_PPC64_REL24:
06946                   if (h != NULL
06947                      && (h == &htab->tls_get_addr->elf
06948                          || h == &htab->tls_get_addr_fd->elf))
06949                     {
06950                      if (!expecting_tls_get_addr
06951                          && rel != relstart
06952                          && ((ELF64_R_TYPE (rel[-1].r_info)
06953                              == R_PPC64_TOC16)
06954                             || (ELF64_R_TYPE (rel[-1].r_info)
06955                                 == R_PPC64_TOC16_LO)))
06956                        {
06957                          /* Check for toc tls entries.  */
06958                          char *toc_tls;
06959                          int retval;
06960 
06961                          retval = get_tls_mask (&toc_tls, NULL, &locsyms,
06962                                              rel - 1, ibfd);
06963                          if (retval == 0)
06964                            goto err_free_rel;
06965                          if (retval > 1 && toc_tls != NULL)
06966                            {
06967                             expecting_tls_get_addr = 1;
06968                             if (toc_ref != NULL)
06969                               toc_ref[toc_ref_index] = 1;
06970                            }
06971                        }
06972 
06973                      if (expecting_tls_get_addr)
06974                        {
06975                          struct plt_entry *ent;
06976                          for (ent = h->plt.plist; ent; ent = ent->next)
06977                            if (ent->addend == 0)
06978                             {
06979                               if (ent->plt.refcount > 0)
06980                                 ent->plt.refcount -= 1;
06981                               break;
06982                             }
06983                        }
06984                     }
06985                   expecting_tls_get_addr = 0;
06986                   continue;
06987 
06988                 case R_PPC64_TOC16:
06989                 case R_PPC64_TOC16_LO:
06990                 case R_PPC64_TLS:
06991                   expecting_tls_get_addr = 0;
06992                   if (sym_sec == toc && toc != NULL)
06993                     {
06994                      /* Mark this toc entry as referenced by a TLS
06995                         code sequence.  We can do that now in the
06996                         case of R_PPC64_TLS, and after checking for
06997                         tls_get_addr for the TOC16 relocs.  */
06998                      if (toc_ref == NULL)
06999                        {
07000                          toc_ref = bfd_zmalloc (toc->size / 8);
07001                          if (toc_ref == NULL)
07002                            goto err_free_rel;
07003                        }
07004                      if (h != NULL)
07005                        value = h->root.u.def.value;
07006                      else
07007                        value = sym->st_value;
07008                      value += rel->r_addend;
07009                      BFD_ASSERT (value < toc->size && value % 8 == 0);
07010                      toc_ref_index = value / 8;
07011                      if (r_type == R_PPC64_TLS)
07012                        toc_ref[toc_ref_index] = 1;
07013                     }
07014                   continue;
07015 
07016                 case R_PPC64_TPREL64:
07017                   expecting_tls_get_addr = 0;
07018                   if (sec != toc
07019                      || toc_ref == NULL
07020                      || !toc_ref[rel->r_offset / 8])
07021                     continue;
07022                   if (ok_tprel)
07023                     {
07024                      /* IE -> LE */
07025                      tls_set = TLS_EXPLICIT;
07026                      tls_clear = TLS_TPREL;
07027                      break;
07028                     }
07029                   else
07030                     continue;
07031 
07032                 case R_PPC64_DTPMOD64:
07033                   expecting_tls_get_addr = 0;
07034                   if (sec != toc
07035                      || toc_ref == NULL
07036                      || !toc_ref[rel->r_offset / 8])
07037                     continue;
07038                   if (rel + 1 < relend
07039                      && (rel[1].r_info
07040                          == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
07041                      && rel[1].r_offset == rel->r_offset + 8)
07042                     {
07043                      if (ok_tprel)
07044                        /* GD -> LE */
07045                        tls_set = TLS_EXPLICIT | TLS_GD;
07046                      else
07047                        /* GD -> IE */
07048                        tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
07049                      tls_clear = TLS_GD;
07050                     }
07051                   else
07052                     {
07053                      if (!is_local)
07054                        continue;
07055 
07056                      /* LD -> LE */
07057                      tls_set = TLS_EXPLICIT;
07058                      tls_clear = TLS_LD;
07059                     }
07060                   break;
07061 
07062                 default:
07063                   expecting_tls_get_addr = 0;
07064                   continue;
07065                 }
07066 
07067               if ((tls_set & TLS_EXPLICIT) == 0)
07068                 {
07069                   struct got_entry *ent;
07070 
07071                   /* Adjust got entry for this reloc.  */
07072                   if (h != NULL)
07073                     ent = h->got.glist;
07074                   else
07075                     ent = elf_local_got_ents (ibfd)[r_symndx];
07076 
07077                   for (; ent != NULL; ent = ent->next)
07078                     if (ent->addend == rel->r_addend
07079                        && ent->owner == ibfd
07080                        && ent->tls_type == tls_type)
07081                      break;
07082                   if (ent == NULL)
07083                     abort ();
07084 
07085                   if (tls_set == 0)
07086                     {
07087                      /* We managed to get rid of a got entry.  */
07088                      if (ent->got.refcount > 0)
07089                        ent->got.refcount -= 1;
07090                     }
07091                 }
07092               else
07093                 {
07094                   /* If we got rid of a DTPMOD/DTPREL reloc pair then
07095                      we'll lose one or two dyn relocs.  */
07096                   if (!dec_dynrel_count (rel->r_info, sec, info,
07097                                       NULL, h, sym_sec))
07098                     return FALSE;
07099 
07100                   if (tls_set == (TLS_EXPLICIT | TLS_GD))
07101                     {
07102                      if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
07103                                           NULL, h, sym_sec))
07104                        return FALSE;
07105                     }
07106                 }
07107 
07108               *tls_mask |= tls_set;
07109               *tls_mask &= ~tls_clear;
07110              }
07111 
07112            if (elf_section_data (sec)->relocs != relstart)
07113              free (relstart);
07114          }
07115 
07116       if (toc_ref != NULL)
07117        free (toc_ref);
07118 
07119       if (locsyms != NULL
07120          && (elf_tdata (ibfd)->symtab_hdr.contents
07121              != (unsigned char *) locsyms))
07122        {
07123          if (!info->keep_memory)
07124            free (locsyms);
07125          else
07126            elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
07127        }
07128     }
07129   return TRUE;
07130 }
07131 
07132 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
07133    the values of any global symbols in a toc section that has been
07134    edited.  Globals in toc sections should be a rarity, so this function
07135    sets a flag if any are found in toc sections other than the one just
07136    edited, so that futher hash table traversals can be avoided.  */
07137 
07138 struct adjust_toc_info
07139 {
07140   asection *toc;
07141   unsigned long *skip;
07142   bfd_boolean global_toc_syms;
07143 };
07144 
07145 static bfd_boolean
07146 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
07147 {
07148   struct ppc_link_hash_entry *eh;
07149   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
07150 
07151   if (h->root.type == bfd_link_hash_indirect)
07152     return TRUE;
07153 
07154   if (h->root.type == bfd_link_hash_warning)
07155     h = (struct elf_link_hash_entry *) h->root.u.i.link;
07156 
07157   if (h->root.type != bfd_link_hash_defined
07158       && h->root.type != bfd_link_hash_defweak)
07159     return TRUE;
07160 
07161   eh = (struct ppc_link_hash_entry *) h;
07162   if (eh->adjust_done)
07163     return TRUE;
07164 
07165   if (eh->elf.root.u.def.section == toc_inf->toc)
07166     {
07167       unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
07168       if (skip != (unsigned long) -1)
07169        eh->elf.root.u.def.value -= skip;
07170       else
07171        {
07172          (*_bfd_error_handler)
07173            (_("%s defined in removed toc entry"), eh->elf.root.root.string);
07174          eh->elf.root.u.def.section = &bfd_abs_section;
07175          eh->elf.root.u.def.value = 0;
07176        }
07177       eh->adjust_done = 1;
07178     }
07179   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
07180     toc_inf->global_toc_syms = TRUE;
07181 
07182   return TRUE;
07183 }
07184 
07185 /* Examine all relocs referencing .toc sections in order to remove
07186    unused .toc entries.  */
07187 
07188 bfd_boolean
07189 ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
07190 {
07191   bfd *ibfd;
07192   struct adjust_toc_info toc_inf;
07193 
07194   toc_inf.global_toc_syms = TRUE;
07195   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
07196     {
07197       asection *toc, *sec;
07198       Elf_Internal_Shdr *symtab_hdr;
07199       Elf_Internal_Sym *local_syms;
07200       struct elf_link_hash_entry **sym_hashes;
07201       Elf_Internal_Rela *relstart, *rel;
07202       unsigned long *skip, *drop;
07203       unsigned char *used;
07204       unsigned char *keep, last, some_unused;
07205 
07206       toc = bfd_get_section_by_name (ibfd, ".toc");
07207       if (toc == NULL
07208          || toc->size == 0
07209          || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
07210          || elf_discarded_section (toc))
07211        continue;
07212 
07213       local_syms = NULL;
07214       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
07215       sym_hashes = elf_sym_hashes (ibfd);
07216 
07217       /* Look at sections dropped from the final link.  */
07218       skip = NULL;
07219       relstart = NULL;
07220       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
07221        {
07222          if (sec->reloc_count == 0
07223              || !elf_discarded_section (sec)
07224              || get_opd_info (sec)
07225              || (sec->flags & SEC_ALLOC) == 0
07226              || (sec->flags & SEC_DEBUGGING) != 0)
07227            continue;
07228 
07229          relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
07230          if (relstart == NULL)
07231            goto error_ret;
07232 
07233          /* Run through the relocs to see which toc entries might be
07234             unused.  */
07235          for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
07236            {
07237              enum elf_ppc64_reloc_type r_type;
07238              unsigned long r_symndx;
07239              asection *sym_sec;
07240              struct elf_link_hash_entry *h;
07241              Elf_Internal_Sym *sym;
07242              bfd_vma val;
07243 
07244              r_type = ELF64_R_TYPE (rel->r_info);
07245              switch (r_type)
07246               {
07247               default:
07248                 continue;
07249 
07250               case R_PPC64_TOC16:
07251               case R_PPC64_TOC16_LO:
07252               case R_PPC64_TOC16_HI:
07253               case R_PPC64_TOC16_HA:
07254               case R_PPC64_TOC16_DS:
07255               case R_PPC64_TOC16_LO_DS:
07256                 break;
07257               }
07258 
07259              r_symndx = ELF64_R_SYM (rel->r_info);
07260              if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
07261                            r_symndx, ibfd))
07262               goto error_ret;
07263 
07264              if (sym_sec != toc)
07265               continue;
07266 
07267              if (h != NULL)
07268               val = h->root.u.def.value;
07269              else
07270               val = sym->st_value;
07271              val += rel->r_addend;
07272 
07273              if (val >= toc->size)
07274               continue;
07275 
07276              /* Anything in the toc ought to be aligned to 8 bytes.
07277                If not, don't mark as unused.  */
07278              if (val & 7)
07279               continue;
07280 
07281              if (skip == NULL)
07282               {
07283                 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
07284                 if (skip == NULL)
07285                   goto error_ret;
07286               }
07287 
07288              skip[val >> 3] = 1;
07289            }
07290 
07291          if (elf_section_data (sec)->relocs != relstart)
07292            free (relstart);
07293        }
07294 
07295       if (skip == NULL)
07296        continue;
07297 
07298       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
07299       if (used == NULL)
07300        {
07301        error_ret:
07302          if (local_syms != NULL
07303              && symtab_hdr->contents != (unsigned char *) local_syms)
07304            free (local_syms);
07305          if (sec != NULL
07306              && relstart != NULL
07307              && elf_section_data (sec)->relocs != relstart)
07308            free (relstart);
07309          if (skip != NULL)
07310            free (skip);
07311          return FALSE;
07312        }
07313 
07314       /* Now check all kept sections that might reference the toc.
07315         Check the toc itself last.  */
07316       for (sec = (ibfd->sections == toc && toc->next ? toc->next
07317                 : ibfd->sections);
07318           sec != NULL;
07319           sec = (sec == toc ? NULL
07320                 : sec->next == NULL ? toc
07321                 : sec->next == toc && toc->next ? toc->next
07322                 : sec->next))
07323        {
07324          int repeat;
07325 
07326          if (sec->reloc_count == 0
07327              || elf_discarded_section (sec)
07328              || get_opd_info (sec)
07329              || (sec->flags & SEC_ALLOC) == 0
07330              || (sec->flags & SEC_DEBUGGING) != 0)
07331            continue;
07332 
07333          relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
07334          if (relstart == NULL)
07335            goto error_ret;
07336 
07337          /* Mark toc entries referenced as used.  */
07338          repeat = 0;
07339          do
07340            for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
07341              {
07342               enum elf_ppc64_reloc_type r_type;
07343               unsigned long r_symndx;
07344               asection *sym_sec;
07345               struct elf_link_hash_entry *h;
07346               Elf_Internal_Sym *sym;
07347               bfd_vma val;
07348 
07349               r_type = ELF64_R_TYPE (rel->r_info);
07350               switch (r_type)
07351                 {
07352                 case R_PPC64_TOC16:
07353                 case R_PPC64_TOC16_LO:
07354                 case R_PPC64_TOC16_HI:
07355                 case R_PPC64_TOC16_HA:
07356                 case R_PPC64_TOC16_DS:
07357                 case R_PPC64_TOC16_LO_DS:
07358                   /* In case we're taking addresses of toc entries.  */
07359                 case R_PPC64_ADDR64:
07360                   break;
07361 
07362                 default:
07363                   continue;
07364                 }
07365 
07366               r_symndx = ELF64_R_SYM (rel->r_info);
07367               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
07368                             r_symndx, ibfd))
07369                 {
07370                   free (used);
07371                   goto error_ret;
07372                 }
07373 
07374               if (sym_sec != toc)
07375                 continue;
07376 
07377               if (h != NULL)
07378                 val = h->root.u.def.value;
07379               else
07380                 val = sym->st_value;
07381               val += rel->r_addend;
07382 
07383               if (val >= toc->size)
07384                 continue;
07385 
07386               /* For the toc section, we only mark as used if
07387                  this entry itself isn't unused.  */
07388               if (sec == toc
07389                   && !used[val >> 3]
07390                   && (used[rel->r_offset >> 3]
07391                      || !skip[rel->r_offset >> 3]))
07392                 /* Do all the relocs again, to catch reference
07393                    chains.  */
07394                 repeat = 1;
07395 
07396               used[val >> 3] = 1;
07397              }
07398          while (repeat);
07399        }
07400 
07401       /* Merge the used and skip arrays.  Assume that TOC
07402         doublewords not appearing as either used or unused belong
07403         to to an entry more than one doubleword in size.  */
07404       for (drop = skip, keep = used, last = 0, some_unused = 0;
07405           drop < skip + (toc->size + 7) / 8;
07406           ++drop, ++keep)
07407        {
07408          if (*keep)
07409            {
07410              *drop = 0;
07411              last = 0;
07412            }
07413          else if (*drop)
07414            {
07415              some_unused = 1;
07416              last = 1;
07417            }
07418          else
07419            *drop = last;
07420        }
07421 
07422       free (used);
07423 
07424       if (some_unused)
07425        {
07426          bfd_byte *contents, *src;
07427          unsigned long off;
07428 
07429          /* Shuffle the toc contents, and at the same time convert the
07430             skip array from booleans into offsets.  */
07431          if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
07432            goto error_ret;
07433 
07434          elf_section_data (toc)->this_hdr.contents = contents;
07435 
07436          for (src = contents, off = 0, drop = skip;
07437               src < contents + toc->size;
07438               src += 8, ++drop)
07439            {
07440              if (*drop)
07441               {
07442                 *drop = (unsigned long) -1;
07443                 off += 8;
07444               }
07445              else if (off != 0)
07446               {
07447                 *drop = off;
07448                 memcpy (src - off, src, 8);
07449               }
07450            }
07451          toc->rawsize = toc->size;
07452          toc->size = src - contents - off;
07453 
07454          if (toc->reloc_count != 0)
07455            {
07456              Elf_Internal_Rela *wrel;
07457              bfd_size_type sz;
07458 
07459              /* Read toc relocs.  */
07460              relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
07461                                               TRUE);
07462              if (relstart == NULL)
07463               goto error_ret;
07464 
07465              /* Remove unused toc relocs, and adjust those we keep.  */
07466              wrel = relstart;
07467              for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
07468               if (skip[rel->r_offset >> 3] != (unsigned long) -1)
07469                 {
07470                   wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
07471                   wrel->r_info = rel->r_info;
07472                   wrel->r_addend = rel->r_addend;
07473                   ++wrel;
07474                 }
07475               else if (!dec_dynrel_count (rel->r_info, toc, info,
07476                                        &local_syms, NULL, NULL))
07477                 goto error_ret;
07478 
07479              toc->reloc_count = wrel - relstart;
07480              sz = elf_section_data (toc)->rel_hdr.sh_entsize;
07481              elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
07482              BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
07483            }
07484 
07485          /* Adjust addends for relocs against the toc section sym.  */
07486          for (sec = ibfd->sections; sec != NULL; sec = sec->next)
07487            {
07488              if (sec->reloc_count == 0
07489                 || elf_discarded_section (sec))
07490               continue;
07491 
07492              relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
07493                                               TRUE);
07494              if (relstart == NULL)
07495               goto error_ret;
07496 
07497              for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
07498               {
07499                 enum elf_ppc64_reloc_type r_type;
07500                 unsigned long r_symndx;
07501                 asection *sym_sec;
07502                 struct elf_link_hash_entry *h;
07503                 Elf_Internal_Sym *sym;
07504 
07505                 r_type = ELF64_R_TYPE (rel->r_info);
07506                 switch (r_type)
07507                   {
07508                   default:
07509                     continue;
07510 
07511                   case R_PPC64_TOC16:
07512                   case R_PPC64_TOC16_LO:
07513                   case R_PPC64_TOC16_HI:
07514                   case R_PPC64_TOC16_HA:
07515                   case R_PPC64_TOC16_DS:
07516                   case R_PPC64_TOC16_LO_DS:
07517                   case R_PPC64_ADDR64:
07518                     break;
07519                   }
07520 
07521                 r_symndx = ELF64_R_SYM (rel->r_info);
07522                 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
07523                               r_symndx, ibfd))
07524                   goto error_ret;
07525 
07526                 if (sym_sec != toc || h != NULL || sym->st_value != 0)
07527                   continue;
07528 
07529                 rel->r_addend -= skip[rel->r_addend >> 3];
07530               }
07531            }
07532 
07533          /* We shouldn't have local or global symbols defined in the TOC,
07534             but handle them anyway.  */
07535          if (local_syms != NULL)
07536            {
07537              Elf_Internal_Sym *sym;
07538 
07539              for (sym = local_syms;
07540                  sym < local_syms + symtab_hdr->sh_info;
07541                  ++sym)
07542               if (sym->st_shndx != SHN_UNDEF
07543                   && (sym->st_shndx < SHN_LORESERVE
07544                      || sym->st_shndx > SHN_HIRESERVE)
07545                   && sym->st_value != 0
07546                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
07547                 {
07548                   if (skip[sym->st_value >> 3] != (unsigned long) -1)
07549                     sym->st_value -= skip[sym->st_value >> 3];
07550                   else
07551                     {
07552                      (*_bfd_error_handler)
07553                        (_("%s defined in removed toc entry"),
07554                         bfd_elf_sym_name (ibfd, symtab_hdr, sym,
07555                                         NULL));
07556                      sym->st_value = 0;
07557                      sym->st_shndx = SHN_ABS;
07558                     }
07559                   symtab_hdr->contents = (unsigned char *) local_syms;
07560                 }
07561            }
07562 
07563          /* Finally, adjust any global syms defined in the toc.  */
07564          if (toc_inf.global_toc_syms)
07565            {
07566              toc_inf.toc = toc;
07567              toc_inf.skip = skip;
07568              toc_inf.global_toc_syms = FALSE;
07569              elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
07570                                   &toc_inf);
07571            }
07572        }
07573 
07574       if (local_syms != NULL
07575          && symtab_hdr->contents != (unsigned char *) local_syms)
07576        {
07577          if (!info->keep_memory)
07578            free (local_syms);
07579          else
07580            symtab_hdr->contents = (unsigned char *) local_syms;
07581        }
07582       free (skip);
07583     }
07584 
07585   return TRUE;
07586 }
07587 
07588 /* Allocate space in .plt, .got and associated reloc sections for
07589    dynamic relocs.  */
07590 
07591 static bfd_boolean
07592 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
07593 {
07594   struct bfd_link_info *info;
07595   struct ppc_link_hash_table *htab;
07596   asection *s;
07597   struct ppc_link_hash_entry *eh;
07598   struct ppc_dyn_relocs *p;
07599   struct got_entry *gent;
07600 
07601   if (h->root.type == bfd_link_hash_indirect)
07602     return TRUE;
07603 
07604   if (h->root.type == bfd_link_hash_warning)
07605     h = (struct elf_link_hash_entry *) h->root.u.i.link;
07606 
07607   info = (struct bfd_link_info *) inf;
07608   htab = ppc_hash_table (info);
07609 
07610   if (htab->elf.dynamic_sections_created
07611       && h->dynindx != -1
07612       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
07613     {
07614       struct plt_entry *pent;
07615       bfd_boolean doneone = FALSE;
07616       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
07617        if (pent->plt.refcount > 0)
07618          {
07619            /* If this is the first .plt entry, make room for the special
07620               first entry.  */
07621            s = htab->plt;
07622            if (s->size == 0)
07623              s->size += PLT_INITIAL_ENTRY_SIZE;
07624 
07625            pent->plt.offset = s->size;
07626 
07627            /* Make room for this entry.  */
07628            s->size += PLT_ENTRY_SIZE;
07629 
07630            /* Make room for the .glink code.  */
07631            s = htab->glink;
07632            if (s->size == 0)
07633              s->size += GLINK_CALL_STUB_SIZE;
07634            /* We need bigger stubs past index 32767.  */
07635            if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
07636              s->size += 4;
07637            s->size += 2*4;
07638 
07639            /* We also need to make an entry in the .rela.plt section.  */
07640            s = htab->relplt;
07641            s->size += sizeof (Elf64_External_Rela);
07642            doneone = TRUE;
07643          }
07644        else
07645          pent->plt.offset = (bfd_vma) -1;
07646       if (!doneone)
07647        {
07648          h->plt.plist = NULL;
07649          h->needs_plt = 0;
07650        }
07651     }
07652   else
07653     {
07654       h->plt.plist = NULL;
07655       h->needs_plt = 0;
07656     }
07657 
07658   eh = (struct ppc_link_hash_entry *) h;
07659   /* Run through the TLS GD got entries first if we're changing them
07660      to TPREL.  */
07661   if ((eh->tls_mask & TLS_TPRELGD) != 0)
07662     for (gent = h->got.glist; gent != NULL; gent = gent->next)
07663       if (gent->got.refcount > 0
07664          && (gent->tls_type & TLS_GD) != 0)
07665        {
07666          /* This was a GD entry that has been converted to TPREL.  If
07667             there happens to be a TPREL entry we can use that one.  */
07668          struct got_entry *ent;
07669          for (ent = h->got.glist; ent != NULL; ent = ent->next)
07670            if (ent->got.refcount > 0
07671               && (ent->tls_type & TLS_TPREL) != 0
07672               && ent->addend == gent->addend
07673               && ent->owner == gent->owner)
07674              {
07675               gent->got.refcount = 0;
07676               break;
07677              }
07678 
07679          /* If not, then we'll be using our own TPREL entry.  */
07680          if (gent->got.refcount != 0)
07681            gent->tls_type = TLS_TLS | TLS_TPREL;
07682        }
07683 
07684   for (gent = h->got.glist; gent != NULL; gent = gent->next)
07685     if (gent->got.refcount > 0)
07686       {
07687        bfd_boolean dyn;
07688 
07689        /* Make sure this symbol is output as a dynamic symbol.
07690           Undefined weak syms won't yet be marked as dynamic,
07691           nor will all TLS symbols.  */
07692        if (h->dynindx == -1
07693            && !h->forced_local)
07694          {
07695            if (! bfd_elf_link_record_dynamic_symbol (info, h))
07696              return FALSE;
07697          }
07698 
07699        if ((gent->tls_type & TLS_LD) != 0
07700            && !h->def_dynamic)
07701          {
07702            gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
07703            continue;
07704          }
07705 
07706        s = ppc64_elf_tdata (gent->owner)->got;
07707        gent->got.offset = s->size;
07708        s->size
07709          += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
07710        dyn = htab->elf.dynamic_sections_created;
07711        if ((info->shared
07712             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
07713            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
07714               || h->root.type != bfd_link_hash_undefweak))
07715          ppc64_elf_tdata (gent->owner)->relgot->size
07716            += (gent->tls_type & eh->tls_mask & TLS_GD
07717               ? 2 * sizeof (Elf64_External_Rela)
07718               : sizeof (Elf64_External_Rela));
07719       }
07720     else
07721       gent->got.offset = (bfd_vma) -1;
07722 
07723   if (eh->dyn_relocs == NULL)
07724     return TRUE;
07725 
07726   /* In the shared -Bsymbolic case, discard space allocated for
07727      dynamic pc-relative relocs against symbols which turn out to be
07728      defined in regular objects.  For the normal shared case, discard
07729      space for relocs that have become local due to symbol visibility
07730      changes.  */
07731 
07732   if (info->shared)
07733     {
07734       /* Relocs that use pc_count are those that appear on a call insn,
07735         or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
07736         generated via assembly.  We want calls to protected symbols to
07737         resolve directly to the function rather than going via the plt.
07738         If people want function pointer comparisons to work as expected
07739         then they should avoid writing weird assembly.  */
07740       if (SYMBOL_CALLS_LOCAL (info, h))
07741        {
07742          struct ppc_dyn_relocs **pp;
07743 
07744          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
07745            {
07746              p->count -= p->pc_count;
07747              p->pc_count = 0;
07748              if (p->count == 0)
07749               *pp = p->next;
07750              else
07751               pp = &p->next;
07752            }
07753        }
07754 
07755       /* Also discard relocs on undefined weak syms with non-default
07756         visibility.  */
07757       if (eh->dyn_relocs != NULL
07758          && h->root.type == bfd_link_hash_undefweak)
07759        {
07760          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
07761            eh->dyn_relocs = NULL;
07762 
07763          /* Make sure this symbol is output as a dynamic symbol.
07764             Undefined weak syms won't yet be marked as dynamic.  */
07765          else if (h->dynindx == -1
07766                  && !h->forced_local)
07767            {
07768              if (! bfd_elf_link_record_dynamic_symbol (info, h))
07769               return FALSE;
07770            }
07771        }
07772     }
07773   else if (ELIMINATE_COPY_RELOCS)
07774     {
07775       /* For the non-shared case, discard space for relocs against
07776         symbols which turn out to need copy relocs or are not
07777         dynamic.  */
07778 
07779       if (!h->non_got_ref
07780          && h->def_dynamic
07781          && !h->def_regular)
07782        {
07783          /* Make sure this symbol is output as a dynamic symbol.
07784             Undefined weak syms won't yet be marked as dynamic.  */
07785          if (h->dynindx == -1
07786              && !h->forced_local)
07787            {
07788              if (! bfd_elf_link_record_dynamic_symbol (info, h))
07789               return FALSE;
07790            }
07791 
07792          /* If that succeeded, we know we'll be keeping all the
07793             relocs.  */
07794          if (h->dynindx != -1)
07795            goto keep;
07796        }
07797 
07798       eh->dyn_relocs = NULL;
07799 
07800     keep: ;
07801     }
07802 
07803   /* Finally, allocate space.  */
07804   for (p = eh->dyn_relocs; p != NULL; p = p->next)
07805     {
07806       asection *sreloc = elf_section_data (p->sec)->sreloc;
07807       sreloc->size += p->count * sizeof (Elf64_External_Rela);
07808     }
07809 
07810   return TRUE;
07811 }
07812 
07813 /* Find any dynamic relocs that apply to read-only sections.  */
07814 
07815 static bfd_boolean
07816 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
07817 {
07818   struct ppc_link_hash_entry *eh;
07819   struct ppc_dyn_relocs *p;
07820 
07821   if (h->root.type == bfd_link_hash_warning)
07822     h = (struct elf_link_hash_entry *) h->root.u.i.link;
07823 
07824   eh = (struct ppc_link_hash_entry *) h;
07825   for (p = eh->dyn_relocs; p != NULL; p = p->next)
07826     {
07827       asection *s = p->sec->output_section;
07828 
07829       if (s != NULL && (s->flags & SEC_READONLY) != 0)
07830        {
07831          struct bfd_link_info *