Home | History | Annotate | Line # | Download | only in bfd
elf64-ppc.c revision 1.1.1.1.2.1
      1 /* PowerPC64-specific support for 64-bit ELF.
      2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
      3    2009, 2010, 2011, 2012 Free Software Foundation, Inc.
      4    Written by Linus Nordberg, Swox AB <info (at) swox.com>,
      5    based on elf32-ppc.c by Ian Lance Taylor.
      6    Largely rewritten by Alan Modra.
      7 
      8    This file is part of BFD, the Binary File Descriptor library.
      9 
     10    This program is free software; you can redistribute it and/or modify
     11    it under the terms of the GNU General Public License as published by
     12    the Free Software Foundation; either version 3 of the License, or
     13    (at your option) any later version.
     14 
     15    This program is distributed in the hope that it will be useful,
     16    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18    GNU General Public License for more details.
     19 
     20    You should have received a copy of the GNU General Public License along
     21    with this program; if not, write to the Free Software Foundation, Inc.,
     22    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     23 
     24 
     25 /* The 64-bit PowerPC ELF ABI may be found at
     26    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
     27    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
     28 
     29 #include "sysdep.h"
     30 #include <stdarg.h>
     31 #include "bfd.h"
     32 #include "bfdlink.h"
     33 #include "libbfd.h"
     34 #include "elf-bfd.h"
     35 #include "elf/ppc64.h"
     36 #include "elf64-ppc.h"
     37 #include "dwarf2.h"
     38 
     39 static bfd_reloc_status_type ppc64_elf_ha_reloc
     40   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     41 static bfd_reloc_status_type ppc64_elf_branch_reloc
     42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     43 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     45 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     46   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     47 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     48   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     49 static bfd_reloc_status_type ppc64_elf_toc_reloc
     50   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     51 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     52   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     53 static bfd_reloc_status_type ppc64_elf_toc64_reloc
     54   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     55 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     56   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     57 static bfd_vma opd_entry_value
     58   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
     59 
     60 #define TARGET_LITTLE_SYM	bfd_elf64_powerpcle_vec
     61 #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     62 #define TARGET_BIG_SYM		bfd_elf64_powerpc_vec
     63 #define TARGET_BIG_NAME		"elf64-powerpc"
     64 #define ELF_ARCH		bfd_arch_powerpc
     65 #define ELF_TARGET_ID		PPC64_ELF_DATA
     66 #define ELF_MACHINE_CODE	EM_PPC64
     67 #define ELF_MAXPAGESIZE		0x10000
     68 #define ELF_COMMONPAGESIZE	0x1000
     69 #define elf_info_to_howto	ppc64_elf_info_to_howto
     70 
     71 #define elf_backend_want_got_sym 0
     72 #define elf_backend_want_plt_sym 0
     73 #define elf_backend_plt_alignment 3
     74 #define elf_backend_plt_not_loaded 1
     75 #define elf_backend_got_header_size 8
     76 #define elf_backend_can_gc_sections 1
     77 #define elf_backend_can_refcount 1
     78 #define elf_backend_rela_normal 1
     79 #define elf_backend_default_execstack 0
     80 
     81 #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     82 #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     83 #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     84 #define bfd_elf64_bfd_merge_private_bfd_data  _bfd_generic_verify_endian_match
     85 #define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
     86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
     87 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
     88 #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
     89 #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
     90 
     91 #define elf_backend_object_p		      ppc64_elf_object_p
     92 #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
     93 #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
     94 #define elf_backend_write_core_note	      ppc64_elf_write_core_note
     95 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
     96 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
     97 #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
     98 #define elf_backend_check_directives	      ppc64_elf_process_dot_syms
     99 #define elf_backend_as_needed_cleanup	      ppc64_elf_as_needed_cleanup
    100 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    101 #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    102 #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    103 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    104 #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    105 #define elf_backend_gc_sweep_hook	      ppc64_elf_gc_sweep_hook
    106 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    107 #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    108 #define elf_backend_maybe_function_sym	      ppc64_elf_maybe_function_sym
    109 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
    110 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    111 #define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
    112 #define elf_backend_action_discarded	      ppc64_elf_action_discarded
    113 #define elf_backend_relocate_section	      ppc64_elf_relocate_section
    114 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
    115 #define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
    116 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
    117 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
    118 #define elf_backend_special_sections	      ppc64_elf_special_sections
    119 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
    120 
    121 /* The name of the dynamic interpreter.  This is put in the .interp
    122    section.  */
    123 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    124 
    125 /* The size in bytes of an entry in the procedure linkage table.  */
    126 #define PLT_ENTRY_SIZE 24
    127 
    128 /* The initial size of the plt reserved for the dynamic linker.  */
    129 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
    130 
    131 /* TOC base pointers offset from start of TOC.  */
    132 #define TOC_BASE_OFF	0x8000
    133 
    134 /* Offset of tp and dtp pointers from start of TLS block.  */
    135 #define TP_OFFSET	0x7000
    136 #define DTP_OFFSET	0x8000
    137 
    138 /* .plt call stub instructions.  The normal stub is like this, but
    139    sometimes the .plt entry crosses a 64k boundary and we need to
    140    insert an addi to adjust r12.  */
    141 #define PLT_CALL_STUB_SIZE (7*4)
    142 #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha     */
    143 #define STD_R2_40R1	0xf8410028	/* std	 %r2,40(%r1)	     */
    144 #define LD_R11_0R12	0xe96c0000	/* ld	 %r11,xxx+0@l(%r12)  */
    145 #define MTCTR_R11	0x7d6903a6	/* mtctr %r11		     */
    146 #define LD_R2_0R12	0xe84c0000	/* ld	 %r2,xxx+8@l(%r12)   */
    147 					/* ld	 %r11,xxx+16@l(%r12) */
    148 #define BCTR		0x4e800420	/* bctr			     */
    149 
    150 
    151 #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,off@ha  */
    152 #define ADDI_R12_R12	0x398c0000	/* addi %r12,%r12,off@l  */
    153 #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha  */
    154 #define ADDI_R2_R2	0x38420000	/* addi  %r2,%r2,off@l   */
    155 
    156 #define XOR_R11_R11_R11	0x7d6b5a78	/* xor   %r11,%r11,%r11  */
    157 #define ADD_R12_R12_R11	0x7d8c5a14	/* add   %r12,%r12,%r11  */
    158 #define ADD_R2_R2_R11	0x7c425a14	/* add   %r2,%r2,%r11    */
    159 #define CMPLDI_R2_0	0x28220000	/* cmpldi %r2,0          */
    160 #define BNECTR		0x4ca20420	/* bnectr+               */
    161 #define BNECTR_P4	0x4ce20420	/* bnectr+               */
    162 
    163 #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    164 #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)  */
    165 
    166 #define LD_R2_40R1	0xe8410028	/* ld    %r2,40(%r1)     */
    167 
    168 /* glink call stub instructions.  We enter with the index in R0.  */
    169 #define GLINK_CALL_STUB_SIZE (16*4)
    170 					/* 0:				*/
    171 					/*  .quad plt0-1f		*/
    172 					/* __glink:			*/
    173 #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    174 #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    175 					/* 1:				*/
    176 #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    177 #define LD_R2_M16R11	0xe84bfff0	/*  ld %2,(0b-1b)(%11)		*/
    178 #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    179 #define ADD_R12_R2_R11	0x7d825a14	/*  add %12,%2,%11		*/
    180 					/*  ld %11,0(%12)		*/
    181 					/*  ld %2,8(%12)		*/
    182 					/*  mtctr %11			*/
    183 					/*  ld %11,16(%12)		*/
    184 					/*  bctr			*/
    185 
    186 /* Pad with this.  */
    187 #define NOP		0x60000000
    188 
    189 /* Some other nops.  */
    190 #define CROR_151515	0x4def7b82
    191 #define CROR_313131	0x4ffffb82
    192 
    193 /* .glink entries for the first 32k functions are two instructions.  */
    194 #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    195 #define B_DOT		0x48000000	/* b     .		*/
    196 
    197 /* After that, we need two instructions to load the index, followed by
    198    a branch.  */
    199 #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    200 #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    201 
    202 /* Instructions used by the save and restore reg functions.  */
    203 #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    204 #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    205 #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    206 #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    207 #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    208 #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    209 #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    210 #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    211 #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    212 #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    213 #define BLR		0x4e800020	/* blr			*/
    214 
    215 /* Since .opd is an array of descriptors and each entry will end up
    216    with identical R_PPC64_RELATIVE relocs, there is really no need to
    217    propagate .opd relocs;  The dynamic linker should be taught to
    218    relocate .opd without reloc entries.  */
    219 #ifndef NO_OPD_RELOCS
    220 #define NO_OPD_RELOCS 0
    221 #endif
    222 
    223 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
    225 
    226 /* Relocation HOWTO's.  */
    227 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    228 
    229 static reloc_howto_type ppc64_elf_howto_raw[] = {
    230   /* This reloc does nothing.  */
    231   HOWTO (R_PPC64_NONE,		/* type */
    232 	 0,			/* rightshift */
    233 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    234 	 32,			/* bitsize */
    235 	 FALSE,			/* pc_relative */
    236 	 0,			/* bitpos */
    237 	 complain_overflow_dont, /* complain_on_overflow */
    238 	 bfd_elf_generic_reloc,	/* special_function */
    239 	 "R_PPC64_NONE",	/* name */
    240 	 FALSE,			/* partial_inplace */
    241 	 0,			/* src_mask */
    242 	 0,			/* dst_mask */
    243 	 FALSE),		/* pcrel_offset */
    244 
    245   /* A standard 32 bit relocation.  */
    246   HOWTO (R_PPC64_ADDR32,	/* type */
    247 	 0,			/* rightshift */
    248 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    249 	 32,			/* bitsize */
    250 	 FALSE,			/* pc_relative */
    251 	 0,			/* bitpos */
    252 	 complain_overflow_bitfield, /* complain_on_overflow */
    253 	 bfd_elf_generic_reloc,	/* special_function */
    254 	 "R_PPC64_ADDR32",	/* name */
    255 	 FALSE,			/* partial_inplace */
    256 	 0,			/* src_mask */
    257 	 0xffffffff,		/* dst_mask */
    258 	 FALSE),		/* pcrel_offset */
    259 
    260   /* An absolute 26 bit branch; the lower two bits must be zero.
    261      FIXME: we don't check that, we just clear them.  */
    262   HOWTO (R_PPC64_ADDR24,	/* type */
    263 	 0,			/* rightshift */
    264 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    265 	 26,			/* bitsize */
    266 	 FALSE,			/* pc_relative */
    267 	 0,			/* bitpos */
    268 	 complain_overflow_bitfield, /* complain_on_overflow */
    269 	 bfd_elf_generic_reloc,	/* special_function */
    270 	 "R_PPC64_ADDR24",	/* name */
    271 	 FALSE,			/* partial_inplace */
    272 	 0,			/* src_mask */
    273 	 0x03fffffc,		/* dst_mask */
    274 	 FALSE),		/* pcrel_offset */
    275 
    276   /* A standard 16 bit relocation.  */
    277   HOWTO (R_PPC64_ADDR16,	/* type */
    278 	 0,			/* rightshift */
    279 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    280 	 16,			/* bitsize */
    281 	 FALSE,			/* pc_relative */
    282 	 0,			/* bitpos */
    283 	 complain_overflow_bitfield, /* complain_on_overflow */
    284 	 bfd_elf_generic_reloc,	/* special_function */
    285 	 "R_PPC64_ADDR16",	/* name */
    286 	 FALSE,			/* partial_inplace */
    287 	 0,			/* src_mask */
    288 	 0xffff,		/* dst_mask */
    289 	 FALSE),		/* pcrel_offset */
    290 
    291   /* A 16 bit relocation without overflow.  */
    292   HOWTO (R_PPC64_ADDR16_LO,	/* type */
    293 	 0,			/* rightshift */
    294 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    295 	 16,			/* bitsize */
    296 	 FALSE,			/* pc_relative */
    297 	 0,			/* bitpos */
    298 	 complain_overflow_dont,/* complain_on_overflow */
    299 	 bfd_elf_generic_reloc,	/* special_function */
    300 	 "R_PPC64_ADDR16_LO",	/* name */
    301 	 FALSE,			/* partial_inplace */
    302 	 0,			/* src_mask */
    303 	 0xffff,		/* dst_mask */
    304 	 FALSE),		/* pcrel_offset */
    305 
    306   /* Bits 16-31 of an address.  */
    307   HOWTO (R_PPC64_ADDR16_HI,	/* type */
    308 	 16,			/* rightshift */
    309 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    310 	 16,			/* bitsize */
    311 	 FALSE,			/* pc_relative */
    312 	 0,			/* bitpos */
    313 	 complain_overflow_dont, /* complain_on_overflow */
    314 	 bfd_elf_generic_reloc,	/* special_function */
    315 	 "R_PPC64_ADDR16_HI",	/* name */
    316 	 FALSE,			/* partial_inplace */
    317 	 0,			/* src_mask */
    318 	 0xffff,		/* dst_mask */
    319 	 FALSE),		/* pcrel_offset */
    320 
    321   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    322      bits, treated as a signed number, is negative.  */
    323   HOWTO (R_PPC64_ADDR16_HA,	/* type */
    324 	 16,			/* rightshift */
    325 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    326 	 16,			/* bitsize */
    327 	 FALSE,			/* pc_relative */
    328 	 0,			/* bitpos */
    329 	 complain_overflow_dont, /* complain_on_overflow */
    330 	 ppc64_elf_ha_reloc,	/* special_function */
    331 	 "R_PPC64_ADDR16_HA",	/* name */
    332 	 FALSE,			/* partial_inplace */
    333 	 0,			/* src_mask */
    334 	 0xffff,		/* dst_mask */
    335 	 FALSE),		/* pcrel_offset */
    336 
    337   /* An absolute 16 bit branch; the lower two bits must be zero.
    338      FIXME: we don't check that, we just clear them.  */
    339   HOWTO (R_PPC64_ADDR14,	/* type */
    340 	 0,			/* rightshift */
    341 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    342 	 16,			/* bitsize */
    343 	 FALSE,			/* pc_relative */
    344 	 0,			/* bitpos */
    345 	 complain_overflow_bitfield, /* complain_on_overflow */
    346 	 ppc64_elf_branch_reloc, /* special_function */
    347 	 "R_PPC64_ADDR14",	/* name */
    348 	 FALSE,			/* partial_inplace */
    349 	 0,			/* src_mask */
    350 	 0x0000fffc,		/* dst_mask */
    351 	 FALSE),		/* pcrel_offset */
    352 
    353   /* An absolute 16 bit branch, for which bit 10 should be set to
    354      indicate that the branch is expected to be taken.  The lower two
    355      bits must be zero.  */
    356   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
    357 	 0,			/* rightshift */
    358 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    359 	 16,			/* bitsize */
    360 	 FALSE,			/* pc_relative */
    361 	 0,			/* bitpos */
    362 	 complain_overflow_bitfield, /* complain_on_overflow */
    363 	 ppc64_elf_brtaken_reloc, /* special_function */
    364 	 "R_PPC64_ADDR14_BRTAKEN",/* name */
    365 	 FALSE,			/* partial_inplace */
    366 	 0,			/* src_mask */
    367 	 0x0000fffc,		/* dst_mask */
    368 	 FALSE),		/* pcrel_offset */
    369 
    370   /* An absolute 16 bit branch, for which bit 10 should be set to
    371      indicate that the branch is not expected to be taken.  The lower
    372      two bits must be zero.  */
    373   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
    374 	 0,			/* rightshift */
    375 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    376 	 16,			/* bitsize */
    377 	 FALSE,			/* pc_relative */
    378 	 0,			/* bitpos */
    379 	 complain_overflow_bitfield, /* complain_on_overflow */
    380 	 ppc64_elf_brtaken_reloc, /* special_function */
    381 	 "R_PPC64_ADDR14_BRNTAKEN",/* name */
    382 	 FALSE,			/* partial_inplace */
    383 	 0,			/* src_mask */
    384 	 0x0000fffc,		/* dst_mask */
    385 	 FALSE),		/* pcrel_offset */
    386 
    387   /* A relative 26 bit branch; the lower two bits must be zero.  */
    388   HOWTO (R_PPC64_REL24,		/* type */
    389 	 0,			/* rightshift */
    390 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    391 	 26,			/* bitsize */
    392 	 TRUE,			/* pc_relative */
    393 	 0,			/* bitpos */
    394 	 complain_overflow_signed, /* complain_on_overflow */
    395 	 ppc64_elf_branch_reloc, /* special_function */
    396 	 "R_PPC64_REL24",	/* name */
    397 	 FALSE,			/* partial_inplace */
    398 	 0,			/* src_mask */
    399 	 0x03fffffc,		/* dst_mask */
    400 	 TRUE),			/* pcrel_offset */
    401 
    402   /* A relative 16 bit branch; the lower two bits must be zero.  */
    403   HOWTO (R_PPC64_REL14,		/* type */
    404 	 0,			/* rightshift */
    405 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    406 	 16,			/* bitsize */
    407 	 TRUE,			/* pc_relative */
    408 	 0,			/* bitpos */
    409 	 complain_overflow_signed, /* complain_on_overflow */
    410 	 ppc64_elf_branch_reloc, /* special_function */
    411 	 "R_PPC64_REL14",	/* name */
    412 	 FALSE,			/* partial_inplace */
    413 	 0,			/* src_mask */
    414 	 0x0000fffc,		/* dst_mask */
    415 	 TRUE),			/* pcrel_offset */
    416 
    417   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    418      the branch is expected to be taken.  The lower two bits must be
    419      zero.  */
    420   HOWTO (R_PPC64_REL14_BRTAKEN,	/* type */
    421 	 0,			/* rightshift */
    422 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    423 	 16,			/* bitsize */
    424 	 TRUE,			/* pc_relative */
    425 	 0,			/* bitpos */
    426 	 complain_overflow_signed, /* complain_on_overflow */
    427 	 ppc64_elf_brtaken_reloc, /* special_function */
    428 	 "R_PPC64_REL14_BRTAKEN", /* name */
    429 	 FALSE,			/* partial_inplace */
    430 	 0,			/* src_mask */
    431 	 0x0000fffc,		/* dst_mask */
    432 	 TRUE),			/* pcrel_offset */
    433 
    434   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    435      the branch is not expected to be taken.  The lower two bits must
    436      be zero.  */
    437   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
    438 	 0,			/* rightshift */
    439 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    440 	 16,			/* bitsize */
    441 	 TRUE,			/* pc_relative */
    442 	 0,			/* bitpos */
    443 	 complain_overflow_signed, /* complain_on_overflow */
    444 	 ppc64_elf_brtaken_reloc, /* special_function */
    445 	 "R_PPC64_REL14_BRNTAKEN",/* name */
    446 	 FALSE,			/* partial_inplace */
    447 	 0,			/* src_mask */
    448 	 0x0000fffc,		/* dst_mask */
    449 	 TRUE),			/* pcrel_offset */
    450 
    451   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    452      symbol.  */
    453   HOWTO (R_PPC64_GOT16,		/* type */
    454 	 0,			/* rightshift */
    455 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    456 	 16,			/* bitsize */
    457 	 FALSE,			/* pc_relative */
    458 	 0,			/* bitpos */
    459 	 complain_overflow_signed, /* complain_on_overflow */
    460 	 ppc64_elf_unhandled_reloc, /* special_function */
    461 	 "R_PPC64_GOT16",	/* name */
    462 	 FALSE,			/* partial_inplace */
    463 	 0,			/* src_mask */
    464 	 0xffff,		/* dst_mask */
    465 	 FALSE),		/* pcrel_offset */
    466 
    467   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    468      the symbol.  */
    469   HOWTO (R_PPC64_GOT16_LO,	/* type */
    470 	 0,			/* rightshift */
    471 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    472 	 16,			/* bitsize */
    473 	 FALSE,			/* pc_relative */
    474 	 0,			/* bitpos */
    475 	 complain_overflow_dont, /* complain_on_overflow */
    476 	 ppc64_elf_unhandled_reloc, /* special_function */
    477 	 "R_PPC64_GOT16_LO",	/* name */
    478 	 FALSE,			/* partial_inplace */
    479 	 0,			/* src_mask */
    480 	 0xffff,		/* dst_mask */
    481 	 FALSE),		/* pcrel_offset */
    482 
    483   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    484      the symbol.  */
    485   HOWTO (R_PPC64_GOT16_HI,	/* type */
    486 	 16,			/* rightshift */
    487 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    488 	 16,			/* bitsize */
    489 	 FALSE,			/* pc_relative */
    490 	 0,			/* bitpos */
    491 	 complain_overflow_dont,/* complain_on_overflow */
    492 	 ppc64_elf_unhandled_reloc, /* special_function */
    493 	 "R_PPC64_GOT16_HI",	/* name */
    494 	 FALSE,			/* partial_inplace */
    495 	 0,			/* src_mask */
    496 	 0xffff,		/* dst_mask */
    497 	 FALSE),		/* pcrel_offset */
    498 
    499   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    500      the symbol.  */
    501   HOWTO (R_PPC64_GOT16_HA,	/* type */
    502 	 16,			/* rightshift */
    503 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    504 	 16,			/* bitsize */
    505 	 FALSE,			/* pc_relative */
    506 	 0,			/* bitpos */
    507 	 complain_overflow_dont,/* complain_on_overflow */
    508 	 ppc64_elf_unhandled_reloc, /* special_function */
    509 	 "R_PPC64_GOT16_HA",	/* name */
    510 	 FALSE,			/* partial_inplace */
    511 	 0,			/* src_mask */
    512 	 0xffff,		/* dst_mask */
    513 	 FALSE),		/* pcrel_offset */
    514 
    515   /* This is used only by the dynamic linker.  The symbol should exist
    516      both in the object being run and in some shared library.  The
    517      dynamic linker copies the data addressed by the symbol from the
    518      shared library into the object, because the object being
    519      run has to have the data at some particular address.  */
    520   HOWTO (R_PPC64_COPY,		/* type */
    521 	 0,			/* rightshift */
    522 	 0,			/* this one is variable size */
    523 	 0,			/* bitsize */
    524 	 FALSE,			/* pc_relative */
    525 	 0,			/* bitpos */
    526 	 complain_overflow_dont, /* complain_on_overflow */
    527 	 ppc64_elf_unhandled_reloc, /* special_function */
    528 	 "R_PPC64_COPY",	/* name */
    529 	 FALSE,			/* partial_inplace */
    530 	 0,			/* src_mask */
    531 	 0,			/* dst_mask */
    532 	 FALSE),		/* pcrel_offset */
    533 
    534   /* Like R_PPC64_ADDR64, but used when setting global offset table
    535      entries.  */
    536   HOWTO (R_PPC64_GLOB_DAT,	/* type */
    537 	 0,			/* rightshift */
    538 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    539 	 64,			/* bitsize */
    540 	 FALSE,			/* pc_relative */
    541 	 0,			/* bitpos */
    542 	 complain_overflow_dont, /* complain_on_overflow */
    543 	 ppc64_elf_unhandled_reloc,  /* special_function */
    544 	 "R_PPC64_GLOB_DAT",	/* name */
    545 	 FALSE,			/* partial_inplace */
    546 	 0,			/* src_mask */
    547 	 ONES (64),		/* dst_mask */
    548 	 FALSE),		/* pcrel_offset */
    549 
    550   /* Created by the link editor.  Marks a procedure linkage table
    551      entry for a symbol.  */
    552   HOWTO (R_PPC64_JMP_SLOT,	/* type */
    553 	 0,			/* rightshift */
    554 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    555 	 0,			/* bitsize */
    556 	 FALSE,			/* pc_relative */
    557 	 0,			/* bitpos */
    558 	 complain_overflow_dont, /* complain_on_overflow */
    559 	 ppc64_elf_unhandled_reloc, /* special_function */
    560 	 "R_PPC64_JMP_SLOT",	/* name */
    561 	 FALSE,			/* partial_inplace */
    562 	 0,			/* src_mask */
    563 	 0,			/* dst_mask */
    564 	 FALSE),		/* pcrel_offset */
    565 
    566   /* Used only by the dynamic linker.  When the object is run, this
    567      doubleword64 is set to the load address of the object, plus the
    568      addend.  */
    569   HOWTO (R_PPC64_RELATIVE,	/* type */
    570 	 0,			/* rightshift */
    571 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    572 	 64,			/* bitsize */
    573 	 FALSE,			/* pc_relative */
    574 	 0,			/* bitpos */
    575 	 complain_overflow_dont, /* complain_on_overflow */
    576 	 bfd_elf_generic_reloc,	/* special_function */
    577 	 "R_PPC64_RELATIVE",	/* name */
    578 	 FALSE,			/* partial_inplace */
    579 	 0,			/* src_mask */
    580 	 ONES (64),		/* dst_mask */
    581 	 FALSE),		/* pcrel_offset */
    582 
    583   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    584   HOWTO (R_PPC64_UADDR32,	/* type */
    585 	 0,			/* rightshift */
    586 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    587 	 32,			/* bitsize */
    588 	 FALSE,			/* pc_relative */
    589 	 0,			/* bitpos */
    590 	 complain_overflow_bitfield, /* complain_on_overflow */
    591 	 bfd_elf_generic_reloc,	/* special_function */
    592 	 "R_PPC64_UADDR32",	/* name */
    593 	 FALSE,			/* partial_inplace */
    594 	 0,			/* src_mask */
    595 	 0xffffffff,		/* dst_mask */
    596 	 FALSE),		/* pcrel_offset */
    597 
    598   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    599   HOWTO (R_PPC64_UADDR16,	/* type */
    600 	 0,			/* rightshift */
    601 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    602 	 16,			/* bitsize */
    603 	 FALSE,			/* pc_relative */
    604 	 0,			/* bitpos */
    605 	 complain_overflow_bitfield, /* complain_on_overflow */
    606 	 bfd_elf_generic_reloc,	/* special_function */
    607 	 "R_PPC64_UADDR16",	/* name */
    608 	 FALSE,			/* partial_inplace */
    609 	 0,			/* src_mask */
    610 	 0xffff,		/* dst_mask */
    611 	 FALSE),		/* pcrel_offset */
    612 
    613   /* 32-bit PC relative.  */
    614   HOWTO (R_PPC64_REL32,		/* type */
    615 	 0,			/* rightshift */
    616 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    617 	 32,			/* bitsize */
    618 	 TRUE,			/* pc_relative */
    619 	 0,			/* bitpos */
    620 	 /* FIXME: Verify.  Was complain_overflow_bitfield.  */
    621 	 complain_overflow_signed, /* complain_on_overflow */
    622 	 bfd_elf_generic_reloc,	/* special_function */
    623 	 "R_PPC64_REL32",	/* name */
    624 	 FALSE,			/* partial_inplace */
    625 	 0,			/* src_mask */
    626 	 0xffffffff,		/* dst_mask */
    627 	 TRUE),			/* pcrel_offset */
    628 
    629   /* 32-bit relocation to the symbol's procedure linkage table.  */
    630   HOWTO (R_PPC64_PLT32,		/* type */
    631 	 0,			/* rightshift */
    632 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    633 	 32,			/* bitsize */
    634 	 FALSE,			/* pc_relative */
    635 	 0,			/* bitpos */
    636 	 complain_overflow_bitfield, /* complain_on_overflow */
    637 	 ppc64_elf_unhandled_reloc, /* special_function */
    638 	 "R_PPC64_PLT32",	/* name */
    639 	 FALSE,			/* partial_inplace */
    640 	 0,			/* src_mask */
    641 	 0xffffffff,		/* dst_mask */
    642 	 FALSE),		/* pcrel_offset */
    643 
    644   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    645      FIXME: R_PPC64_PLTREL32 not supported.  */
    646   HOWTO (R_PPC64_PLTREL32,	/* type */
    647 	 0,			/* rightshift */
    648 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    649 	 32,			/* bitsize */
    650 	 TRUE,			/* pc_relative */
    651 	 0,			/* bitpos */
    652 	 complain_overflow_signed, /* complain_on_overflow */
    653 	 bfd_elf_generic_reloc,	/* special_function */
    654 	 "R_PPC64_PLTREL32",	/* name */
    655 	 FALSE,			/* partial_inplace */
    656 	 0,			/* src_mask */
    657 	 0xffffffff,		/* dst_mask */
    658 	 TRUE),			/* pcrel_offset */
    659 
    660   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    661      the symbol.  */
    662   HOWTO (R_PPC64_PLT16_LO,	/* type */
    663 	 0,			/* rightshift */
    664 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    665 	 16,			/* bitsize */
    666 	 FALSE,			/* pc_relative */
    667 	 0,			/* bitpos */
    668 	 complain_overflow_dont, /* complain_on_overflow */
    669 	 ppc64_elf_unhandled_reloc, /* special_function */
    670 	 "R_PPC64_PLT16_LO",	/* name */
    671 	 FALSE,			/* partial_inplace */
    672 	 0,			/* src_mask */
    673 	 0xffff,		/* dst_mask */
    674 	 FALSE),		/* pcrel_offset */
    675 
    676   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    677      the symbol.  */
    678   HOWTO (R_PPC64_PLT16_HI,	/* type */
    679 	 16,			/* rightshift */
    680 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    681 	 16,			/* bitsize */
    682 	 FALSE,			/* pc_relative */
    683 	 0,			/* bitpos */
    684 	 complain_overflow_dont, /* complain_on_overflow */
    685 	 ppc64_elf_unhandled_reloc, /* special_function */
    686 	 "R_PPC64_PLT16_HI",	/* name */
    687 	 FALSE,			/* partial_inplace */
    688 	 0,			/* src_mask */
    689 	 0xffff,		/* dst_mask */
    690 	 FALSE),		/* pcrel_offset */
    691 
    692   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    693      the symbol.  */
    694   HOWTO (R_PPC64_PLT16_HA,	/* type */
    695 	 16,			/* rightshift */
    696 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    697 	 16,			/* bitsize */
    698 	 FALSE,			/* pc_relative */
    699 	 0,			/* bitpos */
    700 	 complain_overflow_dont, /* complain_on_overflow */
    701 	 ppc64_elf_unhandled_reloc, /* special_function */
    702 	 "R_PPC64_PLT16_HA",	/* name */
    703 	 FALSE,			/* partial_inplace */
    704 	 0,			/* src_mask */
    705 	 0xffff,		/* dst_mask */
    706 	 FALSE),		/* pcrel_offset */
    707 
    708   /* 16-bit section relative relocation.  */
    709   HOWTO (R_PPC64_SECTOFF,	/* type */
    710 	 0,			/* rightshift */
    711 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    712 	 16,			/* bitsize */
    713 	 FALSE,			/* pc_relative */
    714 	 0,			/* bitpos */
    715 	 complain_overflow_bitfield, /* complain_on_overflow */
    716 	 ppc64_elf_sectoff_reloc, /* special_function */
    717 	 "R_PPC64_SECTOFF",	/* name */
    718 	 FALSE,			/* partial_inplace */
    719 	 0,			/* src_mask */
    720 	 0xffff,		/* dst_mask */
    721 	 FALSE),		/* pcrel_offset */
    722 
    723   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    724   HOWTO (R_PPC64_SECTOFF_LO,	/* type */
    725 	 0,			/* rightshift */
    726 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    727 	 16,			/* bitsize */
    728 	 FALSE,			/* pc_relative */
    729 	 0,			/* bitpos */
    730 	 complain_overflow_dont, /* complain_on_overflow */
    731 	 ppc64_elf_sectoff_reloc, /* special_function */
    732 	 "R_PPC64_SECTOFF_LO",	/* name */
    733 	 FALSE,			/* partial_inplace */
    734 	 0,			/* src_mask */
    735 	 0xffff,		/* dst_mask */
    736 	 FALSE),		/* pcrel_offset */
    737 
    738   /* 16-bit upper half section relative relocation.  */
    739   HOWTO (R_PPC64_SECTOFF_HI,	/* type */
    740 	 16,			/* rightshift */
    741 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    742 	 16,			/* bitsize */
    743 	 FALSE,			/* pc_relative */
    744 	 0,			/* bitpos */
    745 	 complain_overflow_dont, /* complain_on_overflow */
    746 	 ppc64_elf_sectoff_reloc, /* special_function */
    747 	 "R_PPC64_SECTOFF_HI",	/* name */
    748 	 FALSE,			/* partial_inplace */
    749 	 0,			/* src_mask */
    750 	 0xffff,		/* dst_mask */
    751 	 FALSE),		/* pcrel_offset */
    752 
    753   /* 16-bit upper half adjusted section relative relocation.  */
    754   HOWTO (R_PPC64_SECTOFF_HA,	/* type */
    755 	 16,			/* rightshift */
    756 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    757 	 16,			/* bitsize */
    758 	 FALSE,			/* pc_relative */
    759 	 0,			/* bitpos */
    760 	 complain_overflow_dont, /* complain_on_overflow */
    761 	 ppc64_elf_sectoff_ha_reloc, /* special_function */
    762 	 "R_PPC64_SECTOFF_HA",	/* name */
    763 	 FALSE,			/* partial_inplace */
    764 	 0,			/* src_mask */
    765 	 0xffff,		/* dst_mask */
    766 	 FALSE),		/* pcrel_offset */
    767 
    768   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    769   HOWTO (R_PPC64_REL30,		/* type */
    770 	 2,			/* rightshift */
    771 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    772 	 30,			/* bitsize */
    773 	 TRUE,			/* pc_relative */
    774 	 0,			/* bitpos */
    775 	 complain_overflow_dont, /* complain_on_overflow */
    776 	 bfd_elf_generic_reloc, /* special_function */
    777 	 "R_PPC64_REL30",	/* name */
    778 	 FALSE,			/* partial_inplace */
    779 	 0,			/* src_mask */
    780 	 0xfffffffc,		/* dst_mask */
    781 	 TRUE),			/* pcrel_offset */
    782 
    783   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    784 
    785   /* A standard 64-bit relocation.  */
    786   HOWTO (R_PPC64_ADDR64,	/* type */
    787 	 0,			/* rightshift */
    788 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    789 	 64,			/* bitsize */
    790 	 FALSE,			/* pc_relative */
    791 	 0,			/* bitpos */
    792 	 complain_overflow_dont, /* complain_on_overflow */
    793 	 bfd_elf_generic_reloc,	/* special_function */
    794 	 "R_PPC64_ADDR64",	/* name */
    795 	 FALSE,			/* partial_inplace */
    796 	 0,			/* src_mask */
    797 	 ONES (64),		/* dst_mask */
    798 	 FALSE),		/* pcrel_offset */
    799 
    800   /* The bits 32-47 of an address.  */
    801   HOWTO (R_PPC64_ADDR16_HIGHER,	/* type */
    802 	 32,			/* rightshift */
    803 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    804 	 16,			/* bitsize */
    805 	 FALSE,			/* pc_relative */
    806 	 0,			/* bitpos */
    807 	 complain_overflow_dont, /* complain_on_overflow */
    808 	 bfd_elf_generic_reloc,	/* special_function */
    809 	 "R_PPC64_ADDR16_HIGHER", /* name */
    810 	 FALSE,			/* partial_inplace */
    811 	 0,			/* src_mask */
    812 	 0xffff,		/* dst_mask */
    813 	 FALSE),		/* pcrel_offset */
    814 
    815   /* The bits 32-47 of an address, plus 1 if the contents of the low
    816      16 bits, treated as a signed number, is negative.  */
    817   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
    818 	 32,			/* rightshift */
    819 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    820 	 16,			/* bitsize */
    821 	 FALSE,			/* pc_relative */
    822 	 0,			/* bitpos */
    823 	 complain_overflow_dont, /* complain_on_overflow */
    824 	 ppc64_elf_ha_reloc,	/* special_function */
    825 	 "R_PPC64_ADDR16_HIGHERA", /* name */
    826 	 FALSE,			/* partial_inplace */
    827 	 0,			/* src_mask */
    828 	 0xffff,		/* dst_mask */
    829 	 FALSE),		/* pcrel_offset */
    830 
    831   /* The bits 48-63 of an address.  */
    832   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
    833 	 48,			/* rightshift */
    834 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    835 	 16,			/* bitsize */
    836 	 FALSE,			/* pc_relative */
    837 	 0,			/* bitpos */
    838 	 complain_overflow_dont, /* complain_on_overflow */
    839 	 bfd_elf_generic_reloc,	/* special_function */
    840 	 "R_PPC64_ADDR16_HIGHEST", /* name */
    841 	 FALSE,			/* partial_inplace */
    842 	 0,			/* src_mask */
    843 	 0xffff,		/* dst_mask */
    844 	 FALSE),		/* pcrel_offset */
    845 
    846   /* The bits 48-63 of an address, plus 1 if the contents of the low
    847      16 bits, treated as a signed number, is negative.  */
    848   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
    849 	 48,			/* rightshift */
    850 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    851 	 16,			/* bitsize */
    852 	 FALSE,			/* pc_relative */
    853 	 0,			/* bitpos */
    854 	 complain_overflow_dont, /* complain_on_overflow */
    855 	 ppc64_elf_ha_reloc,	/* special_function */
    856 	 "R_PPC64_ADDR16_HIGHESTA", /* name */
    857 	 FALSE,			/* partial_inplace */
    858 	 0,			/* src_mask */
    859 	 0xffff,		/* dst_mask */
    860 	 FALSE),		/* pcrel_offset */
    861 
    862   /* Like ADDR64, but may be unaligned.  */
    863   HOWTO (R_PPC64_UADDR64,	/* type */
    864 	 0,			/* rightshift */
    865 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    866 	 64,			/* bitsize */
    867 	 FALSE,			/* pc_relative */
    868 	 0,			/* bitpos */
    869 	 complain_overflow_dont, /* complain_on_overflow */
    870 	 bfd_elf_generic_reloc,	/* special_function */
    871 	 "R_PPC64_UADDR64",	/* name */
    872 	 FALSE,			/* partial_inplace */
    873 	 0,			/* src_mask */
    874 	 ONES (64),		/* dst_mask */
    875 	 FALSE),		/* pcrel_offset */
    876 
    877   /* 64-bit relative relocation.  */
    878   HOWTO (R_PPC64_REL64,		/* type */
    879 	 0,			/* rightshift */
    880 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    881 	 64,			/* bitsize */
    882 	 TRUE,			/* pc_relative */
    883 	 0,			/* bitpos */
    884 	 complain_overflow_dont, /* complain_on_overflow */
    885 	 bfd_elf_generic_reloc,	/* special_function */
    886 	 "R_PPC64_REL64",	/* name */
    887 	 FALSE,			/* partial_inplace */
    888 	 0,			/* src_mask */
    889 	 ONES (64),		/* dst_mask */
    890 	 TRUE),			/* pcrel_offset */
    891 
    892   /* 64-bit relocation to the symbol's procedure linkage table.  */
    893   HOWTO (R_PPC64_PLT64,		/* type */
    894 	 0,			/* rightshift */
    895 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    896 	 64,			/* bitsize */
    897 	 FALSE,			/* pc_relative */
    898 	 0,			/* bitpos */
    899 	 complain_overflow_dont, /* complain_on_overflow */
    900 	 ppc64_elf_unhandled_reloc, /* special_function */
    901 	 "R_PPC64_PLT64",	/* name */
    902 	 FALSE,			/* partial_inplace */
    903 	 0,			/* src_mask */
    904 	 ONES (64),		/* dst_mask */
    905 	 FALSE),		/* pcrel_offset */
    906 
    907   /* 64-bit PC relative relocation to the symbol's procedure linkage
    908      table.  */
    909   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    910   HOWTO (R_PPC64_PLTREL64,	/* type */
    911 	 0,			/* rightshift */
    912 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    913 	 64,			/* bitsize */
    914 	 TRUE,			/* pc_relative */
    915 	 0,			/* bitpos */
    916 	 complain_overflow_dont, /* complain_on_overflow */
    917 	 ppc64_elf_unhandled_reloc, /* special_function */
    918 	 "R_PPC64_PLTREL64",	/* name */
    919 	 FALSE,			/* partial_inplace */
    920 	 0,			/* src_mask */
    921 	 ONES (64),		/* dst_mask */
    922 	 TRUE),			/* pcrel_offset */
    923 
    924   /* 16 bit TOC-relative relocation.  */
    925 
    926   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    927   HOWTO (R_PPC64_TOC16,		/* type */
    928 	 0,			/* rightshift */
    929 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    930 	 16,			/* bitsize */
    931 	 FALSE,			/* pc_relative */
    932 	 0,			/* bitpos */
    933 	 complain_overflow_signed, /* complain_on_overflow */
    934 	 ppc64_elf_toc_reloc,	/* special_function */
    935 	 "R_PPC64_TOC16",	/* name */
    936 	 FALSE,			/* partial_inplace */
    937 	 0,			/* src_mask */
    938 	 0xffff,		/* dst_mask */
    939 	 FALSE),		/* pcrel_offset */
    940 
    941   /* 16 bit TOC-relative relocation without overflow.  */
    942 
    943   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    944   HOWTO (R_PPC64_TOC16_LO,	/* type */
    945 	 0,			/* rightshift */
    946 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    947 	 16,			/* bitsize */
    948 	 FALSE,			/* pc_relative */
    949 	 0,			/* bitpos */
    950 	 complain_overflow_dont, /* complain_on_overflow */
    951 	 ppc64_elf_toc_reloc,	/* special_function */
    952 	 "R_PPC64_TOC16_LO",	/* name */
    953 	 FALSE,			/* partial_inplace */
    954 	 0,			/* src_mask */
    955 	 0xffff,		/* dst_mask */
    956 	 FALSE),		/* pcrel_offset */
    957 
    958   /* 16 bit TOC-relative relocation, high 16 bits.  */
    959 
    960   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    961   HOWTO (R_PPC64_TOC16_HI,	/* type */
    962 	 16,			/* rightshift */
    963 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    964 	 16,			/* bitsize */
    965 	 FALSE,			/* pc_relative */
    966 	 0,			/* bitpos */
    967 	 complain_overflow_dont, /* complain_on_overflow */
    968 	 ppc64_elf_toc_reloc,	/* special_function */
    969 	 "R_PPC64_TOC16_HI",	/* name */
    970 	 FALSE,			/* partial_inplace */
    971 	 0,			/* src_mask */
    972 	 0xffff,		/* dst_mask */
    973 	 FALSE),		/* pcrel_offset */
    974 
    975   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
    976      contents of the low 16 bits, treated as a signed number, is
    977      negative.  */
    978 
    979   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
    980   HOWTO (R_PPC64_TOC16_HA,	/* type */
    981 	 16,			/* rightshift */
    982 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    983 	 16,			/* bitsize */
    984 	 FALSE,			/* pc_relative */
    985 	 0,			/* bitpos */
    986 	 complain_overflow_dont, /* complain_on_overflow */
    987 	 ppc64_elf_toc_ha_reloc, /* special_function */
    988 	 "R_PPC64_TOC16_HA",	/* name */
    989 	 FALSE,			/* partial_inplace */
    990 	 0,			/* src_mask */
    991 	 0xffff,		/* dst_mask */
    992 	 FALSE),		/* pcrel_offset */
    993 
    994   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
    995 
    996   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
    997   HOWTO (R_PPC64_TOC,		/* type */
    998 	 0,			/* rightshift */
    999 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1000 	 64,			/* bitsize */
   1001 	 FALSE,			/* pc_relative */
   1002 	 0,			/* bitpos */
   1003 	 complain_overflow_bitfield, /* complain_on_overflow */
   1004 	 ppc64_elf_toc64_reloc,	/* special_function */
   1005 	 "R_PPC64_TOC",		/* name */
   1006 	 FALSE,			/* partial_inplace */
   1007 	 0,			/* src_mask */
   1008 	 ONES (64),		/* dst_mask */
   1009 	 FALSE),		/* pcrel_offset */
   1010 
   1011   /* Like R_PPC64_GOT16, but also informs the link editor that the
   1012      value to relocate may (!) refer to a PLT entry which the link
   1013      editor (a) may replace with the symbol value.  If the link editor
   1014      is unable to fully resolve the symbol, it may (b) create a PLT
   1015      entry and store the address to the new PLT entry in the GOT.
   1016      This permits lazy resolution of function symbols at run time.
   1017      The link editor may also skip all of this and just (c) emit a
   1018      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
   1019   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
   1020     HOWTO (R_PPC64_PLTGOT16,	/* type */
   1021 	 0,			/* rightshift */
   1022 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1023 	 16,			/* bitsize */
   1024 	 FALSE,			/* pc_relative */
   1025 	 0,			/* bitpos */
   1026 	 complain_overflow_signed, /* complain_on_overflow */
   1027 	 ppc64_elf_unhandled_reloc, /* special_function */
   1028 	 "R_PPC64_PLTGOT16",	/* name */
   1029 	 FALSE,			/* partial_inplace */
   1030 	 0,			/* src_mask */
   1031 	 0xffff,		/* dst_mask */
   1032 	 FALSE),		/* pcrel_offset */
   1033 
   1034   /* Like R_PPC64_PLTGOT16, but without overflow.  */
   1035   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1036   HOWTO (R_PPC64_PLTGOT16_LO,	/* type */
   1037 	 0,			/* rightshift */
   1038 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1039 	 16,			/* bitsize */
   1040 	 FALSE,			/* pc_relative */
   1041 	 0,			/* bitpos */
   1042 	 complain_overflow_dont, /* complain_on_overflow */
   1043 	 ppc64_elf_unhandled_reloc, /* special_function */
   1044 	 "R_PPC64_PLTGOT16_LO",	/* name */
   1045 	 FALSE,			/* partial_inplace */
   1046 	 0,			/* src_mask */
   1047 	 0xffff,		/* dst_mask */
   1048 	 FALSE),		/* pcrel_offset */
   1049 
   1050   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
   1051   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
   1052   HOWTO (R_PPC64_PLTGOT16_HI,	/* type */
   1053 	 16,			/* rightshift */
   1054 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1055 	 16,			/* bitsize */
   1056 	 FALSE,			/* pc_relative */
   1057 	 0,			/* bitpos */
   1058 	 complain_overflow_dont, /* complain_on_overflow */
   1059 	 ppc64_elf_unhandled_reloc, /* special_function */
   1060 	 "R_PPC64_PLTGOT16_HI",	/* name */
   1061 	 FALSE,			/* partial_inplace */
   1062 	 0,			/* src_mask */
   1063 	 0xffff,		/* dst_mask */
   1064 	 FALSE),		/* pcrel_offset */
   1065 
   1066   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
   1067      1 if the contents of the low 16 bits, treated as a signed number,
   1068      is negative.  */
   1069   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
   1070   HOWTO (R_PPC64_PLTGOT16_HA,	/* type */
   1071 	 16,			/* rightshift */
   1072 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1073 	 16,			/* bitsize */
   1074 	 FALSE,			/* pc_relative */
   1075 	 0,			/* bitpos */
   1076 	 complain_overflow_dont,/* complain_on_overflow */
   1077 	 ppc64_elf_unhandled_reloc, /* special_function */
   1078 	 "R_PPC64_PLTGOT16_HA",	/* name */
   1079 	 FALSE,			/* partial_inplace */
   1080 	 0,			/* src_mask */
   1081 	 0xffff,		/* dst_mask */
   1082 	 FALSE),		/* pcrel_offset */
   1083 
   1084   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
   1085   HOWTO (R_PPC64_ADDR16_DS,	/* type */
   1086 	 0,			/* rightshift */
   1087 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1088 	 16,			/* bitsize */
   1089 	 FALSE,			/* pc_relative */
   1090 	 0,			/* bitpos */
   1091 	 complain_overflow_bitfield, /* complain_on_overflow */
   1092 	 bfd_elf_generic_reloc,	/* special_function */
   1093 	 "R_PPC64_ADDR16_DS",	/* name */
   1094 	 FALSE,			/* partial_inplace */
   1095 	 0,			/* src_mask */
   1096 	 0xfffc,		/* dst_mask */
   1097 	 FALSE),		/* pcrel_offset */
   1098 
   1099   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
   1100   HOWTO (R_PPC64_ADDR16_LO_DS,	/* type */
   1101 	 0,			/* rightshift */
   1102 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1103 	 16,			/* bitsize */
   1104 	 FALSE,			/* pc_relative */
   1105 	 0,			/* bitpos */
   1106 	 complain_overflow_dont,/* complain_on_overflow */
   1107 	 bfd_elf_generic_reloc,	/* special_function */
   1108 	 "R_PPC64_ADDR16_LO_DS",/* name */
   1109 	 FALSE,			/* partial_inplace */
   1110 	 0,			/* src_mask */
   1111 	 0xfffc,		/* dst_mask */
   1112 	 FALSE),		/* pcrel_offset */
   1113 
   1114   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
   1115   HOWTO (R_PPC64_GOT16_DS,	/* type */
   1116 	 0,			/* rightshift */
   1117 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1118 	 16,			/* bitsize */
   1119 	 FALSE,			/* pc_relative */
   1120 	 0,			/* bitpos */
   1121 	 complain_overflow_signed, /* complain_on_overflow */
   1122 	 ppc64_elf_unhandled_reloc, /* special_function */
   1123 	 "R_PPC64_GOT16_DS",	/* name */
   1124 	 FALSE,			/* partial_inplace */
   1125 	 0,			/* src_mask */
   1126 	 0xfffc,		/* dst_mask */
   1127 	 FALSE),		/* pcrel_offset */
   1128 
   1129   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
   1130   HOWTO (R_PPC64_GOT16_LO_DS,	/* type */
   1131 	 0,			/* rightshift */
   1132 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1133 	 16,			/* bitsize */
   1134 	 FALSE,			/* pc_relative */
   1135 	 0,			/* bitpos */
   1136 	 complain_overflow_dont, /* complain_on_overflow */
   1137 	 ppc64_elf_unhandled_reloc, /* special_function */
   1138 	 "R_PPC64_GOT16_LO_DS",	/* name */
   1139 	 FALSE,			/* partial_inplace */
   1140 	 0,			/* src_mask */
   1141 	 0xfffc,		/* dst_mask */
   1142 	 FALSE),		/* pcrel_offset */
   1143 
   1144   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
   1145   HOWTO (R_PPC64_PLT16_LO_DS,	/* type */
   1146 	 0,			/* rightshift */
   1147 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1148 	 16,			/* bitsize */
   1149 	 FALSE,			/* pc_relative */
   1150 	 0,			/* bitpos */
   1151 	 complain_overflow_dont, /* complain_on_overflow */
   1152 	 ppc64_elf_unhandled_reloc, /* special_function */
   1153 	 "R_PPC64_PLT16_LO_DS",	/* name */
   1154 	 FALSE,			/* partial_inplace */
   1155 	 0,			/* src_mask */
   1156 	 0xfffc,		/* dst_mask */
   1157 	 FALSE),		/* pcrel_offset */
   1158 
   1159   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
   1160   HOWTO (R_PPC64_SECTOFF_DS,	/* type */
   1161 	 0,			/* rightshift */
   1162 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1163 	 16,			/* bitsize */
   1164 	 FALSE,			/* pc_relative */
   1165 	 0,			/* bitpos */
   1166 	 complain_overflow_bitfield, /* complain_on_overflow */
   1167 	 ppc64_elf_sectoff_reloc, /* special_function */
   1168 	 "R_PPC64_SECTOFF_DS",	/* name */
   1169 	 FALSE,			/* partial_inplace */
   1170 	 0,			/* src_mask */
   1171 	 0xfffc,		/* dst_mask */
   1172 	 FALSE),		/* pcrel_offset */
   1173 
   1174   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
   1175   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
   1176 	 0,			/* rightshift */
   1177 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1178 	 16,			/* bitsize */
   1179 	 FALSE,			/* pc_relative */
   1180 	 0,			/* bitpos */
   1181 	 complain_overflow_dont, /* complain_on_overflow */
   1182 	 ppc64_elf_sectoff_reloc, /* special_function */
   1183 	 "R_PPC64_SECTOFF_LO_DS",/* name */
   1184 	 FALSE,			/* partial_inplace */
   1185 	 0,			/* src_mask */
   1186 	 0xfffc,		/* dst_mask */
   1187 	 FALSE),		/* pcrel_offset */
   1188 
   1189   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
   1190   HOWTO (R_PPC64_TOC16_DS,	/* type */
   1191 	 0,			/* rightshift */
   1192 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1193 	 16,			/* bitsize */
   1194 	 FALSE,			/* pc_relative */
   1195 	 0,			/* bitpos */
   1196 	 complain_overflow_signed, /* complain_on_overflow */
   1197 	 ppc64_elf_toc_reloc,	/* special_function */
   1198 	 "R_PPC64_TOC16_DS",	/* name */
   1199 	 FALSE,			/* partial_inplace */
   1200 	 0,			/* src_mask */
   1201 	 0xfffc,		/* dst_mask */
   1202 	 FALSE),		/* pcrel_offset */
   1203 
   1204   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
   1205   HOWTO (R_PPC64_TOC16_LO_DS,	/* type */
   1206 	 0,			/* rightshift */
   1207 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1208 	 16,			/* bitsize */
   1209 	 FALSE,			/* pc_relative */
   1210 	 0,			/* bitpos */
   1211 	 complain_overflow_dont, /* complain_on_overflow */
   1212 	 ppc64_elf_toc_reloc,	/* special_function */
   1213 	 "R_PPC64_TOC16_LO_DS",	/* name */
   1214 	 FALSE,			/* partial_inplace */
   1215 	 0,			/* src_mask */
   1216 	 0xfffc,		/* dst_mask */
   1217 	 FALSE),		/* pcrel_offset */
   1218 
   1219   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
   1220   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
   1221   HOWTO (R_PPC64_PLTGOT16_DS,	/* type */
   1222 	 0,			/* rightshift */
   1223 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1224 	 16,			/* bitsize */
   1225 	 FALSE,			/* pc_relative */
   1226 	 0,			/* bitpos */
   1227 	 complain_overflow_signed, /* complain_on_overflow */
   1228 	 ppc64_elf_unhandled_reloc, /* special_function */
   1229 	 "R_PPC64_PLTGOT16_DS",	/* name */
   1230 	 FALSE,			/* partial_inplace */
   1231 	 0,			/* src_mask */
   1232 	 0xfffc,		/* dst_mask */
   1233 	 FALSE),		/* pcrel_offset */
   1234 
   1235   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
   1236   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1237   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
   1238 	 0,			/* rightshift */
   1239 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1240 	 16,			/* bitsize */
   1241 	 FALSE,			/* pc_relative */
   1242 	 0,			/* bitpos */
   1243 	 complain_overflow_dont, /* complain_on_overflow */
   1244 	 ppc64_elf_unhandled_reloc, /* special_function */
   1245 	 "R_PPC64_PLTGOT16_LO_DS",/* name */
   1246 	 FALSE,			/* partial_inplace */
   1247 	 0,			/* src_mask */
   1248 	 0xfffc,		/* dst_mask */
   1249 	 FALSE),		/* pcrel_offset */
   1250 
   1251   /* Marker relocs for TLS.  */
   1252   HOWTO (R_PPC64_TLS,
   1253 	 0,			/* rightshift */
   1254 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1255 	 32,			/* bitsize */
   1256 	 FALSE,			/* pc_relative */
   1257 	 0,			/* bitpos */
   1258 	 complain_overflow_dont, /* complain_on_overflow */
   1259 	 bfd_elf_generic_reloc,	/* special_function */
   1260 	 "R_PPC64_TLS",		/* name */
   1261 	 FALSE,			/* partial_inplace */
   1262 	 0,			/* src_mask */
   1263 	 0,			/* dst_mask */
   1264 	 FALSE),		/* pcrel_offset */
   1265 
   1266   HOWTO (R_PPC64_TLSGD,
   1267 	 0,			/* rightshift */
   1268 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1269 	 32,			/* bitsize */
   1270 	 FALSE,			/* pc_relative */
   1271 	 0,			/* bitpos */
   1272 	 complain_overflow_dont, /* complain_on_overflow */
   1273 	 bfd_elf_generic_reloc,	/* special_function */
   1274 	 "R_PPC64_TLSGD",	/* name */
   1275 	 FALSE,			/* partial_inplace */
   1276 	 0,			/* src_mask */
   1277 	 0,			/* dst_mask */
   1278 	 FALSE),		/* pcrel_offset */
   1279 
   1280   HOWTO (R_PPC64_TLSLD,
   1281 	 0,			/* rightshift */
   1282 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1283 	 32,			/* bitsize */
   1284 	 FALSE,			/* pc_relative */
   1285 	 0,			/* bitpos */
   1286 	 complain_overflow_dont, /* complain_on_overflow */
   1287 	 bfd_elf_generic_reloc,	/* special_function */
   1288 	 "R_PPC64_TLSLD",	/* name */
   1289 	 FALSE,			/* partial_inplace */
   1290 	 0,			/* src_mask */
   1291 	 0,			/* dst_mask */
   1292 	 FALSE),		/* pcrel_offset */
   1293 
   1294   HOWTO (R_PPC64_TOCSAVE,
   1295 	 0,			/* rightshift */
   1296 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1297 	 32,			/* bitsize */
   1298 	 FALSE,			/* pc_relative */
   1299 	 0,			/* bitpos */
   1300 	 complain_overflow_dont, /* complain_on_overflow */
   1301 	 bfd_elf_generic_reloc,	/* special_function */
   1302 	 "R_PPC64_TOCSAVE",	/* name */
   1303 	 FALSE,			/* partial_inplace */
   1304 	 0,			/* src_mask */
   1305 	 0,			/* dst_mask */
   1306 	 FALSE),		/* pcrel_offset */
   1307 
   1308   /* Computes the load module index of the load module that contains the
   1309      definition of its TLS sym.  */
   1310   HOWTO (R_PPC64_DTPMOD64,
   1311 	 0,			/* rightshift */
   1312 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1313 	 64,			/* bitsize */
   1314 	 FALSE,			/* pc_relative */
   1315 	 0,			/* bitpos */
   1316 	 complain_overflow_dont, /* complain_on_overflow */
   1317 	 ppc64_elf_unhandled_reloc, /* special_function */
   1318 	 "R_PPC64_DTPMOD64",	/* name */
   1319 	 FALSE,			/* partial_inplace */
   1320 	 0,			/* src_mask */
   1321 	 ONES (64),		/* dst_mask */
   1322 	 FALSE),		/* pcrel_offset */
   1323 
   1324   /* Computes a dtv-relative displacement, the difference between the value
   1325      of sym+add and the base address of the thread-local storage block that
   1326      contains the definition of sym, minus 0x8000.  */
   1327   HOWTO (R_PPC64_DTPREL64,
   1328 	 0,			/* rightshift */
   1329 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1330 	 64,			/* bitsize */
   1331 	 FALSE,			/* pc_relative */
   1332 	 0,			/* bitpos */
   1333 	 complain_overflow_dont, /* complain_on_overflow */
   1334 	 ppc64_elf_unhandled_reloc, /* special_function */
   1335 	 "R_PPC64_DTPREL64",	/* name */
   1336 	 FALSE,			/* partial_inplace */
   1337 	 0,			/* src_mask */
   1338 	 ONES (64),		/* dst_mask */
   1339 	 FALSE),		/* pcrel_offset */
   1340 
   1341   /* A 16 bit dtprel reloc.  */
   1342   HOWTO (R_PPC64_DTPREL16,
   1343 	 0,			/* rightshift */
   1344 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1345 	 16,			/* bitsize */
   1346 	 FALSE,			/* pc_relative */
   1347 	 0,			/* bitpos */
   1348 	 complain_overflow_signed, /* complain_on_overflow */
   1349 	 ppc64_elf_unhandled_reloc, /* special_function */
   1350 	 "R_PPC64_DTPREL16",	/* name */
   1351 	 FALSE,			/* partial_inplace */
   1352 	 0,			/* src_mask */
   1353 	 0xffff,		/* dst_mask */
   1354 	 FALSE),		/* pcrel_offset */
   1355 
   1356   /* Like DTPREL16, but no overflow.  */
   1357   HOWTO (R_PPC64_DTPREL16_LO,
   1358 	 0,			/* rightshift */
   1359 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1360 	 16,			/* bitsize */
   1361 	 FALSE,			/* pc_relative */
   1362 	 0,			/* bitpos */
   1363 	 complain_overflow_dont, /* complain_on_overflow */
   1364 	 ppc64_elf_unhandled_reloc, /* special_function */
   1365 	 "R_PPC64_DTPREL16_LO",	/* name */
   1366 	 FALSE,			/* partial_inplace */
   1367 	 0,			/* src_mask */
   1368 	 0xffff,		/* dst_mask */
   1369 	 FALSE),		/* pcrel_offset */
   1370 
   1371   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
   1372   HOWTO (R_PPC64_DTPREL16_HI,
   1373 	 16,			/* rightshift */
   1374 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1375 	 16,			/* bitsize */
   1376 	 FALSE,			/* pc_relative */
   1377 	 0,			/* bitpos */
   1378 	 complain_overflow_dont, /* complain_on_overflow */
   1379 	 ppc64_elf_unhandled_reloc, /* special_function */
   1380 	 "R_PPC64_DTPREL16_HI",	/* name */
   1381 	 FALSE,			/* partial_inplace */
   1382 	 0,			/* src_mask */
   1383 	 0xffff,		/* dst_mask */
   1384 	 FALSE),		/* pcrel_offset */
   1385 
   1386   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
   1387   HOWTO (R_PPC64_DTPREL16_HA,
   1388 	 16,			/* rightshift */
   1389 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1390 	 16,			/* bitsize */
   1391 	 FALSE,			/* pc_relative */
   1392 	 0,			/* bitpos */
   1393 	 complain_overflow_dont, /* complain_on_overflow */
   1394 	 ppc64_elf_unhandled_reloc, /* special_function */
   1395 	 "R_PPC64_DTPREL16_HA",	/* name */
   1396 	 FALSE,			/* partial_inplace */
   1397 	 0,			/* src_mask */
   1398 	 0xffff,		/* dst_mask */
   1399 	 FALSE),		/* pcrel_offset */
   1400 
   1401   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
   1402   HOWTO (R_PPC64_DTPREL16_HIGHER,
   1403 	 32,			/* rightshift */
   1404 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1405 	 16,			/* bitsize */
   1406 	 FALSE,			/* pc_relative */
   1407 	 0,			/* bitpos */
   1408 	 complain_overflow_dont, /* complain_on_overflow */
   1409 	 ppc64_elf_unhandled_reloc, /* special_function */
   1410 	 "R_PPC64_DTPREL16_HIGHER", /* name */
   1411 	 FALSE,			/* partial_inplace */
   1412 	 0,			/* src_mask */
   1413 	 0xffff,		/* dst_mask */
   1414 	 FALSE),		/* pcrel_offset */
   1415 
   1416   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
   1417   HOWTO (R_PPC64_DTPREL16_HIGHERA,
   1418 	 32,			/* rightshift */
   1419 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1420 	 16,			/* bitsize */
   1421 	 FALSE,			/* pc_relative */
   1422 	 0,			/* bitpos */
   1423 	 complain_overflow_dont, /* complain_on_overflow */
   1424 	 ppc64_elf_unhandled_reloc, /* special_function */
   1425 	 "R_PPC64_DTPREL16_HIGHERA", /* name */
   1426 	 FALSE,			/* partial_inplace */
   1427 	 0,			/* src_mask */
   1428 	 0xffff,		/* dst_mask */
   1429 	 FALSE),		/* pcrel_offset */
   1430 
   1431   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
   1432   HOWTO (R_PPC64_DTPREL16_HIGHEST,
   1433 	 48,			/* rightshift */
   1434 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1435 	 16,			/* bitsize */
   1436 	 FALSE,			/* pc_relative */
   1437 	 0,			/* bitpos */
   1438 	 complain_overflow_dont, /* complain_on_overflow */
   1439 	 ppc64_elf_unhandled_reloc, /* special_function */
   1440 	 "R_PPC64_DTPREL16_HIGHEST", /* name */
   1441 	 FALSE,			/* partial_inplace */
   1442 	 0,			/* src_mask */
   1443 	 0xffff,		/* dst_mask */
   1444 	 FALSE),		/* pcrel_offset */
   1445 
   1446   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
   1447   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
   1448 	 48,			/* rightshift */
   1449 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1450 	 16,			/* bitsize */
   1451 	 FALSE,			/* pc_relative */
   1452 	 0,			/* bitpos */
   1453 	 complain_overflow_dont, /* complain_on_overflow */
   1454 	 ppc64_elf_unhandled_reloc, /* special_function */
   1455 	 "R_PPC64_DTPREL16_HIGHESTA", /* name */
   1456 	 FALSE,			/* partial_inplace */
   1457 	 0,			/* src_mask */
   1458 	 0xffff,		/* dst_mask */
   1459 	 FALSE),		/* pcrel_offset */
   1460 
   1461   /* Like DTPREL16, but for insns with a DS field.  */
   1462   HOWTO (R_PPC64_DTPREL16_DS,
   1463 	 0,			/* rightshift */
   1464 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1465 	 16,			/* bitsize */
   1466 	 FALSE,			/* pc_relative */
   1467 	 0,			/* bitpos */
   1468 	 complain_overflow_signed, /* complain_on_overflow */
   1469 	 ppc64_elf_unhandled_reloc, /* special_function */
   1470 	 "R_PPC64_DTPREL16_DS",	/* name */
   1471 	 FALSE,			/* partial_inplace */
   1472 	 0,			/* src_mask */
   1473 	 0xfffc,		/* dst_mask */
   1474 	 FALSE),		/* pcrel_offset */
   1475 
   1476   /* Like DTPREL16_DS, but no overflow.  */
   1477   HOWTO (R_PPC64_DTPREL16_LO_DS,
   1478 	 0,			/* rightshift */
   1479 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1480 	 16,			/* bitsize */
   1481 	 FALSE,			/* pc_relative */
   1482 	 0,			/* bitpos */
   1483 	 complain_overflow_dont, /* complain_on_overflow */
   1484 	 ppc64_elf_unhandled_reloc, /* special_function */
   1485 	 "R_PPC64_DTPREL16_LO_DS", /* name */
   1486 	 FALSE,			/* partial_inplace */
   1487 	 0,			/* src_mask */
   1488 	 0xfffc,		/* dst_mask */
   1489 	 FALSE),		/* pcrel_offset */
   1490 
   1491   /* Computes a tp-relative displacement, the difference between the value of
   1492      sym+add and the value of the thread pointer (r13).  */
   1493   HOWTO (R_PPC64_TPREL64,
   1494 	 0,			/* rightshift */
   1495 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1496 	 64,			/* bitsize */
   1497 	 FALSE,			/* pc_relative */
   1498 	 0,			/* bitpos */
   1499 	 complain_overflow_dont, /* complain_on_overflow */
   1500 	 ppc64_elf_unhandled_reloc, /* special_function */
   1501 	 "R_PPC64_TPREL64",	/* name */
   1502 	 FALSE,			/* partial_inplace */
   1503 	 0,			/* src_mask */
   1504 	 ONES (64),		/* dst_mask */
   1505 	 FALSE),		/* pcrel_offset */
   1506 
   1507   /* A 16 bit tprel reloc.  */
   1508   HOWTO (R_PPC64_TPREL16,
   1509 	 0,			/* rightshift */
   1510 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1511 	 16,			/* bitsize */
   1512 	 FALSE,			/* pc_relative */
   1513 	 0,			/* bitpos */
   1514 	 complain_overflow_signed, /* complain_on_overflow */
   1515 	 ppc64_elf_unhandled_reloc, /* special_function */
   1516 	 "R_PPC64_TPREL16",	/* name */
   1517 	 FALSE,			/* partial_inplace */
   1518 	 0,			/* src_mask */
   1519 	 0xffff,		/* dst_mask */
   1520 	 FALSE),		/* pcrel_offset */
   1521 
   1522   /* Like TPREL16, but no overflow.  */
   1523   HOWTO (R_PPC64_TPREL16_LO,
   1524 	 0,			/* rightshift */
   1525 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1526 	 16,			/* bitsize */
   1527 	 FALSE,			/* pc_relative */
   1528 	 0,			/* bitpos */
   1529 	 complain_overflow_dont, /* complain_on_overflow */
   1530 	 ppc64_elf_unhandled_reloc, /* special_function */
   1531 	 "R_PPC64_TPREL16_LO",	/* name */
   1532 	 FALSE,			/* partial_inplace */
   1533 	 0,			/* src_mask */
   1534 	 0xffff,		/* dst_mask */
   1535 	 FALSE),		/* pcrel_offset */
   1536 
   1537   /* Like TPREL16_LO, but next higher group of 16 bits.  */
   1538   HOWTO (R_PPC64_TPREL16_HI,
   1539 	 16,			/* rightshift */
   1540 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1541 	 16,			/* bitsize */
   1542 	 FALSE,			/* pc_relative */
   1543 	 0,			/* bitpos */
   1544 	 complain_overflow_dont, /* complain_on_overflow */
   1545 	 ppc64_elf_unhandled_reloc, /* special_function */
   1546 	 "R_PPC64_TPREL16_HI",	/* name */
   1547 	 FALSE,			/* partial_inplace */
   1548 	 0,			/* src_mask */
   1549 	 0xffff,		/* dst_mask */
   1550 	 FALSE),		/* pcrel_offset */
   1551 
   1552   /* Like TPREL16_HI, but adjust for low 16 bits.  */
   1553   HOWTO (R_PPC64_TPREL16_HA,
   1554 	 16,			/* rightshift */
   1555 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1556 	 16,			/* bitsize */
   1557 	 FALSE,			/* pc_relative */
   1558 	 0,			/* bitpos */
   1559 	 complain_overflow_dont, /* complain_on_overflow */
   1560 	 ppc64_elf_unhandled_reloc, /* special_function */
   1561 	 "R_PPC64_TPREL16_HA",	/* name */
   1562 	 FALSE,			/* partial_inplace */
   1563 	 0,			/* src_mask */
   1564 	 0xffff,		/* dst_mask */
   1565 	 FALSE),		/* pcrel_offset */
   1566 
   1567   /* Like TPREL16_HI, but next higher group of 16 bits.  */
   1568   HOWTO (R_PPC64_TPREL16_HIGHER,
   1569 	 32,			/* rightshift */
   1570 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1571 	 16,			/* bitsize */
   1572 	 FALSE,			/* pc_relative */
   1573 	 0,			/* bitpos */
   1574 	 complain_overflow_dont, /* complain_on_overflow */
   1575 	 ppc64_elf_unhandled_reloc, /* special_function */
   1576 	 "R_PPC64_TPREL16_HIGHER",	/* name */
   1577 	 FALSE,			/* partial_inplace */
   1578 	 0,			/* src_mask */
   1579 	 0xffff,		/* dst_mask */
   1580 	 FALSE),		/* pcrel_offset */
   1581 
   1582   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
   1583   HOWTO (R_PPC64_TPREL16_HIGHERA,
   1584 	 32,			/* rightshift */
   1585 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1586 	 16,			/* bitsize */
   1587 	 FALSE,			/* pc_relative */
   1588 	 0,			/* bitpos */
   1589 	 complain_overflow_dont, /* complain_on_overflow */
   1590 	 ppc64_elf_unhandled_reloc, /* special_function */
   1591 	 "R_PPC64_TPREL16_HIGHERA", /* name */
   1592 	 FALSE,			/* partial_inplace */
   1593 	 0,			/* src_mask */
   1594 	 0xffff,		/* dst_mask */
   1595 	 FALSE),		/* pcrel_offset */
   1596 
   1597   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
   1598   HOWTO (R_PPC64_TPREL16_HIGHEST,
   1599 	 48,			/* rightshift */
   1600 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1601 	 16,			/* bitsize */
   1602 	 FALSE,			/* pc_relative */
   1603 	 0,			/* bitpos */
   1604 	 complain_overflow_dont, /* complain_on_overflow */
   1605 	 ppc64_elf_unhandled_reloc, /* special_function */
   1606 	 "R_PPC64_TPREL16_HIGHEST", /* name */
   1607 	 FALSE,			/* partial_inplace */
   1608 	 0,			/* src_mask */
   1609 	 0xffff,		/* dst_mask */
   1610 	 FALSE),		/* pcrel_offset */
   1611 
   1612   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
   1613   HOWTO (R_PPC64_TPREL16_HIGHESTA,
   1614 	 48,			/* rightshift */
   1615 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1616 	 16,			/* bitsize */
   1617 	 FALSE,			/* pc_relative */
   1618 	 0,			/* bitpos */
   1619 	 complain_overflow_dont, /* complain_on_overflow */
   1620 	 ppc64_elf_unhandled_reloc, /* special_function */
   1621 	 "R_PPC64_TPREL16_HIGHESTA", /* name */
   1622 	 FALSE,			/* partial_inplace */
   1623 	 0,			/* src_mask */
   1624 	 0xffff,		/* dst_mask */
   1625 	 FALSE),		/* pcrel_offset */
   1626 
   1627   /* Like TPREL16, but for insns with a DS field.  */
   1628   HOWTO (R_PPC64_TPREL16_DS,
   1629 	 0,			/* rightshift */
   1630 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1631 	 16,			/* bitsize */
   1632 	 FALSE,			/* pc_relative */
   1633 	 0,			/* bitpos */
   1634 	 complain_overflow_signed, /* complain_on_overflow */
   1635 	 ppc64_elf_unhandled_reloc, /* special_function */
   1636 	 "R_PPC64_TPREL16_DS",	/* name */
   1637 	 FALSE,			/* partial_inplace */
   1638 	 0,			/* src_mask */
   1639 	 0xfffc,		/* dst_mask */
   1640 	 FALSE),		/* pcrel_offset */
   1641 
   1642   /* Like TPREL16_DS, but no overflow.  */
   1643   HOWTO (R_PPC64_TPREL16_LO_DS,
   1644 	 0,			/* rightshift */
   1645 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1646 	 16,			/* bitsize */
   1647 	 FALSE,			/* pc_relative */
   1648 	 0,			/* bitpos */
   1649 	 complain_overflow_dont, /* complain_on_overflow */
   1650 	 ppc64_elf_unhandled_reloc, /* special_function */
   1651 	 "R_PPC64_TPREL16_LO_DS", /* name */
   1652 	 FALSE,			/* partial_inplace */
   1653 	 0,			/* src_mask */
   1654 	 0xfffc,		/* dst_mask */
   1655 	 FALSE),		/* pcrel_offset */
   1656 
   1657   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1658      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
   1659      to the first entry relative to the TOC base (r2).  */
   1660   HOWTO (R_PPC64_GOT_TLSGD16,
   1661 	 0,			/* rightshift */
   1662 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1663 	 16,			/* bitsize */
   1664 	 FALSE,			/* pc_relative */
   1665 	 0,			/* bitpos */
   1666 	 complain_overflow_signed, /* complain_on_overflow */
   1667 	 ppc64_elf_unhandled_reloc, /* special_function */
   1668 	 "R_PPC64_GOT_TLSGD16",	/* name */
   1669 	 FALSE,			/* partial_inplace */
   1670 	 0,			/* src_mask */
   1671 	 0xffff,		/* dst_mask */
   1672 	 FALSE),		/* pcrel_offset */
   1673 
   1674   /* Like GOT_TLSGD16, but no overflow.  */
   1675   HOWTO (R_PPC64_GOT_TLSGD16_LO,
   1676 	 0,			/* rightshift */
   1677 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1678 	 16,			/* bitsize */
   1679 	 FALSE,			/* pc_relative */
   1680 	 0,			/* bitpos */
   1681 	 complain_overflow_dont, /* complain_on_overflow */
   1682 	 ppc64_elf_unhandled_reloc, /* special_function */
   1683 	 "R_PPC64_GOT_TLSGD16_LO", /* name */
   1684 	 FALSE,			/* partial_inplace */
   1685 	 0,			/* src_mask */
   1686 	 0xffff,		/* dst_mask */
   1687 	 FALSE),		/* pcrel_offset */
   1688 
   1689   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
   1690   HOWTO (R_PPC64_GOT_TLSGD16_HI,
   1691 	 16,			/* rightshift */
   1692 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1693 	 16,			/* bitsize */
   1694 	 FALSE,			/* pc_relative */
   1695 	 0,			/* bitpos */
   1696 	 complain_overflow_dont, /* complain_on_overflow */
   1697 	 ppc64_elf_unhandled_reloc, /* special_function */
   1698 	 "R_PPC64_GOT_TLSGD16_HI", /* name */
   1699 	 FALSE,			/* partial_inplace */
   1700 	 0,			/* src_mask */
   1701 	 0xffff,		/* dst_mask */
   1702 	 FALSE),		/* pcrel_offset */
   1703 
   1704   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
   1705   HOWTO (R_PPC64_GOT_TLSGD16_HA,
   1706 	 16,			/* rightshift */
   1707 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1708 	 16,			/* bitsize */
   1709 	 FALSE,			/* pc_relative */
   1710 	 0,			/* bitpos */
   1711 	 complain_overflow_dont, /* complain_on_overflow */
   1712 	 ppc64_elf_unhandled_reloc, /* special_function */
   1713 	 "R_PPC64_GOT_TLSGD16_HA", /* name */
   1714 	 FALSE,			/* partial_inplace */
   1715 	 0,			/* src_mask */
   1716 	 0xffff,		/* dst_mask */
   1717 	 FALSE),		/* pcrel_offset */
   1718 
   1719   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1720      with values (sym+add)@dtpmod and zero, and computes the offset to the
   1721      first entry relative to the TOC base (r2).  */
   1722   HOWTO (R_PPC64_GOT_TLSLD16,
   1723 	 0,			/* rightshift */
   1724 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1725 	 16,			/* bitsize */
   1726 	 FALSE,			/* pc_relative */
   1727 	 0,			/* bitpos */
   1728 	 complain_overflow_signed, /* complain_on_overflow */
   1729 	 ppc64_elf_unhandled_reloc, /* special_function */
   1730 	 "R_PPC64_GOT_TLSLD16",	/* name */
   1731 	 FALSE,			/* partial_inplace */
   1732 	 0,			/* src_mask */
   1733 	 0xffff,		/* dst_mask */
   1734 	 FALSE),		/* pcrel_offset */
   1735 
   1736   /* Like GOT_TLSLD16, but no overflow.  */
   1737   HOWTO (R_PPC64_GOT_TLSLD16_LO,
   1738 	 0,			/* rightshift */
   1739 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1740 	 16,			/* bitsize */
   1741 	 FALSE,			/* pc_relative */
   1742 	 0,			/* bitpos */
   1743 	 complain_overflow_dont, /* complain_on_overflow */
   1744 	 ppc64_elf_unhandled_reloc, /* special_function */
   1745 	 "R_PPC64_GOT_TLSLD16_LO", /* name */
   1746 	 FALSE,			/* partial_inplace */
   1747 	 0,			/* src_mask */
   1748 	 0xffff,		/* dst_mask */
   1749 	 FALSE),		/* pcrel_offset */
   1750 
   1751   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
   1752   HOWTO (R_PPC64_GOT_TLSLD16_HI,
   1753 	 16,			/* rightshift */
   1754 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1755 	 16,			/* bitsize */
   1756 	 FALSE,			/* pc_relative */
   1757 	 0,			/* bitpos */
   1758 	 complain_overflow_dont, /* complain_on_overflow */
   1759 	 ppc64_elf_unhandled_reloc, /* special_function */
   1760 	 "R_PPC64_GOT_TLSLD16_HI", /* name */
   1761 	 FALSE,			/* partial_inplace */
   1762 	 0,			/* src_mask */
   1763 	 0xffff,		/* dst_mask */
   1764 	 FALSE),		/* pcrel_offset */
   1765 
   1766   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
   1767   HOWTO (R_PPC64_GOT_TLSLD16_HA,
   1768 	 16,			/* rightshift */
   1769 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1770 	 16,			/* bitsize */
   1771 	 FALSE,			/* pc_relative */
   1772 	 0,			/* bitpos */
   1773 	 complain_overflow_dont, /* complain_on_overflow */
   1774 	 ppc64_elf_unhandled_reloc, /* special_function */
   1775 	 "R_PPC64_GOT_TLSLD16_HA", /* name */
   1776 	 FALSE,			/* partial_inplace */
   1777 	 0,			/* src_mask */
   1778 	 0xffff,		/* dst_mask */
   1779 	 FALSE),		/* pcrel_offset */
   1780 
   1781   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
   1782      the offset to the entry relative to the TOC base (r2).  */
   1783   HOWTO (R_PPC64_GOT_DTPREL16_DS,
   1784 	 0,			/* rightshift */
   1785 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1786 	 16,			/* bitsize */
   1787 	 FALSE,			/* pc_relative */
   1788 	 0,			/* bitpos */
   1789 	 complain_overflow_signed, /* complain_on_overflow */
   1790 	 ppc64_elf_unhandled_reloc, /* special_function */
   1791 	 "R_PPC64_GOT_DTPREL16_DS", /* name */
   1792 	 FALSE,			/* partial_inplace */
   1793 	 0,			/* src_mask */
   1794 	 0xfffc,		/* dst_mask */
   1795 	 FALSE),		/* pcrel_offset */
   1796 
   1797   /* Like GOT_DTPREL16_DS, but no overflow.  */
   1798   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
   1799 	 0,			/* rightshift */
   1800 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1801 	 16,			/* bitsize */
   1802 	 FALSE,			/* pc_relative */
   1803 	 0,			/* bitpos */
   1804 	 complain_overflow_dont, /* complain_on_overflow */
   1805 	 ppc64_elf_unhandled_reloc, /* special_function */
   1806 	 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
   1807 	 FALSE,			/* partial_inplace */
   1808 	 0,			/* src_mask */
   1809 	 0xfffc,		/* dst_mask */
   1810 	 FALSE),		/* pcrel_offset */
   1811 
   1812   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
   1813   HOWTO (R_PPC64_GOT_DTPREL16_HI,
   1814 	 16,			/* rightshift */
   1815 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1816 	 16,			/* bitsize */
   1817 	 FALSE,			/* pc_relative */
   1818 	 0,			/* bitpos */
   1819 	 complain_overflow_dont, /* complain_on_overflow */
   1820 	 ppc64_elf_unhandled_reloc, /* special_function */
   1821 	 "R_PPC64_GOT_DTPREL16_HI", /* name */
   1822 	 FALSE,			/* partial_inplace */
   1823 	 0,			/* src_mask */
   1824 	 0xffff,		/* dst_mask */
   1825 	 FALSE),		/* pcrel_offset */
   1826 
   1827   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
   1828   HOWTO (R_PPC64_GOT_DTPREL16_HA,
   1829 	 16,			/* rightshift */
   1830 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1831 	 16,			/* bitsize */
   1832 	 FALSE,			/* pc_relative */
   1833 	 0,			/* bitpos */
   1834 	 complain_overflow_dont, /* complain_on_overflow */
   1835 	 ppc64_elf_unhandled_reloc, /* special_function */
   1836 	 "R_PPC64_GOT_DTPREL16_HA", /* name */
   1837 	 FALSE,			/* partial_inplace */
   1838 	 0,			/* src_mask */
   1839 	 0xffff,		/* dst_mask */
   1840 	 FALSE),		/* pcrel_offset */
   1841 
   1842   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
   1843      offset to the entry relative to the TOC base (r2).  */
   1844   HOWTO (R_PPC64_GOT_TPREL16_DS,
   1845 	 0,			/* rightshift */
   1846 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1847 	 16,			/* bitsize */
   1848 	 FALSE,			/* pc_relative */
   1849 	 0,			/* bitpos */
   1850 	 complain_overflow_signed, /* complain_on_overflow */
   1851 	 ppc64_elf_unhandled_reloc, /* special_function */
   1852 	 "R_PPC64_GOT_TPREL16_DS", /* name */
   1853 	 FALSE,			/* partial_inplace */
   1854 	 0,			/* src_mask */
   1855 	 0xfffc,		/* dst_mask */
   1856 	 FALSE),		/* pcrel_offset */
   1857 
   1858   /* Like GOT_TPREL16_DS, but no overflow.  */
   1859   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
   1860 	 0,			/* rightshift */
   1861 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1862 	 16,			/* bitsize */
   1863 	 FALSE,			/* pc_relative */
   1864 	 0,			/* bitpos */
   1865 	 complain_overflow_dont, /* complain_on_overflow */
   1866 	 ppc64_elf_unhandled_reloc, /* special_function */
   1867 	 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
   1868 	 FALSE,			/* partial_inplace */
   1869 	 0,			/* src_mask */
   1870 	 0xfffc,		/* dst_mask */
   1871 	 FALSE),		/* pcrel_offset */
   1872 
   1873   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
   1874   HOWTO (R_PPC64_GOT_TPREL16_HI,
   1875 	 16,			/* rightshift */
   1876 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1877 	 16,			/* bitsize */
   1878 	 FALSE,			/* pc_relative */
   1879 	 0,			/* bitpos */
   1880 	 complain_overflow_dont, /* complain_on_overflow */
   1881 	 ppc64_elf_unhandled_reloc, /* special_function */
   1882 	 "R_PPC64_GOT_TPREL16_HI", /* name */
   1883 	 FALSE,			/* partial_inplace */
   1884 	 0,			/* src_mask */
   1885 	 0xffff,		/* dst_mask */
   1886 	 FALSE),		/* pcrel_offset */
   1887 
   1888   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
   1889   HOWTO (R_PPC64_GOT_TPREL16_HA,
   1890 	 16,			/* rightshift */
   1891 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1892 	 16,			/* bitsize */
   1893 	 FALSE,			/* pc_relative */
   1894 	 0,			/* bitpos */
   1895 	 complain_overflow_dont, /* complain_on_overflow */
   1896 	 ppc64_elf_unhandled_reloc, /* special_function */
   1897 	 "R_PPC64_GOT_TPREL16_HA", /* name */
   1898 	 FALSE,			/* partial_inplace */
   1899 	 0,			/* src_mask */
   1900 	 0xffff,		/* dst_mask */
   1901 	 FALSE),		/* pcrel_offset */
   1902 
   1903   HOWTO (R_PPC64_JMP_IREL,	/* type */
   1904 	 0,			/* rightshift */
   1905 	 0,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1906 	 0,			/* bitsize */
   1907 	 FALSE,			/* pc_relative */
   1908 	 0,			/* bitpos */
   1909 	 complain_overflow_dont, /* complain_on_overflow */
   1910 	 ppc64_elf_unhandled_reloc, /* special_function */
   1911 	 "R_PPC64_JMP_IREL",	/* name */
   1912 	 FALSE,			/* partial_inplace */
   1913 	 0,			/* src_mask */
   1914 	 0,			/* dst_mask */
   1915 	 FALSE),		/* pcrel_offset */
   1916 
   1917   HOWTO (R_PPC64_IRELATIVE,	/* type */
   1918 	 0,			/* rightshift */
   1919 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1920 	 64,			/* bitsize */
   1921 	 FALSE,			/* pc_relative */
   1922 	 0,			/* bitpos */
   1923 	 complain_overflow_dont, /* complain_on_overflow */
   1924 	 bfd_elf_generic_reloc,	/* special_function */
   1925 	 "R_PPC64_IRELATIVE",	/* name */
   1926 	 FALSE,			/* partial_inplace */
   1927 	 0,			/* src_mask */
   1928 	 ONES (64),		/* dst_mask */
   1929 	 FALSE),		/* pcrel_offset */
   1930 
   1931   /* A 16 bit relative relocation.  */
   1932   HOWTO (R_PPC64_REL16,		/* type */
   1933 	 0,			/* rightshift */
   1934 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1935 	 16,			/* bitsize */
   1936 	 TRUE,			/* pc_relative */
   1937 	 0,			/* bitpos */
   1938 	 complain_overflow_bitfield, /* complain_on_overflow */
   1939 	 bfd_elf_generic_reloc,	/* special_function */
   1940 	 "R_PPC64_REL16",	/* name */
   1941 	 FALSE,			/* partial_inplace */
   1942 	 0,			/* src_mask */
   1943 	 0xffff,		/* dst_mask */
   1944 	 TRUE),			/* pcrel_offset */
   1945 
   1946   /* A 16 bit relative relocation without overflow.  */
   1947   HOWTO (R_PPC64_REL16_LO,	/* type */
   1948 	 0,			/* rightshift */
   1949 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1950 	 16,			/* bitsize */
   1951 	 TRUE,			/* pc_relative */
   1952 	 0,			/* bitpos */
   1953 	 complain_overflow_dont,/* complain_on_overflow */
   1954 	 bfd_elf_generic_reloc,	/* special_function */
   1955 	 "R_PPC64_REL16_LO",	/* name */
   1956 	 FALSE,			/* partial_inplace */
   1957 	 0,			/* src_mask */
   1958 	 0xffff,		/* dst_mask */
   1959 	 TRUE),			/* pcrel_offset */
   1960 
   1961   /* The high order 16 bits of a relative address.  */
   1962   HOWTO (R_PPC64_REL16_HI,	/* type */
   1963 	 16,			/* rightshift */
   1964 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1965 	 16,			/* bitsize */
   1966 	 TRUE,			/* pc_relative */
   1967 	 0,			/* bitpos */
   1968 	 complain_overflow_dont, /* complain_on_overflow */
   1969 	 bfd_elf_generic_reloc,	/* special_function */
   1970 	 "R_PPC64_REL16_HI",	/* name */
   1971 	 FALSE,			/* partial_inplace */
   1972 	 0,			/* src_mask */
   1973 	 0xffff,		/* dst_mask */
   1974 	 TRUE),			/* pcrel_offset */
   1975 
   1976   /* The high order 16 bits of a relative address, plus 1 if the contents of
   1977      the low 16 bits, treated as a signed number, is negative.  */
   1978   HOWTO (R_PPC64_REL16_HA,	/* type */
   1979 	 16,			/* rightshift */
   1980 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1981 	 16,			/* bitsize */
   1982 	 TRUE,			/* pc_relative */
   1983 	 0,			/* bitpos */
   1984 	 complain_overflow_dont, /* complain_on_overflow */
   1985 	 ppc64_elf_ha_reloc,	/* special_function */
   1986 	 "R_PPC64_REL16_HA",	/* name */
   1987 	 FALSE,			/* partial_inplace */
   1988 	 0,			/* src_mask */
   1989 	 0xffff,		/* dst_mask */
   1990 	 TRUE),			/* pcrel_offset */
   1991 
   1992   /* GNU extension to record C++ vtable hierarchy.  */
   1993   HOWTO (R_PPC64_GNU_VTINHERIT,	/* type */
   1994 	 0,			/* rightshift */
   1995 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1996 	 0,			/* bitsize */
   1997 	 FALSE,			/* pc_relative */
   1998 	 0,			/* bitpos */
   1999 	 complain_overflow_dont, /* complain_on_overflow */
   2000 	 NULL,			/* special_function */
   2001 	 "R_PPC64_GNU_VTINHERIT", /* name */
   2002 	 FALSE,			/* partial_inplace */
   2003 	 0,			/* src_mask */
   2004 	 0,			/* dst_mask */
   2005 	 FALSE),		/* pcrel_offset */
   2006 
   2007   /* GNU extension to record C++ vtable member usage.  */
   2008   HOWTO (R_PPC64_GNU_VTENTRY,	/* type */
   2009 	 0,			/* rightshift */
   2010 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2011 	 0,			/* bitsize */
   2012 	 FALSE,			/* pc_relative */
   2013 	 0,			/* bitpos */
   2014 	 complain_overflow_dont, /* complain_on_overflow */
   2015 	 NULL,			/* special_function */
   2016 	 "R_PPC64_GNU_VTENTRY",	/* name */
   2017 	 FALSE,			/* partial_inplace */
   2018 	 0,			/* src_mask */
   2019 	 0,			/* dst_mask */
   2020 	 FALSE),		/* pcrel_offset */
   2021 };
   2022 
   2023 
   2024 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
   2026    be done.  */
   2027 
   2028 static void
   2029 ppc_howto_init (void)
   2030 {
   2031   unsigned int i, type;
   2032 
   2033   for (i = 0;
   2034        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2035        i++)
   2036     {
   2037       type = ppc64_elf_howto_raw[i].type;
   2038       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
   2039 			  / sizeof (ppc64_elf_howto_table[0])));
   2040       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   2041     }
   2042 }
   2043 
   2044 static reloc_howto_type *
   2045 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2046 			     bfd_reloc_code_real_type code)
   2047 {
   2048   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   2049 
   2050   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2051     /* Initialize howto table if needed.  */
   2052     ppc_howto_init ();
   2053 
   2054   switch (code)
   2055     {
   2056     default:
   2057       return NULL;
   2058 
   2059     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   2060       break;
   2061     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   2062       break;
   2063     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   2064       break;
   2065     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   2066       break;
   2067     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   2068       break;
   2069     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   2070       break;
   2071     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   2072       break;
   2073     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   2074       break;
   2075     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   2076       break;
   2077     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   2078       break;
   2079     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   2080       break;
   2081     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   2082       break;
   2083     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   2084       break;
   2085     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   2086       break;
   2087     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   2088       break;
   2089     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   2090       break;
   2091     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   2092       break;
   2093     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   2094       break;
   2095     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   2096       break;
   2097     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   2098       break;
   2099     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   2100       break;
   2101     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   2102       break;
   2103     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   2104       break;
   2105     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   2106       break;
   2107     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   2108       break;
   2109     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   2110       break;
   2111     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   2112       break;
   2113     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   2114       break;
   2115     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   2116       break;
   2117     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   2118       break;
   2119     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   2120       break;
   2121     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   2122       break;
   2123     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   2124       break;
   2125     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   2126       break;
   2127     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   2128       break;
   2129     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   2130       break;
   2131     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   2132       break;
   2133     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   2134       break;
   2135     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   2136       break;
   2137     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   2138       break;
   2139     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   2140       break;
   2141     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   2142       break;
   2143     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   2144       break;
   2145     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   2146       break;
   2147     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   2148       break;
   2149     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   2150       break;
   2151     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   2152       break;
   2153     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   2154       break;
   2155     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   2156       break;
   2157     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   2158       break;
   2159     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   2160       break;
   2161     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   2162       break;
   2163     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   2164       break;
   2165     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   2166       break;
   2167     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   2168       break;
   2169     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   2170       break;
   2171     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   2172       break;
   2173     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   2174       break;
   2175     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   2176       break;
   2177     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   2178       break;
   2179     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   2180       break;
   2181     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   2182       break;
   2183     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   2184       break;
   2185     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   2186       break;
   2187     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   2188       break;
   2189     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   2190       break;
   2191     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   2192       break;
   2193     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   2194       break;
   2195     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   2196       break;
   2197     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   2198       break;
   2199     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   2200       break;
   2201     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   2202       break;
   2203     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   2204       break;
   2205     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   2206       break;
   2207     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   2208       break;
   2209     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   2210       break;
   2211     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   2212       break;
   2213     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   2214       break;
   2215     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   2216       break;
   2217     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   2218       break;
   2219     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   2220       break;
   2221     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   2222       break;
   2223     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   2224       break;
   2225     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   2226       break;
   2227     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   2228       break;
   2229     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   2230       break;
   2231     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   2232       break;
   2233     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   2234       break;
   2235     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   2236       break;
   2237     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   2238       break;
   2239     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   2240       break;
   2241     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   2242       break;
   2243     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   2244       break;
   2245     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   2246       break;
   2247     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   2248       break;
   2249     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   2250       break;
   2251     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   2252       break;
   2253     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   2254       break;
   2255     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   2256       break;
   2257     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   2258       break;
   2259     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   2260       break;
   2261     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   2262       break;
   2263     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   2264       break;
   2265     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   2266       break;
   2267     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   2268       break;
   2269     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   2270       break;
   2271     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   2272       break;
   2273     }
   2274 
   2275   return ppc64_elf_howto_table[r];
   2276 };
   2277 
   2278 static reloc_howto_type *
   2279 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2280 			     const char *r_name)
   2281 {
   2282   unsigned int i;
   2283 
   2284   for (i = 0;
   2285        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2286        i++)
   2287     if (ppc64_elf_howto_raw[i].name != NULL
   2288 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   2289       return &ppc64_elf_howto_raw[i];
   2290 
   2291   return NULL;
   2292 }
   2293 
   2294 /* Set the howto pointer for a PowerPC ELF reloc.  */
   2295 
   2296 static void
   2297 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2298 			 Elf_Internal_Rela *dst)
   2299 {
   2300   unsigned int type;
   2301 
   2302   /* Initialize howto table if needed.  */
   2303   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2304     ppc_howto_init ();
   2305 
   2306   type = ELF64_R_TYPE (dst->r_info);
   2307   if (type >= (sizeof (ppc64_elf_howto_table)
   2308 	       / sizeof (ppc64_elf_howto_table[0])))
   2309     {
   2310       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
   2311 			     abfd, (int) type);
   2312       type = R_PPC64_NONE;
   2313     }
   2314   cache_ptr->howto = ppc64_elf_howto_table[type];
   2315 }
   2316 
   2317 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   2318 
   2319 static bfd_reloc_status_type
   2320 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2321 		    void *data, asection *input_section,
   2322 		    bfd *output_bfd, char **error_message)
   2323 {
   2324   /* If this is a relocatable link (output_bfd test tells us), just
   2325      call the generic function.  Any adjustment will be done at final
   2326      link time.  */
   2327   if (output_bfd != NULL)
   2328     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2329 				  input_section, output_bfd, error_message);
   2330 
   2331   /* Adjust the addend for sign extension of the low 16 bits.
   2332      We won't actually be using the low 16 bits, so trashing them
   2333      doesn't matter.  */
   2334   reloc_entry->addend += 0x8000;
   2335   return bfd_reloc_continue;
   2336 }
   2337 
   2338 static bfd_reloc_status_type
   2339 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2340 			void *data, asection *input_section,
   2341 			bfd *output_bfd, char **error_message)
   2342 {
   2343   if (output_bfd != NULL)
   2344     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2345 				  input_section, output_bfd, error_message);
   2346 
   2347   if (strcmp (symbol->section->name, ".opd") == 0
   2348       && (symbol->section->owner->flags & DYNAMIC) == 0)
   2349     {
   2350       bfd_vma dest = opd_entry_value (symbol->section,
   2351 				      symbol->value + reloc_entry->addend,
   2352 				      NULL, NULL, FALSE);
   2353       if (dest != (bfd_vma) -1)
   2354 	reloc_entry->addend = dest - (symbol->value
   2355 				      + symbol->section->output_section->vma
   2356 				      + symbol->section->output_offset);
   2357     }
   2358   return bfd_reloc_continue;
   2359 }
   2360 
   2361 static bfd_reloc_status_type
   2362 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2363 			 void *data, asection *input_section,
   2364 			 bfd *output_bfd, char **error_message)
   2365 {
   2366   long insn;
   2367   enum elf_ppc64_reloc_type r_type;
   2368   bfd_size_type octets;
   2369   /* Assume 'at' branch hints.  */
   2370   bfd_boolean is_isa_v2 = TRUE;
   2371 
   2372   /* If this is a relocatable link (output_bfd test tells us), just
   2373      call the generic function.  Any adjustment will be done at final
   2374      link time.  */
   2375   if (output_bfd != NULL)
   2376     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2377 				  input_section, output_bfd, error_message);
   2378 
   2379   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2380   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   2381   insn &= ~(0x01 << 21);
   2382   r_type = reloc_entry->howto->type;
   2383   if (r_type == R_PPC64_ADDR14_BRTAKEN
   2384       || r_type == R_PPC64_REL14_BRTAKEN)
   2385     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   2386 
   2387   if (is_isa_v2)
   2388     {
   2389       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   2390 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   2391 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   2392       if ((insn & (0x14 << 21)) == (0x04 << 21))
   2393 	insn |= 0x02 << 21;
   2394       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   2395 	insn |= 0x08 << 21;
   2396       else
   2397 	goto out;
   2398     }
   2399   else
   2400     {
   2401       bfd_vma target = 0;
   2402       bfd_vma from;
   2403 
   2404       if (!bfd_is_com_section (symbol->section))
   2405 	target = symbol->value;
   2406       target += symbol->section->output_section->vma;
   2407       target += symbol->section->output_offset;
   2408       target += reloc_entry->addend;
   2409 
   2410       from = (reloc_entry->address
   2411 	      + input_section->output_offset
   2412 	      + input_section->output_section->vma);
   2413 
   2414       /* Invert 'y' bit if not the default.  */
   2415       if ((bfd_signed_vma) (target - from) < 0)
   2416 	insn ^= 0x01 << 21;
   2417     }
   2418   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   2419  out:
   2420   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   2421 				 input_section, output_bfd, error_message);
   2422 }
   2423 
   2424 static bfd_reloc_status_type
   2425 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2426 			 void *data, asection *input_section,
   2427 			 bfd *output_bfd, char **error_message)
   2428 {
   2429   /* If this is a relocatable link (output_bfd test tells us), just
   2430      call the generic function.  Any adjustment will be done at final
   2431      link time.  */
   2432   if (output_bfd != NULL)
   2433     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2434 				  input_section, output_bfd, error_message);
   2435 
   2436   /* Subtract the symbol section base address.  */
   2437   reloc_entry->addend -= symbol->section->output_section->vma;
   2438   return bfd_reloc_continue;
   2439 }
   2440 
   2441 static bfd_reloc_status_type
   2442 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2443 			    void *data, asection *input_section,
   2444 			    bfd *output_bfd, char **error_message)
   2445 {
   2446   /* If this is a relocatable link (output_bfd test tells us), just
   2447      call the generic function.  Any adjustment will be done at final
   2448      link time.  */
   2449   if (output_bfd != NULL)
   2450     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2451 				  input_section, output_bfd, error_message);
   2452 
   2453   /* Subtract the symbol section base address.  */
   2454   reloc_entry->addend -= symbol->section->output_section->vma;
   2455 
   2456   /* Adjust the addend for sign extension of the low 16 bits.  */
   2457   reloc_entry->addend += 0x8000;
   2458   return bfd_reloc_continue;
   2459 }
   2460 
   2461 static bfd_reloc_status_type
   2462 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2463 		     void *data, asection *input_section,
   2464 		     bfd *output_bfd, char **error_message)
   2465 {
   2466   bfd_vma TOCstart;
   2467 
   2468   /* If this is a relocatable link (output_bfd test tells us), just
   2469      call the generic function.  Any adjustment will be done at final
   2470      link time.  */
   2471   if (output_bfd != NULL)
   2472     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2473 				  input_section, output_bfd, error_message);
   2474 
   2475   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2476   if (TOCstart == 0)
   2477     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2478 
   2479   /* Subtract the TOC base address.  */
   2480   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2481   return bfd_reloc_continue;
   2482 }
   2483 
   2484 static bfd_reloc_status_type
   2485 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2486 			void *data, asection *input_section,
   2487 			bfd *output_bfd, char **error_message)
   2488 {
   2489   bfd_vma TOCstart;
   2490 
   2491   /* If this is a relocatable link (output_bfd test tells us), just
   2492      call the generic function.  Any adjustment will be done at final
   2493      link time.  */
   2494   if (output_bfd != NULL)
   2495     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2496 				  input_section, output_bfd, error_message);
   2497 
   2498   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2499   if (TOCstart == 0)
   2500     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2501 
   2502   /* Subtract the TOC base address.  */
   2503   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2504 
   2505   /* Adjust the addend for sign extension of the low 16 bits.  */
   2506   reloc_entry->addend += 0x8000;
   2507   return bfd_reloc_continue;
   2508 }
   2509 
   2510 static bfd_reloc_status_type
   2511 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2512 		       void *data, asection *input_section,
   2513 		       bfd *output_bfd, char **error_message)
   2514 {
   2515   bfd_vma TOCstart;
   2516   bfd_size_type octets;
   2517 
   2518   /* If this is a relocatable link (output_bfd test tells us), just
   2519      call the generic function.  Any adjustment will be done at final
   2520      link time.  */
   2521   if (output_bfd != NULL)
   2522     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2523 				  input_section, output_bfd, error_message);
   2524 
   2525   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2526   if (TOCstart == 0)
   2527     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2528 
   2529   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2530   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   2531   return bfd_reloc_ok;
   2532 }
   2533 
   2534 static bfd_reloc_status_type
   2535 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2536 			   void *data, asection *input_section,
   2537 			   bfd *output_bfd, char **error_message)
   2538 {
   2539   /* If this is a relocatable link (output_bfd test tells us), just
   2540      call the generic function.  Any adjustment will be done at final
   2541      link time.  */
   2542   if (output_bfd != NULL)
   2543     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2544 				  input_section, output_bfd, error_message);
   2545 
   2546   if (error_message != NULL)
   2547     {
   2548       static char buf[60];
   2549       sprintf (buf, "generic linker can't handle %s",
   2550 	       reloc_entry->howto->name);
   2551       *error_message = buf;
   2552     }
   2553   return bfd_reloc_dangerous;
   2554 }
   2555 
   2556 /* Track GOT entries needed for a given symbol.  We might need more
   2557    than one got entry per symbol.  */
   2558 struct got_entry
   2559 {
   2560   struct got_entry *next;
   2561 
   2562   /* The symbol addend that we'll be placing in the GOT.  */
   2563   bfd_vma addend;
   2564 
   2565   /* Unlike other ELF targets, we use separate GOT entries for the same
   2566      symbol referenced from different input files.  This is to support
   2567      automatic multiple TOC/GOT sections, where the TOC base can vary
   2568      from one input file to another.  After partitioning into TOC groups
   2569      we merge entries within the group.
   2570 
   2571      Point to the BFD owning this GOT entry.  */
   2572   bfd *owner;
   2573 
   2574   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   2575      TLS_TPREL or TLS_DTPREL for tls entries.  */
   2576   unsigned char tls_type;
   2577 
   2578   /* Non-zero if got.ent points to real entry.  */
   2579   unsigned char is_indirect;
   2580 
   2581   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   2582   union
   2583     {
   2584       bfd_signed_vma refcount;
   2585       bfd_vma offset;
   2586       struct got_entry *ent;
   2587     } got;
   2588 };
   2589 
   2590 /* The same for PLT.  */
   2591 struct plt_entry
   2592 {
   2593   struct plt_entry *next;
   2594 
   2595   bfd_vma addend;
   2596 
   2597   union
   2598     {
   2599       bfd_signed_vma refcount;
   2600       bfd_vma offset;
   2601     } plt;
   2602 };
   2603 
   2604 struct ppc64_elf_obj_tdata
   2605 {
   2606   struct elf_obj_tdata elf;
   2607 
   2608   /* Shortcuts to dynamic linker sections.  */
   2609   asection *got;
   2610   asection *relgot;
   2611 
   2612   /* Used during garbage collection.  We attach global symbols defined
   2613      on removed .opd entries to this section so that the sym is removed.  */
   2614   asection *deleted_section;
   2615 
   2616   /* TLS local dynamic got entry handling.  Support for multiple GOT
   2617      sections means we potentially need one of these for each input bfd.  */
   2618   struct got_entry tlsld_got;
   2619 
   2620   /* A copy of relocs before they are modified for --emit-relocs.  */
   2621   Elf_Internal_Rela *opd_relocs;
   2622 
   2623   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   2624      the reloc to be in the range -32768 to 32767.  */
   2625   unsigned int has_small_toc_reloc : 1;
   2626 
   2627   /* Set if toc/got ha relocs detected not using r2, or lo reloc
   2628      instruction not one we handle.  */
   2629   unsigned int unexpected_toc_insn : 1;
   2630 };
   2631 
   2632 #define ppc64_elf_tdata(bfd) \
   2633   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   2634 
   2635 #define ppc64_tlsld_got(bfd) \
   2636   (&ppc64_elf_tdata (bfd)->tlsld_got)
   2637 
   2638 #define is_ppc64_elf(bfd) \
   2639   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   2640    && elf_object_id (bfd) == PPC64_ELF_DATA)
   2641 
   2642 /* Override the generic function because we store some extras.  */
   2643 
   2644 static bfd_boolean
   2645 ppc64_elf_mkobject (bfd *abfd)
   2646 {
   2647   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   2648 				  PPC64_ELF_DATA);
   2649 }
   2650 
   2651 /* Fix bad default arch selected for a 64 bit input bfd when the
   2652    default is 32 bit.  */
   2653 
   2654 static bfd_boolean
   2655 ppc64_elf_object_p (bfd *abfd)
   2656 {
   2657   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
   2658     {
   2659       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   2660 
   2661       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   2662 	{
   2663 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   2664 	  abfd->arch_info = abfd->arch_info->next;
   2665 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   2666 	}
   2667     }
   2668   return TRUE;
   2669 }
   2670 
   2671 /* Support for core dump NOTE sections.  */
   2672 
   2673 static bfd_boolean
   2674 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   2675 {
   2676   size_t offset, size;
   2677 
   2678   if (note->descsz != 504)
   2679     return FALSE;
   2680 
   2681   /* pr_cursig */
   2682   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   2683 
   2684   /* pr_pid */
   2685   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
   2686 
   2687   /* pr_reg */
   2688   offset = 112;
   2689   size = 384;
   2690 
   2691   /* Make a ".reg/999" section.  */
   2692   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   2693 					  size, note->descpos + offset);
   2694 }
   2695 
   2696 static bfd_boolean
   2697 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   2698 {
   2699   if (note->descsz != 136)
   2700     return FALSE;
   2701 
   2702   elf_tdata (abfd)->core->pid
   2703     = bfd_get_32 (abfd, note->descdata + 24);
   2704   elf_tdata (abfd)->core->program
   2705     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   2706   elf_tdata (abfd)->core->command
   2707     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   2708 
   2709   return TRUE;
   2710 }
   2711 
   2712 static char *
   2713 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   2714 			   ...)
   2715 {
   2716   switch (note_type)
   2717     {
   2718     default:
   2719       return NULL;
   2720 
   2721     case NT_PRPSINFO:
   2722       {
   2723 	char data[136];
   2724 	va_list ap;
   2725 
   2726 	va_start (ap, note_type);
   2727 	memset (data, 0, sizeof (data));
   2728 	strncpy (data + 40, va_arg (ap, const char *), 16);
   2729 	strncpy (data + 56, va_arg (ap, const char *), 80);
   2730 	va_end (ap);
   2731 	return elfcore_write_note (abfd, buf, bufsiz,
   2732 				   "CORE", note_type, data, sizeof (data));
   2733       }
   2734 
   2735     case NT_PRSTATUS:
   2736       {
   2737 	char data[504];
   2738 	va_list ap;
   2739 	long pid;
   2740 	int cursig;
   2741 	const void *greg;
   2742 
   2743 	va_start (ap, note_type);
   2744 	memset (data, 0, 112);
   2745 	pid = va_arg (ap, long);
   2746 	bfd_put_32 (abfd, pid, data + 32);
   2747 	cursig = va_arg (ap, int);
   2748 	bfd_put_16 (abfd, cursig, data + 12);
   2749 	greg = va_arg (ap, const void *);
   2750 	memcpy (data + 112, greg, 384);
   2751 	memset (data + 496, 0, 8);
   2752 	va_end (ap);
   2753 	return elfcore_write_note (abfd, buf, bufsiz,
   2754 				   "CORE", note_type, data, sizeof (data));
   2755       }
   2756     }
   2757 }
   2758 
   2759 /* Add extra PPC sections.  */
   2760 
   2761 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
   2762 {
   2763   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
   2764   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2765   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2766   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2767   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2768   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2769   { NULL,                     0,  0, 0,            0 }
   2770 };
   2771 
   2772 enum _ppc64_sec_type {
   2773   sec_normal = 0,
   2774   sec_opd = 1,
   2775   sec_toc = 2
   2776 };
   2777 
   2778 struct _ppc64_elf_section_data
   2779 {
   2780   struct bfd_elf_section_data elf;
   2781 
   2782   union
   2783   {
   2784     /* An array with one entry for each opd function descriptor.  */
   2785     struct _opd_sec_data
   2786     {
   2787       /* Points to the function code section for local opd entries.  */
   2788       asection **func_sec;
   2789 
   2790       /* After editing .opd, adjust references to opd local syms.  */
   2791       long *adjust;
   2792     } opd;
   2793 
   2794     /* An array for toc sections, indexed by offset/8.  */
   2795     struct _toc_sec_data
   2796     {
   2797       /* Specifies the relocation symbol index used at a given toc offset.  */
   2798       unsigned *symndx;
   2799 
   2800       /* And the relocation addend.  */
   2801       bfd_vma *add;
   2802     } toc;
   2803   } u;
   2804 
   2805   enum _ppc64_sec_type sec_type:2;
   2806 
   2807   /* Flag set when small branches are detected.  Used to
   2808      select suitable defaults for the stub group size.  */
   2809   unsigned int has_14bit_branch:1;
   2810 };
   2811 
   2812 #define ppc64_elf_section_data(sec) \
   2813   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
   2814 
   2815 static bfd_boolean
   2816 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
   2817 {
   2818   if (!sec->used_by_bfd)
   2819     {
   2820       struct _ppc64_elf_section_data *sdata;
   2821       bfd_size_type amt = sizeof (*sdata);
   2822 
   2823       sdata = bfd_zalloc (abfd, amt);
   2824       if (sdata == NULL)
   2825 	return FALSE;
   2826       sec->used_by_bfd = sdata;
   2827     }
   2828 
   2829   return _bfd_elf_new_section_hook (abfd, sec);
   2830 }
   2831 
   2832 static struct _opd_sec_data *
   2833 get_opd_info (asection * sec)
   2834 {
   2835   if (sec != NULL
   2836       && ppc64_elf_section_data (sec) != NULL
   2837       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   2838     return &ppc64_elf_section_data (sec)->u.opd;
   2839   return NULL;
   2840 }
   2841 
   2842 /* Parameters for the qsort hook.  */
   2844 static bfd_boolean synthetic_relocatable;
   2845 
   2846 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
   2847 
   2848 static int
   2849 compare_symbols (const void *ap, const void *bp)
   2850 {
   2851   const asymbol *a = * (const asymbol **) ap;
   2852   const asymbol *b = * (const asymbol **) bp;
   2853 
   2854   /* Section symbols first.  */
   2855   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
   2856     return -1;
   2857   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
   2858     return 1;
   2859 
   2860   /* then .opd symbols.  */
   2861   if (strcmp (a->section->name, ".opd") == 0
   2862       && strcmp (b->section->name, ".opd") != 0)
   2863     return -1;
   2864   if (strcmp (a->section->name, ".opd") != 0
   2865       && strcmp (b->section->name, ".opd") == 0)
   2866     return 1;
   2867 
   2868   /* then other code symbols.  */
   2869   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2870       == (SEC_CODE | SEC_ALLOC)
   2871       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2872 	 != (SEC_CODE | SEC_ALLOC))
   2873     return -1;
   2874 
   2875   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2876       != (SEC_CODE | SEC_ALLOC)
   2877       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2878 	 == (SEC_CODE | SEC_ALLOC))
   2879     return 1;
   2880 
   2881   if (synthetic_relocatable)
   2882     {
   2883       if (a->section->id < b->section->id)
   2884 	return -1;
   2885 
   2886       if (a->section->id > b->section->id)
   2887 	return 1;
   2888     }
   2889 
   2890   if (a->value + a->section->vma < b->value + b->section->vma)
   2891     return -1;
   2892 
   2893   if (a->value + a->section->vma > b->value + b->section->vma)
   2894     return 1;
   2895 
   2896   /* For syms with the same value, prefer strong dynamic global function
   2897      syms over other syms.  */
   2898   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
   2899     return -1;
   2900 
   2901   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
   2902     return 1;
   2903 
   2904   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
   2905     return -1;
   2906 
   2907   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
   2908     return 1;
   2909 
   2910   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
   2911     return -1;
   2912 
   2913   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
   2914     return 1;
   2915 
   2916   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
   2917     return -1;
   2918 
   2919   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
   2920     return 1;
   2921 
   2922   return 0;
   2923 }
   2924 
   2925 /* Search SYMS for a symbol of the given VALUE.  */
   2926 
   2927 static asymbol *
   2928 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
   2929 {
   2930   long mid;
   2931 
   2932   if (id == -1)
   2933     {
   2934       while (lo < hi)
   2935 	{
   2936 	  mid = (lo + hi) >> 1;
   2937 	  if (syms[mid]->value + syms[mid]->section->vma < value)
   2938 	    lo = mid + 1;
   2939 	  else if (syms[mid]->value + syms[mid]->section->vma > value)
   2940 	    hi = mid;
   2941 	  else
   2942 	    return syms[mid];
   2943 	}
   2944     }
   2945   else
   2946     {
   2947       while (lo < hi)
   2948 	{
   2949 	  mid = (lo + hi) >> 1;
   2950 	  if (syms[mid]->section->id < id)
   2951 	    lo = mid + 1;
   2952 	  else if (syms[mid]->section->id > id)
   2953 	    hi = mid;
   2954 	  else if (syms[mid]->value < value)
   2955 	    lo = mid + 1;
   2956 	  else if (syms[mid]->value > value)
   2957 	    hi = mid;
   2958 	  else
   2959 	    return syms[mid];
   2960 	}
   2961     }
   2962   return NULL;
   2963 }
   2964 
   2965 static bfd_boolean
   2966 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   2967 {
   2968   bfd_vma vma = *(bfd_vma *) ptr;
   2969   return ((section->flags & SEC_ALLOC) != 0
   2970 	  && section->vma <= vma
   2971 	  && vma < section->vma + section->size);
   2972 }
   2973 
   2974 /* Create synthetic symbols, effectively restoring "dot-symbol" function
   2975    entry syms.  Also generate @plt symbols for the glink branch table.  */
   2976 
   2977 static long
   2978 ppc64_elf_get_synthetic_symtab (bfd *abfd,
   2979 				long static_count, asymbol **static_syms,
   2980 				long dyn_count, asymbol **dyn_syms,
   2981 				asymbol **ret)
   2982 {
   2983   asymbol *s;
   2984   long i;
   2985   long count;
   2986   char *names;
   2987   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
   2988   asection *opd;
   2989   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   2990   asymbol **syms;
   2991 
   2992   *ret = NULL;
   2993 
   2994   opd = bfd_get_section_by_name (abfd, ".opd");
   2995   if (opd == NULL)
   2996     return 0;
   2997 
   2998   symcount = static_count;
   2999   if (!relocatable)
   3000     symcount += dyn_count;
   3001   if (symcount == 0)
   3002     return 0;
   3003 
   3004   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
   3005   if (syms == NULL)
   3006     return -1;
   3007 
   3008   if (!relocatable && static_count != 0 && dyn_count != 0)
   3009     {
   3010       /* Use both symbol tables.  */
   3011       memcpy (syms, static_syms, static_count * sizeof (*syms));
   3012       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
   3013     }
   3014   else if (!relocatable && static_count == 0)
   3015     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
   3016   else
   3017     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
   3018 
   3019   synthetic_relocatable = relocatable;
   3020   qsort (syms, symcount, sizeof (*syms), compare_symbols);
   3021 
   3022   if (!relocatable && symcount > 1)
   3023     {
   3024       long j;
   3025       /* Trim duplicate syms, since we may have merged the normal and
   3026 	 dynamic symbols.  Actually, we only care about syms that have
   3027 	 different values, so trim any with the same value.  */
   3028       for (i = 1, j = 1; i < symcount; ++i)
   3029 	if (syms[i - 1]->value + syms[i - 1]->section->vma
   3030 	    != syms[i]->value + syms[i]->section->vma)
   3031 	  syms[j++] = syms[i];
   3032       symcount = j;
   3033     }
   3034 
   3035   i = 0;
   3036   if (strcmp (syms[i]->section->name, ".opd") == 0)
   3037     ++i;
   3038   codesecsym = i;
   3039 
   3040   for (; i < symcount; ++i)
   3041     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3042 	 != (SEC_CODE | SEC_ALLOC))
   3043 	|| (syms[i]->flags & BSF_SECTION_SYM) == 0)
   3044       break;
   3045   codesecsymend = i;
   3046 
   3047   for (; i < symcount; ++i)
   3048     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
   3049       break;
   3050   secsymend = i;
   3051 
   3052   for (; i < symcount; ++i)
   3053     if (strcmp (syms[i]->section->name, ".opd") != 0)
   3054       break;
   3055   opdsymend = i;
   3056 
   3057   for (; i < symcount; ++i)
   3058     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3059 	!= (SEC_CODE | SEC_ALLOC))
   3060       break;
   3061   symcount = i;
   3062 
   3063   count = 0;
   3064 
   3065   if (relocatable)
   3066     {
   3067       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3068       arelent *r;
   3069       size_t size;
   3070       long relcount;
   3071 
   3072       if (opdsymend == secsymend)
   3073 	goto done;
   3074 
   3075       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3076       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
   3077       if (relcount == 0)
   3078 	goto done;
   3079 
   3080       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
   3081 	{
   3082 	  count = -1;
   3083 	  goto done;
   3084 	}
   3085 
   3086       size = 0;
   3087       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3088 	{
   3089 	  asymbol *sym;
   3090 
   3091 	  while (r < opd->relocation + relcount
   3092 		 && r->address < syms[i]->value + opd->vma)
   3093 	    ++r;
   3094 
   3095 	  if (r == opd->relocation + relcount)
   3096 	    break;
   3097 
   3098 	  if (r->address != syms[i]->value + opd->vma)
   3099 	    continue;
   3100 
   3101 	  if (r->howto->type != R_PPC64_ADDR64)
   3102 	    continue;
   3103 
   3104 	  sym = *r->sym_ptr_ptr;
   3105 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3106 			      sym->section->id, sym->value + r->addend))
   3107 	    {
   3108 	      ++count;
   3109 	      size += sizeof (asymbol);
   3110 	      size += strlen (syms[i]->name) + 2;
   3111 	    }
   3112 	}
   3113 
   3114       s = *ret = bfd_malloc (size);
   3115       if (s == NULL)
   3116 	{
   3117 	  count = -1;
   3118 	  goto done;
   3119 	}
   3120 
   3121       names = (char *) (s + count);
   3122 
   3123       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3124 	{
   3125 	  asymbol *sym;
   3126 
   3127 	  while (r < opd->relocation + relcount
   3128 		 && r->address < syms[i]->value + opd->vma)
   3129 	    ++r;
   3130 
   3131 	  if (r == opd->relocation + relcount)
   3132 	    break;
   3133 
   3134 	  if (r->address != syms[i]->value + opd->vma)
   3135 	    continue;
   3136 
   3137 	  if (r->howto->type != R_PPC64_ADDR64)
   3138 	    continue;
   3139 
   3140 	  sym = *r->sym_ptr_ptr;
   3141 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3142 			      sym->section->id, sym->value + r->addend))
   3143 	    {
   3144 	      size_t len;
   3145 
   3146 	      *s = *syms[i];
   3147 	      s->flags |= BSF_SYNTHETIC;
   3148 	      s->section = sym->section;
   3149 	      s->value = sym->value + r->addend;
   3150 	      s->name = names;
   3151 	      *names++ = '.';
   3152 	      len = strlen (syms[i]->name);
   3153 	      memcpy (names, syms[i]->name, len + 1);
   3154 	      names += len + 1;
   3155 	      /* Have udata.p point back to the original symbol this
   3156 		 synthetic symbol was derived from.  */
   3157 	      s->udata.p = syms[i];
   3158 	      s++;
   3159 	    }
   3160 	}
   3161     }
   3162   else
   3163     {
   3164       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3165       bfd_byte *contents;
   3166       size_t size;
   3167       long plt_count = 0;
   3168       bfd_vma glink_vma = 0, resolv_vma = 0;
   3169       asection *dynamic, *glink = NULL, *relplt = NULL;
   3170       arelent *p;
   3171 
   3172       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
   3173 	{
   3174 	  if (contents)
   3175 	    {
   3176 	    free_contents_and_exit:
   3177 	      free (contents);
   3178 	    }
   3179 	  count = -1;
   3180 	  goto done;
   3181 	}
   3182 
   3183       size = 0;
   3184       for (i = secsymend; i < opdsymend; ++i)
   3185 	{
   3186 	  bfd_vma ent;
   3187 
   3188 	  /* Ignore bogus symbols.  */
   3189 	  if (syms[i]->value > opd->size - 8)
   3190 	    continue;
   3191 
   3192 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3193 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3194 	    {
   3195 	      ++count;
   3196 	      size += sizeof (asymbol);
   3197 	      size += strlen (syms[i]->name) + 2;
   3198 	    }
   3199 	}
   3200 
   3201       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
   3202       if (dyn_count != 0
   3203 	  && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
   3204 	{
   3205 	  bfd_byte *dynbuf, *extdyn, *extdynend;
   3206 	  size_t extdynsize;
   3207 	  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3208 
   3209 	  if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   3210 	    goto free_contents_and_exit;
   3211 
   3212 	  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   3213 	  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   3214 
   3215 	  extdyn = dynbuf;
   3216 	  extdynend = extdyn + dynamic->size;
   3217 	  for (; extdyn < extdynend; extdyn += extdynsize)
   3218 	    {
   3219 	      Elf_Internal_Dyn dyn;
   3220 	      (*swap_dyn_in) (abfd, extdyn, &dyn);
   3221 
   3222 	      if (dyn.d_tag == DT_NULL)
   3223 		break;
   3224 
   3225 	      if (dyn.d_tag == DT_PPC64_GLINK)
   3226 		{
   3227 		  /* The first glink stub starts at offset 32; see comment in
   3228 		     ppc64_elf_finish_dynamic_sections. */
   3229 		  glink_vma = dyn.d_un.d_val + 32;
   3230 		  /* The .glink section usually does not survive the final
   3231 		     link; search for the section (usually .text) where the
   3232 		     glink stubs now reside.  */
   3233 		  glink = bfd_sections_find_if (abfd, section_covers_vma,
   3234 						&glink_vma);
   3235 		  break;
   3236 		}
   3237 	    }
   3238 
   3239 	  free (dynbuf);
   3240 	}
   3241 
   3242       if (glink != NULL)
   3243 	{
   3244 	  /* Determine __glink trampoline by reading the relative branch
   3245 	     from the first glink stub.  */
   3246 	  bfd_byte buf[4];
   3247 	  if (bfd_get_section_contents (abfd, glink, buf,
   3248 					glink_vma + 4 - glink->vma, 4))
   3249 	    {
   3250 	      unsigned int insn = bfd_get_32 (abfd, buf);
   3251 	      insn ^= B_DOT;
   3252 	      if ((insn & ~0x3fffffc) == 0)
   3253 		resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
   3254 	    }
   3255 
   3256 	  if (resolv_vma)
   3257 	    size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   3258 
   3259 	  relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   3260 	  if (relplt != NULL)
   3261 	    {
   3262 	      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3263 	      if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
   3264 		goto free_contents_and_exit;
   3265 
   3266 	      plt_count = relplt->size / sizeof (Elf64_External_Rela);
   3267 	      size += plt_count * sizeof (asymbol);
   3268 
   3269 	      p = relplt->relocation;
   3270 	      for (i = 0; i < plt_count; i++, p++)
   3271 		{
   3272 		  size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   3273 		  if (p->addend != 0)
   3274 		    size += sizeof ("+0x") - 1 + 16;
   3275 		}
   3276 	    }
   3277 	}
   3278 
   3279       s = *ret = bfd_malloc (size);
   3280       if (s == NULL)
   3281 	goto free_contents_and_exit;
   3282 
   3283       names = (char *) (s + count + plt_count + (resolv_vma != 0));
   3284 
   3285       for (i = secsymend; i < opdsymend; ++i)
   3286 	{
   3287 	  bfd_vma ent;
   3288 
   3289 	  if (syms[i]->value > opd->size - 8)
   3290 	    continue;
   3291 
   3292 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3293 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3294 	    {
   3295 	      long lo, hi;
   3296 	      size_t len;
   3297 	      asection *sec = abfd->sections;
   3298 
   3299 	      *s = *syms[i];
   3300 	      lo = codesecsym;
   3301 	      hi = codesecsymend;
   3302 	      while (lo < hi)
   3303 		{
   3304 		  long mid = (lo + hi) >> 1;
   3305 		  if (syms[mid]->section->vma < ent)
   3306 		    lo = mid + 1;
   3307 		  else if (syms[mid]->section->vma > ent)
   3308 		    hi = mid;
   3309 		  else
   3310 		    {
   3311 		      sec = syms[mid]->section;
   3312 		      break;
   3313 		    }
   3314 		}
   3315 
   3316 	      if (lo >= hi && lo > codesecsym)
   3317 		sec = syms[lo - 1]->section;
   3318 
   3319 	      for (; sec != NULL; sec = sec->next)
   3320 		{
   3321 		  if (sec->vma > ent)
   3322 		    break;
   3323 		  /* SEC_LOAD may not be set if SEC is from a separate debug
   3324 		     info file.  */
   3325 		  if ((sec->flags & SEC_ALLOC) == 0)
   3326 		    break;
   3327 		  if ((sec->flags & SEC_CODE) != 0)
   3328 		    s->section = sec;
   3329 		}
   3330 	      s->flags |= BSF_SYNTHETIC;
   3331 	      s->value = ent - s->section->vma;
   3332 	      s->name = names;
   3333 	      *names++ = '.';
   3334 	      len = strlen (syms[i]->name);
   3335 	      memcpy (names, syms[i]->name, len + 1);
   3336 	      names += len + 1;
   3337 	      /* Have udata.p point back to the original symbol this
   3338 		 synthetic symbol was derived from.  */
   3339 	      s->udata.p = syms[i];
   3340 	      s++;
   3341 	    }
   3342 	}
   3343       free (contents);
   3344 
   3345       if (glink != NULL && relplt != NULL)
   3346 	{
   3347 	  if (resolv_vma)
   3348 	    {
   3349 	      /* Add a symbol for the main glink trampoline.  */
   3350 	      memset (s, 0, sizeof *s);
   3351 	      s->the_bfd = abfd;
   3352 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   3353 	      s->section = glink;
   3354 	      s->value = resolv_vma - glink->vma;
   3355 	      s->name = names;
   3356 	      memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   3357 	      names += sizeof ("__glink_PLTresolve");
   3358 	      s++;
   3359 	      count++;
   3360 	    }
   3361 
   3362 	  /* FIXME: It would be very much nicer to put sym@plt on the
   3363 	     stub rather than on the glink branch table entry.  The
   3364 	     objdump disassembler would then use a sensible symbol
   3365 	     name on plt calls.  The difficulty in doing so is
   3366 	     a) finding the stubs, and,
   3367 	     b) matching stubs against plt entries, and,
   3368 	     c) there can be multiple stubs for a given plt entry.
   3369 
   3370 	     Solving (a) could be done by code scanning, but older
   3371 	     ppc64 binaries used different stubs to current code.
   3372 	     (b) is the tricky one since you need to known the toc
   3373 	     pointer for at least one function that uses a pic stub to
   3374 	     be able to calculate the plt address referenced.
   3375 	     (c) means gdb would need to set multiple breakpoints (or
   3376 	     find the glink branch itself) when setting breakpoints
   3377 	     for pending shared library loads.  */
   3378 	  p = relplt->relocation;
   3379 	  for (i = 0; i < plt_count; i++, p++)
   3380 	    {
   3381 	      size_t len;
   3382 
   3383 	      *s = **p->sym_ptr_ptr;
   3384 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   3385 		 we are defining a symbol, ensure one of them is set.  */
   3386 	      if ((s->flags & BSF_LOCAL) == 0)
   3387 		s->flags |= BSF_GLOBAL;
   3388 	      s->flags |= BSF_SYNTHETIC;
   3389 	      s->section = glink;
   3390 	      s->value = glink_vma - glink->vma;
   3391 	      s->name = names;
   3392 	      s->udata.p = NULL;
   3393 	      len = strlen ((*p->sym_ptr_ptr)->name);
   3394 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   3395 	      names += len;
   3396 	      if (p->addend != 0)
   3397 		{
   3398 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   3399 		  names += sizeof ("+0x") - 1;
   3400 		  bfd_sprintf_vma (abfd, names, p->addend);
   3401 		  names += strlen (names);
   3402 		}
   3403 	      memcpy (names, "@plt", sizeof ("@plt"));
   3404 	      names += sizeof ("@plt");
   3405 	      s++;
   3406 	      glink_vma += 8;
   3407 	      if (i >= 0x8000)
   3408 		glink_vma += 4;
   3409 	    }
   3410 	  count += plt_count;
   3411 	}
   3412     }
   3413 
   3414  done:
   3415   free (syms);
   3416   return count;
   3417 }
   3418 
   3419 /* The following functions are specific to the ELF linker, while
   3421    functions above are used generally.  Those named ppc64_elf_* are
   3422    called by the main ELF linker code.  They appear in this file more
   3423    or less in the order in which they are called.  eg.
   3424    ppc64_elf_check_relocs is called early in the link process,
   3425    ppc64_elf_finish_dynamic_sections is one of the last functions
   3426    called.
   3427 
   3428    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   3429    functions have both a function code symbol and a function descriptor
   3430    symbol.  A call to foo in a relocatable object file looks like:
   3431 
   3432    .		.text
   3433    .	x:
   3434    .		bl	.foo
   3435    .		nop
   3436 
   3437    The function definition in another object file might be:
   3438 
   3439    .		.section .opd
   3440    .	foo:	.quad	.foo
   3441    .		.quad	.TOC.@tocbase
   3442    .		.quad	0
   3443    .
   3444    .		.text
   3445    .	.foo:	blr
   3446 
   3447    When the linker resolves the call during a static link, the branch
   3448    unsurprisingly just goes to .foo and the .opd information is unused.
   3449    If the function definition is in a shared library, things are a little
   3450    different:  The call goes via a plt call stub, the opd information gets
   3451    copied to the plt, and the linker patches the nop.
   3452 
   3453    .	x:
   3454    .		bl	.foo_stub
   3455    .		ld	2,40(1)
   3456    .
   3457    .
   3458    .	.foo_stub:
   3459    .		addis	12,2,Lfoo@toc@ha	# in practice, the call stub
   3460    .		addi	12,12,Lfoo@toc@l	# is slightly optimized, but
   3461    .		std	2,40(1)			# this is the general idea
   3462    .		ld	11,0(12)
   3463    .		ld	2,8(12)
   3464    .		mtctr	11
   3465    .		ld	11,16(12)
   3466    .		bctr
   3467    .
   3468    .		.section .plt
   3469    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   3470 
   3471    The "reloc ()" notation is supposed to indicate that the linker emits
   3472    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   3473    copying.
   3474 
   3475    What are the difficulties here?  Well, firstly, the relocations
   3476    examined by the linker in check_relocs are against the function code
   3477    sym .foo, while the dynamic relocation in the plt is emitted against
   3478    the function descriptor symbol, foo.  Somewhere along the line, we need
   3479    to carefully copy dynamic link information from one symbol to the other.
   3480    Secondly, the generic part of the elf linker will make .foo a dynamic
   3481    symbol as is normal for most other backends.  We need foo dynamic
   3482    instead, at least for an application final link.  However, when
   3483    creating a shared library containing foo, we need to have both symbols
   3484    dynamic so that references to .foo are satisfied during the early
   3485    stages of linking.  Otherwise the linker might decide to pull in a
   3486    definition from some other object, eg. a static library.
   3487 
   3488    Update: As of August 2004, we support a new convention.  Function
   3489    calls may use the function descriptor symbol, ie. "bl foo".  This
   3490    behaves exactly as "bl .foo".  */
   3491 
   3492 /* Of those relocs that might be copied as dynamic relocs, this function
   3493    selects those that must be copied when linking a shared library,
   3494    even when the symbol is local.  */
   3495 
   3496 static int
   3497 must_be_dyn_reloc (struct bfd_link_info *info,
   3498 		   enum elf_ppc64_reloc_type r_type)
   3499 {
   3500   switch (r_type)
   3501     {
   3502     default:
   3503       return 1;
   3504 
   3505     case R_PPC64_REL32:
   3506     case R_PPC64_REL64:
   3507     case R_PPC64_REL30:
   3508       return 0;
   3509 
   3510     case R_PPC64_TPREL16:
   3511     case R_PPC64_TPREL16_LO:
   3512     case R_PPC64_TPREL16_HI:
   3513     case R_PPC64_TPREL16_HA:
   3514     case R_PPC64_TPREL16_DS:
   3515     case R_PPC64_TPREL16_LO_DS:
   3516     case R_PPC64_TPREL16_HIGHER:
   3517     case R_PPC64_TPREL16_HIGHERA:
   3518     case R_PPC64_TPREL16_HIGHEST:
   3519     case R_PPC64_TPREL16_HIGHESTA:
   3520     case R_PPC64_TPREL64:
   3521       return !info->executable;
   3522     }
   3523 }
   3524 
   3525 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   3526    copying dynamic variables from a shared lib into an app's dynbss
   3527    section, and instead use a dynamic relocation to point into the
   3528    shared lib.  With code that gcc generates, it's vital that this be
   3529    enabled;  In the PowerPC64 ABI, the address of a function is actually
   3530    the address of a function descriptor, which resides in the .opd
   3531    section.  gcc uses the descriptor directly rather than going via the
   3532    GOT as some other ABI's do, which means that initialized function
   3533    pointers must reference the descriptor.  Thus, a function pointer
   3534    initialized to the address of a function in a shared library will
   3535    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
   3536    redefines the function descriptor symbol to point to the copy.  This
   3537    presents a problem as a plt entry for that function is also
   3538    initialized from the function descriptor symbol and the copy reloc
   3539    may not be initialized first.  */
   3540 #define ELIMINATE_COPY_RELOCS 1
   3541 
   3542 /* Section name for stubs is the associated section name plus this
   3543    string.  */
   3544 #define STUB_SUFFIX ".stub"
   3545 
   3546 /* Linker stubs.
   3547    ppc_stub_long_branch:
   3548    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   3549    destination, but a 24 bit branch in a stub section will reach.
   3550    .	b	dest
   3551 
   3552    ppc_stub_plt_branch:
   3553    Similar to the above, but a 24 bit branch in the stub section won't
   3554    reach its destination.
   3555    .	addis	%r12,%r2,xxx@toc@ha
   3556    .	ld	%r11,xxx@toc@l(%r12)
   3557    .	mtctr	%r11
   3558    .	bctr
   3559 
   3560    ppc_stub_plt_call:
   3561    Used to call a function in a shared library.  If it so happens that
   3562    the plt entry referenced crosses a 64k boundary, then an extra
   3563    "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
   3564    .	addis	%r12,%r2,xxx@toc@ha
   3565    .	std	%r2,40(%r1)
   3566    .	ld	%r11,xxx+0@toc@l(%r12)
   3567    .	mtctr	%r11
   3568    .	ld	%r2,xxx+8@toc@l(%r12)
   3569    .	ld	%r11,xxx+16@toc@l(%r12)
   3570    .	bctr
   3571 
   3572    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   3573    code to adjust the value and save r2 to support multiple toc sections.
   3574    A ppc_stub_long_branch with an r2 offset looks like:
   3575    .	std	%r2,40(%r1)
   3576    .	addis	%r2,%r2,off@ha
   3577    .	addi	%r2,%r2,off@l
   3578    .	b	dest
   3579 
   3580    A ppc_stub_plt_branch with an r2 offset looks like:
   3581    .	std	%r2,40(%r1)
   3582    .	addis	%r12,%r2,xxx@toc@ha
   3583    .	ld	%r11,xxx@toc@l(%r12)
   3584    .	addis	%r2,%r2,off@ha
   3585    .	addi	%r2,%r2,off@l
   3586    .	mtctr	%r11
   3587    .	bctr
   3588 
   3589    In cases where the "addis" instruction would add zero, the "addis" is
   3590    omitted and following instructions modified slightly in some cases.
   3591 */
   3592 
   3593 enum ppc_stub_type {
   3594   ppc_stub_none,
   3595   ppc_stub_long_branch,
   3596   ppc_stub_long_branch_r2off,
   3597   ppc_stub_plt_branch,
   3598   ppc_stub_plt_branch_r2off,
   3599   ppc_stub_plt_call,
   3600   ppc_stub_plt_call_r2save
   3601 };
   3602 
   3603 struct ppc_stub_hash_entry {
   3604 
   3605   /* Base hash table entry structure.  */
   3606   struct bfd_hash_entry root;
   3607 
   3608   enum ppc_stub_type stub_type;
   3609 
   3610   /* The stub section.  */
   3611   asection *stub_sec;
   3612 
   3613   /* Offset within stub_sec of the beginning of this stub.  */
   3614   bfd_vma stub_offset;
   3615 
   3616   /* Given the symbol's value and its section we can determine its final
   3617      value when building the stubs (so the stub knows where to jump.  */
   3618   bfd_vma target_value;
   3619   asection *target_section;
   3620 
   3621   /* The symbol table entry, if any, that this was derived from.  */
   3622   struct ppc_link_hash_entry *h;
   3623   struct plt_entry *plt_ent;
   3624 
   3625   /* And the reloc addend that this was derived from.  */
   3626   bfd_vma addend;
   3627 
   3628   /* Where this stub is being called from, or, in the case of combined
   3629      stub sections, the first input section in the group.  */
   3630   asection *id_sec;
   3631 };
   3632 
   3633 struct ppc_branch_hash_entry {
   3634 
   3635   /* Base hash table entry structure.  */
   3636   struct bfd_hash_entry root;
   3637 
   3638   /* Offset within branch lookup table.  */
   3639   unsigned int offset;
   3640 
   3641   /* Generation marker.  */
   3642   unsigned int iter;
   3643 };
   3644 
   3645 struct ppc_link_hash_entry
   3646 {
   3647   struct elf_link_hash_entry elf;
   3648 
   3649   union {
   3650     /* A pointer to the most recently used stub hash entry against this
   3651        symbol.  */
   3652     struct ppc_stub_hash_entry *stub_cache;
   3653 
   3654     /* A pointer to the next symbol starting with a '.'  */
   3655     struct ppc_link_hash_entry *next_dot_sym;
   3656   } u;
   3657 
   3658   /* Track dynamic relocs copied for this symbol.  */
   3659   struct elf_dyn_relocs *dyn_relocs;
   3660 
   3661   /* Link between function code and descriptor symbols.  */
   3662   struct ppc_link_hash_entry *oh;
   3663 
   3664   /* Flag function code and descriptor symbols.  */
   3665   unsigned int is_func:1;
   3666   unsigned int is_func_descriptor:1;
   3667   unsigned int fake:1;
   3668 
   3669   /* Whether global opd/toc sym has been adjusted or not.
   3670      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3671      should be set for all globals defined in any opd/toc section.  */
   3672   unsigned int adjust_done:1;
   3673 
   3674   /* Set if we twiddled this symbol to weak at some stage.  */
   3675   unsigned int was_undefined:1;
   3676 
   3677   /* Contexts in which symbol is used in the GOT (or TOC).
   3678      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
   3679      corresponding relocs are encountered during check_relocs.
   3680      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
   3681      indicate the corresponding GOT entry type is not needed.
   3682      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
   3683      a TPREL one.  We use a separate flag rather than setting TPREL
   3684      just for convenience in distinguishing the two cases.  */
   3685 #define TLS_GD		 1	/* GD reloc. */
   3686 #define TLS_LD		 2	/* LD reloc. */
   3687 #define TLS_TPREL	 4	/* TPREL reloc, => IE. */
   3688 #define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
   3689 #define TLS_TLS		16	/* Any TLS reloc.  */
   3690 #define TLS_EXPLICIT	32	/* Marks TOC section TLS relocs. */
   3691 #define TLS_TPRELGD	64	/* TPREL reloc resulting from GD->IE. */
   3692 #define PLT_IFUNC      128	/* STT_GNU_IFUNC.  */
   3693   unsigned char tls_mask;
   3694 };
   3695 
   3696 /* ppc64 ELF linker hash table.  */
   3697 
   3698 struct ppc_link_hash_table
   3699 {
   3700   struct elf_link_hash_table elf;
   3701 
   3702   /* The stub hash table.  */
   3703   struct bfd_hash_table stub_hash_table;
   3704 
   3705   /* Another hash table for plt_branch stubs.  */
   3706   struct bfd_hash_table branch_hash_table;
   3707 
   3708   /* Hash table for function prologue tocsave.  */
   3709   htab_t tocsave_htab;
   3710 
   3711   /* Linker stub bfd.  */
   3712   bfd *stub_bfd;
   3713 
   3714   /* Linker call-backs.  */
   3715   asection * (*add_stub_section) (const char *, asection *);
   3716   void (*layout_sections_again) (void);
   3717 
   3718   /* Array to keep track of which stub sections have been created, and
   3719      information on stub grouping.  */
   3720   struct map_stub {
   3721     /* This is the section to which stubs in the group will be attached.  */
   3722     asection *link_sec;
   3723     /* The stub section.  */
   3724     asection *stub_sec;
   3725     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
   3726     bfd_vma toc_off;
   3727   } *stub_group;
   3728 
   3729   /* Temp used when calculating TOC pointers.  */
   3730   bfd_vma toc_curr;
   3731   bfd *toc_bfd;
   3732   asection *toc_first_sec;
   3733 
   3734   /* Highest input section id.  */
   3735   int top_id;
   3736 
   3737   /* Highest output section index.  */
   3738   int top_index;
   3739 
   3740   /* Used when adding symbols.  */
   3741   struct ppc_link_hash_entry *dot_syms;
   3742 
   3743   /* List of input sections for each output section.  */
   3744   asection **input_list;
   3745 
   3746   /* Short-cuts to get to dynamic linker sections.  */
   3747   asection *got;
   3748   asection *plt;
   3749   asection *relplt;
   3750   asection *iplt;
   3751   asection *reliplt;
   3752   asection *dynbss;
   3753   asection *relbss;
   3754   asection *glink;
   3755   asection *sfpr;
   3756   asection *brlt;
   3757   asection *relbrlt;
   3758   asection *glink_eh_frame;
   3759 
   3760   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   3761   struct ppc_link_hash_entry *tls_get_addr;
   3762   struct ppc_link_hash_entry *tls_get_addr_fd;
   3763 
   3764   /* The special .TOC. symbol.  */
   3765   struct ppc_link_hash_entry *dot_toc_dot;
   3766 
   3767   /* The size of reliplt used by got entry relocs.  */
   3768   bfd_size_type got_reli_size;
   3769 
   3770   /* Statistics.  */
   3771   unsigned long stub_count[ppc_stub_plt_call_r2save];
   3772 
   3773   /* Number of stubs against global syms.  */
   3774   unsigned long stub_globals;
   3775 
   3776   /* Alignment of PLT call stubs.  */
   3777   unsigned int plt_stub_align:4;
   3778 
   3779   /* Set if PLT call stubs should load r11.  */
   3780   unsigned int plt_static_chain:1;
   3781 
   3782   /* Set if PLT call stubs need a read-read barrier.  */
   3783   unsigned int plt_thread_safe:1;
   3784 
   3785   /* Set if we should emit symbols for stubs.  */
   3786   unsigned int emit_stub_syms:1;
   3787 
   3788   /* Set if __tls_get_addr optimization should not be done.  */
   3789   unsigned int no_tls_get_addr_opt:1;
   3790 
   3791   /* Support for multiple toc sections.  */
   3792   unsigned int do_multi_toc:1;
   3793   unsigned int multi_toc_needed:1;
   3794   unsigned int second_toc_pass:1;
   3795   unsigned int do_toc_opt:1;
   3796 
   3797   /* Set on error.  */
   3798   unsigned int stub_error:1;
   3799 
   3800   /* Temp used by ppc64_elf_process_dot_syms.  */
   3801   unsigned int twiddled_syms:1;
   3802 
   3803   /* Incremented every time we size stubs.  */
   3804   unsigned int stub_iteration;
   3805 
   3806   /* Small local sym cache.  */
   3807   struct sym_cache sym_cache;
   3808 };
   3809 
   3810 /* Rename some of the generic section flags to better document how they
   3811    are used here.  */
   3812 
   3813 /* Nonzero if this section has TLS related relocations.  */
   3814 #define has_tls_reloc sec_flg0
   3815 
   3816 /* Nonzero if this section has a call to __tls_get_addr.  */
   3817 #define has_tls_get_addr_call sec_flg1
   3818 
   3819 /* Nonzero if this section has any toc or got relocs.  */
   3820 #define has_toc_reloc sec_flg2
   3821 
   3822 /* Nonzero if this section has a call to another section that uses
   3823    the toc or got.  */
   3824 #define makes_toc_func_call sec_flg3
   3825 
   3826 /* Recursion protection when determining above flag.  */
   3827 #define call_check_in_progress sec_flg4
   3828 #define call_check_done sec_flg5
   3829 
   3830 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   3831 
   3832 #define ppc_hash_table(p) \
   3833   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
   3834   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
   3835 
   3836 #define ppc_stub_hash_lookup(table, string, create, copy) \
   3837   ((struct ppc_stub_hash_entry *) \
   3838    bfd_hash_lookup ((table), (string), (create), (copy)))
   3839 
   3840 #define ppc_branch_hash_lookup(table, string, create, copy) \
   3841   ((struct ppc_branch_hash_entry *) \
   3842    bfd_hash_lookup ((table), (string), (create), (copy)))
   3843 
   3844 /* Create an entry in the stub hash table.  */
   3845 
   3846 static struct bfd_hash_entry *
   3847 stub_hash_newfunc (struct bfd_hash_entry *entry,
   3848 		   struct bfd_hash_table *table,
   3849 		   const char *string)
   3850 {
   3851   /* Allocate the structure if it has not already been allocated by a
   3852      subclass.  */
   3853   if (entry == NULL)
   3854     {
   3855       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   3856       if (entry == NULL)
   3857 	return entry;
   3858     }
   3859 
   3860   /* Call the allocation method of the superclass.  */
   3861   entry = bfd_hash_newfunc (entry, table, string);
   3862   if (entry != NULL)
   3863     {
   3864       struct ppc_stub_hash_entry *eh;
   3865 
   3866       /* Initialize the local fields.  */
   3867       eh = (struct ppc_stub_hash_entry *) entry;
   3868       eh->stub_type = ppc_stub_none;
   3869       eh->stub_sec = NULL;
   3870       eh->stub_offset = 0;
   3871       eh->target_value = 0;
   3872       eh->target_section = NULL;
   3873       eh->h = NULL;
   3874       eh->id_sec = NULL;
   3875     }
   3876 
   3877   return entry;
   3878 }
   3879 
   3880 /* Create an entry in the branch hash table.  */
   3881 
   3882 static struct bfd_hash_entry *
   3883 branch_hash_newfunc (struct bfd_hash_entry *entry,
   3884 		     struct bfd_hash_table *table,
   3885 		     const char *string)
   3886 {
   3887   /* Allocate the structure if it has not already been allocated by a
   3888      subclass.  */
   3889   if (entry == NULL)
   3890     {
   3891       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   3892       if (entry == NULL)
   3893 	return entry;
   3894     }
   3895 
   3896   /* Call the allocation method of the superclass.  */
   3897   entry = bfd_hash_newfunc (entry, table, string);
   3898   if (entry != NULL)
   3899     {
   3900       struct ppc_branch_hash_entry *eh;
   3901 
   3902       /* Initialize the local fields.  */
   3903       eh = (struct ppc_branch_hash_entry *) entry;
   3904       eh->offset = 0;
   3905       eh->iter = 0;
   3906     }
   3907 
   3908   return entry;
   3909 }
   3910 
   3911 /* Create an entry in a ppc64 ELF linker hash table.  */
   3912 
   3913 static struct bfd_hash_entry *
   3914 link_hash_newfunc (struct bfd_hash_entry *entry,
   3915 		   struct bfd_hash_table *table,
   3916 		   const char *string)
   3917 {
   3918   /* Allocate the structure if it has not already been allocated by a
   3919      subclass.  */
   3920   if (entry == NULL)
   3921     {
   3922       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   3923       if (entry == NULL)
   3924 	return entry;
   3925     }
   3926 
   3927   /* Call the allocation method of the superclass.  */
   3928   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   3929   if (entry != NULL)
   3930     {
   3931       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   3932 
   3933       memset (&eh->u.stub_cache, 0,
   3934 	      (sizeof (struct ppc_link_hash_entry)
   3935 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   3936 
   3937       /* When making function calls, old ABI code references function entry
   3938 	 points (dot symbols), while new ABI code references the function
   3939 	 descriptor symbol.  We need to make any combination of reference and
   3940 	 definition work together, without breaking archive linking.
   3941 
   3942 	 For a defined function "foo" and an undefined call to "bar":
   3943 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   3944 	 "bar" too).
   3945 	 A new object defines "foo" and references "bar".
   3946 
   3947 	 A new object thus has no problem with its undefined symbols being
   3948 	 satisfied by definitions in an old object.  On the other hand, the
   3949 	 old object won't have ".bar" satisfied by a new object.
   3950 
   3951 	 Keep a list of newly added dot-symbols.  */
   3952 
   3953       if (string[0] == '.')
   3954 	{
   3955 	  struct ppc_link_hash_table *htab;
   3956 
   3957 	  htab = (struct ppc_link_hash_table *) table;
   3958 	  eh->u.next_dot_sym = htab->dot_syms;
   3959 	  htab->dot_syms = eh;
   3960 	}
   3961     }
   3962 
   3963   return entry;
   3964 }
   3965 
   3966 struct tocsave_entry {
   3967   asection *sec;
   3968   bfd_vma offset;
   3969 };
   3970 
   3971 static hashval_t
   3972 tocsave_htab_hash (const void *p)
   3973 {
   3974   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
   3975   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
   3976 }
   3977 
   3978 static int
   3979 tocsave_htab_eq (const void *p1, const void *p2)
   3980 {
   3981   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
   3982   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
   3983   return e1->sec == e2->sec && e1->offset == e2->offset;
   3984 }
   3985 
   3986 /* Create a ppc64 ELF linker hash table.  */
   3987 
   3988 static struct bfd_link_hash_table *
   3989 ppc64_elf_link_hash_table_create (bfd *abfd)
   3990 {
   3991   struct ppc_link_hash_table *htab;
   3992   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
   3993 
   3994   htab = bfd_zmalloc (amt);
   3995   if (htab == NULL)
   3996     return NULL;
   3997 
   3998   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   3999 				      sizeof (struct ppc_link_hash_entry),
   4000 				      PPC64_ELF_DATA))
   4001     {
   4002       free (htab);
   4003       return NULL;
   4004     }
   4005 
   4006   /* Init the stub hash table too.  */
   4007   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   4008 			    sizeof (struct ppc_stub_hash_entry)))
   4009     return NULL;
   4010 
   4011   /* And the branch hash table.  */
   4012   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   4013 			    sizeof (struct ppc_branch_hash_entry)))
   4014     return NULL;
   4015 
   4016   htab->tocsave_htab = htab_try_create (1024,
   4017 					tocsave_htab_hash,
   4018 					tocsave_htab_eq,
   4019 					NULL);
   4020   if (htab->tocsave_htab == NULL)
   4021     return NULL;
   4022 
   4023   /* Initializing two fields of the union is just cosmetic.  We really
   4024      only care about glist, but when compiled on a 32-bit host the
   4025      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   4026      debugger inspection of these fields look nicer.  */
   4027   htab->elf.init_got_refcount.refcount = 0;
   4028   htab->elf.init_got_refcount.glist = NULL;
   4029   htab->elf.init_plt_refcount.refcount = 0;
   4030   htab->elf.init_plt_refcount.glist = NULL;
   4031   htab->elf.init_got_offset.offset = 0;
   4032   htab->elf.init_got_offset.glist = NULL;
   4033   htab->elf.init_plt_offset.offset = 0;
   4034   htab->elf.init_plt_offset.glist = NULL;
   4035 
   4036   return &htab->elf.root;
   4037 }
   4038 
   4039 /* Free the derived linker hash table.  */
   4040 
   4041 static void
   4042 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
   4043 {
   4044   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
   4045 
   4046   bfd_hash_table_free (&htab->stub_hash_table);
   4047   bfd_hash_table_free (&htab->branch_hash_table);
   4048   if (htab->tocsave_htab)
   4049     htab_delete (htab->tocsave_htab);
   4050   _bfd_elf_link_hash_table_free (hash);
   4051 }
   4052 
   4053 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   4054 
   4055 void
   4056 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
   4057 {
   4058   struct ppc_link_hash_table *htab;
   4059 
   4060   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
   4061 
   4062 /* Always hook our dynamic sections into the first bfd, which is the
   4063    linker created stub bfd.  This ensures that the GOT header is at
   4064    the start of the output TOC section.  */
   4065   htab = ppc_hash_table (info);
   4066   if (htab == NULL)
   4067     return;
   4068   htab->stub_bfd = abfd;
   4069   htab->elf.dynobj = abfd;
   4070 }
   4071 
   4072 /* Build a name for an entry in the stub hash table.  */
   4073 
   4074 static char *
   4075 ppc_stub_name (const asection *input_section,
   4076 	       const asection *sym_sec,
   4077 	       const struct ppc_link_hash_entry *h,
   4078 	       const Elf_Internal_Rela *rel)
   4079 {
   4080   char *stub_name;
   4081   ssize_t len;
   4082 
   4083   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   4084      offsets from a sym as a branch target?  In fact, we could
   4085      probably assume the addend is always zero.  */
   4086   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   4087 
   4088   if (h)
   4089     {
   4090       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   4091       stub_name = bfd_malloc (len);
   4092       if (stub_name == NULL)
   4093 	return stub_name;
   4094 
   4095       len = sprintf (stub_name, "%08x.%s+%x",
   4096 		     input_section->id & 0xffffffff,
   4097 		     h->elf.root.root.string,
   4098 		     (int) rel->r_addend & 0xffffffff);
   4099     }
   4100   else
   4101     {
   4102       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   4103       stub_name = bfd_malloc (len);
   4104       if (stub_name == NULL)
   4105 	return stub_name;
   4106 
   4107       len = sprintf (stub_name, "%08x.%x:%x+%x",
   4108 		     input_section->id & 0xffffffff,
   4109 		     sym_sec->id & 0xffffffff,
   4110 		     (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   4111 		     (int) rel->r_addend & 0xffffffff);
   4112     }
   4113   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   4114     stub_name[len - 2] = 0;
   4115   return stub_name;
   4116 }
   4117 
   4118 /* Look up an entry in the stub hash.  Stub entries are cached because
   4119    creating the stub name takes a bit of time.  */
   4120 
   4121 static struct ppc_stub_hash_entry *
   4122 ppc_get_stub_entry (const asection *input_section,
   4123 		    const asection *sym_sec,
   4124 		    struct ppc_link_hash_entry *h,
   4125 		    const Elf_Internal_Rela *rel,
   4126 		    struct ppc_link_hash_table *htab)
   4127 {
   4128   struct ppc_stub_hash_entry *stub_entry;
   4129   const asection *id_sec;
   4130 
   4131   /* If this input section is part of a group of sections sharing one
   4132      stub section, then use the id of the first section in the group.
   4133      Stub names need to include a section id, as there may well be
   4134      more than one stub used to reach say, printf, and we need to
   4135      distinguish between them.  */
   4136   id_sec = htab->stub_group[input_section->id].link_sec;
   4137 
   4138   if (h != NULL && h->u.stub_cache != NULL
   4139       && h->u.stub_cache->h == h
   4140       && h->u.stub_cache->id_sec == id_sec)
   4141     {
   4142       stub_entry = h->u.stub_cache;
   4143     }
   4144   else
   4145     {
   4146       char *stub_name;
   4147 
   4148       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
   4149       if (stub_name == NULL)
   4150 	return NULL;
   4151 
   4152       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   4153 					 stub_name, FALSE, FALSE);
   4154       if (h != NULL)
   4155 	h->u.stub_cache = stub_entry;
   4156 
   4157       free (stub_name);
   4158     }
   4159 
   4160   return stub_entry;
   4161 }
   4162 
   4163 /* Add a new stub entry to the stub hash.  Not all fields of the new
   4164    stub entry are initialised.  */
   4165 
   4166 static struct ppc_stub_hash_entry *
   4167 ppc_add_stub (const char *stub_name,
   4168 	      asection *section,
   4169 	      struct bfd_link_info *info)
   4170 {
   4171   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4172   asection *link_sec;
   4173   asection *stub_sec;
   4174   struct ppc_stub_hash_entry *stub_entry;
   4175 
   4176   link_sec = htab->stub_group[section->id].link_sec;
   4177   stub_sec = htab->stub_group[section->id].stub_sec;
   4178   if (stub_sec == NULL)
   4179     {
   4180       stub_sec = htab->stub_group[link_sec->id].stub_sec;
   4181       if (stub_sec == NULL)
   4182 	{
   4183 	  size_t namelen;
   4184 	  bfd_size_type len;
   4185 	  char *s_name;
   4186 
   4187 	  namelen = strlen (link_sec->name);
   4188 	  len = namelen + sizeof (STUB_SUFFIX);
   4189 	  s_name = bfd_alloc (htab->stub_bfd, len);
   4190 	  if (s_name == NULL)
   4191 	    return NULL;
   4192 
   4193 	  memcpy (s_name, link_sec->name, namelen);
   4194 	  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   4195 	  stub_sec = (*htab->add_stub_section) (s_name, link_sec);
   4196 	  if (stub_sec == NULL)
   4197 	    return NULL;
   4198 	  htab->stub_group[link_sec->id].stub_sec = stub_sec;
   4199 	}
   4200       htab->stub_group[section->id].stub_sec = stub_sec;
   4201     }
   4202 
   4203   /* Enter this entry into the linker stub hash table.  */
   4204   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   4205 				     TRUE, FALSE);
   4206   if (stub_entry == NULL)
   4207     {
   4208       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
   4209 			      section->owner, stub_name);
   4210       return NULL;
   4211     }
   4212 
   4213   stub_entry->stub_sec = stub_sec;
   4214   stub_entry->stub_offset = 0;
   4215   stub_entry->id_sec = link_sec;
   4216   return stub_entry;
   4217 }
   4218 
   4219 /* Create sections for linker generated code.  */
   4220 
   4221 static bfd_boolean
   4222 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   4223 {
   4224   struct ppc_link_hash_table *htab;
   4225   flagword flags;
   4226 
   4227   htab = ppc_hash_table (info);
   4228   if (htab == NULL)
   4229     return FALSE;
   4230 
   4231   /* Create .sfpr for code to save and restore fp regs.  */
   4232   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   4233 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4234   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   4235 						   flags);
   4236   if (htab->sfpr == NULL
   4237       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
   4238     return FALSE;
   4239 
   4240   /* Create .glink for lazy dynamic linking support.  */
   4241   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   4242 						    flags);
   4243   if (htab->glink == NULL
   4244       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
   4245     return FALSE;
   4246 
   4247   if (!info->no_ld_generated_unwind_info)
   4248     {
   4249       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   4250 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4251       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
   4252 								 ".eh_frame",
   4253 								 flags);
   4254       if (htab->glink_eh_frame == NULL
   4255 	  || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
   4256 	return FALSE;
   4257     }
   4258 
   4259   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   4260   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   4261   if (htab->iplt == NULL
   4262       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
   4263     return FALSE;
   4264 
   4265   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4266 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4267   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
   4268 						      ".rela.iplt",
   4269 						      flags);
   4270   if (htab->reliplt == NULL
   4271       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
   4272     return FALSE;
   4273 
   4274   /* Create branch lookup table for plt_branch stubs.  */
   4275   flags = (SEC_ALLOC | SEC_LOAD
   4276 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4277   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   4278 						   flags);
   4279   if (htab->brlt == NULL
   4280       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
   4281     return FALSE;
   4282 
   4283   if (!info->shared)
   4284     return TRUE;
   4285 
   4286   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4287 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4288   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
   4289 						      ".rela.branch_lt",
   4290 						      flags);
   4291   if (htab->relbrlt == NULL
   4292       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
   4293     return FALSE;
   4294 
   4295   return TRUE;
   4296 }
   4297 
   4298 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   4299    not already done.  */
   4300 
   4301 static bfd_boolean
   4302 create_got_section (bfd *abfd, struct bfd_link_info *info)
   4303 {
   4304   asection *got, *relgot;
   4305   flagword flags;
   4306   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4307 
   4308   if (!is_ppc64_elf (abfd))
   4309     return FALSE;
   4310   if (htab == NULL)
   4311     return FALSE;
   4312 
   4313   if (!htab->got)
   4314     {
   4315       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
   4316 	return FALSE;
   4317 
   4318       htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
   4319       if (!htab->got)
   4320 	abort ();
   4321     }
   4322 
   4323   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4324 	   | SEC_LINKER_CREATED);
   4325 
   4326   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4327   if (!got
   4328       || !bfd_set_section_alignment (abfd, got, 3))
   4329     return FALSE;
   4330 
   4331   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   4332 					       flags | SEC_READONLY);
   4333   if (!relgot
   4334       || ! bfd_set_section_alignment (abfd, relgot, 3))
   4335     return FALSE;
   4336 
   4337   ppc64_elf_tdata (abfd)->got = got;
   4338   ppc64_elf_tdata (abfd)->relgot = relgot;
   4339   return TRUE;
   4340 }
   4341 
   4342 /* Create the dynamic sections, and set up shortcuts.  */
   4343 
   4344 static bfd_boolean
   4345 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   4346 {
   4347   struct ppc_link_hash_table *htab;
   4348 
   4349   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   4350     return FALSE;
   4351 
   4352   htab = ppc_hash_table (info);
   4353   if (htab == NULL)
   4354     return FALSE;
   4355 
   4356   if (!htab->got)
   4357     htab->got = bfd_get_linker_section (dynobj, ".got");
   4358   htab->plt = bfd_get_linker_section (dynobj, ".plt");
   4359   htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
   4360   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
   4361   if (!info->shared)
   4362     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
   4363 
   4364   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
   4365       || (!info->shared && !htab->relbss))
   4366     abort ();
   4367 
   4368   return TRUE;
   4369 }
   4370 
   4371 /* Follow indirect and warning symbol links.  */
   4372 
   4373 static inline struct bfd_link_hash_entry *
   4374 follow_link (struct bfd_link_hash_entry *h)
   4375 {
   4376   while (h->type == bfd_link_hash_indirect
   4377 	 || h->type == bfd_link_hash_warning)
   4378     h = h->u.i.link;
   4379   return h;
   4380 }
   4381 
   4382 static inline struct elf_link_hash_entry *
   4383 elf_follow_link (struct elf_link_hash_entry *h)
   4384 {
   4385   return (struct elf_link_hash_entry *) follow_link (&h->root);
   4386 }
   4387 
   4388 static inline struct ppc_link_hash_entry *
   4389 ppc_follow_link (struct ppc_link_hash_entry *h)
   4390 {
   4391   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
   4392 }
   4393 
   4394 /* Merge PLT info on FROM with that on TO.  */
   4395 
   4396 static void
   4397 move_plt_plist (struct ppc_link_hash_entry *from,
   4398 		struct ppc_link_hash_entry *to)
   4399 {
   4400   if (from->elf.plt.plist != NULL)
   4401     {
   4402       if (to->elf.plt.plist != NULL)
   4403 	{
   4404 	  struct plt_entry **entp;
   4405 	  struct plt_entry *ent;
   4406 
   4407 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   4408 	    {
   4409 	      struct plt_entry *dent;
   4410 
   4411 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   4412 		if (dent->addend == ent->addend)
   4413 		  {
   4414 		    dent->plt.refcount += ent->plt.refcount;
   4415 		    *entp = ent->next;
   4416 		    break;
   4417 		  }
   4418 	      if (dent == NULL)
   4419 		entp = &ent->next;
   4420 	    }
   4421 	  *entp = to->elf.plt.plist;
   4422 	}
   4423 
   4424       to->elf.plt.plist = from->elf.plt.plist;
   4425       from->elf.plt.plist = NULL;
   4426     }
   4427 }
   4428 
   4429 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   4430 
   4431 static void
   4432 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   4433 				struct elf_link_hash_entry *dir,
   4434 				struct elf_link_hash_entry *ind)
   4435 {
   4436   struct ppc_link_hash_entry *edir, *eind;
   4437 
   4438   edir = (struct ppc_link_hash_entry *) dir;
   4439   eind = (struct ppc_link_hash_entry *) ind;
   4440 
   4441   edir->is_func |= eind->is_func;
   4442   edir->is_func_descriptor |= eind->is_func_descriptor;
   4443   edir->tls_mask |= eind->tls_mask;
   4444   if (eind->oh != NULL)
   4445     edir->oh = ppc_follow_link (eind->oh);
   4446 
   4447   /* If called to transfer flags for a weakdef during processing
   4448      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
   4449      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
   4450   if (!(ELIMINATE_COPY_RELOCS
   4451 	&& eind->elf.root.type != bfd_link_hash_indirect
   4452 	&& edir->elf.dynamic_adjusted))
   4453     edir->elf.non_got_ref |= eind->elf.non_got_ref;
   4454 
   4455   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   4456   edir->elf.ref_regular |= eind->elf.ref_regular;
   4457   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   4458   edir->elf.needs_plt |= eind->elf.needs_plt;
   4459 
   4460   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   4461   if (eind->dyn_relocs != NULL)
   4462     {
   4463       if (edir->dyn_relocs != NULL)
   4464 	{
   4465 	  struct elf_dyn_relocs **pp;
   4466 	  struct elf_dyn_relocs *p;
   4467 
   4468 	  /* Add reloc counts against the indirect sym to the direct sym
   4469 	     list.  Merge any entries against the same section.  */
   4470 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   4471 	    {
   4472 	      struct elf_dyn_relocs *q;
   4473 
   4474 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   4475 		if (q->sec == p->sec)
   4476 		  {
   4477 		    q->pc_count += p->pc_count;
   4478 		    q->count += p->count;
   4479 		    *pp = p->next;
   4480 		    break;
   4481 		  }
   4482 	      if (q == NULL)
   4483 		pp = &p->next;
   4484 	    }
   4485 	  *pp = edir->dyn_relocs;
   4486 	}
   4487 
   4488       edir->dyn_relocs = eind->dyn_relocs;
   4489       eind->dyn_relocs = NULL;
   4490     }
   4491 
   4492   /* If we were called to copy over info for a weak sym, that's all.
   4493      You might think dyn_relocs need not be copied over;  After all,
   4494      both syms will be dynamic or both non-dynamic so we're just
   4495      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
   4496      code in ppc64_elf_adjust_dynamic_symbol needs to check for
   4497      dyn_relocs in read-only sections, and it does so on what is the
   4498      DIR sym here.  */
   4499   if (eind->elf.root.type != bfd_link_hash_indirect)
   4500     return;
   4501 
   4502   /* Copy over got entries that we may have already seen to the
   4503      symbol which just became indirect.  */
   4504   if (eind->elf.got.glist != NULL)
   4505     {
   4506       if (edir->elf.got.glist != NULL)
   4507 	{
   4508 	  struct got_entry **entp;
   4509 	  struct got_entry *ent;
   4510 
   4511 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   4512 	    {
   4513 	      struct got_entry *dent;
   4514 
   4515 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4516 		if (dent->addend == ent->addend
   4517 		    && dent->owner == ent->owner
   4518 		    && dent->tls_type == ent->tls_type)
   4519 		  {
   4520 		    dent->got.refcount += ent->got.refcount;
   4521 		    *entp = ent->next;
   4522 		    break;
   4523 		  }
   4524 	      if (dent == NULL)
   4525 		entp = &ent->next;
   4526 	    }
   4527 	  *entp = edir->elf.got.glist;
   4528 	}
   4529 
   4530       edir->elf.got.glist = eind->elf.got.glist;
   4531       eind->elf.got.glist = NULL;
   4532     }
   4533 
   4534   /* And plt entries.  */
   4535   move_plt_plist (eind, edir);
   4536 
   4537   if (eind->elf.dynindx != -1)
   4538     {
   4539       if (edir->elf.dynindx != -1)
   4540 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4541 				edir->elf.dynstr_index);
   4542       edir->elf.dynindx = eind->elf.dynindx;
   4543       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4544       eind->elf.dynindx = -1;
   4545       eind->elf.dynstr_index = 0;
   4546     }
   4547 }
   4548 
   4549 /* Find the function descriptor hash entry from the given function code
   4550    hash entry FH.  Link the entries via their OH fields.  */
   4551 
   4552 static struct ppc_link_hash_entry *
   4553 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4554 {
   4555   struct ppc_link_hash_entry *fdh = fh->oh;
   4556 
   4557   if (fdh == NULL)
   4558     {
   4559       const char *fd_name = fh->elf.root.root.string + 1;
   4560 
   4561       fdh = (struct ppc_link_hash_entry *)
   4562 	elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
   4563       if (fdh == NULL)
   4564 	return fdh;
   4565 
   4566       fdh->is_func_descriptor = 1;
   4567       fdh->oh = fh;
   4568       fh->is_func = 1;
   4569       fh->oh = fdh;
   4570     }
   4571 
   4572   return ppc_follow_link (fdh);
   4573 }
   4574 
   4575 /* Make a fake function descriptor sym for the code sym FH.  */
   4576 
   4577 static struct ppc_link_hash_entry *
   4578 make_fdh (struct bfd_link_info *info,
   4579 	  struct ppc_link_hash_entry *fh)
   4580 {
   4581   bfd *abfd;
   4582   asymbol *newsym;
   4583   struct bfd_link_hash_entry *bh;
   4584   struct ppc_link_hash_entry *fdh;
   4585 
   4586   abfd = fh->elf.root.u.undef.abfd;
   4587   newsym = bfd_make_empty_symbol (abfd);
   4588   newsym->name = fh->elf.root.root.string + 1;
   4589   newsym->section = bfd_und_section_ptr;
   4590   newsym->value = 0;
   4591   newsym->flags = BSF_WEAK;
   4592 
   4593   bh = NULL;
   4594   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
   4595 					 newsym->flags, newsym->section,
   4596 					 newsym->value, NULL, FALSE, FALSE,
   4597 					 &bh))
   4598     return NULL;
   4599 
   4600   fdh = (struct ppc_link_hash_entry *) bh;
   4601   fdh->elf.non_elf = 0;
   4602   fdh->fake = 1;
   4603   fdh->is_func_descriptor = 1;
   4604   fdh->oh = fh;
   4605   fh->is_func = 1;
   4606   fh->oh = fdh;
   4607   return fdh;
   4608 }
   4609 
   4610 /* Fix function descriptor symbols defined in .opd sections to be
   4611    function type.  */
   4612 
   4613 static bfd_boolean
   4614 ppc64_elf_add_symbol_hook (bfd *ibfd,
   4615 			   struct bfd_link_info *info,
   4616 			   Elf_Internal_Sym *isym,
   4617 			   const char **name ATTRIBUTE_UNUSED,
   4618 			   flagword *flags ATTRIBUTE_UNUSED,
   4619 			   asection **sec,
   4620 			   bfd_vma *value ATTRIBUTE_UNUSED)
   4621 {
   4622   if ((ibfd->flags & DYNAMIC) == 0
   4623       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
   4624     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
   4625 
   4626   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4627     {
   4628       if ((ibfd->flags & DYNAMIC) == 0)
   4629 	elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
   4630     }
   4631   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
   4632     ;
   4633   else if (*sec != NULL
   4634 	   && strcmp ((*sec)->name, ".opd") == 0)
   4635     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4636 
   4637   return TRUE;
   4638 }
   4639 
   4640 /* This function makes an old ABI object reference to ".bar" cause the
   4641    inclusion of a new ABI object archive that defines "bar".
   4642    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4643    table that might be satisfied by the archive symbols.  */
   4644 
   4645 static struct elf_link_hash_entry *
   4646 ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4647 				 struct bfd_link_info *info,
   4648 				 const char *name)
   4649 {
   4650   struct elf_link_hash_entry *h;
   4651   char *dot_name;
   4652   size_t len;
   4653 
   4654   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4655   if (h != NULL
   4656       /* Don't return this sym if it is a fake function descriptor
   4657 	 created by add_symbol_adjust.  */
   4658       && !(h->root.type == bfd_link_hash_undefweak
   4659 	   && ((struct ppc_link_hash_entry *) h)->fake))
   4660     return h;
   4661 
   4662   if (name[0] == '.')
   4663     return h;
   4664 
   4665   len = strlen (name);
   4666   dot_name = bfd_alloc (abfd, len + 2);
   4667   if (dot_name == NULL)
   4668     return (struct elf_link_hash_entry *) 0 - 1;
   4669   dot_name[0] = '.';
   4670   memcpy (dot_name + 1, name, len + 1);
   4671   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   4672   bfd_release (abfd, dot_name);
   4673   return h;
   4674 }
   4675 
   4676 /* This function satisfies all old ABI object references to ".bar" if a
   4677    new ABI object defines "bar".  Well, at least, undefined dot symbols
   4678    are made weak.  This stops later archive searches from including an
   4679    object if we already have a function descriptor definition.  It also
   4680    prevents the linker complaining about undefined symbols.
   4681    We also check and correct mismatched symbol visibility here.  The
   4682    most restrictive visibility of the function descriptor and the
   4683    function entry symbol is used.  */
   4684 
   4685 static bfd_boolean
   4686 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   4687 {
   4688   struct ppc_link_hash_table *htab;
   4689   struct ppc_link_hash_entry *fdh;
   4690 
   4691   if (eh->elf.root.type == bfd_link_hash_indirect)
   4692     return TRUE;
   4693 
   4694   if (eh->elf.root.type == bfd_link_hash_warning)
   4695     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   4696 
   4697   if (eh->elf.root.root.string[0] != '.')
   4698     abort ();
   4699 
   4700   htab = ppc_hash_table (info);
   4701   if (htab == NULL)
   4702     return FALSE;
   4703 
   4704   fdh = lookup_fdh (eh, htab);
   4705   if (fdh == NULL)
   4706     {
   4707       if (!info->relocatable
   4708 	  && (eh->elf.root.type == bfd_link_hash_undefined
   4709 	      || eh->elf.root.type == bfd_link_hash_undefweak)
   4710 	  && eh->elf.ref_regular)
   4711 	{
   4712 	  /* Make an undefweak function descriptor sym, which is enough to
   4713 	     pull in an --as-needed shared lib, but won't cause link
   4714 	     errors.  Archives are handled elsewhere.  */
   4715 	  fdh = make_fdh (info, eh);
   4716 	  if (fdh == NULL)
   4717 	    return FALSE;
   4718 	  fdh->elf.ref_regular = 1;
   4719 	}
   4720     }
   4721   else
   4722     {
   4723       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   4724       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   4725       if (entry_vis < descr_vis)
   4726 	fdh->elf.other += entry_vis - descr_vis;
   4727       else if (entry_vis > descr_vis)
   4728 	eh->elf.other += descr_vis - entry_vis;
   4729 
   4730       if ((fdh->elf.root.type == bfd_link_hash_defined
   4731 	   || fdh->elf.root.type == bfd_link_hash_defweak)
   4732 	  && eh->elf.root.type == bfd_link_hash_undefined)
   4733 	{
   4734 	  eh->elf.root.type = bfd_link_hash_undefweak;
   4735 	  eh->was_undefined = 1;
   4736 	  htab->twiddled_syms = 1;
   4737 	}
   4738     }
   4739 
   4740   return TRUE;
   4741 }
   4742 
   4743 /* Process list of dot-symbols we made in link_hash_newfunc.  */
   4744 
   4745 static bfd_boolean
   4746 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
   4747 {
   4748   struct ppc_link_hash_table *htab;
   4749   struct ppc_link_hash_entry **p, *eh;
   4750 
   4751   if (!is_ppc64_elf (info->output_bfd))
   4752     return TRUE;
   4753   htab = ppc_hash_table (info);
   4754   if (htab == NULL)
   4755     return FALSE;
   4756 
   4757   if (is_ppc64_elf (ibfd))
   4758     {
   4759       p = &htab->dot_syms;
   4760       while ((eh = *p) != NULL)
   4761 	{
   4762 	  *p = NULL;
   4763 	  if (!add_symbol_adjust (eh, info))
   4764 	    return FALSE;
   4765 	  p = &eh->u.next_dot_sym;
   4766 	}
   4767     }
   4768 
   4769   /* Clear the list for non-ppc64 input files.  */
   4770   p = &htab->dot_syms;
   4771   while ((eh = *p) != NULL)
   4772     {
   4773       *p = NULL;
   4774       p = &eh->u.next_dot_sym;
   4775     }
   4776 
   4777   /* We need to fix the undefs list for any syms we have twiddled to
   4778      undef_weak.  */
   4779   if (htab->twiddled_syms)
   4780     {
   4781       bfd_link_repair_undef_list (&htab->elf.root);
   4782       htab->twiddled_syms = 0;
   4783     }
   4784   return TRUE;
   4785 }
   4786 
   4787 /* Undo hash table changes when an --as-needed input file is determined
   4788    not to be needed.  */
   4789 
   4790 static bfd_boolean
   4791 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
   4792 			     struct bfd_link_info *info)
   4793 {
   4794   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4795 
   4796   if (htab == NULL)
   4797     return FALSE;
   4798 
   4799   htab->dot_syms = NULL;
   4800   return TRUE;
   4801 }
   4802 
   4803 /* If --just-symbols against a final linked binary, then assume we need
   4804    toc adjusting stubs when calling functions defined there.  */
   4805 
   4806 static void
   4807 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   4808 {
   4809   if ((sec->flags & SEC_CODE) != 0
   4810       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   4811       && is_ppc64_elf (sec->owner))
   4812     {
   4813       asection *got = bfd_get_section_by_name (sec->owner, ".got");
   4814       if (got != NULL
   4815 	  && got->size >= elf_backend_got_header_size
   4816 	  && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   4817 	sec->has_toc_reloc = 1;
   4818     }
   4819   _bfd_elf_link_just_syms (sec, info);
   4820 }
   4821 
   4822 static struct plt_entry **
   4823 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   4824 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   4825 {
   4826   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   4827   struct plt_entry **local_plt;
   4828   unsigned char *local_got_tls_masks;
   4829 
   4830   if (local_got_ents == NULL)
   4831     {
   4832       bfd_size_type size = symtab_hdr->sh_info;
   4833 
   4834       size *= (sizeof (*local_got_ents)
   4835 	       + sizeof (*local_plt)
   4836 	       + sizeof (*local_got_tls_masks));
   4837       local_got_ents = bfd_zalloc (abfd, size);
   4838       if (local_got_ents == NULL)
   4839 	return NULL;
   4840       elf_local_got_ents (abfd) = local_got_ents;
   4841     }
   4842 
   4843   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
   4844     {
   4845       struct got_entry *ent;
   4846 
   4847       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   4848 	if (ent->addend == r_addend
   4849 	    && ent->owner == abfd
   4850 	    && ent->tls_type == tls_type)
   4851 	  break;
   4852       if (ent == NULL)
   4853 	{
   4854 	  bfd_size_type amt = sizeof (*ent);
   4855 	  ent = bfd_alloc (abfd, amt);
   4856 	  if (ent == NULL)
   4857 	    return FALSE;
   4858 	  ent->next = local_got_ents[r_symndx];
   4859 	  ent->addend = r_addend;
   4860 	  ent->owner = abfd;
   4861 	  ent->tls_type = tls_type;
   4862 	  ent->is_indirect = FALSE;
   4863 	  ent->got.refcount = 0;
   4864 	  local_got_ents[r_symndx] = ent;
   4865 	}
   4866       ent->got.refcount += 1;
   4867     }
   4868 
   4869   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   4870   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   4871   local_got_tls_masks[r_symndx] |= tls_type;
   4872 
   4873   return local_plt + r_symndx;
   4874 }
   4875 
   4876 static bfd_boolean
   4877 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   4878 {
   4879   struct plt_entry *ent;
   4880 
   4881   for (ent = *plist; ent != NULL; ent = ent->next)
   4882     if (ent->addend == addend)
   4883       break;
   4884   if (ent == NULL)
   4885     {
   4886       bfd_size_type amt = sizeof (*ent);
   4887       ent = bfd_alloc (abfd, amt);
   4888       if (ent == NULL)
   4889 	return FALSE;
   4890       ent->next = *plist;
   4891       ent->addend = addend;
   4892       ent->plt.refcount = 0;
   4893       *plist = ent;
   4894     }
   4895   ent->plt.refcount += 1;
   4896   return TRUE;
   4897 }
   4898 
   4899 static bfd_boolean
   4900 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   4901 {
   4902   return (r_type == R_PPC64_REL24
   4903 	  || r_type == R_PPC64_REL14
   4904 	  || r_type == R_PPC64_REL14_BRTAKEN
   4905 	  || r_type == R_PPC64_REL14_BRNTAKEN
   4906 	  || r_type == R_PPC64_ADDR24
   4907 	  || r_type == R_PPC64_ADDR14
   4908 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   4909 	  || r_type == R_PPC64_ADDR14_BRNTAKEN);
   4910 }
   4911 
   4912 /* Look through the relocs for a section during the first phase, and
   4913    calculate needed space in the global offset table, procedure
   4914    linkage table, and dynamic reloc sections.  */
   4915 
   4916 static bfd_boolean
   4917 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   4918 			asection *sec, const Elf_Internal_Rela *relocs)
   4919 {
   4920   struct ppc_link_hash_table *htab;
   4921   Elf_Internal_Shdr *symtab_hdr;
   4922   struct elf_link_hash_entry **sym_hashes;
   4923   const Elf_Internal_Rela *rel;
   4924   const Elf_Internal_Rela *rel_end;
   4925   asection *sreloc;
   4926   asection **opd_sym_map;
   4927   struct elf_link_hash_entry *tga, *dottga;
   4928 
   4929   if (info->relocatable)
   4930     return TRUE;
   4931 
   4932   /* Don't do anything special with non-loaded, non-alloced sections.
   4933      In particular, any relocs in such sections should not affect GOT
   4934      and PLT reference counting (ie. we don't allow them to create GOT
   4935      or PLT entries), there's no possibility or desire to optimize TLS
   4936      relocs, and there's not much point in propagating relocs to shared
   4937      libs that the dynamic linker won't relocate.  */
   4938   if ((sec->flags & SEC_ALLOC) == 0)
   4939     return TRUE;
   4940 
   4941   BFD_ASSERT (is_ppc64_elf (abfd));
   4942 
   4943   htab = ppc_hash_table (info);
   4944   if (htab == NULL)
   4945     return FALSE;
   4946 
   4947   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4948 			      FALSE, FALSE, TRUE);
   4949   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   4950 				 FALSE, FALSE, TRUE);
   4951   symtab_hdr = &elf_symtab_hdr (abfd);
   4952   sym_hashes = elf_sym_hashes (abfd);
   4953   sreloc = NULL;
   4954   opd_sym_map = NULL;
   4955   if (strcmp (sec->name, ".opd") == 0)
   4956     {
   4957       /* Garbage collection needs some extra help with .opd sections.
   4958 	 We don't want to necessarily keep everything referenced by
   4959 	 relocs in .opd, as that would keep all functions.  Instead,
   4960 	 if we reference an .opd symbol (a function descriptor), we
   4961 	 want to keep the function code symbol's section.  This is
   4962 	 easy for global symbols, but for local syms we need to keep
   4963 	 information about the associated function section.  */
   4964       bfd_size_type amt;
   4965 
   4966       amt = sec->size * sizeof (*opd_sym_map) / 8;
   4967       opd_sym_map = bfd_zalloc (abfd, amt);
   4968       if (opd_sym_map == NULL)
   4969 	return FALSE;
   4970       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
   4971       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
   4972       ppc64_elf_section_data (sec)->sec_type = sec_opd;
   4973     }
   4974 
   4975   if (htab->sfpr == NULL
   4976       && !create_linkage_sections (htab->elf.dynobj, info))
   4977     return FALSE;
   4978 
   4979   rel_end = relocs + sec->reloc_count;
   4980   for (rel = relocs; rel < rel_end; rel++)
   4981     {
   4982       unsigned long r_symndx;
   4983       struct elf_link_hash_entry *h;
   4984       enum elf_ppc64_reloc_type r_type;
   4985       int tls_type;
   4986       struct _ppc64_elf_section_data *ppc64_sec;
   4987       struct plt_entry **ifunc;
   4988 
   4989       r_symndx = ELF64_R_SYM (rel->r_info);
   4990       if (r_symndx < symtab_hdr->sh_info)
   4991 	h = NULL;
   4992       else
   4993 	{
   4994 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4995 	  h = elf_follow_link (h);
   4996 	}
   4997 
   4998       tls_type = 0;
   4999       ifunc = NULL;
   5000       if (h != NULL)
   5001 	{
   5002 	  if (h->type == STT_GNU_IFUNC)
   5003 	    {
   5004 	      h->needs_plt = 1;
   5005 	      ifunc = &h->plt.plist;
   5006 	    }
   5007 	}
   5008       else
   5009 	{
   5010 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5011 							  abfd, r_symndx);
   5012 	  if (isym == NULL)
   5013 	    return FALSE;
   5014 
   5015 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   5016 	    {
   5017 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5018 					     rel->r_addend, PLT_IFUNC);
   5019 	      if (ifunc == NULL)
   5020 		return FALSE;
   5021 	    }
   5022 	}
   5023       r_type = ELF64_R_TYPE (rel->r_info);
   5024       if (is_branch_reloc (r_type))
   5025 	{
   5026 	  if (h != NULL && (h == tga || h == dottga))
   5027 	    {
   5028 	      if (rel != relocs
   5029 		  && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   5030 		      || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   5031 		/* We have a new-style __tls_get_addr call with a marker
   5032 		   reloc.  */
   5033 		;
   5034 	      else
   5035 		/* Mark this section as having an old-style call.  */
   5036 		sec->has_tls_get_addr_call = 1;
   5037 	    }
   5038 
   5039 	  /* STT_GNU_IFUNC symbols must have a PLT entry.  */
   5040 	  if (ifunc != NULL
   5041 	      && !update_plt_info (abfd, ifunc, rel->r_addend))
   5042 	    return FALSE;
   5043 	}
   5044 
   5045       switch (r_type)
   5046 	{
   5047 	case R_PPC64_TLSGD:
   5048 	case R_PPC64_TLSLD:
   5049 	  /* These special tls relocs tie a call to __tls_get_addr with
   5050 	     its parameter symbol.  */
   5051 	  break;
   5052 
   5053 	case R_PPC64_GOT_TLSLD16:
   5054 	case R_PPC64_GOT_TLSLD16_LO:
   5055 	case R_PPC64_GOT_TLSLD16_HI:
   5056 	case R_PPC64_GOT_TLSLD16_HA:
   5057 	  tls_type = TLS_TLS | TLS_LD;
   5058 	  goto dogottls;
   5059 
   5060 	case R_PPC64_GOT_TLSGD16:
   5061 	case R_PPC64_GOT_TLSGD16_LO:
   5062 	case R_PPC64_GOT_TLSGD16_HI:
   5063 	case R_PPC64_GOT_TLSGD16_HA:
   5064 	  tls_type = TLS_TLS | TLS_GD;
   5065 	  goto dogottls;
   5066 
   5067 	case R_PPC64_GOT_TPREL16_DS:
   5068 	case R_PPC64_GOT_TPREL16_LO_DS:
   5069 	case R_PPC64_GOT_TPREL16_HI:
   5070 	case R_PPC64_GOT_TPREL16_HA:
   5071 	  if (!info->executable)
   5072 	    info->flags |= DF_STATIC_TLS;
   5073 	  tls_type = TLS_TLS | TLS_TPREL;
   5074 	  goto dogottls;
   5075 
   5076 	case R_PPC64_GOT_DTPREL16_DS:
   5077 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5078 	case R_PPC64_GOT_DTPREL16_HI:
   5079 	case R_PPC64_GOT_DTPREL16_HA:
   5080 	  tls_type = TLS_TLS | TLS_DTPREL;
   5081 	dogottls:
   5082 	  sec->has_tls_reloc = 1;
   5083 	  /* Fall thru */
   5084 
   5085 	case R_PPC64_GOT16:
   5086 	case R_PPC64_GOT16_DS:
   5087 	case R_PPC64_GOT16_HA:
   5088 	case R_PPC64_GOT16_HI:
   5089 	case R_PPC64_GOT16_LO:
   5090 	case R_PPC64_GOT16_LO_DS:
   5091 	  /* This symbol requires a global offset table entry.  */
   5092 	  sec->has_toc_reloc = 1;
   5093 	  if (r_type == R_PPC64_GOT_TLSLD16
   5094 	      || r_type == R_PPC64_GOT_TLSGD16
   5095 	      || r_type == R_PPC64_GOT_TPREL16_DS
   5096 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   5097 	      || r_type == R_PPC64_GOT16
   5098 	      || r_type == R_PPC64_GOT16_DS)
   5099 	    {
   5100 	      htab->do_multi_toc = 1;
   5101 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5102 	    }
   5103 
   5104 	  if (ppc64_elf_tdata (abfd)->got == NULL
   5105 	      && !create_got_section (abfd, info))
   5106 	    return FALSE;
   5107 
   5108 	  if (h != NULL)
   5109 	    {
   5110 	      struct ppc_link_hash_entry *eh;
   5111 	      struct got_entry *ent;
   5112 
   5113 	      eh = (struct ppc_link_hash_entry *) h;
   5114 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   5115 		if (ent->addend == rel->r_addend
   5116 		    && ent->owner == abfd
   5117 		    && ent->tls_type == tls_type)
   5118 		  break;
   5119 	      if (ent == NULL)
   5120 		{
   5121 		  bfd_size_type amt = sizeof (*ent);
   5122 		  ent = bfd_alloc (abfd, amt);
   5123 		  if (ent == NULL)
   5124 		    return FALSE;
   5125 		  ent->next = eh->elf.got.glist;
   5126 		  ent->addend = rel->r_addend;
   5127 		  ent->owner = abfd;
   5128 		  ent->tls_type = tls_type;
   5129 		  ent->is_indirect = FALSE;
   5130 		  ent->got.refcount = 0;
   5131 		  eh->elf.got.glist = ent;
   5132 		}
   5133 	      ent->got.refcount += 1;
   5134 	      eh->tls_mask |= tls_type;
   5135 	    }
   5136 	  else
   5137 	    /* This is a global offset table entry for a local symbol.  */
   5138 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5139 					rel->r_addend, tls_type))
   5140 	      return FALSE;
   5141 	  break;
   5142 
   5143 	case R_PPC64_PLT16_HA:
   5144 	case R_PPC64_PLT16_HI:
   5145 	case R_PPC64_PLT16_LO:
   5146 	case R_PPC64_PLT32:
   5147 	case R_PPC64_PLT64:
   5148 	  /* This symbol requires a procedure linkage table entry.  We
   5149 	     actually build the entry in adjust_dynamic_symbol,
   5150 	     because this might be a case of linking PIC code without
   5151 	     linking in any dynamic objects, in which case we don't
   5152 	     need to generate a procedure linkage table after all.  */
   5153 	  if (h == NULL)
   5154 	    {
   5155 	      /* It does not make sense to have a procedure linkage
   5156 		 table entry for a local symbol.  */
   5157 	      bfd_set_error (bfd_error_bad_value);
   5158 	      return FALSE;
   5159 	    }
   5160 	  else
   5161 	    {
   5162 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5163 		return FALSE;
   5164 	      h->needs_plt = 1;
   5165 	      if (h->root.root.string[0] == '.'
   5166 		  && h->root.root.string[1] != '\0')
   5167 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5168 	    }
   5169 	  break;
   5170 
   5171 	  /* The following relocations don't need to propagate the
   5172 	     relocation if linking a shared object since they are
   5173 	     section relative.  */
   5174 	case R_PPC64_SECTOFF:
   5175 	case R_PPC64_SECTOFF_LO:
   5176 	case R_PPC64_SECTOFF_HI:
   5177 	case R_PPC64_SECTOFF_HA:
   5178 	case R_PPC64_SECTOFF_DS:
   5179 	case R_PPC64_SECTOFF_LO_DS:
   5180 	case R_PPC64_DTPREL16:
   5181 	case R_PPC64_DTPREL16_LO:
   5182 	case R_PPC64_DTPREL16_HI:
   5183 	case R_PPC64_DTPREL16_HA:
   5184 	case R_PPC64_DTPREL16_DS:
   5185 	case R_PPC64_DTPREL16_LO_DS:
   5186 	case R_PPC64_DTPREL16_HIGHER:
   5187 	case R_PPC64_DTPREL16_HIGHERA:
   5188 	case R_PPC64_DTPREL16_HIGHEST:
   5189 	case R_PPC64_DTPREL16_HIGHESTA:
   5190 	  break;
   5191 
   5192 	  /* Nor do these.  */
   5193 	case R_PPC64_REL16:
   5194 	case R_PPC64_REL16_LO:
   5195 	case R_PPC64_REL16_HI:
   5196 	case R_PPC64_REL16_HA:
   5197 	  break;
   5198 
   5199 	case R_PPC64_TOC16:
   5200 	case R_PPC64_TOC16_DS:
   5201 	  htab->do_multi_toc = 1;
   5202 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5203 	case R_PPC64_TOC16_LO:
   5204 	case R_PPC64_TOC16_HI:
   5205 	case R_PPC64_TOC16_HA:
   5206 	case R_PPC64_TOC16_LO_DS:
   5207 	  sec->has_toc_reloc = 1;
   5208 	  break;
   5209 
   5210 	  /* This relocation describes the C++ object vtable hierarchy.
   5211 	     Reconstruct it for later use during GC.  */
   5212 	case R_PPC64_GNU_VTINHERIT:
   5213 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   5214 	    return FALSE;
   5215 	  break;
   5216 
   5217 	  /* This relocation describes which C++ vtable entries are actually
   5218 	     used.  Record for later use during GC.  */
   5219 	case R_PPC64_GNU_VTENTRY:
   5220 	  BFD_ASSERT (h != NULL);
   5221 	  if (h != NULL
   5222 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   5223 	    return FALSE;
   5224 	  break;
   5225 
   5226 	case R_PPC64_REL14:
   5227 	case R_PPC64_REL14_BRTAKEN:
   5228 	case R_PPC64_REL14_BRNTAKEN:
   5229 	  {
   5230 	    asection *dest = NULL;
   5231 
   5232 	    /* Heuristic: If jumping outside our section, chances are
   5233 	       we are going to need a stub.  */
   5234 	    if (h != NULL)
   5235 	      {
   5236 		/* If the sym is weak it may be overridden later, so
   5237 		   don't assume we know where a weak sym lives.  */
   5238 		if (h->root.type == bfd_link_hash_defined)
   5239 		  dest = h->root.u.def.section;
   5240 	      }
   5241 	    else
   5242 	      {
   5243 		Elf_Internal_Sym *isym;
   5244 
   5245 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5246 					      abfd, r_symndx);
   5247 		if (isym == NULL)
   5248 		  return FALSE;
   5249 
   5250 		dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5251 	      }
   5252 
   5253 	    if (dest != sec)
   5254 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   5255 	  }
   5256 	  /* Fall through.  */
   5257 
   5258 	case R_PPC64_REL24:
   5259 	  if (h != NULL && ifunc == NULL)
   5260 	    {
   5261 	      /* We may need a .plt entry if the function this reloc
   5262 		 refers to is in a shared lib.  */
   5263 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5264 		return FALSE;
   5265 	      h->needs_plt = 1;
   5266 	      if (h->root.root.string[0] == '.'
   5267 		  && h->root.root.string[1] != '\0')
   5268 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5269 	      if (h == tga || h == dottga)
   5270 		sec->has_tls_reloc = 1;
   5271 	    }
   5272 	  break;
   5273 
   5274 	case R_PPC64_TPREL64:
   5275 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5276 	  if (!info->executable)
   5277 	    info->flags |= DF_STATIC_TLS;
   5278 	  goto dotlstoc;
   5279 
   5280 	case R_PPC64_DTPMOD64:
   5281 	  if (rel + 1 < rel_end
   5282 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5283 	      && rel[1].r_offset == rel->r_offset + 8)
   5284 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5285 	  else
   5286 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5287 	  goto dotlstoc;
   5288 
   5289 	case R_PPC64_DTPREL64:
   5290 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5291 	  if (rel != relocs
   5292 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5293 	      && rel[-1].r_offset == rel->r_offset - 8)
   5294 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5295 	       Don't mark with TLS_DTPREL.  */
   5296 	    goto dodyn;
   5297 
   5298 	dotlstoc:
   5299 	  sec->has_tls_reloc = 1;
   5300 	  if (h != NULL)
   5301 	    {
   5302 	      struct ppc_link_hash_entry *eh;
   5303 	      eh = (struct ppc_link_hash_entry *) h;
   5304 	      eh->tls_mask |= tls_type;
   5305 	    }
   5306 	  else
   5307 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5308 					rel->r_addend, tls_type))
   5309 	      return FALSE;
   5310 
   5311 	  ppc64_sec = ppc64_elf_section_data (sec);
   5312 	  if (ppc64_sec->sec_type != sec_toc)
   5313 	    {
   5314 	      bfd_size_type amt;
   5315 
   5316 	      /* One extra to simplify get_tls_mask.  */
   5317 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5318 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5319 	      if (ppc64_sec->u.toc.symndx == NULL)
   5320 		return FALSE;
   5321 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5322 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5323 	      if (ppc64_sec->u.toc.add == NULL)
   5324 		return FALSE;
   5325 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5326 	      ppc64_sec->sec_type = sec_toc;
   5327 	    }
   5328 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5329 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5330 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5331 
   5332 	  /* Mark the second slot of a GD or LD entry.
   5333 	     -1 to indicate GD and -2 to indicate LD.  */
   5334 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5335 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5336 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5337 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5338 	  goto dodyn;
   5339 
   5340 	case R_PPC64_TPREL16:
   5341 	case R_PPC64_TPREL16_LO:
   5342 	case R_PPC64_TPREL16_HI:
   5343 	case R_PPC64_TPREL16_HA:
   5344 	case R_PPC64_TPREL16_DS:
   5345 	case R_PPC64_TPREL16_LO_DS:
   5346 	case R_PPC64_TPREL16_HIGHER:
   5347 	case R_PPC64_TPREL16_HIGHERA:
   5348 	case R_PPC64_TPREL16_HIGHEST:
   5349 	case R_PPC64_TPREL16_HIGHESTA:
   5350 	  if (info->shared)
   5351 	    {
   5352 	      if (!info->executable)
   5353 		info->flags |= DF_STATIC_TLS;
   5354 	      goto dodyn;
   5355 	    }
   5356 	  break;
   5357 
   5358 	case R_PPC64_ADDR64:
   5359 	  if (opd_sym_map != NULL
   5360 	      && rel + 1 < rel_end
   5361 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5362 	    {
   5363 	      if (h != NULL)
   5364 		{
   5365 		  if (h->root.root.string[0] == '.'
   5366 		      && h->root.root.string[1] != 0
   5367 		      && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
   5368 		    ;
   5369 		  else
   5370 		    ((struct ppc_link_hash_entry *) h)->is_func = 1;
   5371 		}
   5372 	      else
   5373 		{
   5374 		  asection *s;
   5375 		  Elf_Internal_Sym *isym;
   5376 
   5377 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5378 						abfd, r_symndx);
   5379 		  if (isym == NULL)
   5380 		    return FALSE;
   5381 
   5382 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5383 		  if (s != NULL && s != sec)
   5384 		    opd_sym_map[rel->r_offset / 8] = s;
   5385 		}
   5386 	    }
   5387 	  /* Fall through.  */
   5388 
   5389 	case R_PPC64_REL30:
   5390 	case R_PPC64_REL32:
   5391 	case R_PPC64_REL64:
   5392 	case R_PPC64_ADDR14:
   5393 	case R_PPC64_ADDR14_BRNTAKEN:
   5394 	case R_PPC64_ADDR14_BRTAKEN:
   5395 	case R_PPC64_ADDR16:
   5396 	case R_PPC64_ADDR16_DS:
   5397 	case R_PPC64_ADDR16_HA:
   5398 	case R_PPC64_ADDR16_HI:
   5399 	case R_PPC64_ADDR16_HIGHER:
   5400 	case R_PPC64_ADDR16_HIGHERA:
   5401 	case R_PPC64_ADDR16_HIGHEST:
   5402 	case R_PPC64_ADDR16_HIGHESTA:
   5403 	case R_PPC64_ADDR16_LO:
   5404 	case R_PPC64_ADDR16_LO_DS:
   5405 	case R_PPC64_ADDR24:
   5406 	case R_PPC64_ADDR32:
   5407 	case R_PPC64_UADDR16:
   5408 	case R_PPC64_UADDR32:
   5409 	case R_PPC64_UADDR64:
   5410 	case R_PPC64_TOC:
   5411 	  if (h != NULL && !info->shared)
   5412 	    /* We may need a copy reloc.  */
   5413 	    h->non_got_ref = 1;
   5414 
   5415 	  /* Don't propagate .opd relocs.  */
   5416 	  if (NO_OPD_RELOCS && opd_sym_map != NULL)
   5417 	    break;
   5418 
   5419 	  /* If we are creating a shared library, and this is a reloc
   5420 	     against a global symbol, or a non PC relative reloc
   5421 	     against a local symbol, then we need to copy the reloc
   5422 	     into the shared library.  However, if we are linking with
   5423 	     -Bsymbolic, we do not need to copy a reloc against a
   5424 	     global symbol which is defined in an object we are
   5425 	     including in the link (i.e., DEF_REGULAR is set).  At
   5426 	     this point we have not seen all the input files, so it is
   5427 	     possible that DEF_REGULAR is not set now but will be set
   5428 	     later (it is never cleared).  In case of a weak definition,
   5429 	     DEF_REGULAR may be cleared later by a strong definition in
   5430 	     a shared library.  We account for that possibility below by
   5431 	     storing information in the dyn_relocs field of the hash
   5432 	     table entry.  A similar situation occurs when creating
   5433 	     shared libraries and symbol visibility changes render the
   5434 	     symbol local.
   5435 
   5436 	     If on the other hand, we are creating an executable, we
   5437 	     may need to keep relocations for symbols satisfied by a
   5438 	     dynamic library if we manage to avoid copy relocs for the
   5439 	     symbol.  */
   5440 	dodyn:
   5441 	  if ((info->shared
   5442 	       && (must_be_dyn_reloc (info, r_type)
   5443 		   || (h != NULL
   5444 		       && (! info->symbolic
   5445 			   || h->root.type == bfd_link_hash_defweak
   5446 			   || !h->def_regular))))
   5447 	      || (ELIMINATE_COPY_RELOCS
   5448 		  && !info->shared
   5449 		  && h != NULL
   5450 		  && (h->root.type == bfd_link_hash_defweak
   5451 		      || !h->def_regular))
   5452 	      || (!info->shared
   5453 		  && ifunc != NULL))
   5454 	    {
   5455 	      struct elf_dyn_relocs *p;
   5456 	      struct elf_dyn_relocs **head;
   5457 
   5458 	      /* We must copy these reloc types into the output file.
   5459 		 Create a reloc section in dynobj and make room for
   5460 		 this reloc.  */
   5461 	      if (sreloc == NULL)
   5462 		{
   5463 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5464 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
   5465 
   5466 		  if (sreloc == NULL)
   5467 		    return FALSE;
   5468 		}
   5469 
   5470 	      /* If this is a global symbol, we count the number of
   5471 		 relocations we need for this symbol.  */
   5472 	      if (h != NULL)
   5473 		{
   5474 		  head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   5475 		}
   5476 	      else
   5477 		{
   5478 		  /* Track dynamic relocs needed for local syms too.
   5479 		     We really need local syms available to do this
   5480 		     easily.  Oh well.  */
   5481 		  asection *s;
   5482 		  void *vpp;
   5483 		  Elf_Internal_Sym *isym;
   5484 
   5485 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5486 						abfd, r_symndx);
   5487 		  if (isym == NULL)
   5488 		    return FALSE;
   5489 
   5490 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5491 		  if (s == NULL)
   5492 		    s = sec;
   5493 
   5494 		  vpp = &elf_section_data (s)->local_dynrel;
   5495 		  head = (struct elf_dyn_relocs **) vpp;
   5496 		}
   5497 
   5498 	      p = *head;
   5499 	      if (p == NULL || p->sec != sec)
   5500 		{
   5501 		  p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5502 		  if (p == NULL)
   5503 		    return FALSE;
   5504 		  p->next = *head;
   5505 		  *head = p;
   5506 		  p->sec = sec;
   5507 		  p->count = 0;
   5508 		  p->pc_count = 0;
   5509 		}
   5510 
   5511 	      p->count += 1;
   5512 	      if (!must_be_dyn_reloc (info, r_type))
   5513 		p->pc_count += 1;
   5514 	    }
   5515 	  break;
   5516 
   5517 	default:
   5518 	  break;
   5519 	}
   5520     }
   5521 
   5522   return TRUE;
   5523 }
   5524 
   5525 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   5526    of the code entry point, and its section.  */
   5527 
   5528 static bfd_vma
   5529 opd_entry_value (asection *opd_sec,
   5530 		 bfd_vma offset,
   5531 		 asection **code_sec,
   5532 		 bfd_vma *code_off,
   5533 		 bfd_boolean in_code_sec)
   5534 {
   5535   bfd *opd_bfd = opd_sec->owner;
   5536   Elf_Internal_Rela *relocs;
   5537   Elf_Internal_Rela *lo, *hi, *look;
   5538   bfd_vma val;
   5539 
   5540   /* No relocs implies we are linking a --just-symbols object, or looking
   5541      at a final linked executable with addr2line or somesuch.  */
   5542   if (opd_sec->reloc_count == 0)
   5543     {
   5544       char buf[8];
   5545 
   5546       if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
   5547 	return (bfd_vma) -1;
   5548 
   5549       val = bfd_get_64 (opd_bfd, buf);
   5550       if (code_sec != NULL)
   5551 	{
   5552 	  asection *sec, *likely = NULL;
   5553 
   5554 	  if (in_code_sec)
   5555 	    {
   5556 	      sec = *code_sec;
   5557 	      if (sec->vma <= val
   5558 		  && val < sec->vma + sec->size)
   5559 		likely = sec;
   5560 	      else
   5561 		val = -1;
   5562 	    }
   5563 	  else
   5564 	    for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   5565 	      if (sec->vma <= val
   5566 		  && (sec->flags & SEC_LOAD) != 0
   5567 		  && (sec->flags & SEC_ALLOC) != 0)
   5568 		likely = sec;
   5569 	  if (likely != NULL)
   5570 	    {
   5571 	      *code_sec = likely;
   5572 	      if (code_off != NULL)
   5573 		*code_off = val - likely->vma;
   5574 	    }
   5575 	}
   5576       return val;
   5577     }
   5578 
   5579   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   5580 
   5581   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
   5582   if (relocs == NULL)
   5583     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
   5584 
   5585   /* Go find the opd reloc at the sym address.  */
   5586   lo = relocs;
   5587   BFD_ASSERT (lo != NULL);
   5588   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   5589   val = (bfd_vma) -1;
   5590   while (lo < hi)
   5591     {
   5592       look = lo + (hi - lo) / 2;
   5593       if (look->r_offset < offset)
   5594 	lo = look + 1;
   5595       else if (look->r_offset > offset)
   5596 	hi = look;
   5597       else
   5598 	{
   5599 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   5600 
   5601 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   5602 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   5603 	    {
   5604 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   5605 	      asection *sec;
   5606 
   5607 	      if (symndx < symtab_hdr->sh_info
   5608 		  || elf_sym_hashes (opd_bfd) == NULL)
   5609 		{
   5610 		  Elf_Internal_Sym *sym;
   5611 
   5612 		  sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   5613 		  if (sym == NULL)
   5614 		    {
   5615 		      size_t symcnt = symtab_hdr->sh_info;
   5616 		      if (elf_sym_hashes (opd_bfd) == NULL)
   5617 			symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
   5618 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
   5619 						  0, NULL, NULL, NULL);
   5620 		      if (sym == NULL)
   5621 			break;
   5622 		      symtab_hdr->contents = (bfd_byte *) sym;
   5623 		    }
   5624 
   5625 		  sym += symndx;
   5626 		  val = sym->st_value;
   5627 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   5628 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   5629 		}
   5630 	      else
   5631 		{
   5632 		  struct elf_link_hash_entry **sym_hashes;
   5633 		  struct elf_link_hash_entry *rh;
   5634 
   5635 		  sym_hashes = elf_sym_hashes (opd_bfd);
   5636 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   5637 		  rh = elf_follow_link (rh);
   5638 		  BFD_ASSERT (rh->root.type == bfd_link_hash_defined
   5639 			      || rh->root.type == bfd_link_hash_defweak);
   5640 		  val = rh->root.u.def.value;
   5641 		  sec = rh->root.u.def.section;
   5642 		}
   5643 	      val += look->r_addend;
   5644 	      if (code_off != NULL)
   5645 		*code_off = val;
   5646 	      if (code_sec != NULL)
   5647 		{
   5648 		  if (in_code_sec && *code_sec != sec)
   5649 		    return -1;
   5650 		  else
   5651 		    *code_sec = sec;
   5652 		}
   5653 	      if (sec != NULL && sec->output_section != NULL)
   5654 		val += sec->output_section->vma + sec->output_offset;
   5655 	    }
   5656 	  break;
   5657 	}
   5658     }
   5659 
   5660   return val;
   5661 }
   5662 
   5663 /* If the ELF symbol SYM might be a function in SEC, return the
   5664    function size and set *CODE_OFF to the function's entry point,
   5665    otherwise return zero.  */
   5666 
   5667 static bfd_size_type
   5668 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   5669 			      bfd_vma *code_off)
   5670 {
   5671   bfd_size_type size;
   5672 
   5673   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   5674 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
   5675     return 0;
   5676 
   5677   size = 0;
   5678   if (!(sym->flags & BSF_SYNTHETIC))
   5679     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   5680 
   5681   if (strcmp (sym->section->name, ".opd") == 0)
   5682     {
   5683       if (opd_entry_value (sym->section, sym->value,
   5684 			   &sec, code_off, TRUE) == (bfd_vma) -1)
   5685 	return 0;
   5686       /* An old ABI binary with dot-syms has a size of 24 on the .opd
   5687 	 symbol.  This size has nothing to do with the code size of the
   5688 	 function, which is what we're supposed to return, but the
   5689 	 code size isn't available without looking up the dot-sym.
   5690 	 However, doing that would be a waste of time particularly
   5691 	 since elf_find_function will look at the dot-sym anyway.
   5692 	 Now, elf_find_function will keep the largest size of any
   5693 	 function sym found at the code address of interest, so return
   5694 	 1 here to avoid it incorrectly caching a larger function size
   5695 	 for a small function.  This does mean we return the wrong
   5696 	 size for a new-ABI function of size 24, but all that does is
   5697 	 disable caching for such functions.  */
   5698       if (size == 24)
   5699 	size = 1;
   5700     }
   5701   else
   5702     {
   5703       if (sym->section != sec)
   5704 	return 0;
   5705       *code_off = sym->value;
   5706     }
   5707   if (size == 0)
   5708     size = 1;
   5709   return size;
   5710 }
   5711 
   5712 /* Return true if symbol is defined in a regular object file.  */
   5713 
   5714 static bfd_boolean
   5715 is_static_defined (struct elf_link_hash_entry *h)
   5716 {
   5717   return ((h->root.type == bfd_link_hash_defined
   5718 	   || h->root.type == bfd_link_hash_defweak)
   5719 	  && h->root.u.def.section != NULL
   5720 	  && h->root.u.def.section->output_section != NULL);
   5721 }
   5722 
   5723 /* If FDH is a function descriptor symbol, return the associated code
   5724    entry symbol if it is defined.  Return NULL otherwise.  */
   5725 
   5726 static struct ppc_link_hash_entry *
   5727 defined_code_entry (struct ppc_link_hash_entry *fdh)
   5728 {
   5729   if (fdh->is_func_descriptor)
   5730     {
   5731       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   5732       if (fh->elf.root.type == bfd_link_hash_defined
   5733 	  || fh->elf.root.type == bfd_link_hash_defweak)
   5734 	return fh;
   5735     }
   5736   return NULL;
   5737 }
   5738 
   5739 /* If FH is a function code entry symbol, return the associated
   5740    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   5741 
   5742 static struct ppc_link_hash_entry *
   5743 defined_func_desc (struct ppc_link_hash_entry *fh)
   5744 {
   5745   if (fh->oh != NULL
   5746       && fh->oh->is_func_descriptor)
   5747     {
   5748       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   5749       if (fdh->elf.root.type == bfd_link_hash_defined
   5750 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   5751 	return fdh;
   5752     }
   5753   return NULL;
   5754 }
   5755 
   5756 /* Mark all our entry sym sections, both opd and code section.  */
   5757 
   5758 static void
   5759 ppc64_elf_gc_keep (struct bfd_link_info *info)
   5760 {
   5761   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5762   struct bfd_sym_chain *sym;
   5763 
   5764   if (htab == NULL)
   5765     return;
   5766 
   5767   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   5768     {
   5769       struct ppc_link_hash_entry *eh, *fh;
   5770       asection *sec;
   5771 
   5772       eh = (struct ppc_link_hash_entry *)
   5773 	elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
   5774       if (eh == NULL)
   5775 	continue;
   5776       if (eh->elf.root.type != bfd_link_hash_defined
   5777 	  && eh->elf.root.type != bfd_link_hash_defweak)
   5778 	continue;
   5779 
   5780       fh = defined_code_entry (eh);
   5781       if (fh != NULL)
   5782 	{
   5783 	  sec = fh->elf.root.u.def.section;
   5784 	  sec->flags |= SEC_KEEP;
   5785 	}
   5786       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5787 	       && opd_entry_value (eh->elf.root.u.def.section,
   5788 				   eh->elf.root.u.def.value,
   5789 				   &sec, NULL, FALSE) != (bfd_vma) -1)
   5790 	sec->flags |= SEC_KEEP;
   5791 
   5792       sec = eh->elf.root.u.def.section;
   5793       sec->flags |= SEC_KEEP;
   5794     }
   5795 }
   5796 
   5797 /* Mark sections containing dynamically referenced symbols.  When
   5798    building shared libraries, we must assume that any visible symbol is
   5799    referenced.  */
   5800 
   5801 static bfd_boolean
   5802 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   5803 {
   5804   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   5805   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   5806   struct ppc_link_hash_entry *fdh;
   5807 
   5808   /* Dynamic linking info is on the func descriptor sym.  */
   5809   fdh = defined_func_desc (eh);
   5810   if (fdh != NULL)
   5811     eh = fdh;
   5812 
   5813   if ((eh->elf.root.type == bfd_link_hash_defined
   5814        || eh->elf.root.type == bfd_link_hash_defweak)
   5815       && (eh->elf.ref_dynamic
   5816 	  || (!info->executable
   5817 	      && eh->elf.def_regular
   5818 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   5819 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
   5820 	      && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
   5821 		  || !bfd_hide_sym_by_version (info->version_info,
   5822 					       eh->elf.root.root.string)))))
   5823     {
   5824       asection *code_sec;
   5825       struct ppc_link_hash_entry *fh;
   5826 
   5827       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   5828 
   5829       /* Function descriptor syms cause the associated
   5830 	 function code sym section to be marked.  */
   5831       fh = defined_code_entry (eh);
   5832       if (fh != NULL)
   5833 	{
   5834 	  code_sec = fh->elf.root.u.def.section;
   5835 	  code_sec->flags |= SEC_KEEP;
   5836 	}
   5837       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5838 	       && opd_entry_value (eh->elf.root.u.def.section,
   5839 				   eh->elf.root.u.def.value,
   5840 				   &code_sec, NULL, FALSE) != (bfd_vma) -1)
   5841 	code_sec->flags |= SEC_KEEP;
   5842     }
   5843 
   5844   return TRUE;
   5845 }
   5846 
   5847 /* Return the section that should be marked against GC for a given
   5848    relocation.  */
   5849 
   5850 static asection *
   5851 ppc64_elf_gc_mark_hook (asection *sec,
   5852 			struct bfd_link_info *info,
   5853 			Elf_Internal_Rela *rel,
   5854 			struct elf_link_hash_entry *h,
   5855 			Elf_Internal_Sym *sym)
   5856 {
   5857   asection *rsec;
   5858 
   5859   /* Syms return NULL if we're marking .opd, so we avoid marking all
   5860      function sections, as all functions are referenced in .opd.  */
   5861   rsec = NULL;
   5862   if (get_opd_info (sec) != NULL)
   5863     return rsec;
   5864 
   5865   if (h != NULL)
   5866     {
   5867       enum elf_ppc64_reloc_type r_type;
   5868       struct ppc_link_hash_entry *eh, *fh, *fdh;
   5869 
   5870       r_type = ELF64_R_TYPE (rel->r_info);
   5871       switch (r_type)
   5872 	{
   5873 	case R_PPC64_GNU_VTINHERIT:
   5874 	case R_PPC64_GNU_VTENTRY:
   5875 	  break;
   5876 
   5877 	default:
   5878 	  switch (h->root.type)
   5879 	    {
   5880 	    case bfd_link_hash_defined:
   5881 	    case bfd_link_hash_defweak:
   5882 	      eh = (struct ppc_link_hash_entry *) h;
   5883 	      fdh = defined_func_desc (eh);
   5884 	      if (fdh != NULL)
   5885 		eh = fdh;
   5886 
   5887 	      /* Function descriptor syms cause the associated
   5888 		 function code sym section to be marked.  */
   5889 	      fh = defined_code_entry (eh);
   5890 	      if (fh != NULL)
   5891 		{
   5892 		  /* They also mark their opd section.  */
   5893 		  eh->elf.root.u.def.section->gc_mark = 1;
   5894 
   5895 		  rsec = fh->elf.root.u.def.section;
   5896 		}
   5897 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5898 		       && opd_entry_value (eh->elf.root.u.def.section,
   5899 					   eh->elf.root.u.def.value,
   5900 					   &rsec, NULL, FALSE) != (bfd_vma) -1)
   5901 		eh->elf.root.u.def.section->gc_mark = 1;
   5902 	      else
   5903 		rsec = h->root.u.def.section;
   5904 	      break;
   5905 
   5906 	    case bfd_link_hash_common:
   5907 	      rsec = h->root.u.c.p->section;
   5908 	      break;
   5909 
   5910 	    default:
   5911 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   5912 	    }
   5913 	}
   5914     }
   5915   else
   5916     {
   5917       struct _opd_sec_data *opd;
   5918 
   5919       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   5920       opd = get_opd_info (rsec);
   5921       if (opd != NULL && opd->func_sec != NULL)
   5922 	{
   5923 	  rsec->gc_mark = 1;
   5924 
   5925 	  rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
   5926 	}
   5927     }
   5928 
   5929   return rsec;
   5930 }
   5931 
   5932 /* Update the .got, .plt. and dynamic reloc reference counts for the
   5933    section being removed.  */
   5934 
   5935 static bfd_boolean
   5936 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   5937 			 asection *sec, const Elf_Internal_Rela *relocs)
   5938 {
   5939   struct ppc_link_hash_table *htab;
   5940   Elf_Internal_Shdr *symtab_hdr;
   5941   struct elf_link_hash_entry **sym_hashes;
   5942   struct got_entry **local_got_ents;
   5943   const Elf_Internal_Rela *rel, *relend;
   5944 
   5945   if (info->relocatable)
   5946     return TRUE;
   5947 
   5948   if ((sec->flags & SEC_ALLOC) == 0)
   5949     return TRUE;
   5950 
   5951   elf_section_data (sec)->local_dynrel = NULL;
   5952 
   5953   htab = ppc_hash_table (info);
   5954   if (htab == NULL)
   5955     return FALSE;
   5956 
   5957   symtab_hdr = &elf_symtab_hdr (abfd);
   5958   sym_hashes = elf_sym_hashes (abfd);
   5959   local_got_ents = elf_local_got_ents (abfd);
   5960 
   5961   relend = relocs + sec->reloc_count;
   5962   for (rel = relocs; rel < relend; rel++)
   5963     {
   5964       unsigned long r_symndx;
   5965       enum elf_ppc64_reloc_type r_type;
   5966       struct elf_link_hash_entry *h = NULL;
   5967       unsigned char tls_type = 0;
   5968 
   5969       r_symndx = ELF64_R_SYM (rel->r_info);
   5970       r_type = ELF64_R_TYPE (rel->r_info);
   5971       if (r_symndx >= symtab_hdr->sh_info)
   5972 	{
   5973 	  struct ppc_link_hash_entry *eh;
   5974 	  struct elf_dyn_relocs **pp;
   5975 	  struct elf_dyn_relocs *p;
   5976 
   5977 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5978 	  h = elf_follow_link (h);
   5979 	  eh = (struct ppc_link_hash_entry *) h;
   5980 
   5981 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   5982 	    if (p->sec == sec)
   5983 	      {
   5984 		/* Everything must go for SEC.  */
   5985 		*pp = p->next;
   5986 		break;
   5987 	      }
   5988 	}
   5989 
   5990       if (is_branch_reloc (r_type))
   5991 	{
   5992 	  struct plt_entry **ifunc = NULL;
   5993 	  if (h != NULL)
   5994 	    {
   5995 	      if (h->type == STT_GNU_IFUNC)
   5996 		ifunc = &h->plt.plist;
   5997 	    }
   5998 	  else if (local_got_ents != NULL)
   5999 	    {
   6000 	      struct plt_entry **local_plt = (struct plt_entry **)
   6001 		(local_got_ents + symtab_hdr->sh_info);
   6002 	      unsigned char *local_got_tls_masks = (unsigned char *)
   6003 		(local_plt + symtab_hdr->sh_info);
   6004 	      if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
   6005 		ifunc = local_plt + r_symndx;
   6006 	    }
   6007 	  if (ifunc != NULL)
   6008 	    {
   6009 	      struct plt_entry *ent;
   6010 
   6011 	      for (ent = *ifunc; ent != NULL; ent = ent->next)
   6012 		if (ent->addend == rel->r_addend)
   6013 		  break;
   6014 	      if (ent == NULL)
   6015 		abort ();
   6016 	      if (ent->plt.refcount > 0)
   6017 		ent->plt.refcount -= 1;
   6018 	      continue;
   6019 	    }
   6020 	}
   6021 
   6022       switch (r_type)
   6023 	{
   6024 	case R_PPC64_GOT_TLSLD16:
   6025 	case R_PPC64_GOT_TLSLD16_LO:
   6026 	case R_PPC64_GOT_TLSLD16_HI:
   6027 	case R_PPC64_GOT_TLSLD16_HA:
   6028 	  tls_type = TLS_TLS | TLS_LD;
   6029 	  goto dogot;
   6030 
   6031 	case R_PPC64_GOT_TLSGD16:
   6032 	case R_PPC64_GOT_TLSGD16_LO:
   6033 	case R_PPC64_GOT_TLSGD16_HI:
   6034 	case R_PPC64_GOT_TLSGD16_HA:
   6035 	  tls_type = TLS_TLS | TLS_GD;
   6036 	  goto dogot;
   6037 
   6038 	case R_PPC64_GOT_TPREL16_DS:
   6039 	case R_PPC64_GOT_TPREL16_LO_DS:
   6040 	case R_PPC64_GOT_TPREL16_HI:
   6041 	case R_PPC64_GOT_TPREL16_HA:
   6042 	  tls_type = TLS_TLS | TLS_TPREL;
   6043 	  goto dogot;
   6044 
   6045 	case R_PPC64_GOT_DTPREL16_DS:
   6046 	case R_PPC64_GOT_DTPREL16_LO_DS:
   6047 	case R_PPC64_GOT_DTPREL16_HI:
   6048 	case R_PPC64_GOT_DTPREL16_HA:
   6049 	  tls_type = TLS_TLS | TLS_DTPREL;
   6050 	  goto dogot;
   6051 
   6052 	case R_PPC64_GOT16:
   6053 	case R_PPC64_GOT16_DS:
   6054 	case R_PPC64_GOT16_HA:
   6055 	case R_PPC64_GOT16_HI:
   6056 	case R_PPC64_GOT16_LO:
   6057 	case R_PPC64_GOT16_LO_DS:
   6058 	dogot:
   6059 	  {
   6060 	    struct got_entry *ent;
   6061 
   6062 	    if (h != NULL)
   6063 	      ent = h->got.glist;
   6064 	    else
   6065 	      ent = local_got_ents[r_symndx];
   6066 
   6067 	    for (; ent != NULL; ent = ent->next)
   6068 	      if (ent->addend == rel->r_addend
   6069 		  && ent->owner == abfd
   6070 		  && ent->tls_type == tls_type)
   6071 		break;
   6072 	    if (ent == NULL)
   6073 	      abort ();
   6074 	    if (ent->got.refcount > 0)
   6075 	      ent->got.refcount -= 1;
   6076 	  }
   6077 	  break;
   6078 
   6079 	case R_PPC64_PLT16_HA:
   6080 	case R_PPC64_PLT16_HI:
   6081 	case R_PPC64_PLT16_LO:
   6082 	case R_PPC64_PLT32:
   6083 	case R_PPC64_PLT64:
   6084 	case R_PPC64_REL14:
   6085 	case R_PPC64_REL14_BRNTAKEN:
   6086 	case R_PPC64_REL14_BRTAKEN:
   6087 	case R_PPC64_REL24:
   6088 	  if (h != NULL)
   6089 	    {
   6090 	      struct plt_entry *ent;
   6091 
   6092 	      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6093 		if (ent->addend == rel->r_addend)
   6094 		  break;
   6095 	      if (ent != NULL && ent->plt.refcount > 0)
   6096 		ent->plt.refcount -= 1;
   6097 	    }
   6098 	  break;
   6099 
   6100 	default:
   6101 	  break;
   6102 	}
   6103     }
   6104   return TRUE;
   6105 }
   6106 
   6107 /* The maximum size of .sfpr.  */
   6108 #define SFPR_MAX (218*4)
   6109 
   6110 struct sfpr_def_parms
   6111 {
   6112   const char name[12];
   6113   unsigned char lo, hi;
   6114   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
   6115   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
   6116 };
   6117 
   6118 /* Auto-generate _save*, _rest* functions in .sfpr.  */
   6119 
   6120 static bfd_boolean
   6121 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
   6122 {
   6123   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6124   unsigned int i;
   6125   size_t len = strlen (parm->name);
   6126   bfd_boolean writing = FALSE;
   6127   char sym[16];
   6128 
   6129   if (htab == NULL)
   6130     return FALSE;
   6131 
   6132   memcpy (sym, parm->name, len);
   6133   sym[len + 2] = 0;
   6134 
   6135   for (i = parm->lo; i <= parm->hi; i++)
   6136     {
   6137       struct elf_link_hash_entry *h;
   6138 
   6139       sym[len + 0] = i / 10 + '0';
   6140       sym[len + 1] = i % 10 + '0';
   6141       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
   6142       if (h != NULL
   6143 	  && !h->def_regular)
   6144 	{
   6145 	  h->root.type = bfd_link_hash_defined;
   6146 	  h->root.u.def.section = htab->sfpr;
   6147 	  h->root.u.def.value = htab->sfpr->size;
   6148 	  h->type = STT_FUNC;
   6149 	  h->def_regular = 1;
   6150 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   6151 	  writing = TRUE;
   6152 	  if (htab->sfpr->contents == NULL)
   6153 	    {
   6154 	      htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   6155 	      if (htab->sfpr->contents == NULL)
   6156 		return FALSE;
   6157 	    }
   6158 	}
   6159       if (writing)
   6160 	{
   6161 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   6162 	  if (i != parm->hi)
   6163 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   6164 	  else
   6165 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   6166 	  htab->sfpr->size = p - htab->sfpr->contents;
   6167 	}
   6168     }
   6169 
   6170   return TRUE;
   6171 }
   6172 
   6173 static bfd_byte *
   6174 savegpr0 (bfd *abfd, bfd_byte *p, int r)
   6175 {
   6176   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6177   return p + 4;
   6178 }
   6179 
   6180 static bfd_byte *
   6181 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6182 {
   6183   p = savegpr0 (abfd, p, r);
   6184   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6185   p = p + 4;
   6186   bfd_put_32 (abfd, BLR, p);
   6187   return p + 4;
   6188 }
   6189 
   6190 static bfd_byte *
   6191 restgpr0 (bfd *abfd, bfd_byte *p, int r)
   6192 {
   6193   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6194   return p + 4;
   6195 }
   6196 
   6197 static bfd_byte *
   6198 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6199 {
   6200   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6201   p = p + 4;
   6202   p = restgpr0 (abfd, p, r);
   6203   bfd_put_32 (abfd, MTLR_R0, p);
   6204   p = p + 4;
   6205   if (r == 29)
   6206     {
   6207       p = restgpr0 (abfd, p, 30);
   6208       p = restgpr0 (abfd, p, 31);
   6209     }
   6210   bfd_put_32 (abfd, BLR, p);
   6211   return p + 4;
   6212 }
   6213 
   6214 static bfd_byte *
   6215 savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6216 {
   6217   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6218   return p + 4;
   6219 }
   6220 
   6221 static bfd_byte *
   6222 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6223 {
   6224   p = savegpr1 (abfd, p, r);
   6225   bfd_put_32 (abfd, BLR, p);
   6226   return p + 4;
   6227 }
   6228 
   6229 static bfd_byte *
   6230 restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6231 {
   6232   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6233   return p + 4;
   6234 }
   6235 
   6236 static bfd_byte *
   6237 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6238 {
   6239   p = restgpr1 (abfd, p, r);
   6240   bfd_put_32 (abfd, BLR, p);
   6241   return p + 4;
   6242 }
   6243 
   6244 static bfd_byte *
   6245 savefpr (bfd *abfd, bfd_byte *p, int r)
   6246 {
   6247   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6248   return p + 4;
   6249 }
   6250 
   6251 static bfd_byte *
   6252 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6253 {
   6254   p = savefpr (abfd, p, r);
   6255   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6256   p = p + 4;
   6257   bfd_put_32 (abfd, BLR, p);
   6258   return p + 4;
   6259 }
   6260 
   6261 static bfd_byte *
   6262 restfpr (bfd *abfd, bfd_byte *p, int r)
   6263 {
   6264   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6265   return p + 4;
   6266 }
   6267 
   6268 static bfd_byte *
   6269 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6270 {
   6271   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6272   p = p + 4;
   6273   p = restfpr (abfd, p, r);
   6274   bfd_put_32 (abfd, MTLR_R0, p);
   6275   p = p + 4;
   6276   if (r == 29)
   6277     {
   6278       p = restfpr (abfd, p, 30);
   6279       p = restfpr (abfd, p, 31);
   6280     }
   6281   bfd_put_32 (abfd, BLR, p);
   6282   return p + 4;
   6283 }
   6284 
   6285 static bfd_byte *
   6286 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6287 {
   6288   p = savefpr (abfd, p, r);
   6289   bfd_put_32 (abfd, BLR, p);
   6290   return p + 4;
   6291 }
   6292 
   6293 static bfd_byte *
   6294 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6295 {
   6296   p = restfpr (abfd, p, r);
   6297   bfd_put_32 (abfd, BLR, p);
   6298   return p + 4;
   6299 }
   6300 
   6301 static bfd_byte *
   6302 savevr (bfd *abfd, bfd_byte *p, int r)
   6303 {
   6304   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6305   p = p + 4;
   6306   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6307   return p + 4;
   6308 }
   6309 
   6310 static bfd_byte *
   6311 savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6312 {
   6313   p = savevr (abfd, p, r);
   6314   bfd_put_32 (abfd, BLR, p);
   6315   return p + 4;
   6316 }
   6317 
   6318 static bfd_byte *
   6319 restvr (bfd *abfd, bfd_byte *p, int r)
   6320 {
   6321   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6322   p = p + 4;
   6323   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6324   return p + 4;
   6325 }
   6326 
   6327 static bfd_byte *
   6328 restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6329 {
   6330   p = restvr (abfd, p, r);
   6331   bfd_put_32 (abfd, BLR, p);
   6332   return p + 4;
   6333 }
   6334 
   6335 /* Called via elf_link_hash_traverse to transfer dynamic linking
   6336    information on function code symbol entries to their corresponding
   6337    function descriptor symbol entries.  */
   6338 
   6339 static bfd_boolean
   6340 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6341 {
   6342   struct bfd_link_info *info;
   6343   struct ppc_link_hash_table *htab;
   6344   struct plt_entry *ent;
   6345   struct ppc_link_hash_entry *fh;
   6346   struct ppc_link_hash_entry *fdh;
   6347   bfd_boolean force_local;
   6348 
   6349   fh = (struct ppc_link_hash_entry *) h;
   6350   if (fh->elf.root.type == bfd_link_hash_indirect)
   6351     return TRUE;
   6352 
   6353   info = inf;
   6354   htab = ppc_hash_table (info);
   6355   if (htab == NULL)
   6356     return FALSE;
   6357 
   6358   /* Resolve undefined references to dot-symbols as the value
   6359      in the function descriptor, if we have one in a regular object.
   6360      This is to satisfy cases like ".quad .foo".  Calls to functions
   6361      in dynamic objects are handled elsewhere.  */
   6362   if (fh->elf.root.type == bfd_link_hash_undefweak
   6363       && fh->was_undefined
   6364       && (fdh = defined_func_desc (fh)) != NULL
   6365       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6366       && opd_entry_value (fdh->elf.root.u.def.section,
   6367 			  fdh->elf.root.u.def.value,
   6368 			  &fh->elf.root.u.def.section,
   6369 			  &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
   6370     {
   6371       fh->elf.root.type = fdh->elf.root.type;
   6372       fh->elf.forced_local = 1;
   6373       fh->elf.def_regular = fdh->elf.def_regular;
   6374       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6375     }
   6376 
   6377   /* If this is a function code symbol, transfer dynamic linking
   6378      information to the function descriptor symbol.  */
   6379   if (!fh->is_func)
   6380     return TRUE;
   6381 
   6382   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6383     if (ent->plt.refcount > 0)
   6384       break;
   6385   if (ent == NULL
   6386       || fh->elf.root.root.string[0] != '.'
   6387       || fh->elf.root.root.string[1] == '\0')
   6388     return TRUE;
   6389 
   6390   /* Find the corresponding function descriptor symbol.  Create it
   6391      as undefined if necessary.  */
   6392 
   6393   fdh = lookup_fdh (fh, htab);
   6394   if (fdh == NULL
   6395       && !info->executable
   6396       && (fh->elf.root.type == bfd_link_hash_undefined
   6397 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6398     {
   6399       fdh = make_fdh (info, fh);
   6400       if (fdh == NULL)
   6401 	return FALSE;
   6402     }
   6403 
   6404   /* Fake function descriptors are made undefweak.  If the function
   6405      code symbol is strong undefined, make the fake sym the same.
   6406      If the function code symbol is defined, then force the fake
   6407      descriptor local;  We can't support overriding of symbols in a
   6408      shared library on a fake descriptor.  */
   6409 
   6410   if (fdh != NULL
   6411       && fdh->fake
   6412       && fdh->elf.root.type == bfd_link_hash_undefweak)
   6413     {
   6414       if (fh->elf.root.type == bfd_link_hash_undefined)
   6415 	{
   6416 	  fdh->elf.root.type = bfd_link_hash_undefined;
   6417 	  bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
   6418 	}
   6419       else if (fh->elf.root.type == bfd_link_hash_defined
   6420 	       || fh->elf.root.type == bfd_link_hash_defweak)
   6421 	{
   6422 	  _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
   6423 	}
   6424     }
   6425 
   6426   if (fdh != NULL
   6427       && !fdh->elf.forced_local
   6428       && (!info->executable
   6429 	  || fdh->elf.def_dynamic
   6430 	  || fdh->elf.ref_dynamic
   6431 	  || (fdh->elf.root.type == bfd_link_hash_undefweak
   6432 	      && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
   6433     {
   6434       if (fdh->elf.dynindx == -1)
   6435 	if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   6436 	  return FALSE;
   6437       fdh->elf.ref_regular |= fh->elf.ref_regular;
   6438       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   6439       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   6440       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   6441       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
   6442 	{
   6443 	  move_plt_plist (fh, fdh);
   6444 	  fdh->elf.needs_plt = 1;
   6445 	}
   6446       fdh->is_func_descriptor = 1;
   6447       fdh->oh = fh;
   6448       fh->oh = fdh;
   6449     }
   6450 
   6451   /* Now that the info is on the function descriptor, clear the
   6452      function code sym info.  Any function code syms for which we
   6453      don't have a definition in a regular file, we force local.
   6454      This prevents a shared library from exporting syms that have
   6455      been imported from another library.  Function code syms that
   6456      are really in the library we must leave global to prevent the
   6457      linker dragging in a definition from a static library.  */
   6458   force_local = (!fh->elf.def_regular
   6459 		 || fdh == NULL
   6460 		 || !fdh->elf.def_regular
   6461 		 || fdh->elf.forced_local);
   6462   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6463 
   6464   return TRUE;
   6465 }
   6466 
   6467 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   6468    this hook to a) provide some gcc support functions, and b) transfer
   6469    dynamic linking information gathered so far on function code symbol
   6470    entries, to their corresponding function descriptor symbol entries.  */
   6471 
   6472 static bfd_boolean
   6473 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
   6474 			    struct bfd_link_info *info)
   6475 {
   6476   struct ppc_link_hash_table *htab;
   6477   unsigned int i;
   6478   static const struct sfpr_def_parms funcs[] =
   6479     {
   6480       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   6481       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   6482       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   6483       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   6484       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   6485       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   6486       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   6487       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   6488       { "._savef", 14, 31, savefpr, savefpr1_tail },
   6489       { "._restf", 14, 31, restfpr, restfpr1_tail },
   6490       { "_savevr_", 20, 31, savevr, savevr_tail },
   6491       { "_restvr_", 20, 31, restvr, restvr_tail }
   6492     };
   6493 
   6494   htab = ppc_hash_table (info);
   6495   if (htab == NULL)
   6496     return FALSE;
   6497 
   6498   if (htab->sfpr == NULL)
   6499     /* We don't have any relocs.  */
   6500     return TRUE;
   6501 
   6502   /* Provide any missing _save* and _rest* functions.  */
   6503   htab->sfpr->size = 0;
   6504   if (!info->relocatable)
   6505     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
   6506       if (!sfpr_define (info, &funcs[i]))
   6507 	return FALSE;
   6508 
   6509   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   6510 
   6511   if (htab->sfpr->size == 0)
   6512     htab->sfpr->flags |= SEC_EXCLUDE;
   6513 
   6514   return TRUE;
   6515 }
   6516 
   6517 /* Adjust a symbol defined by a dynamic object and referenced by a
   6518    regular object.  The current definition is in some section of the
   6519    dynamic object, but we're not including those sections.  We have to
   6520    change the definition to something the rest of the link can
   6521    understand.  */
   6522 
   6523 static bfd_boolean
   6524 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   6525 				 struct elf_link_hash_entry *h)
   6526 {
   6527   struct ppc_link_hash_table *htab;
   6528   asection *s;
   6529 
   6530   htab = ppc_hash_table (info);
   6531   if (htab == NULL)
   6532     return FALSE;
   6533 
   6534   /* Deal with function syms.  */
   6535   if (h->type == STT_FUNC
   6536       || h->type == STT_GNU_IFUNC
   6537       || h->needs_plt)
   6538     {
   6539       /* Clear procedure linkage table information for any symbol that
   6540 	 won't need a .plt entry.  */
   6541       struct plt_entry *ent;
   6542       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6543 	if (ent->plt.refcount > 0)
   6544 	  break;
   6545       if (ent == NULL
   6546 	  || (h->type != STT_GNU_IFUNC
   6547 	      && (SYMBOL_CALLS_LOCAL (info, h)
   6548 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   6549 		      && h->root.type == bfd_link_hash_undefweak))))
   6550 	{
   6551 	  h->plt.plist = NULL;
   6552 	  h->needs_plt = 0;
   6553 	}
   6554     }
   6555   else
   6556     h->plt.plist = NULL;
   6557 
   6558   /* If this is a weak symbol, and there is a real definition, the
   6559      processor independent code will have arranged for us to see the
   6560      real definition first, and we can just use the same value.  */
   6561   if (h->u.weakdef != NULL)
   6562     {
   6563       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   6564 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   6565       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   6566       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   6567       if (ELIMINATE_COPY_RELOCS)
   6568 	h->non_got_ref = h->u.weakdef->non_got_ref;
   6569       return TRUE;
   6570     }
   6571 
   6572   /* If we are creating a shared library, we must presume that the
   6573      only references to the symbol are via the global offset table.
   6574      For such cases we need not do anything here; the relocations will
   6575      be handled correctly by relocate_section.  */
   6576   if (info->shared)
   6577     return TRUE;
   6578 
   6579   /* If there are no references to this symbol that do not use the
   6580      GOT, we don't need to generate a copy reloc.  */
   6581   if (!h->non_got_ref)
   6582     return TRUE;
   6583 
   6584   /* Don't generate a copy reloc for symbols defined in the executable.  */
   6585   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
   6586     return TRUE;
   6587 
   6588   if (ELIMINATE_COPY_RELOCS)
   6589     {
   6590       struct ppc_link_hash_entry * eh;
   6591       struct elf_dyn_relocs *p;
   6592 
   6593       eh = (struct ppc_link_hash_entry *) h;
   6594       for (p = eh->dyn_relocs; p != NULL; p = p->next)
   6595 	{
   6596 	  s = p->sec->output_section;
   6597 	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
   6598 	    break;
   6599 	}
   6600 
   6601       /* If we didn't find any dynamic relocs in read-only sections, then
   6602 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   6603       if (p == NULL)
   6604 	{
   6605 	  h->non_got_ref = 0;
   6606 	  return TRUE;
   6607 	}
   6608     }
   6609 
   6610   if (h->plt.plist != NULL)
   6611     {
   6612       /* We should never get here, but unfortunately there are versions
   6613 	 of gcc out there that improperly (for this ABI) put initialized
   6614 	 function pointers, vtable refs and suchlike in read-only
   6615 	 sections.  Allow them to proceed, but warn that this might
   6616 	 break at runtime.  */
   6617       info->callbacks->einfo
   6618 	(_("%P: copy reloc against `%T' requires lazy plt linking; "
   6619 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
   6620 	 h->root.root.string);
   6621     }
   6622 
   6623   /* This is a reference to a symbol defined by a dynamic object which
   6624      is not a function.  */
   6625 
   6626   /* We must allocate the symbol in our .dynbss section, which will
   6627      become part of the .bss section of the executable.  There will be
   6628      an entry for this symbol in the .dynsym section.  The dynamic
   6629      object will contain position independent code, so all references
   6630      from the dynamic object to this symbol will go through the global
   6631      offset table.  The dynamic linker will use the .dynsym entry to
   6632      determine the address it must put in the global offset table, so
   6633      both the dynamic object and the regular object will refer to the
   6634      same memory location for the variable.  */
   6635 
   6636   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
   6637      to copy the initial value out of the dynamic object and into the
   6638      runtime process image.  We need to remember the offset into the
   6639      .rela.bss section we are going to use.  */
   6640   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   6641     {
   6642       htab->relbss->size += sizeof (Elf64_External_Rela);
   6643       h->needs_copy = 1;
   6644     }
   6645 
   6646   s = htab->dynbss;
   6647 
   6648   return _bfd_elf_adjust_dynamic_copy (h, s);
   6649 }
   6650 
   6651 /* If given a function descriptor symbol, hide both the function code
   6652    sym and the descriptor.  */
   6653 static void
   6654 ppc64_elf_hide_symbol (struct bfd_link_info *info,
   6655 		       struct elf_link_hash_entry *h,
   6656 		       bfd_boolean force_local)
   6657 {
   6658   struct ppc_link_hash_entry *eh;
   6659   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   6660 
   6661   eh = (struct ppc_link_hash_entry *) h;
   6662   if (eh->is_func_descriptor)
   6663     {
   6664       struct ppc_link_hash_entry *fh = eh->oh;
   6665 
   6666       if (fh == NULL)
   6667 	{
   6668 	  const char *p, *q;
   6669 	  struct ppc_link_hash_table *htab;
   6670 	  char save;
   6671 
   6672 	  /* We aren't supposed to use alloca in BFD because on
   6673 	     systems which do not have alloca the version in libiberty
   6674 	     calls xmalloc, which might cause the program to crash
   6675 	     when it runs out of memory.  This function doesn't have a
   6676 	     return status, so there's no way to gracefully return an
   6677 	     error.  So cheat.  We know that string[-1] can be safely
   6678 	     accessed;  It's either a string in an ELF string table,
   6679 	     or allocated in an objalloc structure.  */
   6680 
   6681 	  p = eh->elf.root.root.string - 1;
   6682 	  save = *p;
   6683 	  *(char *) p = '.';
   6684 	  htab = ppc_hash_table (info);
   6685 	  if (htab == NULL)
   6686 	    return;
   6687 
   6688 	  fh = (struct ppc_link_hash_entry *)
   6689 	    elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6690 	  *(char *) p = save;
   6691 
   6692 	  /* Unfortunately, if it so happens that the string we were
   6693 	     looking for was allocated immediately before this string,
   6694 	     then we overwrote the string terminator.  That's the only
   6695 	     reason the lookup should fail.  */
   6696 	  if (fh == NULL)
   6697 	    {
   6698 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   6699 	      while (q >= eh->elf.root.root.string && *q == *p)
   6700 		--q, --p;
   6701 	      if (q < eh->elf.root.root.string && *p == '.')
   6702 		fh = (struct ppc_link_hash_entry *)
   6703 		  elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6704 	    }
   6705 	  if (fh != NULL)
   6706 	    {
   6707 	      eh->oh = fh;
   6708 	      fh->oh = eh;
   6709 	    }
   6710 	}
   6711       if (fh != NULL)
   6712 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6713     }
   6714 }
   6715 
   6716 static bfd_boolean
   6717 get_sym_h (struct elf_link_hash_entry **hp,
   6718 	   Elf_Internal_Sym **symp,
   6719 	   asection **symsecp,
   6720 	   unsigned char **tls_maskp,
   6721 	   Elf_Internal_Sym **locsymsp,
   6722 	   unsigned long r_symndx,
   6723 	   bfd *ibfd)
   6724 {
   6725   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   6726 
   6727   if (r_symndx >= symtab_hdr->sh_info)
   6728     {
   6729       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   6730       struct elf_link_hash_entry *h;
   6731 
   6732       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6733       h = elf_follow_link (h);
   6734 
   6735       if (hp != NULL)
   6736 	*hp = h;
   6737 
   6738       if (symp != NULL)
   6739 	*symp = NULL;
   6740 
   6741       if (symsecp != NULL)
   6742 	{
   6743 	  asection *symsec = NULL;
   6744 	  if (h->root.type == bfd_link_hash_defined
   6745 	      || h->root.type == bfd_link_hash_defweak)
   6746 	    symsec = h->root.u.def.section;
   6747 	  *symsecp = symsec;
   6748 	}
   6749 
   6750       if (tls_maskp != NULL)
   6751 	{
   6752 	  struct ppc_link_hash_entry *eh;
   6753 
   6754 	  eh = (struct ppc_link_hash_entry *) h;
   6755 	  *tls_maskp = &eh->tls_mask;
   6756 	}
   6757     }
   6758   else
   6759     {
   6760       Elf_Internal_Sym *sym;
   6761       Elf_Internal_Sym *locsyms = *locsymsp;
   6762 
   6763       if (locsyms == NULL)
   6764 	{
   6765 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   6766 	  if (locsyms == NULL)
   6767 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   6768 					    symtab_hdr->sh_info,
   6769 					    0, NULL, NULL, NULL);
   6770 	  if (locsyms == NULL)
   6771 	    return FALSE;
   6772 	  *locsymsp = locsyms;
   6773 	}
   6774       sym = locsyms + r_symndx;
   6775 
   6776       if (hp != NULL)
   6777 	*hp = NULL;
   6778 
   6779       if (symp != NULL)
   6780 	*symp = sym;
   6781 
   6782       if (symsecp != NULL)
   6783 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   6784 
   6785       if (tls_maskp != NULL)
   6786 	{
   6787 	  struct got_entry **lgot_ents;
   6788 	  unsigned char *tls_mask;
   6789 
   6790 	  tls_mask = NULL;
   6791 	  lgot_ents = elf_local_got_ents (ibfd);
   6792 	  if (lgot_ents != NULL)
   6793 	    {
   6794 	      struct plt_entry **local_plt = (struct plt_entry **)
   6795 		(lgot_ents + symtab_hdr->sh_info);
   6796 	      unsigned char *lgot_masks = (unsigned char *)
   6797 		(local_plt + symtab_hdr->sh_info);
   6798 	      tls_mask = &lgot_masks[r_symndx];
   6799 	    }
   6800 	  *tls_maskp = tls_mask;
   6801 	}
   6802     }
   6803   return TRUE;
   6804 }
   6805 
   6806 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   6807    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   6808    type suitable for optimization, and 1 otherwise.  */
   6809 
   6810 static int
   6811 get_tls_mask (unsigned char **tls_maskp,
   6812 	      unsigned long *toc_symndx,
   6813 	      bfd_vma *toc_addend,
   6814 	      Elf_Internal_Sym **locsymsp,
   6815 	      const Elf_Internal_Rela *rel,
   6816 	      bfd *ibfd)
   6817 {
   6818   unsigned long r_symndx;
   6819   int next_r;
   6820   struct elf_link_hash_entry *h;
   6821   Elf_Internal_Sym *sym;
   6822   asection *sec;
   6823   bfd_vma off;
   6824 
   6825   r_symndx = ELF64_R_SYM (rel->r_info);
   6826   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6827     return 0;
   6828 
   6829   if ((*tls_maskp != NULL && **tls_maskp != 0)
   6830       || sec == NULL
   6831       || ppc64_elf_section_data (sec) == NULL
   6832       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   6833     return 1;
   6834 
   6835   /* Look inside a TOC section too.  */
   6836   if (h != NULL)
   6837     {
   6838       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   6839       off = h->root.u.def.value;
   6840     }
   6841   else
   6842     off = sym->st_value;
   6843   off += rel->r_addend;
   6844   BFD_ASSERT (off % 8 == 0);
   6845   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   6846   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   6847   if (toc_symndx != NULL)
   6848     *toc_symndx = r_symndx;
   6849   if (toc_addend != NULL)
   6850     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   6851   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6852     return 0;
   6853   if ((h == NULL || is_static_defined (h))
   6854       && (next_r == -1 || next_r == -2))
   6855     return 1 - next_r;
   6856   return 1;
   6857 }
   6858 
   6859 /* Find (or create) an entry in the tocsave hash table.  */
   6860 
   6861 static struct tocsave_entry *
   6862 tocsave_find (struct ppc_link_hash_table *htab,
   6863 	      enum insert_option insert,
   6864 	      Elf_Internal_Sym **local_syms,
   6865 	      const Elf_Internal_Rela *irela,
   6866 	      bfd *ibfd)
   6867 {
   6868   unsigned long r_indx;
   6869   struct elf_link_hash_entry *h;
   6870   Elf_Internal_Sym *sym;
   6871   struct tocsave_entry ent, *p;
   6872   hashval_t hash;
   6873   struct tocsave_entry **slot;
   6874 
   6875   r_indx = ELF64_R_SYM (irela->r_info);
   6876   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
   6877     return NULL;
   6878   if (ent.sec == NULL || ent.sec->output_section == NULL)
   6879     {
   6880       (*_bfd_error_handler)
   6881 	(_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
   6882       return NULL;
   6883     }
   6884 
   6885   if (h != NULL)
   6886     ent.offset = h->root.u.def.value;
   6887   else
   6888     ent.offset = sym->st_value;
   6889   ent.offset += irela->r_addend;
   6890 
   6891   hash = tocsave_htab_hash (&ent);
   6892   slot = ((struct tocsave_entry **)
   6893 	  htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
   6894   if (slot == NULL)
   6895     return NULL;
   6896 
   6897   if (*slot == NULL)
   6898     {
   6899       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
   6900       if (p == NULL)
   6901 	return NULL;
   6902       *p = ent;
   6903       *slot = p;
   6904     }
   6905   return *slot;
   6906 }
   6907 
   6908 /* Adjust all global syms defined in opd sections.  In gcc generated
   6909    code for the old ABI, these will already have been done.  */
   6910 
   6911 static bfd_boolean
   6912 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   6913 {
   6914   struct ppc_link_hash_entry *eh;
   6915   asection *sym_sec;
   6916   struct _opd_sec_data *opd;
   6917 
   6918   if (h->root.type == bfd_link_hash_indirect)
   6919     return TRUE;
   6920 
   6921   if (h->root.type != bfd_link_hash_defined
   6922       && h->root.type != bfd_link_hash_defweak)
   6923     return TRUE;
   6924 
   6925   eh = (struct ppc_link_hash_entry *) h;
   6926   if (eh->adjust_done)
   6927     return TRUE;
   6928 
   6929   sym_sec = eh->elf.root.u.def.section;
   6930   opd = get_opd_info (sym_sec);
   6931   if (opd != NULL && opd->adjust != NULL)
   6932     {
   6933       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
   6934       if (adjust == -1)
   6935 	{
   6936 	  /* This entry has been deleted.  */
   6937 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   6938 	  if (dsec == NULL)
   6939 	    {
   6940 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   6941 		if (discarded_section (dsec))
   6942 		  {
   6943 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   6944 		    break;
   6945 		  }
   6946 	    }
   6947 	  eh->elf.root.u.def.value = 0;
   6948 	  eh->elf.root.u.def.section = dsec;
   6949 	}
   6950       else
   6951 	eh->elf.root.u.def.value += adjust;
   6952       eh->adjust_done = 1;
   6953     }
   6954   return TRUE;
   6955 }
   6956 
   6957 /* Handles decrementing dynamic reloc counts for the reloc specified by
   6958    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
   6959    have already been determined.  */
   6960 
   6961 static bfd_boolean
   6962 dec_dynrel_count (bfd_vma r_info,
   6963 		  asection *sec,
   6964 		  struct bfd_link_info *info,
   6965 		  Elf_Internal_Sym **local_syms,
   6966 		  struct elf_link_hash_entry *h,
   6967 		  asection *sym_sec)
   6968 {
   6969   enum elf_ppc64_reloc_type r_type;
   6970   struct elf_dyn_relocs *p;
   6971   struct elf_dyn_relocs **pp;
   6972 
   6973   /* Can this reloc be dynamic?  This switch, and later tests here
   6974      should be kept in sync with the code in check_relocs.  */
   6975   r_type = ELF64_R_TYPE (r_info);
   6976   switch (r_type)
   6977     {
   6978     default:
   6979       return TRUE;
   6980 
   6981     case R_PPC64_TPREL16:
   6982     case R_PPC64_TPREL16_LO:
   6983     case R_PPC64_TPREL16_HI:
   6984     case R_PPC64_TPREL16_HA:
   6985     case R_PPC64_TPREL16_DS:
   6986     case R_PPC64_TPREL16_LO_DS:
   6987     case R_PPC64_TPREL16_HIGHER:
   6988     case R_PPC64_TPREL16_HIGHERA:
   6989     case R_PPC64_TPREL16_HIGHEST:
   6990     case R_PPC64_TPREL16_HIGHESTA:
   6991       if (!info->shared)
   6992 	return TRUE;
   6993 
   6994     case R_PPC64_TPREL64:
   6995     case R_PPC64_DTPMOD64:
   6996     case R_PPC64_DTPREL64:
   6997     case R_PPC64_ADDR64:
   6998     case R_PPC64_REL30:
   6999     case R_PPC64_REL32:
   7000     case R_PPC64_REL64:
   7001     case R_PPC64_ADDR14:
   7002     case R_PPC64_ADDR14_BRNTAKEN:
   7003     case R_PPC64_ADDR14_BRTAKEN:
   7004     case R_PPC64_ADDR16:
   7005     case R_PPC64_ADDR16_DS:
   7006     case R_PPC64_ADDR16_HA:
   7007     case R_PPC64_ADDR16_HI:
   7008     case R_PPC64_ADDR16_HIGHER:
   7009     case R_PPC64_ADDR16_HIGHERA:
   7010     case R_PPC64_ADDR16_HIGHEST:
   7011     case R_PPC64_ADDR16_HIGHESTA:
   7012     case R_PPC64_ADDR16_LO:
   7013     case R_PPC64_ADDR16_LO_DS:
   7014     case R_PPC64_ADDR24:
   7015     case R_PPC64_ADDR32:
   7016     case R_PPC64_UADDR16:
   7017     case R_PPC64_UADDR32:
   7018     case R_PPC64_UADDR64:
   7019     case R_PPC64_TOC:
   7020       break;
   7021     }
   7022 
   7023   if (local_syms != NULL)
   7024     {
   7025       unsigned long r_symndx;
   7026       Elf_Internal_Sym *sym;
   7027       bfd *ibfd = sec->owner;
   7028 
   7029       r_symndx = ELF64_R_SYM (r_info);
   7030       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   7031 	return FALSE;
   7032     }
   7033 
   7034   if ((info->shared
   7035        && (must_be_dyn_reloc (info, r_type)
   7036 	   || (h != NULL
   7037 	       && (!info->symbolic
   7038 		   || h->root.type == bfd_link_hash_defweak
   7039 		   || !h->def_regular))))
   7040       || (ELIMINATE_COPY_RELOCS
   7041 	  && !info->shared
   7042 	  && h != NULL
   7043 	  && (h->root.type == bfd_link_hash_defweak
   7044 	      || !h->def_regular)))
   7045     ;
   7046   else
   7047     return TRUE;
   7048 
   7049   if (h != NULL)
   7050     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   7051   else
   7052     {
   7053       if (sym_sec != NULL)
   7054 	{
   7055 	  void *vpp = &elf_section_data (sym_sec)->local_dynrel;
   7056 	  pp = (struct elf_dyn_relocs **) vpp;
   7057 	}
   7058       else
   7059 	{
   7060 	  void *vpp = &elf_section_data (sec)->local_dynrel;
   7061 	  pp = (struct elf_dyn_relocs **) vpp;
   7062 	}
   7063     }
   7064 
   7065   /* elf_gc_sweep may have already removed all dyn relocs associated
   7066      with local syms for a given section.  Also, symbol flags are
   7067      changed by elf_gc_sweep_symbol, confusing the test above.  Don't
   7068      report a dynreloc miscount.  */
   7069   if (*pp == NULL && info->gc_sections)
   7070     return TRUE;
   7071 
   7072   while ((p = *pp) != NULL)
   7073     {
   7074       if (p->sec == sec)
   7075 	{
   7076 	  if (!must_be_dyn_reloc (info, r_type))
   7077 	    p->pc_count -= 1;
   7078 	  p->count -= 1;
   7079 	  if (p->count == 0)
   7080 	    *pp = p->next;
   7081 	  return TRUE;
   7082 	}
   7083       pp = &p->next;
   7084     }
   7085 
   7086   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
   7087 			  sec->owner, sec);
   7088   bfd_set_error (bfd_error_bad_value);
   7089   return FALSE;
   7090 }
   7091 
   7092 /* Remove unused Official Procedure Descriptor entries.  Currently we
   7093    only remove those associated with functions in discarded link-once
   7094    sections, or weakly defined functions that have been overridden.  It
   7095    would be possible to remove many more entries for statically linked
   7096    applications.  */
   7097 
   7098 bfd_boolean
   7099 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
   7100 {
   7101   bfd *ibfd;
   7102   bfd_boolean some_edited = FALSE;
   7103   asection *need_pad = NULL;
   7104 
   7105   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7106     {
   7107       asection *sec;
   7108       Elf_Internal_Rela *relstart, *rel, *relend;
   7109       Elf_Internal_Shdr *symtab_hdr;
   7110       Elf_Internal_Sym *local_syms;
   7111       bfd_vma offset;
   7112       struct _opd_sec_data *opd;
   7113       bfd_boolean need_edit, add_aux_fields;
   7114       bfd_size_type cnt_16b = 0;
   7115 
   7116       if (!is_ppc64_elf (ibfd))
   7117 	continue;
   7118 
   7119       sec = bfd_get_section_by_name (ibfd, ".opd");
   7120       if (sec == NULL || sec->size == 0)
   7121 	continue;
   7122 
   7123       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7124 	continue;
   7125 
   7126       if (sec->output_section == bfd_abs_section_ptr)
   7127 	continue;
   7128 
   7129       /* Look through the section relocs.  */
   7130       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   7131 	continue;
   7132 
   7133       local_syms = NULL;
   7134       symtab_hdr = &elf_symtab_hdr (ibfd);
   7135 
   7136       /* Read the relocations.  */
   7137       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7138 					    info->keep_memory);
   7139       if (relstart == NULL)
   7140 	return FALSE;
   7141 
   7142       /* First run through the relocs to check they are sane, and to
   7143 	 determine whether we need to edit this opd section.  */
   7144       need_edit = FALSE;
   7145       need_pad = sec;
   7146       offset = 0;
   7147       relend = relstart + sec->reloc_count;
   7148       for (rel = relstart; rel < relend; )
   7149 	{
   7150 	  enum elf_ppc64_reloc_type r_type;
   7151 	  unsigned long r_symndx;
   7152 	  asection *sym_sec;
   7153 	  struct elf_link_hash_entry *h;
   7154 	  Elf_Internal_Sym *sym;
   7155 
   7156 	  /* .opd contains a regular array of 16 or 24 byte entries.  We're
   7157 	     only interested in the reloc pointing to a function entry
   7158 	     point.  */
   7159 	  if (rel->r_offset != offset
   7160 	      || rel + 1 >= relend
   7161 	      || (rel + 1)->r_offset != offset + 8)
   7162 	    {
   7163 	      /* If someone messes with .opd alignment then after a
   7164 		 "ld -r" we might have padding in the middle of .opd.
   7165 		 Also, there's nothing to prevent someone putting
   7166 		 something silly in .opd with the assembler.  No .opd
   7167 		 optimization for them!  */
   7168 	    broken_opd:
   7169 	      (*_bfd_error_handler)
   7170 		(_("%B: .opd is not a regular array of opd entries"), ibfd);
   7171 	      need_edit = FALSE;
   7172 	      break;
   7173 	    }
   7174 
   7175 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7176 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7177 	    {
   7178 	      (*_bfd_error_handler)
   7179 		(_("%B: unexpected reloc type %u in .opd section"),
   7180 		 ibfd, r_type);
   7181 	      need_edit = FALSE;
   7182 	      break;
   7183 	    }
   7184 
   7185 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7186 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7187 			  r_symndx, ibfd))
   7188 	    goto error_ret;
   7189 
   7190 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7191 	    {
   7192 	      const char *sym_name;
   7193 	      if (h != NULL)
   7194 		sym_name = h->root.root.string;
   7195 	      else
   7196 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7197 					     sym_sec);
   7198 
   7199 	      (*_bfd_error_handler)
   7200 		(_("%B: undefined sym `%s' in .opd section"),
   7201 		 ibfd, sym_name);
   7202 	      need_edit = FALSE;
   7203 	      break;
   7204 	    }
   7205 
   7206 	  /* opd entries are always for functions defined in the
   7207 	     current input bfd.  If the symbol isn't defined in the
   7208 	     input bfd, then we won't be using the function in this
   7209 	     bfd;  It must be defined in a linkonce section in another
   7210 	     bfd, or is weak.  It's also possible that we are
   7211 	     discarding the function due to a linker script /DISCARD/,
   7212 	     which we test for via the output_section.  */
   7213 	  if (sym_sec->owner != ibfd
   7214 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7215 	    need_edit = TRUE;
   7216 
   7217 	  rel += 2;
   7218 	  if (rel == relend
   7219 	      || (rel + 1 == relend && rel->r_offset == offset + 16))
   7220 	    {
   7221 	      if (sec->size == offset + 24)
   7222 		{
   7223 		  need_pad = NULL;
   7224 		  break;
   7225 		}
   7226 	      if (rel == relend && sec->size == offset + 16)
   7227 		{
   7228 		  cnt_16b++;
   7229 		  break;
   7230 		}
   7231 	      goto broken_opd;
   7232 	    }
   7233 
   7234 	  if (rel->r_offset == offset + 24)
   7235 	    offset += 24;
   7236 	  else if (rel->r_offset != offset + 16)
   7237 	    goto broken_opd;
   7238 	  else if (rel + 1 < relend
   7239 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7240 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7241 	    {
   7242 	      offset += 16;
   7243 	      cnt_16b++;
   7244 	    }
   7245 	  else if (rel + 2 < relend
   7246 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
   7247 		   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
   7248 	    {
   7249 	      offset += 24;
   7250 	      rel += 1;
   7251 	    }
   7252 	  else
   7253 	    goto broken_opd;
   7254 	}
   7255 
   7256       add_aux_fields = non_overlapping && cnt_16b > 0;
   7257 
   7258       if (need_edit || add_aux_fields)
   7259 	{
   7260 	  Elf_Internal_Rela *write_rel;
   7261 	  Elf_Internal_Shdr *rel_hdr;
   7262 	  bfd_byte *rptr, *wptr;
   7263 	  bfd_byte *new_contents;
   7264 	  bfd_boolean skip;
   7265 	  long opd_ent_size;
   7266 	  bfd_size_type amt;
   7267 
   7268 	  new_contents = NULL;
   7269 	  amt = sec->size * sizeof (long) / 8;
   7270 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7271 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7272 	  if (opd->adjust == NULL)
   7273 	    return FALSE;
   7274 	  ppc64_elf_section_data (sec)->sec_type = sec_opd;
   7275 
   7276 	  /* This seems a waste of time as input .opd sections are all
   7277 	     zeros as generated by gcc, but I suppose there's no reason
   7278 	     this will always be so.  We might start putting something in
   7279 	     the third word of .opd entries.  */
   7280 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7281 	    {
   7282 	      bfd_byte *loc;
   7283 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7284 		{
   7285 		  if (loc != NULL)
   7286 		    free (loc);
   7287 		error_ret:
   7288 		  if (local_syms != NULL
   7289 		      && symtab_hdr->contents != (unsigned char *) local_syms)
   7290 		    free (local_syms);
   7291 		  if (elf_section_data (sec)->relocs != relstart)
   7292 		    free (relstart);
   7293 		  return FALSE;
   7294 		}
   7295 	      sec->contents = loc;
   7296 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7297 	    }
   7298 
   7299 	  elf_section_data (sec)->relocs = relstart;
   7300 
   7301 	  new_contents = sec->contents;
   7302 	  if (add_aux_fields)
   7303 	    {
   7304 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7305 	      if (new_contents == NULL)
   7306 		return FALSE;
   7307 	      need_pad = FALSE;
   7308 	    }
   7309 	  wptr = new_contents;
   7310 	  rptr = sec->contents;
   7311 
   7312 	  write_rel = relstart;
   7313 	  skip = FALSE;
   7314 	  offset = 0;
   7315 	  opd_ent_size = 0;
   7316 	  for (rel = relstart; rel < relend; rel++)
   7317 	    {
   7318 	      unsigned long r_symndx;
   7319 	      asection *sym_sec;
   7320 	      struct elf_link_hash_entry *h;
   7321 	      Elf_Internal_Sym *sym;
   7322 
   7323 	      r_symndx = ELF64_R_SYM (rel->r_info);
   7324 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7325 			      r_symndx, ibfd))
   7326 		goto error_ret;
   7327 
   7328 	      if (rel->r_offset == offset)
   7329 		{
   7330 		  struct ppc_link_hash_entry *fdh = NULL;
   7331 
   7332 		  /* See if the .opd entry is full 24 byte or
   7333 		     16 byte (with fd_aux entry overlapped with next
   7334 		     fd_func).  */
   7335 		  opd_ent_size = 24;
   7336 		  if ((rel + 2 == relend && sec->size == offset + 16)
   7337 		      || (rel + 3 < relend
   7338 			  && rel[2].r_offset == offset + 16
   7339 			  && rel[3].r_offset == offset + 24
   7340 			  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
   7341 			  && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
   7342 		    opd_ent_size = 16;
   7343 
   7344 		  if (h != NULL
   7345 		      && h->root.root.string[0] == '.')
   7346 		    {
   7347 		      struct ppc_link_hash_table *htab;
   7348 
   7349 		      htab = ppc_hash_table (info);
   7350 		      if (htab != NULL)
   7351 			fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
   7352 					  htab);
   7353 		      if (fdh != NULL
   7354 			  && fdh->elf.root.type != bfd_link_hash_defined
   7355 			  && fdh->elf.root.type != bfd_link_hash_defweak)
   7356 			fdh = NULL;
   7357 		    }
   7358 
   7359 		  skip = (sym_sec->owner != ibfd
   7360 			  || sym_sec->output_section == bfd_abs_section_ptr);
   7361 		  if (skip)
   7362 		    {
   7363 		      if (fdh != NULL && sym_sec->owner == ibfd)
   7364 			{
   7365 			  /* Arrange for the function descriptor sym
   7366 			     to be dropped.  */
   7367 			  fdh->elf.root.u.def.value = 0;
   7368 			  fdh->elf.root.u.def.section = sym_sec;
   7369 			}
   7370 		      opd->adjust[rel->r_offset / 8] = -1;
   7371 		    }
   7372 		  else
   7373 		    {
   7374 		      /* We'll be keeping this opd entry.  */
   7375 
   7376 		      if (fdh != NULL)
   7377 			{
   7378 			  /* Redefine the function descriptor symbol to
   7379 			     this location in the opd section.  It is
   7380 			     necessary to update the value here rather
   7381 			     than using an array of adjustments as we do
   7382 			     for local symbols, because various places
   7383 			     in the generic ELF code use the value
   7384 			     stored in u.def.value.  */
   7385 			  fdh->elf.root.u.def.value = wptr - new_contents;
   7386 			  fdh->adjust_done = 1;
   7387 			}
   7388 
   7389 		      /* Local syms are a bit tricky.  We could
   7390 			 tweak them as they can be cached, but
   7391 			 we'd need to look through the local syms
   7392 			 for the function descriptor sym which we
   7393 			 don't have at the moment.  So keep an
   7394 			 array of adjustments.  */
   7395 		      opd->adjust[rel->r_offset / 8]
   7396 			= (wptr - new_contents) - (rptr - sec->contents);
   7397 
   7398 		      if (wptr != rptr)
   7399 			memcpy (wptr, rptr, opd_ent_size);
   7400 		      wptr += opd_ent_size;
   7401 		      if (add_aux_fields && opd_ent_size == 16)
   7402 			{
   7403 			  memset (wptr, '\0', 8);
   7404 			  wptr += 8;
   7405 			}
   7406 		    }
   7407 		  rptr += opd_ent_size;
   7408 		  offset += opd_ent_size;
   7409 		}
   7410 
   7411 	      if (skip)
   7412 		{
   7413 		  if (!NO_OPD_RELOCS
   7414 		      && !info->relocatable
   7415 		      && !dec_dynrel_count (rel->r_info, sec, info,
   7416 					    NULL, h, sym_sec))
   7417 		    goto error_ret;
   7418 		}
   7419 	      else
   7420 		{
   7421 		  /* We need to adjust any reloc offsets to point to the
   7422 		     new opd entries.  While we're at it, we may as well
   7423 		     remove redundant relocs.  */
   7424 		  rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
   7425 		  if (write_rel != rel)
   7426 		    memcpy (write_rel, rel, sizeof (*rel));
   7427 		  ++write_rel;
   7428 		}
   7429 	    }
   7430 
   7431 	  sec->size = wptr - new_contents;
   7432 	  sec->reloc_count = write_rel - relstart;
   7433 	  if (add_aux_fields)
   7434 	    {
   7435 	      free (sec->contents);
   7436 	      sec->contents = new_contents;
   7437 	    }
   7438 
   7439 	  /* Fudge the header size too, as this is used later in
   7440 	     elf_bfd_final_link if we are emitting relocs.  */
   7441 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   7442 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   7443 	  some_edited = TRUE;
   7444 	}
   7445       else if (elf_section_data (sec)->relocs != relstart)
   7446 	free (relstart);
   7447 
   7448       if (local_syms != NULL
   7449 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7450 	{
   7451 	  if (!info->keep_memory)
   7452 	    free (local_syms);
   7453 	  else
   7454 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7455 	}
   7456     }
   7457 
   7458   if (some_edited)
   7459     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   7460 
   7461   /* If we are doing a final link and the last .opd entry is just 16 byte
   7462      long, add a 8 byte padding after it.  */
   7463   if (need_pad != NULL && !info->relocatable)
   7464     {
   7465       bfd_byte *p;
   7466 
   7467       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   7468 	{
   7469 	  BFD_ASSERT (need_pad->size > 0);
   7470 
   7471 	  p = bfd_malloc (need_pad->size + 8);
   7472 	  if (p == NULL)
   7473 	    return FALSE;
   7474 
   7475 	  if (! bfd_get_section_contents (need_pad->owner, need_pad,
   7476 					  p, 0, need_pad->size))
   7477 	    return FALSE;
   7478 
   7479 	  need_pad->contents = p;
   7480 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7481 	}
   7482       else
   7483 	{
   7484 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   7485 	  if (p == NULL)
   7486 	    return FALSE;
   7487 
   7488 	  need_pad->contents = p;
   7489 	}
   7490 
   7491       memset (need_pad->contents + need_pad->size, 0, 8);
   7492       need_pad->size += 8;
   7493     }
   7494 
   7495   return TRUE;
   7496 }
   7497 
   7498 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
   7499 
   7500 asection *
   7501 ppc64_elf_tls_setup (struct bfd_link_info *info,
   7502 		     int no_tls_get_addr_opt,
   7503 		     int *no_multi_toc)
   7504 {
   7505   struct ppc_link_hash_table *htab;
   7506 
   7507   htab = ppc_hash_table (info);
   7508   if (htab == NULL)
   7509     return NULL;
   7510 
   7511   if (*no_multi_toc)
   7512     htab->do_multi_toc = 0;
   7513   else if (!htab->do_multi_toc)
   7514     *no_multi_toc = 1;
   7515 
   7516   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
   7517 			elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   7518 					      FALSE, FALSE, TRUE));
   7519   /* Move dynamic linking info to the function descriptor sym.  */
   7520   if (htab->tls_get_addr != NULL)
   7521     func_desc_adjust (&htab->tls_get_addr->elf, info);
   7522   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
   7523 			   elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   7524 						 FALSE, FALSE, TRUE));
   7525   if (!no_tls_get_addr_opt)
   7526     {
   7527       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
   7528 
   7529       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   7530 				  FALSE, FALSE, TRUE);
   7531       if (opt != NULL)
   7532 	func_desc_adjust (opt, info);
   7533       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   7534 				     FALSE, FALSE, TRUE);
   7535       if (opt_fd != NULL
   7536 	  && (opt_fd->root.type == bfd_link_hash_defined
   7537 	      || opt_fd->root.type == bfd_link_hash_defweak))
   7538 	{
   7539 	  /* If glibc supports an optimized __tls_get_addr call stub,
   7540 	     signalled by the presence of __tls_get_addr_opt, and we'll
   7541 	     be calling __tls_get_addr via a plt call stub, then
   7542 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   7543 	  tga_fd = &htab->tls_get_addr_fd->elf;
   7544 	  if (htab->elf.dynamic_sections_created
   7545 	      && tga_fd != NULL
   7546 	      && (tga_fd->type == STT_FUNC
   7547 		  || tga_fd->needs_plt)
   7548 	      && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   7549 		   || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
   7550 		       && tga_fd->root.type == bfd_link_hash_undefweak)))
   7551 	    {
   7552 	      struct plt_entry *ent;
   7553 
   7554 	      for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   7555 		if (ent->plt.refcount > 0)
   7556 		  break;
   7557 	      if (ent != NULL)
   7558 		{
   7559 		  tga_fd->root.type = bfd_link_hash_indirect;
   7560 		  tga_fd->root.u.i.link = &opt_fd->root;
   7561 		  ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   7562 		  if (opt_fd->dynindx != -1)
   7563 		    {
   7564 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   7565 		      opt_fd->dynindx = -1;
   7566 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7567 					      opt_fd->dynstr_index);
   7568 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   7569 			return NULL;
   7570 		    }
   7571 		  htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
   7572 		  tga = &htab->tls_get_addr->elf;
   7573 		  if (opt != NULL && tga != NULL)
   7574 		    {
   7575 		      tga->root.type = bfd_link_hash_indirect;
   7576 		      tga->root.u.i.link = &opt->root;
   7577 		      ppc64_elf_copy_indirect_symbol (info, opt, tga);
   7578 		      _bfd_elf_link_hash_hide_symbol (info, opt,
   7579 						      tga->forced_local);
   7580 		      htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
   7581 		    }
   7582 		  htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   7583 		  htab->tls_get_addr_fd->is_func_descriptor = 1;
   7584 		  if (htab->tls_get_addr != NULL)
   7585 		    {
   7586 		      htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   7587 		      htab->tls_get_addr->is_func = 1;
   7588 		    }
   7589 		}
   7590 	    }
   7591 	}
   7592       else
   7593 	no_tls_get_addr_opt = TRUE;
   7594     }
   7595   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
   7596   return _bfd_elf_tls_setup (info->output_bfd, info);
   7597 }
   7598 
   7599 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   7600    HASH1 or HASH2.  */
   7601 
   7602 static bfd_boolean
   7603 branch_reloc_hash_match (const bfd *ibfd,
   7604 			 const Elf_Internal_Rela *rel,
   7605 			 const struct ppc_link_hash_entry *hash1,
   7606 			 const struct ppc_link_hash_entry *hash2)
   7607 {
   7608   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   7609   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   7610   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   7611 
   7612   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   7613     {
   7614       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   7615       struct elf_link_hash_entry *h;
   7616 
   7617       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   7618       h = elf_follow_link (h);
   7619       if (h == &hash1->elf || h == &hash2->elf)
   7620 	return TRUE;
   7621     }
   7622   return FALSE;
   7623 }
   7624 
   7625 /* Run through all the TLS relocs looking for optimization
   7626    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   7627    a preliminary section layout so that we know the TLS segment
   7628    offsets.  We can't optimize earlier because some optimizations need
   7629    to know the tp offset, and we need to optimize before allocating
   7630    dynamic relocations.  */
   7631 
   7632 bfd_boolean
   7633 ppc64_elf_tls_optimize (struct bfd_link_info *info)
   7634 {
   7635   bfd *ibfd;
   7636   asection *sec;
   7637   struct ppc_link_hash_table *htab;
   7638   unsigned char *toc_ref;
   7639   int pass;
   7640 
   7641   if (info->relocatable || !info->executable)
   7642     return TRUE;
   7643 
   7644   htab = ppc_hash_table (info);
   7645   if (htab == NULL)
   7646     return FALSE;
   7647 
   7648   /* Make two passes over the relocs.  On the first pass, mark toc
   7649      entries involved with tls relocs, and check that tls relocs
   7650      involved in setting up a tls_get_addr call are indeed followed by
   7651      such a call.  If they are not, we can't do any tls optimization.
   7652      On the second pass twiddle tls_mask flags to notify
   7653      relocate_section that optimization can be done, and adjust got
   7654      and plt refcounts.  */
   7655   toc_ref = NULL;
   7656   for (pass = 0; pass < 2; ++pass)
   7657     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7658       {
   7659 	Elf_Internal_Sym *locsyms = NULL;
   7660 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   7661 
   7662 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7663 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   7664 	    {
   7665 	      Elf_Internal_Rela *relstart, *rel, *relend;
   7666 	      bfd_boolean found_tls_get_addr_arg = 0;
   7667 
   7668 	      /* Read the relocations.  */
   7669 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7670 						    info->keep_memory);
   7671 	      if (relstart == NULL)
   7672 		return FALSE;
   7673 
   7674 	      relend = relstart + sec->reloc_count;
   7675 	      for (rel = relstart; rel < relend; rel++)
   7676 		{
   7677 		  enum elf_ppc64_reloc_type r_type;
   7678 		  unsigned long r_symndx;
   7679 		  struct elf_link_hash_entry *h;
   7680 		  Elf_Internal_Sym *sym;
   7681 		  asection *sym_sec;
   7682 		  unsigned char *tls_mask;
   7683 		  unsigned char tls_set, tls_clear, tls_type = 0;
   7684 		  bfd_vma value;
   7685 		  bfd_boolean ok_tprel, is_local;
   7686 		  long toc_ref_index = 0;
   7687 		  int expecting_tls_get_addr = 0;
   7688 		  bfd_boolean ret = FALSE;
   7689 
   7690 		  r_symndx = ELF64_R_SYM (rel->r_info);
   7691 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   7692 				  r_symndx, ibfd))
   7693 		    {
   7694 		    err_free_rel:
   7695 		      if (elf_section_data (sec)->relocs != relstart)
   7696 			free (relstart);
   7697 		      if (toc_ref != NULL)
   7698 			free (toc_ref);
   7699 		      if (locsyms != NULL
   7700 			  && (elf_symtab_hdr (ibfd).contents
   7701 			      != (unsigned char *) locsyms))
   7702 			free (locsyms);
   7703 		      return ret;
   7704 		    }
   7705 
   7706 		  if (h != NULL)
   7707 		    {
   7708 		      if (h->root.type == bfd_link_hash_defined
   7709 			  || h->root.type == bfd_link_hash_defweak)
   7710 			value = h->root.u.def.value;
   7711 		      else if (h->root.type == bfd_link_hash_undefweak)
   7712 			value = 0;
   7713 		      else
   7714 			{
   7715 			  found_tls_get_addr_arg = 0;
   7716 			  continue;
   7717 			}
   7718 		    }
   7719 		  else
   7720 		    /* Symbols referenced by TLS relocs must be of type
   7721 		       STT_TLS.  So no need for .opd local sym adjust.  */
   7722 		    value = sym->st_value;
   7723 
   7724 		  ok_tprel = FALSE;
   7725 		  is_local = FALSE;
   7726 		  if (h == NULL
   7727 		      || !h->def_dynamic)
   7728 		    {
   7729 		      is_local = TRUE;
   7730 		      if (h != NULL
   7731 			  && h->root.type == bfd_link_hash_undefweak)
   7732 			ok_tprel = TRUE;
   7733 		      else
   7734 			{
   7735 			  value += sym_sec->output_offset;
   7736 			  value += sym_sec->output_section->vma;
   7737 			  value -= htab->elf.tls_sec->vma;
   7738 			  ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
   7739 				      < (bfd_vma) 1 << 32);
   7740 			}
   7741 		    }
   7742 
   7743 		  r_type = ELF64_R_TYPE (rel->r_info);
   7744 		  /* If this section has old-style __tls_get_addr calls
   7745 		     without marker relocs, then check that each
   7746 		     __tls_get_addr call reloc is preceded by a reloc
   7747 		     that conceivably belongs to the __tls_get_addr arg
   7748 		     setup insn.  If we don't find matching arg setup
   7749 		     relocs, don't do any tls optimization.  */
   7750 		  if (pass == 0
   7751 		      && sec->has_tls_get_addr_call
   7752 		      && h != NULL
   7753 		      && (h == &htab->tls_get_addr->elf
   7754 			  || h == &htab->tls_get_addr_fd->elf)
   7755 		      && !found_tls_get_addr_arg
   7756 		      && is_branch_reloc (r_type))
   7757 		    {
   7758 		      info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
   7759 						"TLS optimization disabled\n"),
   7760 					      ibfd, sec, rel->r_offset);
   7761 		      ret = TRUE;
   7762 		      goto err_free_rel;
   7763 		    }
   7764 
   7765 		  found_tls_get_addr_arg = 0;
   7766 		  switch (r_type)
   7767 		    {
   7768 		    case R_PPC64_GOT_TLSLD16:
   7769 		    case R_PPC64_GOT_TLSLD16_LO:
   7770 		      expecting_tls_get_addr = 1;
   7771 		      found_tls_get_addr_arg = 1;
   7772 		      /* Fall thru */
   7773 
   7774 		    case R_PPC64_GOT_TLSLD16_HI:
   7775 		    case R_PPC64_GOT_TLSLD16_HA:
   7776 		      /* These relocs should never be against a symbol
   7777 			 defined in a shared lib.  Leave them alone if
   7778 			 that turns out to be the case.  */
   7779 		      if (!is_local)
   7780 			continue;
   7781 
   7782 		      /* LD -> LE */
   7783 		      tls_set = 0;
   7784 		      tls_clear = TLS_LD;
   7785 		      tls_type = TLS_TLS | TLS_LD;
   7786 		      break;
   7787 
   7788 		    case R_PPC64_GOT_TLSGD16:
   7789 		    case R_PPC64_GOT_TLSGD16_LO:
   7790 		      expecting_tls_get_addr = 1;
   7791 		      found_tls_get_addr_arg = 1;
   7792 		      /* Fall thru */
   7793 
   7794 		    case R_PPC64_GOT_TLSGD16_HI:
   7795 		    case R_PPC64_GOT_TLSGD16_HA:
   7796 		      if (ok_tprel)
   7797 			/* GD -> LE */
   7798 			tls_set = 0;
   7799 		      else
   7800 			/* GD -> IE */
   7801 			tls_set = TLS_TLS | TLS_TPRELGD;
   7802 		      tls_clear = TLS_GD;
   7803 		      tls_type = TLS_TLS | TLS_GD;
   7804 		      break;
   7805 
   7806 		    case R_PPC64_GOT_TPREL16_DS:
   7807 		    case R_PPC64_GOT_TPREL16_LO_DS:
   7808 		    case R_PPC64_GOT_TPREL16_HI:
   7809 		    case R_PPC64_GOT_TPREL16_HA:
   7810 		      if (ok_tprel)
   7811 			{
   7812 			  /* IE -> LE */
   7813 			  tls_set = 0;
   7814 			  tls_clear = TLS_TPREL;
   7815 			  tls_type = TLS_TLS | TLS_TPREL;
   7816 			  break;
   7817 			}
   7818 		      continue;
   7819 
   7820 		    case R_PPC64_TLSGD:
   7821 		    case R_PPC64_TLSLD:
   7822 		      found_tls_get_addr_arg = 1;
   7823 		      /* Fall thru */
   7824 
   7825 		    case R_PPC64_TLS:
   7826 		    case R_PPC64_TOC16:
   7827 		    case R_PPC64_TOC16_LO:
   7828 		      if (sym_sec == NULL || sym_sec != toc)
   7829 			continue;
   7830 
   7831 		      /* Mark this toc entry as referenced by a TLS
   7832 			 code sequence.  We can do that now in the
   7833 			 case of R_PPC64_TLS, and after checking for
   7834 			 tls_get_addr for the TOC16 relocs.  */
   7835 		      if (toc_ref == NULL)
   7836 			toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
   7837 		      if (toc_ref == NULL)
   7838 			goto err_free_rel;
   7839 
   7840 		      if (h != NULL)
   7841 			value = h->root.u.def.value;
   7842 		      else
   7843 			value = sym->st_value;
   7844 		      value += rel->r_addend;
   7845 		      BFD_ASSERT (value < toc->size && value % 8 == 0);
   7846 		      toc_ref_index = (value + toc->output_offset) / 8;
   7847 		      if (r_type == R_PPC64_TLS
   7848 			  || r_type == R_PPC64_TLSGD
   7849 			  || r_type == R_PPC64_TLSLD)
   7850 			{
   7851 			  toc_ref[toc_ref_index] = 1;
   7852 			  continue;
   7853 			}
   7854 
   7855 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   7856 			continue;
   7857 
   7858 		      tls_set = 0;
   7859 		      tls_clear = 0;
   7860 		      expecting_tls_get_addr = 2;
   7861 		      break;
   7862 
   7863 		    case R_PPC64_TPREL64:
   7864 		      if (pass == 0
   7865 			  || sec != toc
   7866 			  || toc_ref == NULL
   7867 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7868 			continue;
   7869 		      if (ok_tprel)
   7870 			{
   7871 			  /* IE -> LE */
   7872 			  tls_set = TLS_EXPLICIT;
   7873 			  tls_clear = TLS_TPREL;
   7874 			  break;
   7875 			}
   7876 		      continue;
   7877 
   7878 		    case R_PPC64_DTPMOD64:
   7879 		      if (pass == 0
   7880 			  || sec != toc
   7881 			  || toc_ref == NULL
   7882 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7883 			continue;
   7884 		      if (rel + 1 < relend
   7885 			  && (rel[1].r_info
   7886 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   7887 			  && rel[1].r_offset == rel->r_offset + 8)
   7888 			{
   7889 			  if (ok_tprel)
   7890 			    /* GD -> LE */
   7891 			    tls_set = TLS_EXPLICIT | TLS_GD;
   7892 			  else
   7893 			    /* GD -> IE */
   7894 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
   7895 			  tls_clear = TLS_GD;
   7896 			}
   7897 		      else
   7898 			{
   7899 			  if (!is_local)
   7900 			    continue;
   7901 
   7902 			  /* LD -> LE */
   7903 			  tls_set = TLS_EXPLICIT;
   7904 			  tls_clear = TLS_LD;
   7905 			}
   7906 		      break;
   7907 
   7908 		    default:
   7909 		      continue;
   7910 		    }
   7911 
   7912 		  if (pass == 0)
   7913 		    {
   7914 		      if (!expecting_tls_get_addr
   7915 			  || !sec->has_tls_get_addr_call)
   7916 			continue;
   7917 
   7918 		      if (rel + 1 < relend
   7919 			  && branch_reloc_hash_match (ibfd, rel + 1,
   7920 						      htab->tls_get_addr,
   7921 						      htab->tls_get_addr_fd))
   7922 			{
   7923 			  if (expecting_tls_get_addr == 2)
   7924 			    {
   7925 			      /* Check for toc tls entries.  */
   7926 			      unsigned char *toc_tls;
   7927 			      int retval;
   7928 
   7929 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   7930 						     &locsyms,
   7931 						     rel, ibfd);
   7932 			      if (retval == 0)
   7933 				goto err_free_rel;
   7934 			      if (toc_tls != NULL)
   7935 				{
   7936 				  if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
   7937 				    found_tls_get_addr_arg = 1;
   7938 				  if (retval > 1)
   7939 				    toc_ref[toc_ref_index] = 1;
   7940 				}
   7941 			    }
   7942 			  continue;
   7943 			}
   7944 
   7945 		      if (expecting_tls_get_addr != 1)
   7946 			continue;
   7947 
   7948 		      /* Uh oh, we didn't find the expected call.  We
   7949 			 could just mark this symbol to exclude it
   7950 			 from tls optimization but it's safer to skip
   7951 			 the entire optimization.  */
   7952 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   7953 						"TLS optimization disabled\n"),
   7954 					      ibfd, sec, rel->r_offset);
   7955 		      ret = TRUE;
   7956 		      goto err_free_rel;
   7957 		    }
   7958 
   7959 		  if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
   7960 		    {
   7961 		      struct plt_entry *ent;
   7962 		      for (ent = htab->tls_get_addr->elf.plt.plist;
   7963 			   ent != NULL;
   7964 			   ent = ent->next)
   7965 			if (ent->addend == 0)
   7966 			  {
   7967 			    if (ent->plt.refcount > 0)
   7968 			      {
   7969 				ent->plt.refcount -= 1;
   7970 				expecting_tls_get_addr = 0;
   7971 			      }
   7972 			    break;
   7973 			  }
   7974 		    }
   7975 
   7976 		  if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
   7977 		    {
   7978 		      struct plt_entry *ent;
   7979 		      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   7980 			   ent != NULL;
   7981 			   ent = ent->next)
   7982 			if (ent->addend == 0)
   7983 			  {
   7984 			    if (ent->plt.refcount > 0)
   7985 			      ent->plt.refcount -= 1;
   7986 			    break;
   7987 			  }
   7988 		    }
   7989 
   7990 		  if (tls_clear == 0)
   7991 		    continue;
   7992 
   7993 		  if ((tls_set & TLS_EXPLICIT) == 0)
   7994 		    {
   7995 		      struct got_entry *ent;
   7996 
   7997 		      /* Adjust got entry for this reloc.  */
   7998 		      if (h != NULL)
   7999 			ent = h->got.glist;
   8000 		      else
   8001 			ent = elf_local_got_ents (ibfd)[r_symndx];
   8002 
   8003 		      for (; ent != NULL; ent = ent->next)
   8004 			if (ent->addend == rel->r_addend
   8005 			    && ent->owner == ibfd
   8006 			    && ent->tls_type == tls_type)
   8007 			  break;
   8008 		      if (ent == NULL)
   8009 			abort ();
   8010 
   8011 		      if (tls_set == 0)
   8012 			{
   8013 			  /* We managed to get rid of a got entry.  */
   8014 			  if (ent->got.refcount > 0)
   8015 			    ent->got.refcount -= 1;
   8016 			}
   8017 		    }
   8018 		  else
   8019 		    {
   8020 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   8021 			 we'll lose one or two dyn relocs.  */
   8022 		      if (!dec_dynrel_count (rel->r_info, sec, info,
   8023 					     NULL, h, sym_sec))
   8024 			return FALSE;
   8025 
   8026 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   8027 			{
   8028 			  if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
   8029 						 NULL, h, sym_sec))
   8030 			    return FALSE;
   8031 			}
   8032 		    }
   8033 
   8034 		  *tls_mask |= tls_set;
   8035 		  *tls_mask &= ~tls_clear;
   8036 		}
   8037 
   8038 	      if (elf_section_data (sec)->relocs != relstart)
   8039 		free (relstart);
   8040 	    }
   8041 
   8042 	if (locsyms != NULL
   8043 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   8044 	  {
   8045 	    if (!info->keep_memory)
   8046 	      free (locsyms);
   8047 	    else
   8048 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   8049 	  }
   8050       }
   8051 
   8052   if (toc_ref != NULL)
   8053     free (toc_ref);
   8054   return TRUE;
   8055 }
   8056 
   8057 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   8058    the values of any global symbols in a toc section that has been
   8059    edited.  Globals in toc sections should be a rarity, so this function
   8060    sets a flag if any are found in toc sections other than the one just
   8061    edited, so that futher hash table traversals can be avoided.  */
   8062 
   8063 struct adjust_toc_info
   8064 {
   8065   asection *toc;
   8066   unsigned long *skip;
   8067   bfd_boolean global_toc_syms;
   8068 };
   8069 
   8070 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   8071 
   8072 static bfd_boolean
   8073 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   8074 {
   8075   struct ppc_link_hash_entry *eh;
   8076   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   8077   unsigned long i;
   8078 
   8079   if (h->root.type != bfd_link_hash_defined
   8080       && h->root.type != bfd_link_hash_defweak)
   8081     return TRUE;
   8082 
   8083   eh = (struct ppc_link_hash_entry *) h;
   8084   if (eh->adjust_done)
   8085     return TRUE;
   8086 
   8087   if (eh->elf.root.u.def.section == toc_inf->toc)
   8088     {
   8089       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   8090 	i = toc_inf->toc->rawsize >> 3;
   8091       else
   8092 	i = eh->elf.root.u.def.value >> 3;
   8093 
   8094       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8095 	{
   8096 	  (*_bfd_error_handler)
   8097 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   8098 	  do
   8099 	    ++i;
   8100 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   8101 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   8102 	}
   8103 
   8104       eh->elf.root.u.def.value -= toc_inf->skip[i];
   8105       eh->adjust_done = 1;
   8106     }
   8107   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   8108     toc_inf->global_toc_syms = TRUE;
   8109 
   8110   return TRUE;
   8111 }
   8112 
   8113 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
   8114 
   8115 static bfd_boolean
   8116 ok_lo_toc_insn (unsigned int insn)
   8117 {
   8118   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
   8119 	  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
   8120 	  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
   8121 	  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
   8122 	  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
   8123 	  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
   8124 	  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
   8125 	  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
   8126 	  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
   8127 	  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
   8128 	  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
   8129 	  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
   8130 	  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
   8131 	  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
   8132 	  || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
   8133 	      && (insn & 3) != 1)
   8134 	  || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
   8135 	      && ((insn & 3) == 0 || (insn & 3) == 3))
   8136 	  || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
   8137 }
   8138 
   8139 /* Examine all relocs referencing .toc sections in order to remove
   8140    unused .toc entries.  */
   8141 
   8142 bfd_boolean
   8143 ppc64_elf_edit_toc (struct bfd_link_info *info)
   8144 {
   8145   bfd *ibfd;
   8146   struct adjust_toc_info toc_inf;
   8147   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8148 
   8149   htab->do_toc_opt = 1;
   8150   toc_inf.global_toc_syms = TRUE;
   8151   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   8152     {
   8153       asection *toc, *sec;
   8154       Elf_Internal_Shdr *symtab_hdr;
   8155       Elf_Internal_Sym *local_syms;
   8156       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   8157       unsigned long *skip, *drop;
   8158       unsigned char *used;
   8159       unsigned char *keep, last, some_unused;
   8160 
   8161       if (!is_ppc64_elf (ibfd))
   8162 	continue;
   8163 
   8164       toc = bfd_get_section_by_name (ibfd, ".toc");
   8165       if (toc == NULL
   8166 	  || toc->size == 0
   8167 	  || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8168 	  || discarded_section (toc))
   8169 	continue;
   8170 
   8171       toc_relocs = NULL;
   8172       local_syms = NULL;
   8173       symtab_hdr = &elf_symtab_hdr (ibfd);
   8174 
   8175       /* Look at sections dropped from the final link.  */
   8176       skip = NULL;
   8177       relstart = NULL;
   8178       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8179 	{
   8180 	  if (sec->reloc_count == 0
   8181 	      || !discarded_section (sec)
   8182 	      || get_opd_info (sec)
   8183 	      || (sec->flags & SEC_ALLOC) == 0
   8184 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8185 	    continue;
   8186 
   8187 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
   8188 	  if (relstart == NULL)
   8189 	    goto error_ret;
   8190 
   8191 	  /* Run through the relocs to see which toc entries might be
   8192 	     unused.  */
   8193 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8194 	    {
   8195 	      enum elf_ppc64_reloc_type r_type;
   8196 	      unsigned long r_symndx;
   8197 	      asection *sym_sec;
   8198 	      struct elf_link_hash_entry *h;
   8199 	      Elf_Internal_Sym *sym;
   8200 	      bfd_vma val;
   8201 
   8202 	      r_type = ELF64_R_TYPE (rel->r_info);
   8203 	      switch (r_type)
   8204 		{
   8205 		default:
   8206 		  continue;
   8207 
   8208 		case R_PPC64_TOC16:
   8209 		case R_PPC64_TOC16_LO:
   8210 		case R_PPC64_TOC16_HI:
   8211 		case R_PPC64_TOC16_HA:
   8212 		case R_PPC64_TOC16_DS:
   8213 		case R_PPC64_TOC16_LO_DS:
   8214 		  break;
   8215 		}
   8216 
   8217 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8218 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8219 			      r_symndx, ibfd))
   8220 		goto error_ret;
   8221 
   8222 	      if (sym_sec != toc)
   8223 		continue;
   8224 
   8225 	      if (h != NULL)
   8226 		val = h->root.u.def.value;
   8227 	      else
   8228 		val = sym->st_value;
   8229 	      val += rel->r_addend;
   8230 
   8231 	      if (val >= toc->size)
   8232 		continue;
   8233 
   8234 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8235 		 If not, don't mark as unused.  */
   8236 	      if (val & 7)
   8237 		continue;
   8238 
   8239 	      if (skip == NULL)
   8240 		{
   8241 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8242 		  if (skip == NULL)
   8243 		    goto error_ret;
   8244 		}
   8245 
   8246 	      skip[val >> 3] = ref_from_discarded;
   8247 	    }
   8248 
   8249 	  if (elf_section_data (sec)->relocs != relstart)
   8250 	    free (relstart);
   8251 	}
   8252 
   8253       /* For largetoc loads of address constants, we can convert
   8254 	 .  addis rx,2,addr@got@ha
   8255 	 .  ld ry,addr@got@l(rx)
   8256 	 to
   8257 	 .  addis rx,2,addr@toc@ha
   8258 	 .  addi ry,rx,addr@toc@l
   8259 	 when addr is within 2G of the toc pointer.  This then means
   8260 	 that the word storing "addr" in the toc is no longer needed.  */
   8261 
   8262       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   8263 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   8264 	  && toc->reloc_count != 0)
   8265 	{
   8266 	  /* Read toc relocs.  */
   8267 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8268 						  info->keep_memory);
   8269 	  if (toc_relocs == NULL)
   8270 	    goto error_ret;
   8271 
   8272 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8273 	    {
   8274 	      enum elf_ppc64_reloc_type r_type;
   8275 	      unsigned long r_symndx;
   8276 	      asection *sym_sec;
   8277 	      struct elf_link_hash_entry *h;
   8278 	      Elf_Internal_Sym *sym;
   8279 	      bfd_vma val, addr;
   8280 
   8281 	      r_type = ELF64_R_TYPE (rel->r_info);
   8282 	      if (r_type != R_PPC64_ADDR64)
   8283 		continue;
   8284 
   8285 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8286 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8287 			      r_symndx, ibfd))
   8288 		goto error_ret;
   8289 
   8290 	      if (sym_sec == NULL
   8291 		  || discarded_section (sym_sec))
   8292 		continue;
   8293 
   8294 	      if (!SYMBOL_CALLS_LOCAL (info, h))
   8295 		continue;
   8296 
   8297 	      if (h != NULL)
   8298 		{
   8299 		  if (h->type == STT_GNU_IFUNC)
   8300 		    continue;
   8301 		  val = h->root.u.def.value;
   8302 		}
   8303 	      else
   8304 		{
   8305 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   8306 		    continue;
   8307 		  val = sym->st_value;
   8308 		}
   8309 	      val += rel->r_addend;
   8310 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   8311 
   8312 	      /* We don't yet know the exact toc pointer value, but we
   8313 		 know it will be somewhere in the toc section.  Don't
   8314 		 optimize if the difference from any possible toc
   8315 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   8316 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   8317 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8318 		continue;
   8319 
   8320 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   8321 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8322 		continue;
   8323 
   8324 	      if (skip == NULL)
   8325 		{
   8326 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8327 		  if (skip == NULL)
   8328 		    goto error_ret;
   8329 		}
   8330 
   8331 	      skip[rel->r_offset >> 3]
   8332 		|= can_optimize | ((rel - toc_relocs) << 2);
   8333 	    }
   8334 	}
   8335 
   8336       if (skip == NULL)
   8337 	continue;
   8338 
   8339       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   8340       if (used == NULL)
   8341 	{
   8342 	error_ret:
   8343 	  if (local_syms != NULL
   8344 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   8345 	    free (local_syms);
   8346 	  if (sec != NULL
   8347 	      && relstart != NULL
   8348 	      && elf_section_data (sec)->relocs != relstart)
   8349 	    free (relstart);
   8350 	  if (toc_relocs != NULL
   8351 	      && elf_section_data (toc)->relocs != toc_relocs)
   8352 	    free (toc_relocs);
   8353 	  if (skip != NULL)
   8354 	    free (skip);
   8355 	  return FALSE;
   8356 	}
   8357 
   8358       /* Now check all kept sections that might reference the toc.
   8359 	 Check the toc itself last.  */
   8360       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   8361 		  : ibfd->sections);
   8362 	   sec != NULL;
   8363 	   sec = (sec == toc ? NULL
   8364 		  : sec->next == NULL ? toc
   8365 		  : sec->next == toc && toc->next ? toc->next
   8366 		  : sec->next))
   8367 	{
   8368 	  int repeat;
   8369 
   8370 	  if (sec->reloc_count == 0
   8371 	      || discarded_section (sec)
   8372 	      || get_opd_info (sec)
   8373 	      || (sec->flags & SEC_ALLOC) == 0
   8374 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8375 	    continue;
   8376 
   8377 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8378 						info->keep_memory);
   8379 	  if (relstart == NULL)
   8380 	    goto error_ret;
   8381 
   8382 	  /* Mark toc entries referenced as used.  */
   8383 	  do
   8384 	    {
   8385 	      repeat = 0;
   8386 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8387 		{
   8388 		  enum elf_ppc64_reloc_type r_type;
   8389 		  unsigned long r_symndx;
   8390 		  asection *sym_sec;
   8391 		  struct elf_link_hash_entry *h;
   8392 		  Elf_Internal_Sym *sym;
   8393 		  bfd_vma val;
   8394 		  enum {no_check, check_lo, check_ha} insn_check;
   8395 
   8396 		  r_type = ELF64_R_TYPE (rel->r_info);
   8397 		  switch (r_type)
   8398 		    {
   8399 		    default:
   8400 		      insn_check = no_check;
   8401 		      break;
   8402 
   8403 		    case R_PPC64_GOT_TLSLD16_HA:
   8404 		    case R_PPC64_GOT_TLSGD16_HA:
   8405 		    case R_PPC64_GOT_TPREL16_HA:
   8406 		    case R_PPC64_GOT_DTPREL16_HA:
   8407 		    case R_PPC64_GOT16_HA:
   8408 		    case R_PPC64_TOC16_HA:
   8409 		      insn_check = check_ha;
   8410 		      break;
   8411 
   8412 		    case R_PPC64_GOT_TLSLD16_LO:
   8413 		    case R_PPC64_GOT_TLSGD16_LO:
   8414 		    case R_PPC64_GOT_TPREL16_LO_DS:
   8415 		    case R_PPC64_GOT_DTPREL16_LO_DS:
   8416 		    case R_PPC64_GOT16_LO:
   8417 		    case R_PPC64_GOT16_LO_DS:
   8418 		    case R_PPC64_TOC16_LO:
   8419 		    case R_PPC64_TOC16_LO_DS:
   8420 		      insn_check = check_lo;
   8421 		      break;
   8422 		    }
   8423 
   8424 		  if (insn_check != no_check)
   8425 		    {
   8426 		      bfd_vma off = rel->r_offset & ~3;
   8427 		      unsigned char buf[4];
   8428 		      unsigned int insn;
   8429 
   8430 		      if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
   8431 			{
   8432 			  free (used);
   8433 			  goto error_ret;
   8434 			}
   8435 		      insn = bfd_get_32 (ibfd, buf);
   8436 		      if (insn_check == check_lo
   8437 			  ? !ok_lo_toc_insn (insn)
   8438 			  : ((insn & ((0x3f << 26) | 0x1f << 16))
   8439 			     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   8440 			{
   8441 			  char str[12];
   8442 
   8443 			  ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
   8444 			  sprintf (str, "%#08x", insn);
   8445 			  info->callbacks->einfo
   8446 			    (_("%P: %H: toc optimization is not supported for"
   8447 			       " %s instruction.\n"),
   8448 			     ibfd, sec, rel->r_offset & ~3, str);
   8449 			}
   8450 		    }
   8451 
   8452 		  switch (r_type)
   8453 		    {
   8454 		    case R_PPC64_TOC16:
   8455 		    case R_PPC64_TOC16_LO:
   8456 		    case R_PPC64_TOC16_HI:
   8457 		    case R_PPC64_TOC16_HA:
   8458 		    case R_PPC64_TOC16_DS:
   8459 		    case R_PPC64_TOC16_LO_DS:
   8460 		      /* In case we're taking addresses of toc entries.  */
   8461 		    case R_PPC64_ADDR64:
   8462 		      break;
   8463 
   8464 		    default:
   8465 		      continue;
   8466 		    }
   8467 
   8468 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8469 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8470 				  r_symndx, ibfd))
   8471 		    {
   8472 		      free (used);
   8473 		      goto error_ret;
   8474 		    }
   8475 
   8476 		  if (sym_sec != toc)
   8477 		    continue;
   8478 
   8479 		  if (h != NULL)
   8480 		    val = h->root.u.def.value;
   8481 		  else
   8482 		    val = sym->st_value;
   8483 		  val += rel->r_addend;
   8484 
   8485 		  if (val >= toc->size)
   8486 		    continue;
   8487 
   8488 		  if ((skip[val >> 3] & can_optimize) != 0)
   8489 		    {
   8490 		      bfd_vma off;
   8491 		      unsigned char opc;
   8492 
   8493 		      switch (r_type)
   8494 			{
   8495 			case R_PPC64_TOC16_HA:
   8496 			  break;
   8497 
   8498 			case R_PPC64_TOC16_LO_DS:
   8499 			  off = rel->r_offset;
   8500 			  off += (bfd_big_endian (ibfd) ? -2 : 3);
   8501 			  if (!bfd_get_section_contents (ibfd, sec, &opc,
   8502 							 off, 1))
   8503 			    {
   8504 			      free (used);
   8505 			      goto error_ret;
   8506 			    }
   8507 			  if ((opc & (0x3f << 2)) == (58u << 2))
   8508 			    break;
   8509 			  /* Fall thru */
   8510 
   8511 			default:
   8512 			  /* Wrong sort of reloc, or not a ld.  We may
   8513 			     as well clear ref_from_discarded too.  */
   8514 			  skip[val >> 3] = 0;
   8515 			}
   8516 		    }
   8517 
   8518 		  if (sec != toc)
   8519 		    used[val >> 3] = 1;
   8520 		  /* For the toc section, we only mark as used if this
   8521 		     entry itself isn't unused.  */
   8522 		  else if ((used[rel->r_offset >> 3]
   8523 			    || !(skip[rel->r_offset >> 3] & ref_from_discarded))
   8524 			   && !used[val >> 3])
   8525 		    {
   8526 		      /* Do all the relocs again, to catch reference
   8527 			 chains.  */
   8528 		      repeat = 1;
   8529 		      used[val >> 3] = 1;
   8530 		    }
   8531 		}
   8532 	    }
   8533 	  while (repeat);
   8534 
   8535 	  if (elf_section_data (sec)->relocs != relstart)
   8536 	    free (relstart);
   8537 	}
   8538 
   8539       /* Merge the used and skip arrays.  Assume that TOC
   8540 	 doublewords not appearing as either used or unused belong
   8541 	 to to an entry more than one doubleword in size.  */
   8542       for (drop = skip, keep = used, last = 0, some_unused = 0;
   8543 	   drop < skip + (toc->size + 7) / 8;
   8544 	   ++drop, ++keep)
   8545 	{
   8546 	  if (*keep)
   8547 	    {
   8548 	      *drop &= ~ref_from_discarded;
   8549 	      if ((*drop & can_optimize) != 0)
   8550 		some_unused = 1;
   8551 	      last = 0;
   8552 	    }
   8553 	  else if ((*drop & ref_from_discarded) != 0)
   8554 	    {
   8555 	      some_unused = 1;
   8556 	      last = ref_from_discarded;
   8557 	    }
   8558 	  else
   8559 	    *drop = last;
   8560 	}
   8561 
   8562       free (used);
   8563 
   8564       if (some_unused)
   8565 	{
   8566 	  bfd_byte *contents, *src;
   8567 	  unsigned long off;
   8568 	  Elf_Internal_Sym *sym;
   8569 	  bfd_boolean local_toc_syms = FALSE;
   8570 
   8571 	  /* Shuffle the toc contents, and at the same time convert the
   8572 	     skip array from booleans into offsets.  */
   8573 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   8574 	    goto error_ret;
   8575 
   8576 	  elf_section_data (toc)->this_hdr.contents = contents;
   8577 
   8578 	  for (src = contents, off = 0, drop = skip;
   8579 	       src < contents + toc->size;
   8580 	       src += 8, ++drop)
   8581 	    {
   8582 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   8583 		off += 8;
   8584 	      else if (off != 0)
   8585 		{
   8586 		  *drop = off;
   8587 		  memcpy (src - off, src, 8);
   8588 		}
   8589 	    }
   8590 	  *drop = off;
   8591 	  toc->rawsize = toc->size;
   8592 	  toc->size = src - contents - off;
   8593 
   8594 	  /* Adjust addends for relocs against the toc section sym,
   8595 	     and optimize any accesses we can.  */
   8596 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8597 	    {
   8598 	      if (sec->reloc_count == 0
   8599 		  || discarded_section (sec))
   8600 		continue;
   8601 
   8602 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8603 						    info->keep_memory);
   8604 	      if (relstart == NULL)
   8605 		goto error_ret;
   8606 
   8607 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8608 		{
   8609 		  enum elf_ppc64_reloc_type r_type;
   8610 		  unsigned long r_symndx;
   8611 		  asection *sym_sec;
   8612 		  struct elf_link_hash_entry *h;
   8613 		  bfd_vma val;
   8614 
   8615 		  r_type = ELF64_R_TYPE (rel->r_info);
   8616 		  switch (r_type)
   8617 		    {
   8618 		    default:
   8619 		      continue;
   8620 
   8621 		    case R_PPC64_TOC16:
   8622 		    case R_PPC64_TOC16_LO:
   8623 		    case R_PPC64_TOC16_HI:
   8624 		    case R_PPC64_TOC16_HA:
   8625 		    case R_PPC64_TOC16_DS:
   8626 		    case R_PPC64_TOC16_LO_DS:
   8627 		    case R_PPC64_ADDR64:
   8628 		      break;
   8629 		    }
   8630 
   8631 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8632 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8633 				  r_symndx, ibfd))
   8634 		    goto error_ret;
   8635 
   8636 		  if (sym_sec != toc)
   8637 		    continue;
   8638 
   8639 		  if (h != NULL)
   8640 		    val = h->root.u.def.value;
   8641 		  else
   8642 		    {
   8643 		      val = sym->st_value;
   8644 		      if (val != 0)
   8645 			local_toc_syms = TRUE;
   8646 		    }
   8647 
   8648 		  val += rel->r_addend;
   8649 
   8650 		  if (val > toc->rawsize)
   8651 		    val = toc->rawsize;
   8652 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   8653 		    continue;
   8654 		  else if ((skip[val >> 3] & can_optimize) != 0)
   8655 		    {
   8656 		      Elf_Internal_Rela *tocrel
   8657 			= toc_relocs + (skip[val >> 3] >> 2);
   8658 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   8659 
   8660 		      switch (r_type)
   8661 			{
   8662 			case R_PPC64_TOC16_HA:
   8663 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   8664 			  break;
   8665 
   8666 			case R_PPC64_TOC16_LO_DS:
   8667 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   8668 			  break;
   8669 
   8670 			default:
   8671 			  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   8672 			    ppc_howto_init ();
   8673 			  info->callbacks->einfo
   8674 			    (_("%P: %H: %s references "
   8675 			       "optimized away TOC entry\n"),
   8676 			     ibfd, sec, rel->r_offset,
   8677 			     ppc64_elf_howto_table[r_type]->name);
   8678 			  bfd_set_error (bfd_error_bad_value);
   8679 			  goto error_ret;
   8680 			}
   8681 		      rel->r_addend = tocrel->r_addend;
   8682 		      elf_section_data (sec)->relocs = relstart;
   8683 		      continue;
   8684 		    }
   8685 
   8686 		  if (h != NULL || sym->st_value != 0)
   8687 		    continue;
   8688 
   8689 		  rel->r_addend -= skip[val >> 3];
   8690 		  elf_section_data (sec)->relocs = relstart;
   8691 		}
   8692 
   8693 	      if (elf_section_data (sec)->relocs != relstart)
   8694 		free (relstart);
   8695 	    }
   8696 
   8697 	  /* We shouldn't have local or global symbols defined in the TOC,
   8698 	     but handle them anyway.  */
   8699 	  if (local_syms != NULL)
   8700 	    for (sym = local_syms;
   8701 		 sym < local_syms + symtab_hdr->sh_info;
   8702 		 ++sym)
   8703 	      if (sym->st_value != 0
   8704 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   8705 		{
   8706 		  unsigned long i;
   8707 
   8708 		  if (sym->st_value > toc->rawsize)
   8709 		    i = toc->rawsize >> 3;
   8710 		  else
   8711 		    i = sym->st_value >> 3;
   8712 
   8713 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8714 		    {
   8715 		      if (local_toc_syms)
   8716 			(*_bfd_error_handler)
   8717 			  (_("%s defined on removed toc entry"),
   8718 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   8719 		      do
   8720 			++i;
   8721 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   8722 		      sym->st_value = (bfd_vma) i << 3;
   8723 		    }
   8724 
   8725 		  sym->st_value -= skip[i];
   8726 		  symtab_hdr->contents = (unsigned char *) local_syms;
   8727 		}
   8728 
   8729 	  /* Adjust any global syms defined in this toc input section.  */
   8730 	  if (toc_inf.global_toc_syms)
   8731 	    {
   8732 	      toc_inf.toc = toc;
   8733 	      toc_inf.skip = skip;
   8734 	      toc_inf.global_toc_syms = FALSE;
   8735 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   8736 				      &toc_inf);
   8737 	    }
   8738 
   8739 	  if (toc->reloc_count != 0)
   8740 	    {
   8741 	      Elf_Internal_Shdr *rel_hdr;
   8742 	      Elf_Internal_Rela *wrel;
   8743 	      bfd_size_type sz;
   8744 
   8745 	      /* Remove unused toc relocs, and adjust those we keep.  */
   8746 	      if (toc_relocs == NULL)
   8747 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8748 							info->keep_memory);
   8749 	      if (toc_relocs == NULL)
   8750 		goto error_ret;
   8751 
   8752 	      wrel = toc_relocs;
   8753 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8754 		if ((skip[rel->r_offset >> 3]
   8755 		     & (ref_from_discarded | can_optimize)) == 0)
   8756 		  {
   8757 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   8758 		    wrel->r_info = rel->r_info;
   8759 		    wrel->r_addend = rel->r_addend;
   8760 		    ++wrel;
   8761 		  }
   8762 		else if (!dec_dynrel_count (rel->r_info, toc, info,
   8763 					    &local_syms, NULL, NULL))
   8764 		  goto error_ret;
   8765 
   8766 	      elf_section_data (toc)->relocs = toc_relocs;
   8767 	      toc->reloc_count = wrel - toc_relocs;
   8768 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   8769 	      sz = rel_hdr->sh_entsize;
   8770 	      rel_hdr->sh_size = toc->reloc_count * sz;
   8771 	    }
   8772 	}
   8773       else if (toc_relocs != NULL
   8774 	       && elf_section_data (toc)->relocs != toc_relocs)
   8775 	free (toc_relocs);
   8776 
   8777       if (local_syms != NULL
   8778 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   8779 	{
   8780 	  if (!info->keep_memory)
   8781 	    free (local_syms);
   8782 	  else
   8783 	    symtab_hdr->contents = (unsigned char *) local_syms;
   8784 	}
   8785       free (skip);
   8786     }
   8787 
   8788   return TRUE;
   8789 }
   8790 
   8791 /* Return true iff input section I references the TOC using
   8792    instructions limited to +/-32k offsets.  */
   8793 
   8794 bfd_boolean
   8795 ppc64_elf_has_small_toc_reloc (asection *i)
   8796 {
   8797   return (is_ppc64_elf (i->owner)
   8798 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   8799 }
   8800 
   8801 /* Allocate space for one GOT entry.  */
   8802 
   8803 static void
   8804 allocate_got (struct elf_link_hash_entry *h,
   8805 	      struct bfd_link_info *info,
   8806 	      struct got_entry *gent)
   8807 {
   8808   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8809   bfd_boolean dyn;
   8810   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   8811   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   8812 		 ? 16 : 8);
   8813   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   8814 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   8815   asection *got = ppc64_elf_tdata (gent->owner)->got;
   8816 
   8817   gent->got.offset = got->size;
   8818   got->size += entsize;
   8819 
   8820   dyn = htab->elf.dynamic_sections_created;
   8821   if ((info->shared
   8822        || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
   8823 	    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   8824 		|| h->root.type != bfd_link_hash_undefweak))
   8825     {
   8826       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   8827       relgot->size += rentsize;
   8828     }
   8829   else if (h->type == STT_GNU_IFUNC)
   8830     {
   8831       asection *relgot = htab->reliplt;
   8832       relgot->size += rentsize;
   8833       htab->got_reli_size += rentsize;
   8834     }
   8835 }
   8836 
   8837 /* This function merges got entries in the same toc group.  */
   8838 
   8839 static void
   8840 merge_got_entries (struct got_entry **pent)
   8841 {
   8842   struct got_entry *ent, *ent2;
   8843 
   8844   for (ent = *pent; ent != NULL; ent = ent->next)
   8845     if (!ent->is_indirect)
   8846       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   8847 	if (!ent2->is_indirect
   8848 	    && ent2->addend == ent->addend
   8849 	    && ent2->tls_type == ent->tls_type
   8850 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   8851 	  {
   8852 	    ent2->is_indirect = TRUE;
   8853 	    ent2->got.ent = ent;
   8854 	  }
   8855 }
   8856 
   8857 /* Allocate space in .plt, .got and associated reloc sections for
   8858    dynamic relocs.  */
   8859 
   8860 static bfd_boolean
   8861 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8862 {
   8863   struct bfd_link_info *info;
   8864   struct ppc_link_hash_table *htab;
   8865   asection *s;
   8866   struct ppc_link_hash_entry *eh;
   8867   struct elf_dyn_relocs *p;
   8868   struct got_entry **pgent, *gent;
   8869 
   8870   if (h->root.type == bfd_link_hash_indirect)
   8871     return TRUE;
   8872 
   8873   info = (struct bfd_link_info *) inf;
   8874   htab = ppc_hash_table (info);
   8875   if (htab == NULL)
   8876     return FALSE;
   8877 
   8878   if ((htab->elf.dynamic_sections_created
   8879        && h->dynindx != -1
   8880        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
   8881       || h->type == STT_GNU_IFUNC)
   8882     {
   8883       struct plt_entry *pent;
   8884       bfd_boolean doneone = FALSE;
   8885       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   8886 	if (pent->plt.refcount > 0)
   8887 	  {
   8888 	    if (!htab->elf.dynamic_sections_created
   8889 		|| h->dynindx == -1)
   8890 	      {
   8891 		s = htab->iplt;
   8892 		pent->plt.offset = s->size;
   8893 		s->size += PLT_ENTRY_SIZE;
   8894 		s = htab->reliplt;
   8895 	      }
   8896 	    else
   8897 	      {
   8898 		/* If this is the first .plt entry, make room for the special
   8899 		   first entry.  */
   8900 		s = htab->plt;
   8901 		if (s->size == 0)
   8902 		  s->size += PLT_INITIAL_ENTRY_SIZE;
   8903 
   8904 		pent->plt.offset = s->size;
   8905 
   8906 		/* Make room for this entry.  */
   8907 		s->size += PLT_ENTRY_SIZE;
   8908 
   8909 		/* Make room for the .glink code.  */
   8910 		s = htab->glink;
   8911 		if (s->size == 0)
   8912 		  s->size += GLINK_CALL_STUB_SIZE;
   8913 		/* We need bigger stubs past index 32767.  */
   8914 		if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
   8915 		  s->size += 4;
   8916 		s->size += 2*4;
   8917 
   8918 		/* We also need to make an entry in the .rela.plt section.  */
   8919 		s = htab->relplt;
   8920 	      }
   8921 	    s->size += sizeof (Elf64_External_Rela);
   8922 	    doneone = TRUE;
   8923 	  }
   8924 	else
   8925 	  pent->plt.offset = (bfd_vma) -1;
   8926       if (!doneone)
   8927 	{
   8928 	  h->plt.plist = NULL;
   8929 	  h->needs_plt = 0;
   8930 	}
   8931     }
   8932   else
   8933     {
   8934       h->plt.plist = NULL;
   8935       h->needs_plt = 0;
   8936     }
   8937 
   8938   eh = (struct ppc_link_hash_entry *) h;
   8939   /* Run through the TLS GD got entries first if we're changing them
   8940      to TPREL.  */
   8941   if ((eh->tls_mask & TLS_TPRELGD) != 0)
   8942     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8943       if (gent->got.refcount > 0
   8944 	  && (gent->tls_type & TLS_GD) != 0)
   8945 	{
   8946 	  /* This was a GD entry that has been converted to TPREL.  If
   8947 	     there happens to be a TPREL entry we can use that one.  */
   8948 	  struct got_entry *ent;
   8949 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   8950 	    if (ent->got.refcount > 0
   8951 		&& (ent->tls_type & TLS_TPREL) != 0
   8952 		&& ent->addend == gent->addend
   8953 		&& ent->owner == gent->owner)
   8954 	      {
   8955 		gent->got.refcount = 0;
   8956 		break;
   8957 	      }
   8958 
   8959 	  /* If not, then we'll be using our own TPREL entry.  */
   8960 	  if (gent->got.refcount != 0)
   8961 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   8962 	}
   8963 
   8964   /* Remove any list entry that won't generate a word in the GOT before
   8965      we call merge_got_entries.  Otherwise we risk merging to empty
   8966      entries.  */
   8967   pgent = &h->got.glist;
   8968   while ((gent = *pgent) != NULL)
   8969     if (gent->got.refcount > 0)
   8970       {
   8971 	if ((gent->tls_type & TLS_LD) != 0
   8972 	    && !h->def_dynamic)
   8973 	  {
   8974 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   8975 	    *pgent = gent->next;
   8976 	  }
   8977 	else
   8978 	  pgent = &gent->next;
   8979       }
   8980     else
   8981       *pgent = gent->next;
   8982 
   8983   if (!htab->do_multi_toc)
   8984     merge_got_entries (&h->got.glist);
   8985 
   8986   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8987     if (!gent->is_indirect)
   8988       {
   8989 	/* Make sure this symbol is output as a dynamic symbol.
   8990 	   Undefined weak syms won't yet be marked as dynamic,
   8991 	   nor will all TLS symbols.  */
   8992 	if (h->dynindx == -1
   8993 	    && !h->forced_local
   8994 	    && h->type != STT_GNU_IFUNC
   8995 	    && htab->elf.dynamic_sections_created)
   8996 	  {
   8997 	    if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8998 	      return FALSE;
   8999 	  }
   9000 
   9001 	if (!is_ppc64_elf (gent->owner))
   9002 	  abort ();
   9003 
   9004 	allocate_got (h, info, gent);
   9005       }
   9006 
   9007   if (eh->dyn_relocs == NULL
   9008       || (!htab->elf.dynamic_sections_created
   9009 	  && h->type != STT_GNU_IFUNC))
   9010     return TRUE;
   9011 
   9012   /* In the shared -Bsymbolic case, discard space allocated for
   9013      dynamic pc-relative relocs against symbols which turn out to be
   9014      defined in regular objects.  For the normal shared case, discard
   9015      space for relocs that have become local due to symbol visibility
   9016      changes.  */
   9017 
   9018   if (info->shared)
   9019     {
   9020       /* Relocs that use pc_count are those that appear on a call insn,
   9021 	 or certain REL relocs (see must_be_dyn_reloc) that can be
   9022 	 generated via assembly.  We want calls to protected symbols to
   9023 	 resolve directly to the function rather than going via the plt.
   9024 	 If people want function pointer comparisons to work as expected
   9025 	 then they should avoid writing weird assembly.  */
   9026       if (SYMBOL_CALLS_LOCAL (info, h))
   9027 	{
   9028 	  struct elf_dyn_relocs **pp;
   9029 
   9030 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   9031 	    {
   9032 	      p->count -= p->pc_count;
   9033 	      p->pc_count = 0;
   9034 	      if (p->count == 0)
   9035 		*pp = p->next;
   9036 	      else
   9037 		pp = &p->next;
   9038 	    }
   9039 	}
   9040 
   9041       /* Also discard relocs on undefined weak syms with non-default
   9042 	 visibility.  */
   9043       if (eh->dyn_relocs != NULL
   9044 	  && h->root.type == bfd_link_hash_undefweak)
   9045 	{
   9046 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   9047 	    eh->dyn_relocs = NULL;
   9048 
   9049 	  /* Make sure this symbol is output as a dynamic symbol.
   9050 	     Undefined weak syms won't yet be marked as dynamic.  */
   9051 	  else if (h->dynindx == -1
   9052 		   && !h->forced_local)
   9053 	    {
   9054 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9055 		return FALSE;
   9056 	    }
   9057 	}
   9058     }
   9059   else if (h->type == STT_GNU_IFUNC)
   9060     {
   9061       if (!h->non_got_ref)
   9062 	eh->dyn_relocs = NULL;
   9063     }
   9064   else if (ELIMINATE_COPY_RELOCS)
   9065     {
   9066       /* For the non-shared case, discard space for relocs against
   9067 	 symbols which turn out to need copy relocs or are not
   9068 	 dynamic.  */
   9069 
   9070       if (!h->non_got_ref
   9071 	  && !h->def_regular)
   9072 	{
   9073 	  /* Make sure this symbol is output as a dynamic symbol.
   9074 	     Undefined weak syms won't yet be marked as dynamic.  */
   9075 	  if (h->dynindx == -1
   9076 	      && !h->forced_local)
   9077 	    {
   9078 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9079 		return FALSE;
   9080 	    }
   9081 
   9082 	  /* If that succeeded, we know we'll be keeping all the
   9083 	     relocs.  */
   9084 	  if (h->dynindx != -1)
   9085 	    goto keep;
   9086 	}
   9087 
   9088       eh->dyn_relocs = NULL;
   9089 
   9090     keep: ;
   9091     }
   9092 
   9093   /* Finally, allocate space.  */
   9094   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   9095     {
   9096       asection *sreloc = elf_section_data (p->sec)->sreloc;
   9097       if (!htab->elf.dynamic_sections_created)
   9098 	sreloc = htab->reliplt;
   9099       sreloc->size += p->count * sizeof (Elf64_External_Rela);
   9100     }
   9101 
   9102   return TRUE;
   9103 }
   9104 
   9105 /* Find any dynamic relocs that apply to read-only sections.  */
   9106 
   9107 static bfd_boolean
   9108 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9109 {
   9110   struct ppc_link_hash_entry *eh;
   9111   struct elf_dyn_relocs *p;
   9112 
   9113   eh = (struct ppc_link_hash_entry *) h;
   9114   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   9115     {
   9116       asection *s = p->sec->output_section;
   9117 
   9118       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   9119 	{
   9120 	  struct bfd_link_info *info = inf;
   9121 
   9122 	  info->flags |= DF_TEXTREL;
   9123 
   9124 	  /* Not an error, just cut short the traversal.  */
   9125 	  return FALSE;
   9126 	}
   9127     }
   9128   return TRUE;
   9129 }
   9130 
   9131 /* Set the sizes of the dynamic sections.  */
   9132 
   9133 static bfd_boolean
   9134 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   9135 				 struct bfd_link_info *info)
   9136 {
   9137   struct ppc_link_hash_table *htab;
   9138   bfd *dynobj;
   9139   asection *s;
   9140   bfd_boolean relocs;
   9141   bfd *ibfd;
   9142   struct got_entry *first_tlsld;
   9143 
   9144   htab = ppc_hash_table (info);
   9145   if (htab == NULL)
   9146     return FALSE;
   9147 
   9148   dynobj = htab->elf.dynobj;
   9149   if (dynobj == NULL)
   9150     abort ();
   9151 
   9152   if (htab->elf.dynamic_sections_created)
   9153     {
   9154       /* Set the contents of the .interp section to the interpreter.  */
   9155       if (info->executable)
   9156 	{
   9157 	  s = bfd_get_linker_section (dynobj, ".interp");
   9158 	  if (s == NULL)
   9159 	    abort ();
   9160 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   9161 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   9162 	}
   9163     }
   9164 
   9165   /* Set up .got offsets for local syms, and space for local dynamic
   9166      relocs.  */
   9167   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9168     {
   9169       struct got_entry **lgot_ents;
   9170       struct got_entry **end_lgot_ents;
   9171       struct plt_entry **local_plt;
   9172       struct plt_entry **end_local_plt;
   9173       unsigned char *lgot_masks;
   9174       bfd_size_type locsymcount;
   9175       Elf_Internal_Shdr *symtab_hdr;
   9176       asection *srel;
   9177 
   9178       if (!is_ppc64_elf (ibfd))
   9179 	continue;
   9180 
   9181       for (s = ibfd->sections; s != NULL; s = s->next)
   9182 	{
   9183 	  struct elf_dyn_relocs *p;
   9184 
   9185 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   9186 	    {
   9187 	      if (!bfd_is_abs_section (p->sec)
   9188 		  && bfd_is_abs_section (p->sec->output_section))
   9189 		{
   9190 		  /* Input section has been discarded, either because
   9191 		     it is a copy of a linkonce section or due to
   9192 		     linker script /DISCARD/, so we'll be discarding
   9193 		     the relocs too.  */
   9194 		}
   9195 	      else if (p->count != 0)
   9196 		{
   9197 		  srel = elf_section_data (p->sec)->sreloc;
   9198 		  if (!htab->elf.dynamic_sections_created)
   9199 		    srel = htab->reliplt;
   9200 		  srel->size += p->count * sizeof (Elf64_External_Rela);
   9201 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   9202 		    info->flags |= DF_TEXTREL;
   9203 		}
   9204 	    }
   9205 	}
   9206 
   9207       lgot_ents = elf_local_got_ents (ibfd);
   9208       if (!lgot_ents)
   9209 	continue;
   9210 
   9211       symtab_hdr = &elf_symtab_hdr (ibfd);
   9212       locsymcount = symtab_hdr->sh_info;
   9213       end_lgot_ents = lgot_ents + locsymcount;
   9214       local_plt = (struct plt_entry **) end_lgot_ents;
   9215       end_local_plt = local_plt + locsymcount;
   9216       lgot_masks = (unsigned char *) end_local_plt;
   9217       s = ppc64_elf_tdata (ibfd)->got;
   9218       srel = ppc64_elf_tdata (ibfd)->relgot;
   9219       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   9220 	{
   9221 	  struct got_entry **pent, *ent;
   9222 
   9223 	  pent = lgot_ents;
   9224 	  while ((ent = *pent) != NULL)
   9225 	    if (ent->got.refcount > 0)
   9226 	      {
   9227 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   9228 		  {
   9229 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   9230 		    *pent = ent->next;
   9231 		  }
   9232 		else
   9233 		  {
   9234 		    unsigned int num = 1;
   9235 		    ent->got.offset = s->size;
   9236 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   9237 		      num = 2;
   9238 		    s->size += num * 8;
   9239 		    if (info->shared)
   9240 		      srel->size += num * sizeof (Elf64_External_Rela);
   9241 		    else if ((*lgot_masks & PLT_IFUNC) != 0)
   9242 		      {
   9243 			htab->reliplt->size
   9244 			  += num * sizeof (Elf64_External_Rela);
   9245 			htab->got_reli_size
   9246 			  += num * sizeof (Elf64_External_Rela);
   9247 		      }
   9248 		    pent = &ent->next;
   9249 		  }
   9250 	      }
   9251 	    else
   9252 	      *pent = ent->next;
   9253 	}
   9254 
   9255       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   9256       for (; local_plt < end_local_plt; ++local_plt)
   9257 	{
   9258 	  struct plt_entry *ent;
   9259 
   9260 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   9261 	    if (ent->plt.refcount > 0)
   9262 	      {
   9263 		s = htab->iplt;
   9264 		ent->plt.offset = s->size;
   9265 		s->size += PLT_ENTRY_SIZE;
   9266 
   9267 		htab->reliplt->size += sizeof (Elf64_External_Rela);
   9268 	      }
   9269 	    else
   9270 	      ent->plt.offset = (bfd_vma) -1;
   9271 	}
   9272     }
   9273 
   9274   /* Allocate global sym .plt and .got entries, and space for global
   9275      sym dynamic relocs.  */
   9276   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   9277 
   9278   first_tlsld = NULL;
   9279   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9280     {
   9281       struct got_entry *ent;
   9282 
   9283       if (!is_ppc64_elf (ibfd))
   9284 	continue;
   9285 
   9286       ent = ppc64_tlsld_got (ibfd);
   9287       if (ent->got.refcount > 0)
   9288 	{
   9289 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   9290 	    {
   9291 	      ent->is_indirect = TRUE;
   9292 	      ent->got.ent = first_tlsld;
   9293 	    }
   9294 	  else
   9295 	    {
   9296 	      if (first_tlsld == NULL)
   9297 		first_tlsld = ent;
   9298 	      s = ppc64_elf_tdata (ibfd)->got;
   9299 	      ent->got.offset = s->size;
   9300 	      ent->owner = ibfd;
   9301 	      s->size += 16;
   9302 	      if (info->shared)
   9303 		{
   9304 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   9305 		  srel->size += sizeof (Elf64_External_Rela);
   9306 		}
   9307 	    }
   9308 	}
   9309       else
   9310 	ent->got.offset = (bfd_vma) -1;
   9311     }
   9312 
   9313   /* We now have determined the sizes of the various dynamic sections.
   9314      Allocate memory for them.  */
   9315   relocs = FALSE;
   9316   for (s = dynobj->sections; s != NULL; s = s->next)
   9317     {
   9318       if ((s->flags & SEC_LINKER_CREATED) == 0)
   9319 	continue;
   9320 
   9321       if (s == htab->brlt || s == htab->relbrlt)
   9322 	/* These haven't been allocated yet;  don't strip.  */
   9323 	continue;
   9324       else if (s == htab->got
   9325 	       || s == htab->plt
   9326 	       || s == htab->iplt
   9327 	       || s == htab->glink
   9328 	       || s == htab->dynbss)
   9329 	{
   9330 	  /* Strip this section if we don't need it; see the
   9331 	     comment below.  */
   9332 	}
   9333       else if (s == htab->glink_eh_frame)
   9334 	{
   9335 	  if (!bfd_is_abs_section (s->output_section))
   9336 	    /* Not sized yet.  */
   9337 	    continue;
   9338 	}
   9339       else if (CONST_STRNEQ (s->name, ".rela"))
   9340 	{
   9341 	  if (s->size != 0)
   9342 	    {
   9343 	      if (s != htab->relplt)
   9344 		relocs = TRUE;
   9345 
   9346 	      /* We use the reloc_count field as a counter if we need
   9347 		 to copy relocs into the output file.  */
   9348 	      s->reloc_count = 0;
   9349 	    }
   9350 	}
   9351       else
   9352 	{
   9353 	  /* It's not one of our sections, so don't allocate space.  */
   9354 	  continue;
   9355 	}
   9356 
   9357       if (s->size == 0)
   9358 	{
   9359 	  /* If we don't need this section, strip it from the
   9360 	     output file.  This is mostly to handle .rela.bss and
   9361 	     .rela.plt.  We must create both sections in
   9362 	     create_dynamic_sections, because they must be created
   9363 	     before the linker maps input sections to output
   9364 	     sections.  The linker does that before
   9365 	     adjust_dynamic_symbol is called, and it is that
   9366 	     function which decides whether anything needs to go
   9367 	     into these sections.  */
   9368 	  s->flags |= SEC_EXCLUDE;
   9369 	  continue;
   9370 	}
   9371 
   9372       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   9373 	continue;
   9374 
   9375       /* Allocate memory for the section contents.  We use bfd_zalloc
   9376 	 here in case unused entries are not reclaimed before the
   9377 	 section's contents are written out.  This should not happen,
   9378 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   9379 	 sections instead of garbage.
   9380 	 We also rely on the section contents being zero when writing
   9381 	 the GOT.  */
   9382       s->contents = bfd_zalloc (dynobj, s->size);
   9383       if (s->contents == NULL)
   9384 	return FALSE;
   9385     }
   9386 
   9387   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9388     {
   9389       if (!is_ppc64_elf (ibfd))
   9390 	continue;
   9391 
   9392       s = ppc64_elf_tdata (ibfd)->got;
   9393       if (s != NULL && s != htab->got)
   9394 	{
   9395 	  if (s->size == 0)
   9396 	    s->flags |= SEC_EXCLUDE;
   9397 	  else
   9398 	    {
   9399 	      s->contents = bfd_zalloc (ibfd, s->size);
   9400 	      if (s->contents == NULL)
   9401 		return FALSE;
   9402 	    }
   9403 	}
   9404       s = ppc64_elf_tdata (ibfd)->relgot;
   9405       if (s != NULL)
   9406 	{
   9407 	  if (s->size == 0)
   9408 	    s->flags |= SEC_EXCLUDE;
   9409 	  else
   9410 	    {
   9411 	      s->contents = bfd_zalloc (ibfd, s->size);
   9412 	      if (s->contents == NULL)
   9413 		return FALSE;
   9414 	      relocs = TRUE;
   9415 	      s->reloc_count = 0;
   9416 	    }
   9417 	}
   9418     }
   9419 
   9420   if (htab->elf.dynamic_sections_created)
   9421     {
   9422       /* Add some entries to the .dynamic section.  We fill in the
   9423 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   9424 	 must add the entries now so that we get the correct size for
   9425 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   9426 	 dynamic linker and used by the debugger.  */
   9427 #define add_dynamic_entry(TAG, VAL) \
   9428   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   9429 
   9430       if (info->executable)
   9431 	{
   9432 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   9433 	    return FALSE;
   9434 	}
   9435 
   9436       if (htab->plt != NULL && htab->plt->size != 0)
   9437 	{
   9438 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   9439 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   9440 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   9441 	      || !add_dynamic_entry (DT_JMPREL, 0)
   9442 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   9443 	    return FALSE;
   9444 	}
   9445 
   9446       if (NO_OPD_RELOCS)
   9447 	{
   9448 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   9449 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   9450 	    return FALSE;
   9451 	}
   9452 
   9453       if (!htab->no_tls_get_addr_opt
   9454 	  && htab->tls_get_addr_fd != NULL
   9455 	  && htab->tls_get_addr_fd->elf.plt.plist != NULL
   9456 	  && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
   9457 	return FALSE;
   9458 
   9459       if (relocs)
   9460 	{
   9461 	  if (!add_dynamic_entry (DT_RELA, 0)
   9462 	      || !add_dynamic_entry (DT_RELASZ, 0)
   9463 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   9464 	    return FALSE;
   9465 
   9466 	  /* If any dynamic relocs apply to a read-only section,
   9467 	     then we need a DT_TEXTREL entry.  */
   9468 	  if ((info->flags & DF_TEXTREL) == 0)
   9469 	    elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
   9470 
   9471 	  if ((info->flags & DF_TEXTREL) != 0)
   9472 	    {
   9473 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   9474 		return FALSE;
   9475 	    }
   9476 	}
   9477     }
   9478 #undef add_dynamic_entry
   9479 
   9480   return TRUE;
   9481 }
   9482 
   9483 /* Determine the type of stub needed, if any, for a call.  */
   9484 
   9485 static inline enum ppc_stub_type
   9486 ppc_type_of_stub (asection *input_sec,
   9487 		  const Elf_Internal_Rela *rel,
   9488 		  struct ppc_link_hash_entry **hash,
   9489 		  struct plt_entry **plt_ent,
   9490 		  bfd_vma destination)
   9491 {
   9492   struct ppc_link_hash_entry *h = *hash;
   9493   bfd_vma location;
   9494   bfd_vma branch_offset;
   9495   bfd_vma max_branch_offset;
   9496   enum elf_ppc64_reloc_type r_type;
   9497 
   9498   if (h != NULL)
   9499     {
   9500       struct plt_entry *ent;
   9501       struct ppc_link_hash_entry *fdh = h;
   9502       if (h->oh != NULL
   9503 	  && h->oh->is_func_descriptor)
   9504 	{
   9505 	  fdh = ppc_follow_link (h->oh);
   9506 	  *hash = fdh;
   9507 	}
   9508 
   9509       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   9510 	if (ent->addend == rel->r_addend
   9511 	    && ent->plt.offset != (bfd_vma) -1)
   9512 	  {
   9513 	    *plt_ent = ent;
   9514 	    return ppc_stub_plt_call;
   9515 	  }
   9516 
   9517       /* Here, we know we don't have a plt entry.  If we don't have a
   9518 	 either a defined function descriptor or a defined entry symbol
   9519 	 in a regular object file, then it is pointless trying to make
   9520 	 any other type of stub.  */
   9521       if (!is_static_defined (&fdh->elf)
   9522 	  && !is_static_defined (&h->elf))
   9523 	return ppc_stub_none;
   9524     }
   9525   else if (elf_local_got_ents (input_sec->owner) != NULL)
   9526     {
   9527       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   9528       struct plt_entry **local_plt = (struct plt_entry **)
   9529 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   9530       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   9531 
   9532       if (local_plt[r_symndx] != NULL)
   9533 	{
   9534 	  struct plt_entry *ent;
   9535 
   9536 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   9537 	    if (ent->addend == rel->r_addend
   9538 		&& ent->plt.offset != (bfd_vma) -1)
   9539 	      {
   9540 		*plt_ent = ent;
   9541 		return ppc_stub_plt_call;
   9542 	      }
   9543 	}
   9544     }
   9545 
   9546   /* Determine where the call point is.  */
   9547   location = (input_sec->output_offset
   9548 	      + input_sec->output_section->vma
   9549 	      + rel->r_offset);
   9550 
   9551   branch_offset = destination - location;
   9552   r_type = ELF64_R_TYPE (rel->r_info);
   9553 
   9554   /* Determine if a long branch stub is needed.  */
   9555   max_branch_offset = 1 << 25;
   9556   if (r_type != R_PPC64_REL24)
   9557     max_branch_offset = 1 << 15;
   9558 
   9559   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
   9560     /* We need a stub.  Figure out whether a long_branch or plt_branch
   9561        is needed later.  */
   9562     return ppc_stub_long_branch;
   9563 
   9564   return ppc_stub_none;
   9565 }
   9566 
   9567 /* With power7 weakly ordered memory model, it is possible for ld.so
   9568    to update a plt entry in one thread and have another thread see a
   9569    stale zero toc entry.  To avoid this we need some sort of acquire
   9570    barrier in the call stub.  One solution is to make the load of the
   9571    toc word seem to appear to depend on the load of the function entry
   9572    word.  Another solution is to test for r2 being zero, and branch to
   9573    the appropriate glink entry if so.
   9574 
   9575    .	fake dep barrier	compare
   9576    .	ld 11,xxx(2)		ld 11,xxx(2)
   9577    .	mtctr 11		mtctr 11
   9578    .	xor 11,11,11		ld 2,xxx+8(2)
   9579    .	add 2,2,11		cmpldi 2,0
   9580    .	ld 2,xxx+8(2)		bnectr+
   9581    .	bctr			b <glink_entry>
   9582 
   9583    The solution involving the compare turns out to be faster, so
   9584    that's what we use unless the branch won't reach.  */
   9585 
   9586 #define ALWAYS_USE_FAKE_DEP 0
   9587 #define ALWAYS_EMIT_R2SAVE 0
   9588 
   9589 #define PPC_LO(v) ((v) & 0xffff)
   9590 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   9591 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   9592 
   9593 static inline unsigned int
   9594 plt_stub_size (struct ppc_link_hash_table *htab,
   9595 	       struct ppc_stub_hash_entry *stub_entry,
   9596 	       bfd_vma off)
   9597 {
   9598   unsigned size = PLT_CALL_STUB_SIZE;
   9599 
   9600   if (!(ALWAYS_EMIT_R2SAVE
   9601 	|| stub_entry->stub_type == ppc_stub_plt_call_r2save))
   9602     size -= 4;
   9603   if (!htab->plt_static_chain)
   9604     size -= 4;
   9605   if (htab->plt_thread_safe)
   9606     size += 8;
   9607   if (PPC_HA (off) == 0)
   9608     size -= 4;
   9609   if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
   9610     size += 4;
   9611   if (stub_entry->h != NULL
   9612       && (stub_entry->h == htab->tls_get_addr_fd
   9613 	  || stub_entry->h == htab->tls_get_addr)
   9614       && !htab->no_tls_get_addr_opt)
   9615     size += 13 * 4;
   9616   return size;
   9617 }
   9618 
   9619 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
   9620    then return the padding needed to do so.  */
   9621 static inline unsigned int
   9622 plt_stub_pad (struct ppc_link_hash_table *htab,
   9623 	      struct ppc_stub_hash_entry *stub_entry,
   9624 	      bfd_vma plt_off)
   9625 {
   9626   int stub_align = 1 << htab->plt_stub_align;
   9627   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
   9628   bfd_vma stub_off = stub_entry->stub_sec->size;
   9629 
   9630   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
   9631       > (stub_size & -stub_align))
   9632     return stub_align - (stub_off & (stub_align - 1));
   9633   return 0;
   9634 }
   9635 
   9636 /* Build a .plt call stub.  */
   9637 
   9638 static inline bfd_byte *
   9639 build_plt_stub (struct ppc_link_hash_table *htab,
   9640 		struct ppc_stub_hash_entry *stub_entry,
   9641 		bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   9642 {
   9643   bfd *obfd = htab->stub_bfd;
   9644   bfd_boolean plt_static_chain = htab->plt_static_chain;
   9645   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
   9646   bfd_boolean use_fake_dep = plt_thread_safe;
   9647   bfd_vma cmp_branch_off = 0;
   9648 
   9649   if (!ALWAYS_USE_FAKE_DEP
   9650       && plt_thread_safe
   9651       && !(stub_entry->h != NULL
   9652 	   && (stub_entry->h == htab->tls_get_addr_fd
   9653 	       || stub_entry->h == htab->tls_get_addr)
   9654 	   && !htab->no_tls_get_addr_opt))
   9655     {
   9656       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
   9657       bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
   9658       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
   9659       bfd_vma to, from;
   9660 
   9661       if (pltindex > 32768)
   9662 	glinkoff += (pltindex - 32768) * 4;
   9663       to = (glinkoff
   9664 	    + htab->glink->output_offset
   9665 	    + htab->glink->output_section->vma);
   9666       from = (p - stub_entry->stub_sec->contents
   9667 	      + 4 * (ALWAYS_EMIT_R2SAVE
   9668 		     || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9669 	      + 4 * (PPC_HA (offset) != 0)
   9670 	      + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
   9671 		     != PPC_HA (offset))
   9672 	      + 4 * (plt_static_chain != 0)
   9673 	      + 20
   9674 	      + stub_entry->stub_sec->output_offset
   9675 	      + stub_entry->stub_sec->output_section->vma);
   9676       cmp_branch_off = to - from;
   9677       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
   9678     }
   9679 
   9680   if (PPC_HA (offset) != 0)
   9681     {
   9682       if (r != NULL)
   9683 	{
   9684 	  if (ALWAYS_EMIT_R2SAVE
   9685 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9686 	    r[0].r_offset += 4;
   9687 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   9688 	  r[1].r_offset = r[0].r_offset + 4;
   9689 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9690 	  r[1].r_addend = r[0].r_addend;
   9691 	  if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9692 	    {
   9693 	      r[2].r_offset = r[1].r_offset + 4;
   9694 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   9695 	      r[2].r_addend = r[0].r_addend;
   9696 	    }
   9697 	  else
   9698 	    {
   9699 	      r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
   9700 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9701 	      r[2].r_addend = r[0].r_addend + 8;
   9702 	      if (plt_static_chain)
   9703 		{
   9704 		  r[3].r_offset = r[2].r_offset + 4;
   9705 		  r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9706 		  r[3].r_addend = r[0].r_addend + 16;
   9707 		}
   9708 	    }
   9709 	}
   9710       if (ALWAYS_EMIT_R2SAVE
   9711 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9712 	bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9713       bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   9714       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),	p += 4;
   9715       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9716 	{
   9717 	  bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p),	p += 4;
   9718 	  offset = 0;
   9719 	}
   9720       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9721       if (use_fake_dep)
   9722 	{
   9723 	  bfd_put_32 (obfd, XOR_R11_R11_R11, p),		p += 4;
   9724 	  bfd_put_32 (obfd, ADD_R12_R12_R11, p),		p += 4;
   9725 	}
   9726       bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p),	p += 4;
   9727       if (plt_static_chain)
   9728 	bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
   9729     }
   9730   else
   9731     {
   9732       if (r != NULL)
   9733 	{
   9734 	  if (ALWAYS_EMIT_R2SAVE
   9735 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9736 	    r[0].r_offset += 4;
   9737 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9738 	  if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9739 	    {
   9740 	      r[1].r_offset = r[0].r_offset + 4;
   9741 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   9742 	      r[1].r_addend = r[0].r_addend;
   9743 	    }
   9744 	  else
   9745 	    {
   9746 	      r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
   9747 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9748 	      r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
   9749 	      if (plt_static_chain)
   9750 		{
   9751 		  r[2].r_offset = r[1].r_offset + 4;
   9752 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9753 		  r[2].r_addend = r[0].r_addend + 8;
   9754 		}
   9755 	    }
   9756 	}
   9757       if (ALWAYS_EMIT_R2SAVE
   9758 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9759 	bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9760       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p),	p += 4;
   9761       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9762 	{
   9763 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   9764 	  offset = 0;
   9765 	}
   9766       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9767       if (use_fake_dep)
   9768 	{
   9769 	  bfd_put_32 (obfd, XOR_R11_R11_R11, p),		p += 4;
   9770 	  bfd_put_32 (obfd, ADD_R2_R2_R11, p),			p += 4;
   9771 	}
   9772       if (plt_static_chain)
   9773 	bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
   9774       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),	p += 4;
   9775     }
   9776   if (plt_thread_safe && !use_fake_dep)
   9777     {
   9778       bfd_put_32 (obfd, CMPLDI_R2_0, p),			p += 4;
   9779       bfd_put_32 (obfd, BNECTR_P4, p),				p += 4;
   9780       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
   9781     }
   9782   else
   9783     bfd_put_32 (obfd, BCTR, p),					p += 4;
   9784   return p;
   9785 }
   9786 
   9787 /* Build a special .plt call stub for __tls_get_addr.  */
   9788 
   9789 #define LD_R11_0R3	0xe9630000
   9790 #define LD_R12_0R3	0xe9830000
   9791 #define MR_R0_R3	0x7c601b78
   9792 #define CMPDI_R11_0	0x2c2b0000
   9793 #define ADD_R3_R12_R13	0x7c6c6a14
   9794 #define BEQLR		0x4d820020
   9795 #define MR_R3_R0	0x7c030378
   9796 #define MFLR_R11	0x7d6802a6
   9797 #define STD_R11_0R1	0xf9610000
   9798 #define BCTRL		0x4e800421
   9799 #define LD_R11_0R1	0xe9610000
   9800 #define LD_R2_0R1	0xe8410000
   9801 #define MTLR_R11	0x7d6803a6
   9802 
   9803 static inline bfd_byte *
   9804 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
   9805 			 struct ppc_stub_hash_entry *stub_entry,
   9806 			 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   9807 {
   9808   bfd *obfd = htab->stub_bfd;
   9809 
   9810   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),		p += 4;
   9811   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   9812   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   9813   bfd_put_32 (obfd, CMPDI_R11_0, p),		p += 4;
   9814   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   9815   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   9816   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   9817   bfd_put_32 (obfd, MFLR_R11, p),		p += 4;
   9818   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),	p += 4;
   9819 
   9820   if (r != NULL)
   9821     r[0].r_offset += 9 * 4;
   9822   p = build_plt_stub (htab, stub_entry, p, offset, r);
   9823   bfd_put_32 (obfd, BCTRL, p - 4);
   9824 
   9825   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),	p += 4;
   9826   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),		p += 4;
   9827   bfd_put_32 (obfd, MTLR_R11, p),		p += 4;
   9828   bfd_put_32 (obfd, BLR, p),			p += 4;
   9829 
   9830   return p;
   9831 }
   9832 
   9833 static Elf_Internal_Rela *
   9834 get_relocs (asection *sec, int count)
   9835 {
   9836   Elf_Internal_Rela *relocs;
   9837   struct bfd_elf_section_data *elfsec_data;
   9838 
   9839   elfsec_data = elf_section_data (sec);
   9840   relocs = elfsec_data->relocs;
   9841   if (relocs == NULL)
   9842     {
   9843       bfd_size_type relsize;
   9844       relsize = sec->reloc_count * sizeof (*relocs);
   9845       relocs = bfd_alloc (sec->owner, relsize);
   9846       if (relocs == NULL)
   9847 	return NULL;
   9848       elfsec_data->relocs = relocs;
   9849       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   9850 					  sizeof (Elf_Internal_Shdr));
   9851       if (elfsec_data->rela.hdr == NULL)
   9852 	return NULL;
   9853       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   9854 					* sizeof (Elf64_External_Rela));
   9855       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   9856       sec->reloc_count = 0;
   9857     }
   9858   relocs += sec->reloc_count;
   9859   sec->reloc_count += count;
   9860   return relocs;
   9861 }
   9862 
   9863 static bfd_vma
   9864 get_r2off (struct bfd_link_info *info,
   9865 	   struct ppc_stub_hash_entry *stub_entry)
   9866 {
   9867   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   9868   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
   9869 
   9870   if (r2off == 0)
   9871     {
   9872       /* Support linking -R objects.  Get the toc pointer from the
   9873 	 opd entry.  */
   9874       char buf[8];
   9875       asection *opd = stub_entry->h->elf.root.u.def.section;
   9876       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   9877 
   9878       if (strcmp (opd->name, ".opd") != 0
   9879 	  || opd->reloc_count != 0)
   9880 	{
   9881 	  info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
   9882 				  stub_entry->h->elf.root.root.string);
   9883 	  bfd_set_error (bfd_error_bad_value);
   9884 	  return 0;
   9885 	}
   9886       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   9887 	return 0;
   9888       r2off = bfd_get_64 (opd->owner, buf);
   9889       r2off -= elf_gp (info->output_bfd);
   9890     }
   9891   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
   9892   return r2off;
   9893 }
   9894 
   9895 static bfd_boolean
   9896 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   9897 {
   9898   struct ppc_stub_hash_entry *stub_entry;
   9899   struct ppc_branch_hash_entry *br_entry;
   9900   struct bfd_link_info *info;
   9901   struct ppc_link_hash_table *htab;
   9902   bfd_byte *loc;
   9903   bfd_byte *p;
   9904   bfd_vma dest, off;
   9905   int size;
   9906   Elf_Internal_Rela *r;
   9907   asection *plt;
   9908 
   9909   /* Massage our args to the form they really have.  */
   9910   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   9911   info = in_arg;
   9912 
   9913   htab = ppc_hash_table (info);
   9914   if (htab == NULL)
   9915     return FALSE;
   9916 
   9917   /* Make a note of the offset within the stubs for this entry.  */
   9918   stub_entry->stub_offset = stub_entry->stub_sec->size;
   9919   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
   9920 
   9921   htab->stub_count[stub_entry->stub_type - 1] += 1;
   9922   switch (stub_entry->stub_type)
   9923     {
   9924     case ppc_stub_long_branch:
   9925     case ppc_stub_long_branch_r2off:
   9926       /* Branches are relative.  This is where we are going to.  */
   9927       off = dest = (stub_entry->target_value
   9928 		    + stub_entry->target_section->output_offset
   9929 		    + stub_entry->target_section->output_section->vma);
   9930 
   9931       /* And this is where we are coming from.  */
   9932       off -= (stub_entry->stub_offset
   9933 	      + stub_entry->stub_sec->output_offset
   9934 	      + stub_entry->stub_sec->output_section->vma);
   9935 
   9936       size = 4;
   9937       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   9938 	{
   9939 	  bfd_vma r2off = get_r2off (info, stub_entry);
   9940 
   9941 	  if (r2off == 0)
   9942 	    {
   9943 	      htab->stub_error = TRUE;
   9944 	      return FALSE;
   9945 	    }
   9946 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   9947 	  loc += 4;
   9948 	  size = 12;
   9949 	  if (PPC_HA (r2off) != 0)
   9950 	    {
   9951 	      size = 16;
   9952 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   9953 	      loc += 4;
   9954 	    }
   9955 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   9956 	  loc += 4;
   9957 	  off -= size - 4;
   9958 	}
   9959       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
   9960 
   9961       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   9962 	{
   9963 	  info->callbacks->einfo
   9964 	    (_("%P: long branch stub `%s' offset overflow\n"),
   9965 	     stub_entry->root.string);
   9966 	  htab->stub_error = TRUE;
   9967 	  return FALSE;
   9968 	}
   9969 
   9970       if (info->emitrelocations)
   9971 	{
   9972 	  r = get_relocs (stub_entry->stub_sec, 1);
   9973 	  if (r == NULL)
   9974 	    return FALSE;
   9975 	  r->r_offset = loc - stub_entry->stub_sec->contents;
   9976 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   9977 	  r->r_addend = dest;
   9978 	  if (stub_entry->h != NULL)
   9979 	    {
   9980 	      struct elf_link_hash_entry **hashes;
   9981 	      unsigned long symndx;
   9982 	      struct ppc_link_hash_entry *h;
   9983 
   9984 	      hashes = elf_sym_hashes (htab->stub_bfd);
   9985 	      if (hashes == NULL)
   9986 		{
   9987 		  bfd_size_type hsize;
   9988 
   9989 		  hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   9990 		  hashes = bfd_zalloc (htab->stub_bfd, hsize);
   9991 		  if (hashes == NULL)
   9992 		    return FALSE;
   9993 		  elf_sym_hashes (htab->stub_bfd) = hashes;
   9994 		  htab->stub_globals = 1;
   9995 		}
   9996 	      symndx = htab->stub_globals++;
   9997 	      h = stub_entry->h;
   9998 	      hashes[symndx] = &h->elf;
   9999 	      r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
   10000 	      if (h->oh != NULL && h->oh->is_func)
   10001 		h = ppc_follow_link (h->oh);
   10002 	      if (h->elf.root.u.def.section != stub_entry->target_section)
   10003 		/* H is an opd symbol.  The addend must be zero.  */
   10004 		r->r_addend = 0;
   10005 	      else
   10006 		{
   10007 		  off = (h->elf.root.u.def.value
   10008 			 + h->elf.root.u.def.section->output_offset
   10009 			 + h->elf.root.u.def.section->output_section->vma);
   10010 		  r->r_addend -= off;
   10011 		}
   10012 	    }
   10013 	}
   10014       break;
   10015 
   10016     case ppc_stub_plt_branch:
   10017     case ppc_stub_plt_branch_r2off:
   10018       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10019 					 stub_entry->root.string + 9,
   10020 					 FALSE, FALSE);
   10021       if (br_entry == NULL)
   10022 	{
   10023 	  info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
   10024 				  stub_entry->root.string);
   10025 	  htab->stub_error = TRUE;
   10026 	  return FALSE;
   10027 	}
   10028 
   10029       dest = (stub_entry->target_value
   10030 	      + stub_entry->target_section->output_offset
   10031 	      + stub_entry->target_section->output_section->vma);
   10032 
   10033       bfd_put_64 (htab->brlt->owner, dest,
   10034 		  htab->brlt->contents + br_entry->offset);
   10035 
   10036       if (br_entry->iter == htab->stub_iteration)
   10037 	{
   10038 	  br_entry->iter = 0;
   10039 
   10040 	  if (htab->relbrlt != NULL)
   10041 	    {
   10042 	      /* Create a reloc for the branch lookup table entry.  */
   10043 	      Elf_Internal_Rela rela;
   10044 	      bfd_byte *rl;
   10045 
   10046 	      rela.r_offset = (br_entry->offset
   10047 			       + htab->brlt->output_offset
   10048 			       + htab->brlt->output_section->vma);
   10049 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10050 	      rela.r_addend = dest;
   10051 
   10052 	      rl = htab->relbrlt->contents;
   10053 	      rl += (htab->relbrlt->reloc_count++
   10054 		     * sizeof (Elf64_External_Rela));
   10055 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   10056 	    }
   10057 	  else if (info->emitrelocations)
   10058 	    {
   10059 	      r = get_relocs (htab->brlt, 1);
   10060 	      if (r == NULL)
   10061 		return FALSE;
   10062 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   10063 		 normal reloc processing.  Symbols and offsets are not
   10064 		 translated from input file to output file form, so
   10065 		 set up the offset per the output file.  */
   10066 	      r->r_offset = (br_entry->offset
   10067 			     + htab->brlt->output_offset
   10068 			     + htab->brlt->output_section->vma);
   10069 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10070 	      r->r_addend = dest;
   10071 	    }
   10072 	}
   10073 
   10074       dest = (br_entry->offset
   10075 	      + htab->brlt->output_offset
   10076 	      + htab->brlt->output_section->vma);
   10077 
   10078       off = (dest
   10079 	     - elf_gp (htab->brlt->output_section->owner)
   10080 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10081 
   10082       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   10083 	{
   10084 	  info->callbacks->einfo
   10085 	    (_("%P: linkage table error against `%T'\n"),
   10086 	     stub_entry->root.string);
   10087 	  bfd_set_error (bfd_error_bad_value);
   10088 	  htab->stub_error = TRUE;
   10089 	  return FALSE;
   10090 	}
   10091 
   10092       if (info->emitrelocations)
   10093 	{
   10094 	  r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
   10095 	  if (r == NULL)
   10096 	    return FALSE;
   10097 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   10098 	  if (bfd_big_endian (info->output_bfd))
   10099 	    r[0].r_offset += 2;
   10100 	  if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
   10101 	    r[0].r_offset += 4;
   10102 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10103 	  r[0].r_addend = dest;
   10104 	  if (PPC_HA (off) != 0)
   10105 	    {
   10106 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   10107 	      r[1].r_offset = r[0].r_offset + 4;
   10108 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10109 	      r[1].r_addend = r[0].r_addend;
   10110 	    }
   10111 	}
   10112 
   10113       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10114 	{
   10115 	  if (PPC_HA (off) != 0)
   10116 	    {
   10117 	      size = 16;
   10118 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   10119 	      loc += 4;
   10120 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   10121 	    }
   10122 	  else
   10123 	    {
   10124 	      size = 12;
   10125 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   10126 	    }
   10127 	}
   10128       else
   10129 	{
   10130 	  bfd_vma r2off = get_r2off (info, stub_entry);
   10131 
   10132 	  if (r2off == 0)
   10133 	    {
   10134 	      htab->stub_error = TRUE;
   10135 	      return FALSE;
   10136 	    }
   10137 
   10138 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   10139 	  loc += 4;
   10140 	  size = 20;
   10141 	  if (PPC_HA (off) != 0)
   10142 	    {
   10143 	      size += 4;
   10144 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   10145 	      loc += 4;
   10146 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   10147 	      loc += 4;
   10148 	    }
   10149 	  else
   10150 	    {
   10151 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   10152 	      loc += 4;
   10153 	    }
   10154 
   10155 	  if (PPC_HA (r2off) != 0)
   10156 	    {
   10157 	      size += 4;
   10158 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   10159 	      loc += 4;
   10160 	    }
   10161 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   10162 	}
   10163       loc += 4;
   10164       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
   10165       loc += 4;
   10166       bfd_put_32 (htab->stub_bfd, BCTR, loc);
   10167       break;
   10168 
   10169     case ppc_stub_plt_call:
   10170     case ppc_stub_plt_call_r2save:
   10171       if (stub_entry->h != NULL
   10172 	  && stub_entry->h->is_func_descriptor
   10173 	  && stub_entry->h->oh != NULL)
   10174 	{
   10175 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   10176 
   10177 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   10178 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
   10179 	     FIXME: We used to define the symbol on one of the call
   10180 	     stubs instead, which is why we test symbol section id
   10181 	     against htab->top_id in various places.  Likely all
   10182 	     these checks could now disappear.  */
   10183 	  if (fh->elf.root.type == bfd_link_hash_undefined)
   10184 	    fh->elf.root.type = bfd_link_hash_undefweak;
   10185 	  /* Stop undo_symbol_twiddle changing it back to undefined.  */
   10186 	  fh->was_undefined = 0;
   10187 	}
   10188 
   10189       /* Now build the stub.  */
   10190       dest = stub_entry->plt_ent->plt.offset & ~1;
   10191       if (dest >= (bfd_vma) -2)
   10192 	abort ();
   10193 
   10194       plt = htab->plt;
   10195       if (!htab->elf.dynamic_sections_created
   10196 	  || stub_entry->h == NULL
   10197 	  || stub_entry->h->elf.dynindx == -1)
   10198 	plt = htab->iplt;
   10199 
   10200       dest += plt->output_offset + plt->output_section->vma;
   10201 
   10202       if (stub_entry->h == NULL
   10203 	  && (stub_entry->plt_ent->plt.offset & 1) == 0)
   10204 	{
   10205 	  Elf_Internal_Rela rela;
   10206 	  bfd_byte *rl;
   10207 
   10208 	  rela.r_offset = dest;
   10209 	  rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   10210 	  rela.r_addend = (stub_entry->target_value
   10211 			   + stub_entry->target_section->output_offset
   10212 			   + stub_entry->target_section->output_section->vma);
   10213 
   10214 	  rl = (htab->reliplt->contents
   10215 		+ (htab->reliplt->reloc_count++
   10216 		   * sizeof (Elf64_External_Rela)));
   10217 	  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
   10218 	  stub_entry->plt_ent->plt.offset |= 1;
   10219 	}
   10220 
   10221       off = (dest
   10222 	     - elf_gp (plt->output_section->owner)
   10223 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10224 
   10225       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   10226 	{
   10227 	  info->callbacks->einfo
   10228 	    (_("%P: linkage table error against `%T'\n"),
   10229 	     stub_entry->h != NULL
   10230 	     ? stub_entry->h->elf.root.root.string
   10231 	     : "<local sym>");
   10232 	  bfd_set_error (bfd_error_bad_value);
   10233 	  htab->stub_error = TRUE;
   10234 	  return FALSE;
   10235 	}
   10236 
   10237       if (htab->plt_stub_align != 0)
   10238 	{
   10239 	  unsigned pad = plt_stub_pad (htab, stub_entry, off);
   10240 
   10241 	  stub_entry->stub_sec->size += pad;
   10242 	  stub_entry->stub_offset = stub_entry->stub_sec->size;
   10243 	  loc += pad;
   10244 	}
   10245 
   10246       r = NULL;
   10247       if (info->emitrelocations)
   10248 	{
   10249 	  r = get_relocs (stub_entry->stub_sec,
   10250 			  (2
   10251 			   + (PPC_HA (off) != 0)
   10252 			   + (htab->plt_static_chain
   10253 			      && PPC_HA (off + 16) == PPC_HA (off))));
   10254 	  if (r == NULL)
   10255 	    return FALSE;
   10256 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   10257 	  if (bfd_big_endian (info->output_bfd))
   10258 	    r[0].r_offset += 2;
   10259 	  r[0].r_addend = dest;
   10260 	}
   10261       if (stub_entry->h != NULL
   10262 	  && (stub_entry->h == htab->tls_get_addr_fd
   10263 	      || stub_entry->h == htab->tls_get_addr)
   10264 	  && !htab->no_tls_get_addr_opt)
   10265 	p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
   10266       else
   10267 	p = build_plt_stub (htab, stub_entry, loc, off, r);
   10268       size = p - loc;
   10269       break;
   10270 
   10271     default:
   10272       BFD_FAIL ();
   10273       return FALSE;
   10274     }
   10275 
   10276   stub_entry->stub_sec->size += size;
   10277 
   10278   if (htab->emit_stub_syms)
   10279     {
   10280       struct elf_link_hash_entry *h;
   10281       size_t len1, len2;
   10282       char *name;
   10283       const char *const stub_str[] = { "long_branch",
   10284 				       "long_branch_r2off",
   10285 				       "plt_branch",
   10286 				       "plt_branch_r2off",
   10287 				       "plt_call",
   10288 				       "plt_call" };
   10289 
   10290       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
   10291       len2 = strlen (stub_entry->root.string);
   10292       name = bfd_malloc (len1 + len2 + 2);
   10293       if (name == NULL)
   10294 	return FALSE;
   10295       memcpy (name, stub_entry->root.string, 9);
   10296       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
   10297       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   10298       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   10299       if (h == NULL)
   10300 	return FALSE;
   10301       if (h->root.type == bfd_link_hash_new)
   10302 	{
   10303 	  h->root.type = bfd_link_hash_defined;
   10304 	  h->root.u.def.section = stub_entry->stub_sec;
   10305 	  h->root.u.def.value = stub_entry->stub_offset;
   10306 	  h->ref_regular = 1;
   10307 	  h->def_regular = 1;
   10308 	  h->ref_regular_nonweak = 1;
   10309 	  h->forced_local = 1;
   10310 	  h->non_elf = 0;
   10311 	}
   10312     }
   10313 
   10314   return TRUE;
   10315 }
   10316 
   10317 /* As above, but don't actually build the stub.  Just bump offset so
   10318    we know stub section sizes, and select plt_branch stubs where
   10319    long_branch stubs won't do.  */
   10320 
   10321 static bfd_boolean
   10322 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   10323 {
   10324   struct ppc_stub_hash_entry *stub_entry;
   10325   struct bfd_link_info *info;
   10326   struct ppc_link_hash_table *htab;
   10327   bfd_vma off;
   10328   int size;
   10329 
   10330   /* Massage our args to the form they really have.  */
   10331   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   10332   info = in_arg;
   10333 
   10334   htab = ppc_hash_table (info);
   10335   if (htab == NULL)
   10336     return FALSE;
   10337 
   10338   if (stub_entry->stub_type == ppc_stub_plt_call
   10339       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10340     {
   10341       asection *plt;
   10342       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   10343       if (off >= (bfd_vma) -2)
   10344 	abort ();
   10345       plt = htab->plt;
   10346       if (!htab->elf.dynamic_sections_created
   10347 	  || stub_entry->h == NULL
   10348 	  || stub_entry->h->elf.dynindx == -1)
   10349 	plt = htab->iplt;
   10350       off += (plt->output_offset
   10351 	      + plt->output_section->vma
   10352 	      - elf_gp (plt->output_section->owner)
   10353 	      - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10354 
   10355       size = plt_stub_size (htab, stub_entry, off);
   10356       if (htab->plt_stub_align)
   10357 	size += plt_stub_pad (htab, stub_entry, off);
   10358       if (info->emitrelocations)
   10359 	{
   10360 	  stub_entry->stub_sec->reloc_count
   10361 	    += (2
   10362 		+ (PPC_HA (off) != 0)
   10363 		+ (htab->plt_static_chain
   10364 		   && PPC_HA (off + 16) == PPC_HA (off)));
   10365 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   10366 	}
   10367     }
   10368   else
   10369     {
   10370       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
   10371 	 variants.  */
   10372       bfd_vma r2off = 0;
   10373 
   10374       off = (stub_entry->target_value
   10375 	     + stub_entry->target_section->output_offset
   10376 	     + stub_entry->target_section->output_section->vma);
   10377       off -= (stub_entry->stub_sec->size
   10378 	      + stub_entry->stub_sec->output_offset
   10379 	      + stub_entry->stub_sec->output_section->vma);
   10380 
   10381       /* Reset the stub type from the plt variant in case we now
   10382 	 can reach with a shorter stub.  */
   10383       if (stub_entry->stub_type >= ppc_stub_plt_branch)
   10384 	stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   10385 
   10386       size = 4;
   10387       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   10388 	{
   10389 	  r2off = get_r2off (info, stub_entry);
   10390 	  if (r2off == 0)
   10391 	    {
   10392 	      htab->stub_error = TRUE;
   10393 	      return FALSE;
   10394 	    }
   10395 	  size = 12;
   10396 	  if (PPC_HA (r2off) != 0)
   10397 	    size = 16;
   10398 	  off -= size - 4;
   10399 	}
   10400 
   10401       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
   10402       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   10403 	{
   10404 	  struct ppc_branch_hash_entry *br_entry;
   10405 
   10406 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10407 					     stub_entry->root.string + 9,
   10408 					     TRUE, FALSE);
   10409 	  if (br_entry == NULL)
   10410 	    {
   10411 	      info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
   10412 				      stub_entry->root.string);
   10413 	      htab->stub_error = TRUE;
   10414 	      return FALSE;
   10415 	    }
   10416 
   10417 	  if (br_entry->iter != htab->stub_iteration)
   10418 	    {
   10419 	      br_entry->iter = htab->stub_iteration;
   10420 	      br_entry->offset = htab->brlt->size;
   10421 	      htab->brlt->size += 8;
   10422 
   10423 	      if (htab->relbrlt != NULL)
   10424 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   10425 	      else if (info->emitrelocations)
   10426 		{
   10427 		  htab->brlt->reloc_count += 1;
   10428 		  htab->brlt->flags |= SEC_RELOC;
   10429 		}
   10430 	    }
   10431 
   10432 	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
   10433 	  off = (br_entry->offset
   10434 		 + htab->brlt->output_offset
   10435 		 + htab->brlt->output_section->vma
   10436 		 - elf_gp (htab->brlt->output_section->owner)
   10437 		 - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10438 
   10439 	  if (info->emitrelocations)
   10440 	    {
   10441 	      stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
   10442 	      stub_entry->stub_sec->flags |= SEC_RELOC;
   10443 	    }
   10444 
   10445 	  if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10446 	    {
   10447 	      size = 12;
   10448 	      if (PPC_HA (off) != 0)
   10449 		size = 16;
   10450 	    }
   10451 	  else
   10452 	    {
   10453 	      size = 20;
   10454 	      if (PPC_HA (off) != 0)
   10455 		size += 4;
   10456 
   10457 	      if (PPC_HA (r2off) != 0)
   10458 		size += 4;
   10459 	    }
   10460 	}
   10461       else if (info->emitrelocations)
   10462 	{
   10463 	  stub_entry->stub_sec->reloc_count += 1;
   10464 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   10465 	}
   10466     }
   10467 
   10468   stub_entry->stub_sec->size += size;
   10469   return TRUE;
   10470 }
   10471 
   10472 /* Set up various things so that we can make a list of input sections
   10473    for each output section included in the link.  Returns -1 on error,
   10474    0 when no stubs will be needed, and 1 on success.  */
   10475 
   10476 int
   10477 ppc64_elf_setup_section_lists
   10478   (struct bfd_link_info *info,
   10479    asection *(*add_stub_section) (const char *, asection *),
   10480    void (*layout_sections_again) (void))
   10481 {
   10482   bfd *input_bfd;
   10483   int top_id, top_index, id;
   10484   asection *section;
   10485   asection **input_list;
   10486   bfd_size_type amt;
   10487   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10488 
   10489   if (htab == NULL)
   10490     return -1;
   10491   /* Stash our params away.  */
   10492   htab->add_stub_section = add_stub_section;
   10493   htab->layout_sections_again = layout_sections_again;
   10494 
   10495   if (htab->brlt == NULL)
   10496     return 0;
   10497 
   10498   /* Find the top input section id.  */
   10499   for (input_bfd = info->input_bfds, top_id = 3;
   10500        input_bfd != NULL;
   10501        input_bfd = input_bfd->link_next)
   10502     {
   10503       for (section = input_bfd->sections;
   10504 	   section != NULL;
   10505 	   section = section->next)
   10506 	{
   10507 	  if (top_id < section->id)
   10508 	    top_id = section->id;
   10509 	}
   10510     }
   10511 
   10512   htab->top_id = top_id;
   10513   amt = sizeof (struct map_stub) * (top_id + 1);
   10514   htab->stub_group = bfd_zmalloc (amt);
   10515   if (htab->stub_group == NULL)
   10516     return -1;
   10517 
   10518   /* Set toc_off for com, und, abs and ind sections.  */
   10519   for (id = 0; id < 3; id++)
   10520     htab->stub_group[id].toc_off = TOC_BASE_OFF;
   10521 
   10522   /* We can't use output_bfd->section_count here to find the top output
   10523      section index as some sections may have been removed, and
   10524      strip_excluded_output_sections doesn't renumber the indices.  */
   10525   for (section = info->output_bfd->sections, top_index = 0;
   10526        section != NULL;
   10527        section = section->next)
   10528     {
   10529       if (top_index < section->index)
   10530 	top_index = section->index;
   10531     }
   10532 
   10533   htab->top_index = top_index;
   10534   amt = sizeof (asection *) * (top_index + 1);
   10535   input_list = bfd_zmalloc (amt);
   10536   htab->input_list = input_list;
   10537   if (input_list == NULL)
   10538     return -1;
   10539 
   10540   return 1;
   10541 }
   10542 
   10543 /* Set up for first pass at multitoc partitioning.  */
   10544 
   10545 void
   10546 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   10547 {
   10548   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10549 
   10550   elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
   10551   htab->toc_curr = elf_gp (info->output_bfd);
   10552   htab->toc_bfd = NULL;
   10553   htab->toc_first_sec = NULL;
   10554 }
   10555 
   10556 /* The linker repeatedly calls this function for each TOC input section
   10557    and linker generated GOT section.  Group input bfds such that the toc
   10558    within a group is less than 64k in size.  */
   10559 
   10560 bfd_boolean
   10561 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   10562 {
   10563   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10564   bfd_vma addr, off, limit;
   10565 
   10566   if (htab == NULL)
   10567     return FALSE;
   10568 
   10569   if (!htab->second_toc_pass)
   10570     {
   10571       /* Keep track of the first .toc or .got section for this input bfd.  */
   10572       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
   10573 
   10574       if (new_bfd)
   10575 	{
   10576 	  htab->toc_bfd = isec->owner;
   10577 	  htab->toc_first_sec = isec;
   10578 	}
   10579 
   10580       addr = isec->output_offset + isec->output_section->vma;
   10581       off = addr - htab->toc_curr;
   10582       limit = 0x80008000;
   10583       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   10584 	limit = 0x10000;
   10585       if (off + isec->size > limit)
   10586 	{
   10587 	  addr = (htab->toc_first_sec->output_offset
   10588 		  + htab->toc_first_sec->output_section->vma);
   10589 	  htab->toc_curr = addr;
   10590 	}
   10591 
   10592       /* toc_curr is the base address of this toc group.  Set elf_gp
   10593 	 for the input section to be the offset relative to the
   10594 	 output toc base plus 0x8000.  Making the input elf_gp an
   10595 	 offset allows us to move the toc as a whole without
   10596 	 recalculating input elf_gp.  */
   10597       off = htab->toc_curr - elf_gp (isec->output_section->owner);
   10598       off += TOC_BASE_OFF;
   10599 
   10600       /* Die if someone uses a linker script that doesn't keep input
   10601 	 file .toc and .got together.  */
   10602       if (new_bfd
   10603 	  && elf_gp (isec->owner) != 0
   10604 	  && elf_gp (isec->owner) != off)
   10605 	return FALSE;
   10606 
   10607       elf_gp (isec->owner) = off;
   10608       return TRUE;
   10609     }
   10610 
   10611   /* During the second pass toc_first_sec points to the start of
   10612      a toc group, and toc_curr is used to track the old elf_gp.
   10613      We use toc_bfd to ensure we only look at each bfd once.  */
   10614   if (htab->toc_bfd == isec->owner)
   10615     return TRUE;
   10616   htab->toc_bfd = isec->owner;
   10617 
   10618   if (htab->toc_first_sec == NULL
   10619       || htab->toc_curr != elf_gp (isec->owner))
   10620     {
   10621       htab->toc_curr = elf_gp (isec->owner);
   10622       htab->toc_first_sec = isec;
   10623     }
   10624   addr = (htab->toc_first_sec->output_offset
   10625 	  + htab->toc_first_sec->output_section->vma);
   10626   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
   10627   elf_gp (isec->owner) = off;
   10628 
   10629   return TRUE;
   10630 }
   10631 
   10632 /* Called via elf_link_hash_traverse to merge GOT entries for global
   10633    symbol H.  */
   10634 
   10635 static bfd_boolean
   10636 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   10637 {
   10638   if (h->root.type == bfd_link_hash_indirect)
   10639     return TRUE;
   10640 
   10641   merge_got_entries (&h->got.glist);
   10642 
   10643   return TRUE;
   10644 }
   10645 
   10646 /* Called via elf_link_hash_traverse to allocate GOT entries for global
   10647    symbol H.  */
   10648 
   10649 static bfd_boolean
   10650 reallocate_got (struct elf_link_hash_entry *h, void *inf)
   10651 {
   10652   struct got_entry *gent;
   10653 
   10654   if (h->root.type == bfd_link_hash_indirect)
   10655     return TRUE;
   10656 
   10657   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   10658     if (!gent->is_indirect)
   10659       allocate_got (h, (struct bfd_link_info *) inf, gent);
   10660   return TRUE;
   10661 }
   10662 
   10663 /* Called on the first multitoc pass after the last call to
   10664    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   10665    entries.  */
   10666 
   10667 bfd_boolean
   10668 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   10669 {
   10670   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10671   struct bfd *ibfd, *ibfd2;
   10672   bfd_boolean done_something;
   10673 
   10674   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   10675 
   10676   if (!htab->do_multi_toc)
   10677     return FALSE;
   10678 
   10679   /* Merge global sym got entries within a toc group.  */
   10680   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   10681 
   10682   /* And tlsld_got.  */
   10683   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10684     {
   10685       struct got_entry *ent, *ent2;
   10686 
   10687       if (!is_ppc64_elf (ibfd))
   10688 	continue;
   10689 
   10690       ent = ppc64_tlsld_got (ibfd);
   10691       if (!ent->is_indirect
   10692 	  && ent->got.offset != (bfd_vma) -1)
   10693 	{
   10694 	  for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
   10695 	    {
   10696 	      if (!is_ppc64_elf (ibfd2))
   10697 		continue;
   10698 
   10699 	      ent2 = ppc64_tlsld_got (ibfd2);
   10700 	      if (!ent2->is_indirect
   10701 		  && ent2->got.offset != (bfd_vma) -1
   10702 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   10703 		{
   10704 		  ent2->is_indirect = TRUE;
   10705 		  ent2->got.ent = ent;
   10706 		}
   10707 	    }
   10708 	}
   10709     }
   10710 
   10711   /* Zap sizes of got sections.  */
   10712   htab->reliplt->rawsize = htab->reliplt->size;
   10713   htab->reliplt->size -= htab->got_reli_size;
   10714   htab->got_reli_size = 0;
   10715 
   10716   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10717     {
   10718       asection *got, *relgot;
   10719 
   10720       if (!is_ppc64_elf (ibfd))
   10721 	continue;
   10722 
   10723       got = ppc64_elf_tdata (ibfd)->got;
   10724       if (got != NULL)
   10725 	{
   10726 	  got->rawsize = got->size;
   10727 	  got->size = 0;
   10728 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   10729 	  relgot->rawsize = relgot->size;
   10730 	  relgot->size = 0;
   10731 	}
   10732     }
   10733 
   10734   /* Now reallocate the got, local syms first.  We don't need to
   10735      allocate section contents again since we never increase size.  */
   10736   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10737     {
   10738       struct got_entry **lgot_ents;
   10739       struct got_entry **end_lgot_ents;
   10740       struct plt_entry **local_plt;
   10741       struct plt_entry **end_local_plt;
   10742       unsigned char *lgot_masks;
   10743       bfd_size_type locsymcount;
   10744       Elf_Internal_Shdr *symtab_hdr;
   10745       asection *s, *srel;
   10746 
   10747       if (!is_ppc64_elf (ibfd))
   10748 	continue;
   10749 
   10750       lgot_ents = elf_local_got_ents (ibfd);
   10751       if (!lgot_ents)
   10752 	continue;
   10753 
   10754       symtab_hdr = &elf_symtab_hdr (ibfd);
   10755       locsymcount = symtab_hdr->sh_info;
   10756       end_lgot_ents = lgot_ents + locsymcount;
   10757       local_plt = (struct plt_entry **) end_lgot_ents;
   10758       end_local_plt = local_plt + locsymcount;
   10759       lgot_masks = (unsigned char *) end_local_plt;
   10760       s = ppc64_elf_tdata (ibfd)->got;
   10761       srel = ppc64_elf_tdata (ibfd)->relgot;
   10762       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   10763 	{
   10764 	  struct got_entry *ent;
   10765 
   10766 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   10767 	    {
   10768 	      unsigned int num = 1;
   10769 	      ent->got.offset = s->size;
   10770 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   10771 		num = 2;
   10772 	      s->size += num * 8;
   10773 	      if (info->shared)
   10774 		srel->size += num * sizeof (Elf64_External_Rela);
   10775 	      else if ((*lgot_masks & PLT_IFUNC) != 0)
   10776 		{
   10777 		  htab->reliplt->size
   10778 		    += num * sizeof (Elf64_External_Rela);
   10779 		  htab->got_reli_size
   10780 		    += num * sizeof (Elf64_External_Rela);
   10781 		}
   10782 	    }
   10783 	}
   10784     }
   10785 
   10786   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   10787 
   10788   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10789     {
   10790       struct got_entry *ent;
   10791 
   10792       if (!is_ppc64_elf (ibfd))
   10793 	continue;
   10794 
   10795       ent = ppc64_tlsld_got (ibfd);
   10796       if (!ent->is_indirect
   10797 	  && ent->got.offset != (bfd_vma) -1)
   10798 	{
   10799 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   10800 	  ent->got.offset = s->size;
   10801 	  s->size += 16;
   10802 	  if (info->shared)
   10803 	    {
   10804 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10805 	      srel->size += sizeof (Elf64_External_Rela);
   10806 	    }
   10807 	}
   10808     }
   10809 
   10810   done_something = htab->reliplt->rawsize != htab->reliplt->size;
   10811   if (!done_something)
   10812     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10813       {
   10814 	asection *got;
   10815 
   10816 	if (!is_ppc64_elf (ibfd))
   10817 	  continue;
   10818 
   10819 	got = ppc64_elf_tdata (ibfd)->got;
   10820 	if (got != NULL)
   10821 	  {
   10822 	    done_something = got->rawsize != got->size;
   10823 	    if (done_something)
   10824 	      break;
   10825 	  }
   10826       }
   10827 
   10828   if (done_something)
   10829     (*htab->layout_sections_again) ();
   10830 
   10831   /* Set up for second pass over toc sections to recalculate elf_gp
   10832      on input sections.  */
   10833   htab->toc_bfd = NULL;
   10834   htab->toc_first_sec = NULL;
   10835   htab->second_toc_pass = TRUE;
   10836   return done_something;
   10837 }
   10838 
   10839 /* Called after second pass of multitoc partitioning.  */
   10840 
   10841 void
   10842 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   10843 {
   10844   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10845 
   10846   /* After the second pass, toc_curr tracks the TOC offset used
   10847      for code sections below in ppc64_elf_next_input_section.  */
   10848   htab->toc_curr = TOC_BASE_OFF;
   10849 }
   10850 
   10851 /* No toc references were found in ISEC.  If the code in ISEC makes no
   10852    calls, then there's no need to use toc adjusting stubs when branching
   10853    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   10854    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   10855    needed, and 2 if a cyclical call-graph was found but no other reason
   10856    for a stub was detected.  If called from the top level, a return of
   10857    2 means the same as a return of 0.  */
   10858 
   10859 static int
   10860 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   10861 {
   10862   int ret;
   10863 
   10864   /* Mark this section as checked.  */
   10865   isec->call_check_done = 1;
   10866 
   10867   /* We know none of our code bearing sections will need toc stubs.  */
   10868   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   10869     return 0;
   10870 
   10871   if (isec->size == 0)
   10872     return 0;
   10873 
   10874   if (isec->output_section == NULL)
   10875     return 0;
   10876 
   10877   ret = 0;
   10878   if (isec->reloc_count != 0)
   10879     {
   10880       Elf_Internal_Rela *relstart, *rel;
   10881       Elf_Internal_Sym *local_syms;
   10882       struct ppc_link_hash_table *htab;
   10883 
   10884       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   10885 					    info->keep_memory);
   10886       if (relstart == NULL)
   10887 	return -1;
   10888 
   10889       /* Look for branches to outside of this section.  */
   10890       local_syms = NULL;
   10891       htab = ppc_hash_table (info);
   10892       if (htab == NULL)
   10893 	return -1;
   10894 
   10895       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   10896 	{
   10897 	  enum elf_ppc64_reloc_type r_type;
   10898 	  unsigned long r_symndx;
   10899 	  struct elf_link_hash_entry *h;
   10900 	  struct ppc_link_hash_entry *eh;
   10901 	  Elf_Internal_Sym *sym;
   10902 	  asection *sym_sec;
   10903 	  struct _opd_sec_data *opd;
   10904 	  bfd_vma sym_value;
   10905 	  bfd_vma dest;
   10906 
   10907 	  r_type = ELF64_R_TYPE (rel->r_info);
   10908 	  if (r_type != R_PPC64_REL24
   10909 	      && r_type != R_PPC64_REL14
   10910 	      && r_type != R_PPC64_REL14_BRTAKEN
   10911 	      && r_type != R_PPC64_REL14_BRNTAKEN)
   10912 	    continue;
   10913 
   10914 	  r_symndx = ELF64_R_SYM (rel->r_info);
   10915 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   10916 			  isec->owner))
   10917 	    {
   10918 	      ret = -1;
   10919 	      break;
   10920 	    }
   10921 
   10922 	  /* Calls to dynamic lib functions go through a plt call stub
   10923 	     that uses r2.  */
   10924 	  eh = (struct ppc_link_hash_entry *) h;
   10925 	  if (eh != NULL
   10926 	      && (eh->elf.plt.plist != NULL
   10927 		  || (eh->oh != NULL
   10928 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   10929 	    {
   10930 	      ret = 1;
   10931 	      break;
   10932 	    }
   10933 
   10934 	  if (sym_sec == NULL)
   10935 	    /* Ignore other undefined symbols.  */
   10936 	    continue;
   10937 
   10938 	  /* Assume branches to other sections not included in the
   10939 	     link need stubs too, to cover -R and absolute syms.  */
   10940 	  if (sym_sec->output_section == NULL)
   10941 	    {
   10942 	      ret = 1;
   10943 	      break;
   10944 	    }
   10945 
   10946 	  if (h == NULL)
   10947 	    sym_value = sym->st_value;
   10948 	  else
   10949 	    {
   10950 	      if (h->root.type != bfd_link_hash_defined
   10951 		  && h->root.type != bfd_link_hash_defweak)
   10952 		abort ();
   10953 	      sym_value = h->root.u.def.value;
   10954 	    }
   10955 	  sym_value += rel->r_addend;
   10956 
   10957 	  /* If this branch reloc uses an opd sym, find the code section.  */
   10958 	  opd = get_opd_info (sym_sec);
   10959 	  if (opd != NULL)
   10960 	    {
   10961 	      if (h == NULL && opd->adjust != NULL)
   10962 		{
   10963 		  long adjust;
   10964 
   10965 		  adjust = opd->adjust[sym->st_value / 8];
   10966 		  if (adjust == -1)
   10967 		    /* Assume deleted functions won't ever be called.  */
   10968 		    continue;
   10969 		  sym_value += adjust;
   10970 		}
   10971 
   10972 	      dest = opd_entry_value (sym_sec, sym_value,
   10973 				      &sym_sec, NULL, FALSE);
   10974 	      if (dest == (bfd_vma) -1)
   10975 		continue;
   10976 	    }
   10977 	  else
   10978 	    dest = (sym_value
   10979 		    + sym_sec->output_offset
   10980 		    + sym_sec->output_section->vma);
   10981 
   10982 	  /* Ignore branch to self.  */
   10983 	  if (sym_sec == isec)
   10984 	    continue;
   10985 
   10986 	  /* If the called function uses the toc, we need a stub.  */
   10987 	  if (sym_sec->has_toc_reloc
   10988 	      || sym_sec->makes_toc_func_call)
   10989 	    {
   10990 	      ret = 1;
   10991 	      break;
   10992 	    }
   10993 
   10994 	  /* Assume any branch that needs a long branch stub might in fact
   10995 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   10996 	  else if (dest - (isec->output_offset
   10997 			   + isec->output_section->vma
   10998 			   + rel->r_offset) + (1 << 25) >= (2 << 25))
   10999 	    {
   11000 	      ret = 1;
   11001 	      break;
   11002 	    }
   11003 
   11004 	  /* If calling back to a section in the process of being
   11005 	     tested, we can't say for sure that no toc adjusting stubs
   11006 	     are needed, so don't return zero.  */
   11007 	  else if (sym_sec->call_check_in_progress)
   11008 	    ret = 2;
   11009 
   11010 	  /* Branches to another section that itself doesn't have any TOC
   11011 	     references are OK.  Recursively call ourselves to check.  */
   11012 	  else if (!sym_sec->call_check_done)
   11013 	    {
   11014 	      int recur;
   11015 
   11016 	      /* Mark current section as indeterminate, so that other
   11017 		 sections that call back to current won't be marked as
   11018 		 known.  */
   11019 	      isec->call_check_in_progress = 1;
   11020 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   11021 	      isec->call_check_in_progress = 0;
   11022 
   11023 	      if (recur != 0)
   11024 		{
   11025 		  ret = recur;
   11026 		  if (recur != 2)
   11027 		    break;
   11028 		}
   11029 	    }
   11030 	}
   11031 
   11032       if (local_syms != NULL
   11033 	  && (elf_symtab_hdr (isec->owner).contents
   11034 	      != (unsigned char *) local_syms))
   11035 	free (local_syms);
   11036       if (elf_section_data (isec)->relocs != relstart)
   11037 	free (relstart);
   11038     }
   11039 
   11040   if ((ret & 1) == 0
   11041       && isec->map_head.s != NULL
   11042       && (strcmp (isec->output_section->name, ".init") == 0
   11043 	  || strcmp (isec->output_section->name, ".fini") == 0))
   11044     {
   11045       if (isec->map_head.s->has_toc_reloc
   11046 	  || isec->map_head.s->makes_toc_func_call)
   11047 	ret = 1;
   11048       else if (!isec->map_head.s->call_check_done)
   11049 	{
   11050 	  int recur;
   11051 	  isec->call_check_in_progress = 1;
   11052 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   11053 	  isec->call_check_in_progress = 0;
   11054 	  if (recur != 0)
   11055 	    ret = recur;
   11056 	}
   11057     }
   11058 
   11059   if (ret == 1)
   11060     isec->makes_toc_func_call = 1;
   11061 
   11062   return ret;
   11063 }
   11064 
   11065 /* The linker repeatedly calls this function for each input section,
   11066    in the order that input sections are linked into output sections.
   11067    Build lists of input sections to determine groupings between which
   11068    we may insert linker stubs.  */
   11069 
   11070 bfd_boolean
   11071 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   11072 {
   11073   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11074 
   11075   if (htab == NULL)
   11076     return FALSE;
   11077 
   11078   if ((isec->output_section->flags & SEC_CODE) != 0
   11079       && isec->output_section->index <= htab->top_index)
   11080     {
   11081       asection **list = htab->input_list + isec->output_section->index;
   11082       /* Steal the link_sec pointer for our list.  */
   11083 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   11084       /* This happens to make the list in reverse order,
   11085 	 which is what we want.  */
   11086       PREV_SEC (isec) = *list;
   11087       *list = isec;
   11088     }
   11089 
   11090   if (htab->multi_toc_needed)
   11091     {
   11092       /* If a code section has a function that uses the TOC then we need
   11093 	 to use the right TOC (obviously).  Also, make sure that .opd gets
   11094 	 the correct TOC value for R_PPC64_TOC relocs that don't have or
   11095 	 can't find their function symbol (shouldn't ever happen now).
   11096 	 Also specially treat .fixup for the linux kernel.  .fixup
   11097 	 contains branches, but only back to the function that hit an
   11098 	 exception.  */
   11099       if (isec->has_toc_reloc
   11100 	  || (isec->flags & SEC_CODE) == 0
   11101 	  || strcmp (isec->name, ".fixup") == 0)
   11102 	{
   11103 	  if (elf_gp (isec->owner) != 0)
   11104 	    htab->toc_curr = elf_gp (isec->owner);
   11105 	}
   11106       else
   11107 	{
   11108 	  if (!isec->call_check_done
   11109 	      && toc_adjusting_stub_needed (info, isec) < 0)
   11110 	    return FALSE;
   11111 	  /* If we make a local call from this section, ie. a branch
   11112 	     without a following nop, then we have no place to put a
   11113 	     toc restoring insn.  We must use the same toc group as
   11114 	     the callee.
   11115 	     Testing makes_toc_func_call actually tests for *any*
   11116 	     calls to functions that need a good toc pointer.  A more
   11117 	     precise test would be better, as this one will set
   11118 	     incorrect values for pasted .init/.fini fragments.
   11119 	     (Fixed later in check_pasted_section.)  */
   11120 	  if (isec->makes_toc_func_call
   11121 	      && elf_gp (isec->owner) != 0)
   11122 	    htab->toc_curr = elf_gp (isec->owner);
   11123 	}
   11124     }
   11125 
   11126   /* Functions that don't use the TOC can belong in any TOC group.
   11127      Use the last TOC base.  */
   11128   htab->stub_group[isec->id].toc_off = htab->toc_curr;
   11129   return TRUE;
   11130 }
   11131 
   11132 /* Check that all .init and .fini sections use the same toc, if they
   11133    have toc relocs.  */
   11134 
   11135 static bfd_boolean
   11136 check_pasted_section (struct bfd_link_info *info, const char *name)
   11137 {
   11138   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   11139 
   11140   if (o != NULL)
   11141     {
   11142       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11143       bfd_vma toc_off = 0;
   11144       asection *i;
   11145 
   11146       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11147 	if (i->has_toc_reloc)
   11148 	  {
   11149 	    if (toc_off == 0)
   11150 	      toc_off = htab->stub_group[i->id].toc_off;
   11151 	    else if (toc_off != htab->stub_group[i->id].toc_off)
   11152 	      return FALSE;
   11153 	  }
   11154 
   11155       if (toc_off == 0)
   11156 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11157 	  if (i->makes_toc_func_call)
   11158 	    {
   11159 	      toc_off = htab->stub_group[i->id].toc_off;
   11160 	      break;
   11161 	    }
   11162 
   11163       /* Make sure the whole pasted function uses the same toc offset.  */
   11164       if (toc_off != 0)
   11165 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11166 	  htab->stub_group[i->id].toc_off = toc_off;
   11167     }
   11168   return TRUE;
   11169 }
   11170 
   11171 bfd_boolean
   11172 ppc64_elf_check_init_fini (struct bfd_link_info *info)
   11173 {
   11174   return (check_pasted_section (info, ".init")
   11175 	  & check_pasted_section (info, ".fini"));
   11176 }
   11177 
   11178 /* See whether we can group stub sections together.  Grouping stub
   11179    sections may result in fewer stubs.  More importantly, we need to
   11180    put all .init* and .fini* stubs at the beginning of the .init or
   11181    .fini output sections respectively, because glibc splits the
   11182    _init and _fini functions into multiple parts.  Putting a stub in
   11183    the middle of a function is not a good idea.  */
   11184 
   11185 static void
   11186 group_sections (struct ppc_link_hash_table *htab,
   11187 		bfd_size_type stub_group_size,
   11188 		bfd_boolean stubs_always_before_branch)
   11189 {
   11190   asection **list;
   11191   bfd_size_type stub14_group_size;
   11192   bfd_boolean suppress_size_errors;
   11193 
   11194   suppress_size_errors = FALSE;
   11195   stub14_group_size = stub_group_size;
   11196   if (stub_group_size == 1)
   11197     {
   11198       /* Default values.  */
   11199       if (stubs_always_before_branch)
   11200 	{
   11201 	  stub_group_size = 0x1e00000;
   11202 	  stub14_group_size = 0x7800;
   11203 	}
   11204       else
   11205 	{
   11206 	  stub_group_size = 0x1c00000;
   11207 	  stub14_group_size = 0x7000;
   11208 	}
   11209       suppress_size_errors = TRUE;
   11210     }
   11211 
   11212   list = htab->input_list + htab->top_index;
   11213   do
   11214     {
   11215       asection *tail = *list;
   11216       while (tail != NULL)
   11217 	{
   11218 	  asection *curr;
   11219 	  asection *prev;
   11220 	  bfd_size_type total;
   11221 	  bfd_boolean big_sec;
   11222 	  bfd_vma curr_toc;
   11223 
   11224 	  curr = tail;
   11225 	  total = tail->size;
   11226 	  big_sec = total > (ppc64_elf_section_data (tail) != NULL
   11227 			     && ppc64_elf_section_data (tail)->has_14bit_branch
   11228 			     ? stub14_group_size : stub_group_size);
   11229 	  if (big_sec && !suppress_size_errors)
   11230 	    (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
   11231 				     tail->owner, tail);
   11232 	  curr_toc = htab->stub_group[tail->id].toc_off;
   11233 
   11234 	  while ((prev = PREV_SEC (curr)) != NULL
   11235 		 && ((total += curr->output_offset - prev->output_offset)
   11236 		     < (ppc64_elf_section_data (prev) != NULL
   11237 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   11238 			? stub14_group_size : stub_group_size))
   11239 		 && htab->stub_group[prev->id].toc_off == curr_toc)
   11240 	    curr = prev;
   11241 
   11242 	  /* OK, the size from the start of CURR to the end is less
   11243 	     than stub_group_size and thus can be handled by one stub
   11244 	     section.  (or the tail section is itself larger than
   11245 	     stub_group_size, in which case we may be toast.)  We
   11246 	     should really be keeping track of the total size of stubs
   11247 	     added here, as stubs contribute to the final output
   11248 	     section size.  That's a little tricky, and this way will
   11249 	     only break if stubs added make the total size more than
   11250 	     2^25, ie. for the default stub_group_size, if stubs total
   11251 	     more than 2097152 bytes, or nearly 75000 plt call stubs.  */
   11252 	  do
   11253 	    {
   11254 	      prev = PREV_SEC (tail);
   11255 	      /* Set up this stub group.  */
   11256 	      htab->stub_group[tail->id].link_sec = curr;
   11257 	    }
   11258 	  while (tail != curr && (tail = prev) != NULL);
   11259 
   11260 	  /* But wait, there's more!  Input sections up to stub_group_size
   11261 	     bytes before the stub section can be handled by it too.
   11262 	     Don't do this if we have a really large section after the
   11263 	     stubs, as adding more stubs increases the chance that
   11264 	     branches may not reach into the stub section.  */
   11265 	  if (!stubs_always_before_branch && !big_sec)
   11266 	    {
   11267 	      total = 0;
   11268 	      while (prev != NULL
   11269 		     && ((total += tail->output_offset - prev->output_offset)
   11270 			 < (ppc64_elf_section_data (prev) != NULL
   11271 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   11272 			    ? stub14_group_size : stub_group_size))
   11273 		     && htab->stub_group[prev->id].toc_off == curr_toc)
   11274 		{
   11275 		  tail = prev;
   11276 		  prev = PREV_SEC (tail);
   11277 		  htab->stub_group[tail->id].link_sec = curr;
   11278 		}
   11279 	    }
   11280 	  tail = prev;
   11281 	}
   11282     }
   11283   while (list-- != htab->input_list);
   11284   free (htab->input_list);
   11285 #undef PREV_SEC
   11286 }
   11287 
   11288 static const unsigned char glink_eh_frame_cie[] =
   11289 {
   11290   0, 0, 0, 16,				/* length.  */
   11291   0, 0, 0, 0,				/* id.  */
   11292   1,					/* CIE version.  */
   11293   'z', 'R', 0,				/* Augmentation string.  */
   11294   4,					/* Code alignment.  */
   11295   0x78,					/* Data alignment.  */
   11296   65,					/* RA reg.  */
   11297   1,					/* Augmentation size.  */
   11298   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   11299   DW_CFA_def_cfa, 1, 0			/* def_cfa: r1 offset 0.  */
   11300 };
   11301 
   11302 /* Stripping output sections is normally done before dynamic section
   11303    symbols have been allocated.  This function is called later, and
   11304    handles cases like htab->brlt which is mapped to its own output
   11305    section.  */
   11306 
   11307 static void
   11308 maybe_strip_output (struct bfd_link_info *info, asection *isec)
   11309 {
   11310   if (isec->size == 0
   11311       && isec->output_section->size == 0
   11312       && !(isec->output_section->flags & SEC_KEEP)
   11313       && !bfd_section_removed_from_list (info->output_bfd,
   11314 					 isec->output_section)
   11315       && elf_section_data (isec->output_section)->dynindx == 0)
   11316     {
   11317       isec->output_section->flags |= SEC_EXCLUDE;
   11318       bfd_section_list_remove (info->output_bfd, isec->output_section);
   11319       info->output_bfd->section_count--;
   11320     }
   11321 }
   11322 
   11323 /* Determine and set the size of the stub section for a final link.
   11324 
   11325    The basic idea here is to examine all the relocations looking for
   11326    PC-relative calls to a target that is unreachable with a "bl"
   11327    instruction.  */
   11328 
   11329 bfd_boolean
   11330 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
   11331 		      bfd_boolean plt_static_chain, int plt_thread_safe,
   11332 		      int plt_stub_align)
   11333 {
   11334   bfd_size_type stub_group_size;
   11335   bfd_boolean stubs_always_before_branch;
   11336   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11337 
   11338   if (htab == NULL)
   11339     return FALSE;
   11340 
   11341   htab->plt_static_chain = plt_static_chain;
   11342   htab->plt_stub_align = plt_stub_align;
   11343   if (plt_thread_safe == -1 && !info->executable)
   11344     plt_thread_safe = 1;
   11345   if (plt_thread_safe == -1)
   11346     {
   11347       static const char *const thread_starter[] =
   11348 	{
   11349 	  "pthread_create",
   11350 	  /* libstdc++ */
   11351 	  "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
   11352 	  /* librt */
   11353 	  "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
   11354 	  "mq_notify", "create_timer",
   11355 	  /* libanl */
   11356 	  "getaddrinfo_a",
   11357 	  /* libgomp */
   11358 	  "GOMP_parallel_start",
   11359 	  "GOMP_parallel_loop_static_start",
   11360 	  "GOMP_parallel_loop_dynamic_start",
   11361 	  "GOMP_parallel_loop_guided_start",
   11362 	  "GOMP_parallel_loop_runtime_start",
   11363 	  "GOMP_parallel_sections_start",
   11364 	};
   11365       unsigned i;
   11366 
   11367       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
   11368 	{
   11369 	  struct elf_link_hash_entry *h;
   11370 	  h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
   11371 				    FALSE, FALSE, TRUE);
   11372 	  plt_thread_safe = h != NULL && h->ref_regular;
   11373 	  if (plt_thread_safe)
   11374 	    break;
   11375 	}
   11376     }
   11377   htab->plt_thread_safe = plt_thread_safe;
   11378   htab->dot_toc_dot = ((struct ppc_link_hash_entry *)
   11379 		       elf_link_hash_lookup (&htab->elf, ".TOC.",
   11380 					     FALSE, FALSE, TRUE));
   11381   stubs_always_before_branch = group_size < 0;
   11382   if (group_size < 0)
   11383     stub_group_size = -group_size;
   11384   else
   11385     stub_group_size = group_size;
   11386 
   11387   group_sections (htab, stub_group_size, stubs_always_before_branch);
   11388 
   11389   while (1)
   11390     {
   11391       bfd *input_bfd;
   11392       unsigned int bfd_indx;
   11393       asection *stub_sec;
   11394 
   11395       htab->stub_iteration += 1;
   11396 
   11397       for (input_bfd = info->input_bfds, bfd_indx = 0;
   11398 	   input_bfd != NULL;
   11399 	   input_bfd = input_bfd->link_next, bfd_indx++)
   11400 	{
   11401 	  Elf_Internal_Shdr *symtab_hdr;
   11402 	  asection *section;
   11403 	  Elf_Internal_Sym *local_syms = NULL;
   11404 
   11405 	  if (!is_ppc64_elf (input_bfd))
   11406 	    continue;
   11407 
   11408 	  /* We'll need the symbol table in a second.  */
   11409 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   11410 	  if (symtab_hdr->sh_info == 0)
   11411 	    continue;
   11412 
   11413 	  /* Walk over each section attached to the input bfd.  */
   11414 	  for (section = input_bfd->sections;
   11415 	       section != NULL;
   11416 	       section = section->next)
   11417 	    {
   11418 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   11419 
   11420 	      /* If there aren't any relocs, then there's nothing more
   11421 		 to do.  */
   11422 	      if ((section->flags & SEC_RELOC) == 0
   11423 		  || (section->flags & SEC_ALLOC) == 0
   11424 		  || (section->flags & SEC_LOAD) == 0
   11425 		  || (section->flags & SEC_CODE) == 0
   11426 		  || section->reloc_count == 0)
   11427 		continue;
   11428 
   11429 	      /* If this section is a link-once section that will be
   11430 		 discarded, then don't create any stubs.  */
   11431 	      if (section->output_section == NULL
   11432 		  || section->output_section->owner != info->output_bfd)
   11433 		continue;
   11434 
   11435 	      /* Get the relocs.  */
   11436 	      internal_relocs
   11437 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   11438 					     info->keep_memory);
   11439 	      if (internal_relocs == NULL)
   11440 		goto error_ret_free_local;
   11441 
   11442 	      /* Now examine each relocation.  */
   11443 	      irela = internal_relocs;
   11444 	      irelaend = irela + section->reloc_count;
   11445 	      for (; irela < irelaend; irela++)
   11446 		{
   11447 		  enum elf_ppc64_reloc_type r_type;
   11448 		  unsigned int r_indx;
   11449 		  enum ppc_stub_type stub_type;
   11450 		  struct ppc_stub_hash_entry *stub_entry;
   11451 		  asection *sym_sec, *code_sec;
   11452 		  bfd_vma sym_value, code_value;
   11453 		  bfd_vma destination;
   11454 		  bfd_boolean ok_dest;
   11455 		  struct ppc_link_hash_entry *hash;
   11456 		  struct ppc_link_hash_entry *fdh;
   11457 		  struct elf_link_hash_entry *h;
   11458 		  Elf_Internal_Sym *sym;
   11459 		  char *stub_name;
   11460 		  const asection *id_sec;
   11461 		  struct _opd_sec_data *opd;
   11462 		  struct plt_entry *plt_ent;
   11463 
   11464 		  r_type = ELF64_R_TYPE (irela->r_info);
   11465 		  r_indx = ELF64_R_SYM (irela->r_info);
   11466 
   11467 		  if (r_type >= R_PPC64_max)
   11468 		    {
   11469 		      bfd_set_error (bfd_error_bad_value);
   11470 		      goto error_ret_free_internal;
   11471 		    }
   11472 
   11473 		  /* Only look for stubs on branch instructions.  */
   11474 		  if (r_type != R_PPC64_REL24
   11475 		      && r_type != R_PPC64_REL14
   11476 		      && r_type != R_PPC64_REL14_BRTAKEN
   11477 		      && r_type != R_PPC64_REL14_BRNTAKEN)
   11478 		    continue;
   11479 
   11480 		  /* Now determine the call target, its name, value,
   11481 		     section.  */
   11482 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   11483 				  r_indx, input_bfd))
   11484 		    goto error_ret_free_internal;
   11485 		  hash = (struct ppc_link_hash_entry *) h;
   11486 
   11487 		  ok_dest = FALSE;
   11488 		  fdh = NULL;
   11489 		  sym_value = 0;
   11490 		  if (hash == NULL)
   11491 		    {
   11492 		      sym_value = sym->st_value;
   11493 		      ok_dest = TRUE;
   11494 		    }
   11495 		  else if (hash->elf.root.type == bfd_link_hash_defined
   11496 			   || hash->elf.root.type == bfd_link_hash_defweak)
   11497 		    {
   11498 		      sym_value = hash->elf.root.u.def.value;
   11499 		      if (sym_sec->output_section != NULL)
   11500 			ok_dest = TRUE;
   11501 		    }
   11502 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   11503 			   || hash->elf.root.type == bfd_link_hash_undefined)
   11504 		    {
   11505 		      /* Recognise an old ABI func code entry sym, and
   11506 			 use the func descriptor sym instead if it is
   11507 			 defined.  */
   11508 		      if (hash->elf.root.root.string[0] == '.'
   11509 			  && (fdh = lookup_fdh (hash, htab)) != NULL)
   11510 			{
   11511 			  if (fdh->elf.root.type == bfd_link_hash_defined
   11512 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   11513 			    {
   11514 			      sym_sec = fdh->elf.root.u.def.section;
   11515 			      sym_value = fdh->elf.root.u.def.value;
   11516 			      if (sym_sec->output_section != NULL)
   11517 				ok_dest = TRUE;
   11518 			    }
   11519 			  else
   11520 			    fdh = NULL;
   11521 			}
   11522 		    }
   11523 		  else
   11524 		    {
   11525 		      bfd_set_error (bfd_error_bad_value);
   11526 		      goto error_ret_free_internal;
   11527 		    }
   11528 
   11529 		  destination = 0;
   11530 		  if (ok_dest)
   11531 		    {
   11532 		      sym_value += irela->r_addend;
   11533 		      destination = (sym_value
   11534 				     + sym_sec->output_offset
   11535 				     + sym_sec->output_section->vma);
   11536 		    }
   11537 
   11538 		  code_sec = sym_sec;
   11539 		  code_value = sym_value;
   11540 		  opd = get_opd_info (sym_sec);
   11541 		  if (opd != NULL)
   11542 		    {
   11543 		      bfd_vma dest;
   11544 
   11545 		      if (hash == NULL && opd->adjust != NULL)
   11546 			{
   11547 			  long adjust = opd->adjust[sym_value / 8];
   11548 			  if (adjust == -1)
   11549 			    continue;
   11550 			  code_value += adjust;
   11551 			  sym_value += adjust;
   11552 			}
   11553 		      dest = opd_entry_value (sym_sec, sym_value,
   11554 					      &code_sec, &code_value, FALSE);
   11555 		      if (dest != (bfd_vma) -1)
   11556 			{
   11557 			  destination = dest;
   11558 			  if (fdh != NULL)
   11559 			    {
   11560 			      /* Fixup old ABI sym to point at code
   11561 				 entry.  */
   11562 			      hash->elf.root.type = bfd_link_hash_defweak;
   11563 			      hash->elf.root.u.def.section = code_sec;
   11564 			      hash->elf.root.u.def.value = code_value;
   11565 			    }
   11566 			}
   11567 		    }
   11568 
   11569 		  /* Determine what (if any) linker stub is needed.  */
   11570 		  plt_ent = NULL;
   11571 		  stub_type = ppc_type_of_stub (section, irela, &hash,
   11572 						&plt_ent, destination);
   11573 
   11574 		  if (stub_type != ppc_stub_plt_call)
   11575 		    {
   11576 		      /* Check whether we need a TOC adjusting stub.
   11577 			 Since the linker pastes together pieces from
   11578 			 different object files when creating the
   11579 			 _init and _fini functions, it may be that a
   11580 			 call to what looks like a local sym is in
   11581 			 fact a call needing a TOC adjustment.  */
   11582 		      if (code_sec != NULL
   11583 			  && code_sec->output_section != NULL
   11584 			  && (htab->stub_group[code_sec->id].toc_off
   11585 			      != htab->stub_group[section->id].toc_off)
   11586 			  && (code_sec->has_toc_reloc
   11587 			      || code_sec->makes_toc_func_call))
   11588 			stub_type = ppc_stub_long_branch_r2off;
   11589 		    }
   11590 
   11591 		  if (stub_type == ppc_stub_none)
   11592 		    continue;
   11593 
   11594 		  /* __tls_get_addr calls might be eliminated.  */
   11595 		  if (stub_type != ppc_stub_plt_call
   11596 		      && hash != NULL
   11597 		      && (hash == htab->tls_get_addr
   11598 			  || hash == htab->tls_get_addr_fd)
   11599 		      && section->has_tls_reloc
   11600 		      && irela != internal_relocs)
   11601 		    {
   11602 		      /* Get tls info.  */
   11603 		      unsigned char *tls_mask;
   11604 
   11605 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   11606 					 irela - 1, input_bfd))
   11607 			goto error_ret_free_internal;
   11608 		      if (*tls_mask != 0)
   11609 			continue;
   11610 		    }
   11611 
   11612 		  if (stub_type == ppc_stub_plt_call
   11613 		      && irela + 1 < irelaend
   11614 		      && irela[1].r_offset == irela->r_offset + 4
   11615 		      && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
   11616 		    {
   11617 		      if (!tocsave_find (htab, INSERT,
   11618 					 &local_syms, irela + 1, input_bfd))
   11619 			goto error_ret_free_internal;
   11620 		    }
   11621 		  else if (stub_type == ppc_stub_plt_call)
   11622 		    stub_type = ppc_stub_plt_call_r2save;
   11623 
   11624 		  /* Support for grouping stub sections.  */
   11625 		  id_sec = htab->stub_group[section->id].link_sec;
   11626 
   11627 		  /* Get the name of this stub.  */
   11628 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   11629 		  if (!stub_name)
   11630 		    goto error_ret_free_internal;
   11631 
   11632 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   11633 						     stub_name, FALSE, FALSE);
   11634 		  if (stub_entry != NULL)
   11635 		    {
   11636 		      /* The proper stub has already been created.  */
   11637 		      free (stub_name);
   11638 		      if (stub_type == ppc_stub_plt_call_r2save)
   11639 			stub_entry->stub_type = stub_type;
   11640 		      continue;
   11641 		    }
   11642 
   11643 		  stub_entry = ppc_add_stub (stub_name, section, info);
   11644 		  if (stub_entry == NULL)
   11645 		    {
   11646 		      free (stub_name);
   11647 		    error_ret_free_internal:
   11648 		      if (elf_section_data (section)->relocs == NULL)
   11649 			free (internal_relocs);
   11650 		    error_ret_free_local:
   11651 		      if (local_syms != NULL
   11652 			  && (symtab_hdr->contents
   11653 			      != (unsigned char *) local_syms))
   11654 			free (local_syms);
   11655 		      return FALSE;
   11656 		    }
   11657 
   11658 		  stub_entry->stub_type = stub_type;
   11659 		  if (stub_type != ppc_stub_plt_call
   11660 		      && stub_type != ppc_stub_plt_call_r2save)
   11661 		    {
   11662 		      stub_entry->target_value = code_value;
   11663 		      stub_entry->target_section = code_sec;
   11664 		    }
   11665 		  else
   11666 		    {
   11667 		      stub_entry->target_value = sym_value;
   11668 		      stub_entry->target_section = sym_sec;
   11669 		    }
   11670 		  stub_entry->h = hash;
   11671 		  stub_entry->plt_ent = plt_ent;
   11672 		  stub_entry->addend = irela->r_addend;
   11673 
   11674 		  if (stub_entry->h != NULL)
   11675 		    htab->stub_globals += 1;
   11676 		}
   11677 
   11678 	      /* We're done with the internal relocs, free them.  */
   11679 	      if (elf_section_data (section)->relocs != internal_relocs)
   11680 		free (internal_relocs);
   11681 	    }
   11682 
   11683 	  if (local_syms != NULL
   11684 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   11685 	    {
   11686 	      if (!info->keep_memory)
   11687 		free (local_syms);
   11688 	      else
   11689 		symtab_hdr->contents = (unsigned char *) local_syms;
   11690 	    }
   11691 	}
   11692 
   11693       /* We may have added some stubs.  Find out the new size of the
   11694 	 stub sections.  */
   11695       for (stub_sec = htab->stub_bfd->sections;
   11696 	   stub_sec != NULL;
   11697 	   stub_sec = stub_sec->next)
   11698 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11699 	  {
   11700 	    stub_sec->rawsize = stub_sec->size;
   11701 	    stub_sec->size = 0;
   11702 	    stub_sec->reloc_count = 0;
   11703 	    stub_sec->flags &= ~SEC_RELOC;
   11704 	  }
   11705 
   11706       htab->brlt->size = 0;
   11707       htab->brlt->reloc_count = 0;
   11708       htab->brlt->flags &= ~SEC_RELOC;
   11709       if (htab->relbrlt != NULL)
   11710 	htab->relbrlt->size = 0;
   11711 
   11712       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   11713 
   11714       if (info->emitrelocations
   11715 	  && htab->glink != NULL && htab->glink->size != 0)
   11716 	{
   11717 	  htab->glink->reloc_count = 1;
   11718 	  htab->glink->flags |= SEC_RELOC;
   11719 	}
   11720 
   11721       if (htab->glink_eh_frame != NULL
   11722 	  && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   11723 	  && htab->glink_eh_frame->output_section->size != 0)
   11724 	{
   11725 	  size_t size = 0, align;
   11726 
   11727 	  for (stub_sec = htab->stub_bfd->sections;
   11728 	       stub_sec != NULL;
   11729 	       stub_sec = stub_sec->next)
   11730 	    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11731 	      size += 20;
   11732 	  if (htab->glink != NULL && htab->glink->size != 0)
   11733 	    size += 24;
   11734 	  if (size != 0)
   11735 	    size += sizeof (glink_eh_frame_cie);
   11736 	  align = 1;
   11737 	  align <<= htab->glink_eh_frame->output_section->alignment_power;
   11738 	  align -= 1;
   11739 	  size = (size + align) & ~align;
   11740 	  htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   11741 	  htab->glink_eh_frame->size = size;
   11742 	}
   11743 
   11744       if (htab->plt_stub_align != 0)
   11745 	for (stub_sec = htab->stub_bfd->sections;
   11746 	     stub_sec != NULL;
   11747 	     stub_sec = stub_sec->next)
   11748 	  if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11749 	    stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
   11750 			      & (-1 << htab->plt_stub_align));
   11751 
   11752       for (stub_sec = htab->stub_bfd->sections;
   11753 	   stub_sec != NULL;
   11754 	   stub_sec = stub_sec->next)
   11755 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11756 	    && stub_sec->rawsize != stub_sec->size)
   11757 	  break;
   11758 
   11759       /* Exit from this loop when no stubs have been added, and no stubs
   11760 	 have changed size.  */
   11761       if (stub_sec == NULL
   11762 	  && (htab->glink_eh_frame == NULL
   11763 	      || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
   11764 	break;
   11765 
   11766       /* Ask the linker to do its stuff.  */
   11767       (*htab->layout_sections_again) ();
   11768     }
   11769 
   11770   maybe_strip_output (info, htab->brlt);
   11771   if (htab->glink_eh_frame != NULL)
   11772     maybe_strip_output (info, htab->glink_eh_frame);
   11773 
   11774   return TRUE;
   11775 }
   11776 
   11777 /* Called after we have determined section placement.  If sections
   11778    move, we'll be called again.  Provide a value for TOCstart.  */
   11779 
   11780 bfd_vma
   11781 ppc64_elf_toc (bfd *obfd)
   11782 {
   11783   asection *s;
   11784   bfd_vma TOCstart;
   11785 
   11786   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   11787      order.  The TOC starts where the first of these sections starts.  */
   11788   s = bfd_get_section_by_name (obfd, ".got");
   11789   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11790     s = bfd_get_section_by_name (obfd, ".toc");
   11791   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11792     s = bfd_get_section_by_name (obfd, ".tocbss");
   11793   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11794     s = bfd_get_section_by_name (obfd, ".plt");
   11795   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11796     {
   11797       /* This may happen for
   11798 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   11799 	 .toc directive
   11800 	 o  bad linker script
   11801 	 o --gc-sections and empty TOC sections
   11802 
   11803 	 FIXME: Warn user?  */
   11804 
   11805       /* Look for a likely section.  We probably won't even be
   11806 	 using TOCstart.  */
   11807       for (s = obfd->sections; s != NULL; s = s->next)
   11808 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   11809 			 | SEC_EXCLUDE))
   11810 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   11811 	  break;
   11812       if (s == NULL)
   11813 	for (s = obfd->sections; s != NULL; s = s->next)
   11814 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   11815 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   11816 	    break;
   11817       if (s == NULL)
   11818 	for (s = obfd->sections; s != NULL; s = s->next)
   11819 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   11820 	      == SEC_ALLOC)
   11821 	    break;
   11822       if (s == NULL)
   11823 	for (s = obfd->sections; s != NULL; s = s->next)
   11824 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   11825 	    break;
   11826     }
   11827 
   11828   TOCstart = 0;
   11829   if (s != NULL)
   11830     TOCstart = s->output_section->vma + s->output_offset;
   11831 
   11832   return TOCstart;
   11833 }
   11834 
   11835 /* Build all the stubs associated with the current output file.
   11836    The stubs are kept in a hash table attached to the main linker
   11837    hash table.  This function is called via gldelf64ppc_finish.  */
   11838 
   11839 bfd_boolean
   11840 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
   11841 		       struct bfd_link_info *info,
   11842 		       char **stats)
   11843 {
   11844   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11845   asection *stub_sec;
   11846   bfd_byte *p;
   11847   int stub_sec_count = 0;
   11848 
   11849   if (htab == NULL)
   11850     return FALSE;
   11851 
   11852   htab->emit_stub_syms = emit_stub_syms;
   11853 
   11854   /* Allocate memory to hold the linker stubs.  */
   11855   for (stub_sec = htab->stub_bfd->sections;
   11856        stub_sec != NULL;
   11857        stub_sec = stub_sec->next)
   11858     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11859 	&& stub_sec->size != 0)
   11860       {
   11861 	stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
   11862 	if (stub_sec->contents == NULL)
   11863 	  return FALSE;
   11864 	/* We want to check that built size is the same as calculated
   11865 	   size.  rawsize is a convenient location to use.  */
   11866 	stub_sec->rawsize = stub_sec->size;
   11867 	stub_sec->size = 0;
   11868       }
   11869 
   11870   if (htab->glink != NULL && htab->glink->size != 0)
   11871     {
   11872       unsigned int indx;
   11873       bfd_vma plt0;
   11874 
   11875       /* Build the .glink plt call stub.  */
   11876       if (htab->emit_stub_syms)
   11877 	{
   11878 	  struct elf_link_hash_entry *h;
   11879 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   11880 				    TRUE, FALSE, FALSE);
   11881 	  if (h == NULL)
   11882 	    return FALSE;
   11883 	  if (h->root.type == bfd_link_hash_new)
   11884 	    {
   11885 	      h->root.type = bfd_link_hash_defined;
   11886 	      h->root.u.def.section = htab->glink;
   11887 	      h->root.u.def.value = 8;
   11888 	      h->ref_regular = 1;
   11889 	      h->def_regular = 1;
   11890 	      h->ref_regular_nonweak = 1;
   11891 	      h->forced_local = 1;
   11892 	      h->non_elf = 0;
   11893 	    }
   11894 	}
   11895       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
   11896       if (info->emitrelocations)
   11897 	{
   11898 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   11899 	  if (r == NULL)
   11900 	    return FALSE;
   11901 	  r->r_offset = (htab->glink->output_offset
   11902 			 + htab->glink->output_section->vma);
   11903 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   11904 	  r->r_addend = plt0;
   11905 	}
   11906       p = htab->glink->contents;
   11907       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   11908       bfd_put_64 (htab->glink->owner, plt0, p);
   11909       p += 8;
   11910       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   11911       p += 4;
   11912       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   11913       p += 4;
   11914       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   11915       p += 4;
   11916       bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
   11917       p += 4;
   11918       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   11919       p += 4;
   11920       bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
   11921       p += 4;
   11922       bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
   11923       p += 4;
   11924       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
   11925       p += 4;
   11926       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
   11927       p += 4;
   11928       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
   11929       p += 4;
   11930       bfd_put_32 (htab->glink->owner, BCTR, p);
   11931       p += 4;
   11932       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
   11933 	{
   11934 	  bfd_put_32 (htab->glink->owner, NOP, p);
   11935 	  p += 4;
   11936 	}
   11937 
   11938       /* Build the .glink lazy link call stubs.  */
   11939       indx = 0;
   11940       while (p < htab->glink->contents + htab->glink->size)
   11941 	{
   11942 	  if (indx < 0x8000)
   11943 	    {
   11944 	      bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   11945 	      p += 4;
   11946 	    }
   11947 	  else
   11948 	    {
   11949 	      bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   11950 	      p += 4;
   11951 	      bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
   11952 	      p += 4;
   11953 	    }
   11954 	  bfd_put_32 (htab->glink->owner,
   11955 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   11956 	  indx++;
   11957 	  p += 4;
   11958 	}
   11959       htab->glink->rawsize = p - htab->glink->contents;
   11960     }
   11961 
   11962   if (htab->brlt->size != 0)
   11963     {
   11964       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   11965 					 htab->brlt->size);
   11966       if (htab->brlt->contents == NULL)
   11967 	return FALSE;
   11968     }
   11969   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   11970     {
   11971       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   11972 					    htab->relbrlt->size);
   11973       if (htab->relbrlt->contents == NULL)
   11974 	return FALSE;
   11975     }
   11976 
   11977   if (htab->glink_eh_frame != NULL
   11978       && htab->glink_eh_frame->size != 0)
   11979     {
   11980       bfd_vma val;
   11981       bfd_byte *last_fde;
   11982       size_t last_fde_len, size, align, pad;
   11983 
   11984       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
   11985       if (p == NULL)
   11986 	return FALSE;
   11987       htab->glink_eh_frame->contents = p;
   11988       last_fde = p;
   11989 
   11990       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   11991 
   11992       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   11993       /* CIE length (rewrite in case little-endian).  */
   11994       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
   11995       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   11996       p += sizeof (glink_eh_frame_cie);
   11997 
   11998       for (stub_sec = htab->stub_bfd->sections;
   11999 	   stub_sec != NULL;
   12000 	   stub_sec = stub_sec->next)
   12001 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12002 	  {
   12003 	    last_fde = p;
   12004 	    last_fde_len = 16;
   12005 	    /* FDE length.  */
   12006 	    bfd_put_32 (htab->elf.dynobj, 16, p);
   12007 	    p += 4;
   12008 	    /* CIE pointer.  */
   12009 	    val = p - htab->glink_eh_frame->contents;
   12010 	    bfd_put_32 (htab->elf.dynobj, val, p);
   12011 	    p += 4;
   12012 	    /* Offset to stub section.  */
   12013 	    val = (stub_sec->output_section->vma
   12014 		   + stub_sec->output_offset);
   12015 	    val -= (htab->glink_eh_frame->output_section->vma
   12016 		    + htab->glink_eh_frame->output_offset);
   12017 	    val -= p - htab->glink_eh_frame->contents;
   12018 	    if (val + 0x80000000 > 0xffffffff)
   12019 	      {
   12020 		info->callbacks->einfo
   12021 		  (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   12022 		   stub_sec->name);
   12023 		return FALSE;
   12024 	      }
   12025 	    bfd_put_32 (htab->elf.dynobj, val, p);
   12026 	    p += 4;
   12027 	    /* stub section size.  */
   12028 	    bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
   12029 	    p += 4;
   12030 	    /* Augmentation.  */
   12031 	    p += 1;
   12032 	    /* Pad.  */
   12033 	    p += 3;
   12034 	  }
   12035       if (htab->glink != NULL && htab->glink->size != 0)
   12036 	{
   12037 	  last_fde = p;
   12038 	  last_fde_len = 20;
   12039 	  /* FDE length.  */
   12040 	  bfd_put_32 (htab->elf.dynobj, 20, p);
   12041 	  p += 4;
   12042 	  /* CIE pointer.  */
   12043 	  val = p - htab->glink_eh_frame->contents;
   12044 	  bfd_put_32 (htab->elf.dynobj, val, p);
   12045 	  p += 4;
   12046 	  /* Offset to .glink.  */
   12047 	  val = (htab->glink->output_section->vma
   12048 		 + htab->glink->output_offset
   12049 		 + 8);
   12050 	  val -= (htab->glink_eh_frame->output_section->vma
   12051 		  + htab->glink_eh_frame->output_offset);
   12052 	  val -= p - htab->glink_eh_frame->contents;
   12053 	  if (val + 0x80000000 > 0xffffffff)
   12054 	    {
   12055 	      info->callbacks->einfo
   12056 		(_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   12057 		 htab->glink->name);
   12058 	      return FALSE;
   12059 	    }
   12060 	  bfd_put_32 (htab->elf.dynobj, val, p);
   12061 	  p += 4;
   12062 	  /* .glink size.  */
   12063 	  bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
   12064 	  p += 4;
   12065 	  /* Augmentation.  */
   12066 	  p += 1;
   12067 
   12068 	  *p++ = DW_CFA_advance_loc + 1;
   12069 	  *p++ = DW_CFA_register;
   12070 	  *p++ = 65;
   12071 	  *p++ = 12;
   12072 	  *p++ = DW_CFA_advance_loc + 4;
   12073 	  *p++ = DW_CFA_restore_extended;
   12074 	  *p++ = 65;
   12075 	}
   12076       /* Subsume any padding into the last FDE if user .eh_frame
   12077 	 sections are aligned more than glink_eh_frame.  Otherwise any
   12078 	 zero padding will be seen as a terminator.  */
   12079       size = p - htab->glink_eh_frame->contents;
   12080       align = 1;
   12081       align <<= htab->glink_eh_frame->output_section->alignment_power;
   12082       align -= 1;
   12083       pad = ((size + align) & ~align) - size;
   12084       htab->glink_eh_frame->size = size + pad;
   12085       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
   12086     }
   12087 
   12088   /* Build the stubs as directed by the stub hash table.  */
   12089   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   12090 
   12091   if (htab->relbrlt != NULL)
   12092     htab->relbrlt->reloc_count = 0;
   12093 
   12094   if (htab->plt_stub_align != 0)
   12095     for (stub_sec = htab->stub_bfd->sections;
   12096 	 stub_sec != NULL;
   12097 	 stub_sec = stub_sec->next)
   12098       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12099 	stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
   12100 			  & (-1 << htab->plt_stub_align));
   12101 
   12102   for (stub_sec = htab->stub_bfd->sections;
   12103        stub_sec != NULL;
   12104        stub_sec = stub_sec->next)
   12105     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12106       {
   12107 	stub_sec_count += 1;
   12108 	if (stub_sec->rawsize != stub_sec->size)
   12109 	  break;
   12110       }
   12111 
   12112   if (stub_sec != NULL
   12113       || htab->glink->rawsize != htab->glink->size
   12114       || (htab->glink_eh_frame != NULL
   12115 	  && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
   12116     {
   12117       htab->stub_error = TRUE;
   12118       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
   12119     }
   12120 
   12121   if (htab->stub_error)
   12122     return FALSE;
   12123 
   12124   if (stats != NULL)
   12125     {
   12126       *stats = bfd_malloc (500);
   12127       if (*stats == NULL)
   12128 	return FALSE;
   12129 
   12130       sprintf (*stats, _("linker stubs in %u group%s\n"
   12131 			 "  branch       %lu\n"
   12132 			 "  toc adjust   %lu\n"
   12133 			 "  long branch  %lu\n"
   12134 			 "  long toc adj %lu\n"
   12135 			 "  plt call     %lu\n"
   12136 			 "  plt call toc %lu"),
   12137 	       stub_sec_count,
   12138 	       stub_sec_count == 1 ? "" : "s",
   12139 	       htab->stub_count[ppc_stub_long_branch - 1],
   12140 	       htab->stub_count[ppc_stub_long_branch_r2off - 1],
   12141 	       htab->stub_count[ppc_stub_plt_branch - 1],
   12142 	       htab->stub_count[ppc_stub_plt_branch_r2off - 1],
   12143 	       htab->stub_count[ppc_stub_plt_call - 1],
   12144 	       htab->stub_count[ppc_stub_plt_call_r2save - 1]);
   12145     }
   12146   return TRUE;
   12147 }
   12148 
   12149 /* This function undoes the changes made by add_symbol_adjust.  */
   12150 
   12151 static bfd_boolean
   12152 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   12153 {
   12154   struct ppc_link_hash_entry *eh;
   12155 
   12156   if (h->root.type == bfd_link_hash_indirect)
   12157     return TRUE;
   12158 
   12159   eh = (struct ppc_link_hash_entry *) h;
   12160   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
   12161     return TRUE;
   12162 
   12163   eh->elf.root.type = bfd_link_hash_undefined;
   12164   return TRUE;
   12165 }
   12166 
   12167 void
   12168 ppc64_elf_restore_symbols (struct bfd_link_info *info)
   12169 {
   12170   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12171 
   12172   if (htab != NULL)
   12173     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
   12174 }
   12175 
   12176 /* What to do when ld finds relocations against symbols defined in
   12177    discarded sections.  */
   12178 
   12179 static unsigned int
   12180 ppc64_elf_action_discarded (asection *sec)
   12181 {
   12182   if (strcmp (".opd", sec->name) == 0)
   12183     return 0;
   12184 
   12185   if (strcmp (".toc", sec->name) == 0)
   12186     return 0;
   12187 
   12188   if (strcmp (".toc1", sec->name) == 0)
   12189     return 0;
   12190 
   12191   return _bfd_elf_default_action_discarded (sec);
   12192 }
   12193 
   12194 /* The RELOCATE_SECTION function is called by the ELF backend linker
   12195    to handle the relocations for a section.
   12196 
   12197    The relocs are always passed as Rela structures; if the section
   12198    actually uses Rel structures, the r_addend field will always be
   12199    zero.
   12200 
   12201    This function is responsible for adjust the section contents as
   12202    necessary, and (if using Rela relocs and generating a
   12203    relocatable output file) adjusting the reloc addend as
   12204    necessary.
   12205 
   12206    This function does not have to worry about setting the reloc
   12207    address or the reloc symbol index.
   12208 
   12209    LOCAL_SYMS is a pointer to the swapped in local symbols.
   12210 
   12211    LOCAL_SECTIONS is an array giving the section in the input file
   12212    corresponding to the st_shndx field of each local symbol.
   12213 
   12214    The global hash table entry for the global symbols can be found
   12215    via elf_sym_hashes (input_bfd).
   12216 
   12217    When generating relocatable output, this function must handle
   12218    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   12219    going to be the section symbol corresponding to the output
   12220    section, which means that the addend must be adjusted
   12221    accordingly.  */
   12222 
   12223 static bfd_boolean
   12224 ppc64_elf_relocate_section (bfd *output_bfd,
   12225 			    struct bfd_link_info *info,
   12226 			    bfd *input_bfd,
   12227 			    asection *input_section,
   12228 			    bfd_byte *contents,
   12229 			    Elf_Internal_Rela *relocs,
   12230 			    Elf_Internal_Sym *local_syms,
   12231 			    asection **local_sections)
   12232 {
   12233   struct ppc_link_hash_table *htab;
   12234   Elf_Internal_Shdr *symtab_hdr;
   12235   struct elf_link_hash_entry **sym_hashes;
   12236   Elf_Internal_Rela *rel;
   12237   Elf_Internal_Rela *relend;
   12238   Elf_Internal_Rela outrel;
   12239   bfd_byte *loc;
   12240   struct got_entry **local_got_ents;
   12241   bfd_vma TOCstart;
   12242   bfd_boolean ret = TRUE;
   12243   bfd_boolean is_opd;
   12244   /* Assume 'at' branch hints.  */
   12245   bfd_boolean is_isa_v2 = TRUE;
   12246   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
   12247 
   12248   /* Initialize howto table if needed.  */
   12249   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   12250     ppc_howto_init ();
   12251 
   12252   htab = ppc_hash_table (info);
   12253   if (htab == NULL)
   12254     return FALSE;
   12255 
   12256   /* Don't relocate stub sections.  */
   12257   if (input_section->owner == htab->stub_bfd)
   12258     return TRUE;
   12259 
   12260   BFD_ASSERT (is_ppc64_elf (input_bfd));
   12261 
   12262   local_got_ents = elf_local_got_ents (input_bfd);
   12263   TOCstart = elf_gp (output_bfd);
   12264   symtab_hdr = &elf_symtab_hdr (input_bfd);
   12265   sym_hashes = elf_sym_hashes (input_bfd);
   12266   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   12267 
   12268   rel = relocs;
   12269   relend = relocs + input_section->reloc_count;
   12270   for (; rel < relend; rel++)
   12271     {
   12272       enum elf_ppc64_reloc_type r_type;
   12273       bfd_vma addend;
   12274       bfd_reloc_status_type r;
   12275       Elf_Internal_Sym *sym;
   12276       asection *sec;
   12277       struct elf_link_hash_entry *h_elf;
   12278       struct ppc_link_hash_entry *h;
   12279       struct ppc_link_hash_entry *fdh;
   12280       const char *sym_name;
   12281       unsigned long r_symndx, toc_symndx;
   12282       bfd_vma toc_addend;
   12283       unsigned char tls_mask, tls_gd, tls_type;
   12284       unsigned char sym_type;
   12285       bfd_vma relocation;
   12286       bfd_boolean unresolved_reloc;
   12287       bfd_boolean warned;
   12288       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
   12289       unsigned int insn;
   12290       unsigned int mask;
   12291       struct ppc_stub_hash_entry *stub_entry;
   12292       bfd_vma max_br_offset;
   12293       bfd_vma from;
   12294       const Elf_Internal_Rela orig_rel = *rel;
   12295 
   12296       r_type = ELF64_R_TYPE (rel->r_info);
   12297       r_symndx = ELF64_R_SYM (rel->r_info);
   12298 
   12299       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   12300 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   12301 	 proper TOC base to use.  */
   12302       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   12303 	  && rel != relocs
   12304 	  && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
   12305 	  && is_opd)
   12306 	r_symndx = ELF64_R_SYM (rel[-1].r_info);
   12307 
   12308       sym = NULL;
   12309       sec = NULL;
   12310       h_elf = NULL;
   12311       sym_name = NULL;
   12312       unresolved_reloc = FALSE;
   12313       warned = FALSE;
   12314 
   12315       if (r_symndx < symtab_hdr->sh_info)
   12316 	{
   12317 	  /* It's a local symbol.  */
   12318 	  struct _opd_sec_data *opd;
   12319 
   12320 	  sym = local_syms + r_symndx;
   12321 	  sec = local_sections[r_symndx];
   12322 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   12323 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   12324 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   12325 	  opd = get_opd_info (sec);
   12326 	  if (opd != NULL && opd->adjust != NULL)
   12327 	    {
   12328 	      long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
   12329 	      if (adjust == -1)
   12330 		relocation = 0;
   12331 	      else
   12332 		{
   12333 		  /* If this is a relocation against the opd section sym
   12334 		     and we have edited .opd, adjust the reloc addend so
   12335 		     that ld -r and ld --emit-relocs output is correct.
   12336 		     If it is a reloc against some other .opd symbol,
   12337 		     then the symbol value will be adjusted later.  */
   12338 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   12339 		    rel->r_addend += adjust;
   12340 		  else
   12341 		    relocation += adjust;
   12342 		}
   12343 	    }
   12344 	}
   12345       else
   12346 	{
   12347 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   12348 				   r_symndx, symtab_hdr, sym_hashes,
   12349 				   h_elf, sec, relocation,
   12350 				   unresolved_reloc, warned);
   12351 	  sym_name = h_elf->root.root.string;
   12352 	  sym_type = h_elf->type;
   12353 	  if (sec != NULL
   12354 	      && sec->owner == output_bfd
   12355 	      && strcmp (sec->name, ".opd") == 0)
   12356 	    {
   12357 	      /* This is a symbol defined in a linker script.  All
   12358 		 such are defined in output sections, even those
   12359 		 defined by simple assignment from a symbol defined in
   12360 		 an input section.  Transfer the symbol to an
   12361 		 appropriate input .opd section, so that a branch to
   12362 		 this symbol will be mapped to the location specified
   12363 		 by the opd entry.  */
   12364 	      struct bfd_link_order *lo;
   12365 	      for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
   12366 		if (lo->type == bfd_indirect_link_order)
   12367 		  {
   12368 		    asection *isec = lo->u.indirect.section;
   12369 		    if (h_elf->root.u.def.value >= isec->output_offset
   12370 			&& h_elf->root.u.def.value < (isec->output_offset
   12371 						      + isec->size))
   12372 		      {
   12373 			h_elf->root.u.def.value -= isec->output_offset;
   12374 			h_elf->root.u.def.section = isec;
   12375 			sec = isec;
   12376 			break;
   12377 		      }
   12378 		  }
   12379 	    }
   12380 	  if (h_elf == &htab->dot_toc_dot->elf)
   12381 	    {
   12382 	      relocation = (TOCstart
   12383 			    + htab->stub_group[input_section->id].toc_off);
   12384 	      sec = bfd_abs_section_ptr;
   12385 	      unresolved_reloc = FALSE;
   12386 	    }
   12387 	}
   12388       h = (struct ppc_link_hash_entry *) h_elf;
   12389 
   12390       if (sec != NULL && discarded_section (sec))
   12391 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   12392 					 rel, 1, relend,
   12393 					 ppc64_elf_howto_table[r_type], 0,
   12394 					 contents);
   12395 
   12396       if (info->relocatable)
   12397 	continue;
   12398 
   12399       /* TLS optimizations.  Replace instruction sequences and relocs
   12400 	 based on information we collected in tls_optimize.  We edit
   12401 	 RELOCS so that --emit-relocs will output something sensible
   12402 	 for the final instruction stream.  */
   12403       tls_mask = 0;
   12404       tls_gd = 0;
   12405       toc_symndx = 0;
   12406       if (h != NULL)
   12407 	tls_mask = h->tls_mask;
   12408       else if (local_got_ents != NULL)
   12409 	{
   12410 	  struct plt_entry **local_plt = (struct plt_entry **)
   12411 	    (local_got_ents + symtab_hdr->sh_info);
   12412 	  unsigned char *lgot_masks = (unsigned char *)
   12413 	    (local_plt + symtab_hdr->sh_info);
   12414 	  tls_mask = lgot_masks[r_symndx];
   12415 	}
   12416       if (tls_mask == 0
   12417 	  && (r_type == R_PPC64_TLS
   12418 	      || r_type == R_PPC64_TLSGD
   12419 	      || r_type == R_PPC64_TLSLD))
   12420 	{
   12421 	  /* Check for toc tls entries.  */
   12422 	  unsigned char *toc_tls;
   12423 
   12424 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   12425 			     &local_syms, rel, input_bfd))
   12426 	    return FALSE;
   12427 
   12428 	  if (toc_tls)
   12429 	    tls_mask = *toc_tls;
   12430 	}
   12431 
   12432       /* Check that tls relocs are used with tls syms, and non-tls
   12433 	 relocs are used with non-tls syms.  */
   12434       if (r_symndx != STN_UNDEF
   12435 	  && r_type != R_PPC64_NONE
   12436 	  && (h == NULL
   12437 	      || h->elf.root.type == bfd_link_hash_defined
   12438 	      || h->elf.root.type == bfd_link_hash_defweak)
   12439 	  && (IS_PPC64_TLS_RELOC (r_type)
   12440 	      != (sym_type == STT_TLS
   12441 		  || (sym_type == STT_SECTION
   12442 		      && (sec->flags & SEC_THREAD_LOCAL) != 0))))
   12443 	{
   12444 	  if (tls_mask != 0
   12445 	      && (r_type == R_PPC64_TLS
   12446 		  || r_type == R_PPC64_TLSGD
   12447 		  || r_type == R_PPC64_TLSLD))
   12448 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   12449 	    ;
   12450 	  else
   12451 	    info->callbacks->einfo
   12452 	      (!IS_PPC64_TLS_RELOC (r_type)
   12453 	       ? _("%P: %H: %s used with TLS symbol `%T'\n")
   12454 	       : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
   12455 	       input_bfd, input_section, rel->r_offset,
   12456 	       ppc64_elf_howto_table[r_type]->name,
   12457 	       sym_name);
   12458 	}
   12459 
   12460       /* Ensure reloc mapping code below stays sane.  */
   12461       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   12462 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   12463 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   12464 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   12465 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   12466 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   12467 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   12468 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   12469 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   12470 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   12471 	abort ();
   12472 
   12473       switch (r_type)
   12474 	{
   12475 	default:
   12476 	  break;
   12477 
   12478 	case R_PPC64_LO_DS_OPT:
   12479 	  insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   12480 	  if ((insn & (0x3f << 26)) != 58u << 26)
   12481 	    abort ();
   12482 	  insn += (14u << 26) - (58u << 26);
   12483 	  bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   12484 	  r_type = R_PPC64_TOC16_LO;
   12485 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12486 	  break;
   12487 
   12488 	case R_PPC64_TOC16:
   12489 	case R_PPC64_TOC16_LO:
   12490 	case R_PPC64_TOC16_DS:
   12491 	case R_PPC64_TOC16_LO_DS:
   12492 	  {
   12493 	    /* Check for toc tls entries.  */
   12494 	    unsigned char *toc_tls;
   12495 	    int retval;
   12496 
   12497 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   12498 				   &local_syms, rel, input_bfd);
   12499 	    if (retval == 0)
   12500 	      return FALSE;
   12501 
   12502 	    if (toc_tls)
   12503 	      {
   12504 		tls_mask = *toc_tls;
   12505 		if (r_type == R_PPC64_TOC16_DS
   12506 		    || r_type == R_PPC64_TOC16_LO_DS)
   12507 		  {
   12508 		    if (tls_mask != 0
   12509 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   12510 		      goto toctprel;
   12511 		  }
   12512 		else
   12513 		  {
   12514 		    /* If we found a GD reloc pair, then we might be
   12515 		       doing a GD->IE transition.  */
   12516 		    if (retval == 2)
   12517 		      {
   12518 			tls_gd = TLS_TPRELGD;
   12519 			if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12520 			  goto tls_ldgd_opt;
   12521 		      }
   12522 		    else if (retval == 3)
   12523 		      {
   12524 			if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12525 			  goto tls_ldgd_opt;
   12526 		      }
   12527 		  }
   12528 	      }
   12529 	  }
   12530 	  break;
   12531 
   12532 	case R_PPC64_GOT_TPREL16_HI:
   12533 	case R_PPC64_GOT_TPREL16_HA:
   12534 	  if (tls_mask != 0
   12535 	      && (tls_mask & TLS_TPREL) == 0)
   12536 	    {
   12537 	      rel->r_offset -= d_offset;
   12538 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   12539 	      r_type = R_PPC64_NONE;
   12540 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12541 	    }
   12542 	  break;
   12543 
   12544 	case R_PPC64_GOT_TPREL16_DS:
   12545 	case R_PPC64_GOT_TPREL16_LO_DS:
   12546 	  if (tls_mask != 0
   12547 	      && (tls_mask & TLS_TPREL) == 0)
   12548 	    {
   12549 	    toctprel:
   12550 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   12551 	      insn &= 31 << 21;
   12552 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   12553 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   12554 	      r_type = R_PPC64_TPREL16_HA;
   12555 	      if (toc_symndx != 0)
   12556 		{
   12557 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   12558 		  rel->r_addend = toc_addend;
   12559 		  /* We changed the symbol.  Start over in order to
   12560 		     get h, sym, sec etc. right.  */
   12561 		  rel--;
   12562 		  continue;
   12563 		}
   12564 	      else
   12565 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12566 	    }
   12567 	  break;
   12568 
   12569 	case R_PPC64_TLS:
   12570 	  if (tls_mask != 0
   12571 	      && (tls_mask & TLS_TPREL) == 0)
   12572 	    {
   12573 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
   12574 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   12575 	      if (insn == 0)
   12576 		abort ();
   12577 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   12578 	      /* Was PPC64_TLS which sits on insn boundary, now
   12579 		 PPC64_TPREL16_LO which is at low-order half-word.  */
   12580 	      rel->r_offset += d_offset;
   12581 	      r_type = R_PPC64_TPREL16_LO;
   12582 	      if (toc_symndx != 0)
   12583 		{
   12584 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   12585 		  rel->r_addend = toc_addend;
   12586 		  /* We changed the symbol.  Start over in order to
   12587 		     get h, sym, sec etc. right.  */
   12588 		  rel--;
   12589 		  continue;
   12590 		}
   12591 	      else
   12592 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12593 	    }
   12594 	  break;
   12595 
   12596 	case R_PPC64_GOT_TLSGD16_HI:
   12597 	case R_PPC64_GOT_TLSGD16_HA:
   12598 	  tls_gd = TLS_TPRELGD;
   12599 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12600 	    goto tls_gdld_hi;
   12601 	  break;
   12602 
   12603 	case R_PPC64_GOT_TLSLD16_HI:
   12604 	case R_PPC64_GOT_TLSLD16_HA:
   12605 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12606 	    {
   12607 	    tls_gdld_hi:
   12608 	      if ((tls_mask & tls_gd) != 0)
   12609 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   12610 			  + R_PPC64_GOT_TPREL16_DS);
   12611 	      else
   12612 		{
   12613 		  rel->r_offset -= d_offset;
   12614 		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   12615 		  r_type = R_PPC64_NONE;
   12616 		}
   12617 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12618 	    }
   12619 	  break;
   12620 
   12621 	case R_PPC64_GOT_TLSGD16:
   12622 	case R_PPC64_GOT_TLSGD16_LO:
   12623 	  tls_gd = TLS_TPRELGD;
   12624 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12625 	    goto tls_ldgd_opt;
   12626 	  break;
   12627 
   12628 	case R_PPC64_GOT_TLSLD16:
   12629 	case R_PPC64_GOT_TLSLD16_LO:
   12630 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12631 	    {
   12632 	      unsigned int insn1, insn2, insn3;
   12633 	      bfd_vma offset;
   12634 
   12635 	    tls_ldgd_opt:
   12636 	      offset = (bfd_vma) -1;
   12637 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   12638 		 __tls_get_addr calls, we must trust that the call
   12639 		 stays with its arg setup insns, ie. that the next
   12640 		 reloc is the __tls_get_addr call associated with
   12641 		 the current reloc.  Edit both insns.  */
   12642 	      if (input_section->has_tls_get_addr_call
   12643 		  && rel + 1 < relend
   12644 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   12645 					      htab->tls_get_addr,
   12646 					      htab->tls_get_addr_fd))
   12647 		offset = rel[1].r_offset;
   12648 	      if ((tls_mask & tls_gd) != 0)
   12649 		{
   12650 		  /* IE */
   12651 		  insn1 = bfd_get_32 (output_bfd,
   12652 				      contents + rel->r_offset - d_offset);
   12653 		  insn1 &= (1 << 26) - (1 << 2);
   12654 		  insn1 |= 58 << 26;	/* ld */
   12655 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12656 		  if (offset != (bfd_vma) -1)
   12657 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12658 		  if ((tls_mask & TLS_EXPLICIT) == 0)
   12659 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   12660 			      + R_PPC64_GOT_TPREL16_DS);
   12661 		  else
   12662 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   12663 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12664 		}
   12665 	      else
   12666 		{
   12667 		  /* LE */
   12668 		  insn1 = 0x3c6d0000;	/* addis 3,13,0 */
   12669 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12670 		  if (tls_gd == 0)
   12671 		    {
   12672 		      /* Was an LD reloc.  */
   12673 		      if (toc_symndx)
   12674 			sec = local_sections[toc_symndx];
   12675 		      for (r_symndx = 0;
   12676 			   r_symndx < symtab_hdr->sh_info;
   12677 			   r_symndx++)
   12678 			if (local_sections[r_symndx] == sec)
   12679 			  break;
   12680 		      if (r_symndx >= symtab_hdr->sh_info)
   12681 			r_symndx = STN_UNDEF;
   12682 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12683 		      if (r_symndx != STN_UNDEF)
   12684 			rel->r_addend -= (local_syms[r_symndx].st_value
   12685 					  + sec->output_offset
   12686 					  + sec->output_section->vma);
   12687 		    }
   12688 		  else if (toc_symndx != 0)
   12689 		    {
   12690 		      r_symndx = toc_symndx;
   12691 		      rel->r_addend = toc_addend;
   12692 		    }
   12693 		  r_type = R_PPC64_TPREL16_HA;
   12694 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12695 		  if (offset != (bfd_vma) -1)
   12696 		    {
   12697 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   12698 						    R_PPC64_TPREL16_LO);
   12699 		      rel[1].r_offset = offset + d_offset;
   12700 		      rel[1].r_addend = rel->r_addend;
   12701 		    }
   12702 		}
   12703 	      bfd_put_32 (output_bfd, insn1,
   12704 			  contents + rel->r_offset - d_offset);
   12705 	      if (offset != (bfd_vma) -1)
   12706 		{
   12707 		  insn3 = bfd_get_32 (output_bfd,
   12708 				      contents + offset + 4);
   12709 		  if (insn3 == NOP
   12710 		      || insn3 == CROR_151515 || insn3 == CROR_313131)
   12711 		    {
   12712 		      rel[1].r_offset += 4;
   12713 		      bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12714 		      insn2 = NOP;
   12715 		    }
   12716 		  bfd_put_32 (output_bfd, insn2, contents + offset);
   12717 		}
   12718 	      if ((tls_mask & tls_gd) == 0
   12719 		  && (tls_gd == 0 || toc_symndx != 0))
   12720 		{
   12721 		  /* We changed the symbol.  Start over in order
   12722 		     to get h, sym, sec etc. right.  */
   12723 		  rel--;
   12724 		  continue;
   12725 		}
   12726 	    }
   12727 	  break;
   12728 
   12729 	case R_PPC64_TLSGD:
   12730 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12731 	    {
   12732 	      unsigned int insn2, insn3;
   12733 	      bfd_vma offset = rel->r_offset;
   12734 
   12735 	      if ((tls_mask & TLS_TPRELGD) != 0)
   12736 		{
   12737 		  /* IE */
   12738 		  r_type = R_PPC64_NONE;
   12739 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12740 		}
   12741 	      else
   12742 		{
   12743 		  /* LE */
   12744 		  if (toc_symndx != 0)
   12745 		    {
   12746 		      r_symndx = toc_symndx;
   12747 		      rel->r_addend = toc_addend;
   12748 		    }
   12749 		  r_type = R_PPC64_TPREL16_LO;
   12750 		  rel->r_offset = offset + d_offset;
   12751 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12752 		}
   12753 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12754 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12755 	      BFD_ASSERT (offset == rel[1].r_offset);
   12756 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12757 	      insn3 = bfd_get_32 (output_bfd,
   12758 				  contents + offset + 4);
   12759 	      if (insn3 == NOP
   12760 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12761 		{
   12762 		  rel->r_offset += 4;
   12763 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12764 		  insn2 = NOP;
   12765 		}
   12766 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12767 	      if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
   12768 		{
   12769 		  rel--;
   12770 		  continue;
   12771 		}
   12772 	    }
   12773 	  break;
   12774 
   12775 	case R_PPC64_TLSLD:
   12776 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12777 	    {
   12778 	      unsigned int insn2, insn3;
   12779 	      bfd_vma offset = rel->r_offset;
   12780 
   12781 	      if (toc_symndx)
   12782 		sec = local_sections[toc_symndx];
   12783 	      for (r_symndx = 0;
   12784 		   r_symndx < symtab_hdr->sh_info;
   12785 		   r_symndx++)
   12786 		if (local_sections[r_symndx] == sec)
   12787 		  break;
   12788 	      if (r_symndx >= symtab_hdr->sh_info)
   12789 		r_symndx = STN_UNDEF;
   12790 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12791 	      if (r_symndx != STN_UNDEF)
   12792 		rel->r_addend -= (local_syms[r_symndx].st_value
   12793 				  + sec->output_offset
   12794 				  + sec->output_section->vma);
   12795 
   12796 	      r_type = R_PPC64_TPREL16_LO;
   12797 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12798 	      rel->r_offset = offset + d_offset;
   12799 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12800 	      BFD_ASSERT (offset == rel[1].r_offset);
   12801 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12802 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   12803 	      insn3 = bfd_get_32 (output_bfd,
   12804 				  contents + offset + 4);
   12805 	      if (insn3 == NOP
   12806 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12807 		{
   12808 		  rel->r_offset += 4;
   12809 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12810 		  insn2 = NOP;
   12811 		}
   12812 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12813 	      rel--;
   12814 	      continue;
   12815 	    }
   12816 	  break;
   12817 
   12818 	case R_PPC64_DTPMOD64:
   12819 	  if (rel + 1 < relend
   12820 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   12821 	      && rel[1].r_offset == rel->r_offset + 8)
   12822 	    {
   12823 	      if ((tls_mask & TLS_GD) == 0)
   12824 		{
   12825 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   12826 		  if ((tls_mask & TLS_TPRELGD) != 0)
   12827 		    r_type = R_PPC64_TPREL64;
   12828 		  else
   12829 		    {
   12830 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12831 		      r_type = R_PPC64_NONE;
   12832 		    }
   12833 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12834 		}
   12835 	    }
   12836 	  else
   12837 	    {
   12838 	      if ((tls_mask & TLS_LD) == 0)
   12839 		{
   12840 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12841 		  r_type = R_PPC64_NONE;
   12842 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12843 		}
   12844 	    }
   12845 	  break;
   12846 
   12847 	case R_PPC64_TPREL64:
   12848 	  if ((tls_mask & TLS_TPREL) == 0)
   12849 	    {
   12850 	      r_type = R_PPC64_NONE;
   12851 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12852 	    }
   12853 	  break;
   12854 	}
   12855 
   12856       /* Handle other relocations that tweak non-addend part of insn.  */
   12857       insn = 0;
   12858       max_br_offset = 1 << 25;
   12859       addend = rel->r_addend;
   12860       reloc_dest = DEST_NORMAL;
   12861       switch (r_type)
   12862 	{
   12863 	default:
   12864 	  break;
   12865 
   12866 	case R_PPC64_TOCSAVE:
   12867 	  if (relocation + addend == (rel->r_offset
   12868 				      + input_section->output_offset
   12869 				      + input_section->output_section->vma)
   12870 	      && tocsave_find (htab, NO_INSERT,
   12871 			       &local_syms, rel, input_bfd))
   12872 	    {
   12873 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12874 	      if (insn == NOP
   12875 		  || insn == CROR_151515 || insn == CROR_313131)
   12876 		bfd_put_32 (input_bfd, STD_R2_40R1,
   12877 			    contents + rel->r_offset);
   12878 	    }
   12879 	  break;
   12880 
   12881 	  /* Branch taken prediction relocations.  */
   12882 	case R_PPC64_ADDR14_BRTAKEN:
   12883 	case R_PPC64_REL14_BRTAKEN:
   12884 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   12885 	  /* Fall thru.  */
   12886 
   12887 	  /* Branch not taken prediction relocations.  */
   12888 	case R_PPC64_ADDR14_BRNTAKEN:
   12889 	case R_PPC64_REL14_BRNTAKEN:
   12890 	  insn |= bfd_get_32 (output_bfd,
   12891 			      contents + rel->r_offset) & ~(0x01 << 21);
   12892 	  /* Fall thru.  */
   12893 
   12894 	case R_PPC64_REL14:
   12895 	  max_br_offset = 1 << 15;
   12896 	  /* Fall thru.  */
   12897 
   12898 	case R_PPC64_REL24:
   12899 	  /* Calls to functions with a different TOC, such as calls to
   12900 	     shared objects, need to alter the TOC pointer.  This is
   12901 	     done using a linkage stub.  A REL24 branching to these
   12902 	     linkage stubs needs to be followed by a nop, as the nop
   12903 	     will be replaced with an instruction to restore the TOC
   12904 	     base pointer.  */
   12905 	  fdh = h;
   12906 	  if (h != NULL
   12907 	      && h->oh != NULL
   12908 	      && h->oh->is_func_descriptor)
   12909 	    fdh = ppc_follow_link (h->oh);
   12910 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
   12911 					   htab);
   12912 	  if (stub_entry != NULL
   12913 	      && (stub_entry->stub_type == ppc_stub_plt_call
   12914 		  || stub_entry->stub_type == ppc_stub_plt_call_r2save
   12915 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   12916 		  || stub_entry->stub_type == ppc_stub_long_branch_r2off))
   12917 	    {
   12918 	      bfd_boolean can_plt_call = FALSE;
   12919 
   12920 	      if (rel->r_offset + 8 <= input_section->size)
   12921 		{
   12922 		  unsigned long nop;
   12923 		  nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   12924 		  if (nop == NOP
   12925 		      || nop == CROR_151515 || nop == CROR_313131)
   12926 		    {
   12927 		      if (h != NULL
   12928 			  && (h == htab->tls_get_addr_fd
   12929 			      || h == htab->tls_get_addr)
   12930 			  && !htab->no_tls_get_addr_opt)
   12931 			{
   12932 			  /* Special stub used, leave nop alone.  */
   12933 			}
   12934 		      else
   12935 			bfd_put_32 (input_bfd, LD_R2_40R1,
   12936 				    contents + rel->r_offset + 4);
   12937 		      can_plt_call = TRUE;
   12938 		    }
   12939 		}
   12940 
   12941 	      if (!can_plt_call)
   12942 		{
   12943 		  if (stub_entry->stub_type == ppc_stub_plt_call
   12944 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   12945 		    {
   12946 		      /* If this is a plain branch rather than a branch
   12947 			 and link, don't require a nop.  However, don't
   12948 			 allow tail calls in a shared library as they
   12949 			 will result in r2 being corrupted.  */
   12950 		      unsigned long br;
   12951 		      br = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12952 		      if (info->executable && (br & 1) == 0)
   12953 			can_plt_call = TRUE;
   12954 		      else
   12955 			stub_entry = NULL;
   12956 		    }
   12957 		  else if (h != NULL
   12958 			   && strcmp (h->elf.root.root.string,
   12959 				      ".__libc_start_main") == 0)
   12960 		    {
   12961 		      /* Allow crt1 branch to go via a toc adjusting stub.  */
   12962 		      can_plt_call = TRUE;
   12963 		    }
   12964 		  else
   12965 		    {
   12966 		      info->callbacks->einfo
   12967 			(_("%P: %H: call to `%T' lacks nop, can't restore toc; "
   12968 			   "recompile with -fPIC"),
   12969 			   input_bfd, input_section, rel->r_offset, sym_name);
   12970 
   12971 		      bfd_set_error (bfd_error_bad_value);
   12972 		      ret = FALSE;
   12973 		    }
   12974 		}
   12975 
   12976 	      if (can_plt_call
   12977 		  && (stub_entry->stub_type == ppc_stub_plt_call
   12978 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save))
   12979 		unresolved_reloc = FALSE;
   12980 	    }
   12981 
   12982 	  if ((stub_entry == NULL
   12983 	       || stub_entry->stub_type == ppc_stub_long_branch
   12984 	       || stub_entry->stub_type == ppc_stub_plt_branch)
   12985 	      && get_opd_info (sec) != NULL)
   12986 	    {
   12987 	      /* The branch destination is the value of the opd entry. */
   12988 	      bfd_vma off = (relocation + addend
   12989 			     - sec->output_section->vma
   12990 			     - sec->output_offset);
   12991 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
   12992 	      if (dest != (bfd_vma) -1)
   12993 		{
   12994 		  relocation = dest;
   12995 		  addend = 0;
   12996 		  reloc_dest = DEST_OPD;
   12997 		}
   12998 	    }
   12999 
   13000 	  /* If the branch is out of reach we ought to have a long
   13001 	     branch stub.  */
   13002 	  from = (rel->r_offset
   13003 		  + input_section->output_offset
   13004 		  + input_section->output_section->vma);
   13005 
   13006 	  if (stub_entry != NULL
   13007 	      && (stub_entry->stub_type == ppc_stub_long_branch
   13008 		  || stub_entry->stub_type == ppc_stub_plt_branch)
   13009 	      && (r_type == R_PPC64_ADDR14_BRTAKEN
   13010 		  || r_type == R_PPC64_ADDR14_BRNTAKEN
   13011 		  || (relocation + addend - from + max_br_offset
   13012 		      < 2 * max_br_offset)))
   13013 	    /* Don't use the stub if this branch is in range.  */
   13014 	    stub_entry = NULL;
   13015 
   13016 	  if (stub_entry != NULL)
   13017 	    {
   13018 	      /* Munge up the value and addend so that we call the stub
   13019 		 rather than the procedure directly.  */
   13020 	      relocation = (stub_entry->stub_offset
   13021 			    + stub_entry->stub_sec->output_offset
   13022 			    + stub_entry->stub_sec->output_section->vma);
   13023 	      addend = 0;
   13024 	      reloc_dest = DEST_STUB;
   13025 
   13026  	      if ((stub_entry->stub_type == ppc_stub_plt_call
   13027 		   || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   13028 		  && (ALWAYS_EMIT_R2SAVE
   13029 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   13030 		  && rel + 1 < relend
   13031 		  && rel[1].r_offset == rel->r_offset + 4
   13032 		  && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
   13033 		relocation += 4;
   13034 	    }
   13035 
   13036 	  if (insn != 0)
   13037 	    {
   13038 	      if (is_isa_v2)
   13039 		{
   13040 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   13041 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   13042 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   13043 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   13044 		    insn |= 0x02 << 21;
   13045 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   13046 		    insn |= 0x08 << 21;
   13047 		  else
   13048 		    break;
   13049 		}
   13050 	      else
   13051 		{
   13052 		  /* Invert 'y' bit if not the default.  */
   13053 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   13054 		    insn ^= 0x01 << 21;
   13055 		}
   13056 
   13057 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   13058 	    }
   13059 
   13060 	  /* NOP out calls to undefined weak functions.
   13061 	     We can thus call a weak function without first
   13062 	     checking whether the function is defined.  */
   13063 	  else if (h != NULL
   13064 		   && h->elf.root.type == bfd_link_hash_undefweak
   13065 		   && h->elf.dynindx == -1
   13066 		   && r_type == R_PPC64_REL24
   13067 		   && relocation == 0
   13068 		   && addend == 0)
   13069 	    {
   13070 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   13071 	      continue;
   13072 	    }
   13073 	  break;
   13074 	}
   13075 
   13076       /* Set `addend'.  */
   13077       tls_type = 0;
   13078       switch (r_type)
   13079 	{
   13080 	default:
   13081 	  info->callbacks->einfo
   13082 	    (_("%P: %B: unknown relocation type %d for `%T'\n"),
   13083 	     input_bfd, (int) r_type, sym_name);
   13084 
   13085 	  bfd_set_error (bfd_error_bad_value);
   13086 	  ret = FALSE;
   13087 	  continue;
   13088 
   13089 	case R_PPC64_NONE:
   13090 	case R_PPC64_TLS:
   13091 	case R_PPC64_TLSGD:
   13092 	case R_PPC64_TLSLD:
   13093 	case R_PPC64_TOCSAVE:
   13094 	case R_PPC64_GNU_VTINHERIT:
   13095 	case R_PPC64_GNU_VTENTRY:
   13096 	  continue;
   13097 
   13098 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   13099 	     address in the GOT as relocation value instead of the
   13100 	     symbol's value itself.  Also, create a GOT entry for the
   13101 	     symbol and put the symbol value there.  */
   13102 	case R_PPC64_GOT_TLSGD16:
   13103 	case R_PPC64_GOT_TLSGD16_LO:
   13104 	case R_PPC64_GOT_TLSGD16_HI:
   13105 	case R_PPC64_GOT_TLSGD16_HA:
   13106 	  tls_type = TLS_TLS | TLS_GD;
   13107 	  goto dogot;
   13108 
   13109 	case R_PPC64_GOT_TLSLD16:
   13110 	case R_PPC64_GOT_TLSLD16_LO:
   13111 	case R_PPC64_GOT_TLSLD16_HI:
   13112 	case R_PPC64_GOT_TLSLD16_HA:
   13113 	  tls_type = TLS_TLS | TLS_LD;
   13114 	  goto dogot;
   13115 
   13116 	case R_PPC64_GOT_TPREL16_DS:
   13117 	case R_PPC64_GOT_TPREL16_LO_DS:
   13118 	case R_PPC64_GOT_TPREL16_HI:
   13119 	case R_PPC64_GOT_TPREL16_HA:
   13120 	  tls_type = TLS_TLS | TLS_TPREL;
   13121 	  goto dogot;
   13122 
   13123 	case R_PPC64_GOT_DTPREL16_DS:
   13124 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13125 	case R_PPC64_GOT_DTPREL16_HI:
   13126 	case R_PPC64_GOT_DTPREL16_HA:
   13127 	  tls_type = TLS_TLS | TLS_DTPREL;
   13128 	  goto dogot;
   13129 
   13130 	case R_PPC64_GOT16:
   13131 	case R_PPC64_GOT16_LO:
   13132 	case R_PPC64_GOT16_HI:
   13133 	case R_PPC64_GOT16_HA:
   13134 	case R_PPC64_GOT16_DS:
   13135 	case R_PPC64_GOT16_LO_DS:
   13136 	dogot:
   13137 	  {
   13138 	    /* Relocation is to the entry for this symbol in the global
   13139 	       offset table.  */
   13140 	    asection *got;
   13141 	    bfd_vma *offp;
   13142 	    bfd_vma off;
   13143 	    unsigned long indx = 0;
   13144 	    struct got_entry *ent;
   13145 
   13146 	    if (tls_type == (TLS_TLS | TLS_LD)
   13147 		&& (h == NULL
   13148 		    || !h->elf.def_dynamic))
   13149 	      ent = ppc64_tlsld_got (input_bfd);
   13150 	    else
   13151 	      {
   13152 
   13153 		if (h != NULL)
   13154 		  {
   13155 		    bfd_boolean dyn = htab->elf.dynamic_sections_created;
   13156 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
   13157 							  &h->elf)
   13158 			|| (info->shared
   13159 			    && SYMBOL_CALLS_LOCAL (info, &h->elf)))
   13160 		      /* This is actually a static link, or it is a
   13161 			 -Bsymbolic link and the symbol is defined
   13162 			 locally, or the symbol was forced to be local
   13163 			 because of a version file.  */
   13164 		      ;
   13165 		    else
   13166 		      {
   13167 			BFD_ASSERT (h->elf.dynindx != -1);
   13168 			indx = h->elf.dynindx;
   13169 			unresolved_reloc = FALSE;
   13170 		      }
   13171 		    ent = h->elf.got.glist;
   13172 		  }
   13173 		else
   13174 		  {
   13175 		    if (local_got_ents == NULL)
   13176 		      abort ();
   13177 		    ent = local_got_ents[r_symndx];
   13178 		  }
   13179 
   13180 		for (; ent != NULL; ent = ent->next)
   13181 		  if (ent->addend == orig_rel.r_addend
   13182 		      && ent->owner == input_bfd
   13183 		      && ent->tls_type == tls_type)
   13184 		    break;
   13185 	      }
   13186 
   13187 	    if (ent == NULL)
   13188 	      abort ();
   13189 	    if (ent->is_indirect)
   13190 	      ent = ent->got.ent;
   13191 	    offp = &ent->got.offset;
   13192 	    got = ppc64_elf_tdata (ent->owner)->got;
   13193 	    if (got == NULL)
   13194 	      abort ();
   13195 
   13196 	    /* The offset must always be a multiple of 8.  We use the
   13197 	       least significant bit to record whether we have already
   13198 	       processed this entry.  */
   13199 	    off = *offp;
   13200 	    if ((off & 1) != 0)
   13201 	      off &= ~1;
   13202 	    else
   13203 	      {
   13204 		/* Generate relocs for the dynamic linker, except in
   13205 		   the case of TLSLD where we'll use one entry per
   13206 		   module.  */
   13207 		asection *relgot;
   13208 		bfd_boolean ifunc;
   13209 
   13210 		*offp = off | 1;
   13211 		relgot = NULL;
   13212 		ifunc = (h != NULL
   13213 			 ? h->elf.type == STT_GNU_IFUNC
   13214 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   13215 		if ((info->shared || indx != 0)
   13216 		    && (h == NULL
   13217 			|| (tls_type == (TLS_TLS | TLS_LD)
   13218 			    && !h->elf.def_dynamic)
   13219 			|| ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   13220 			|| h->elf.root.type != bfd_link_hash_undefweak))
   13221 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   13222 		else if (ifunc)
   13223 		  relgot = htab->reliplt;
   13224 		if (relgot != NULL)
   13225 		  {
   13226 		    outrel.r_offset = (got->output_section->vma
   13227 				       + got->output_offset
   13228 				       + off);
   13229 		    outrel.r_addend = addend;
   13230 		    if (tls_type & (TLS_LD | TLS_GD))
   13231 		      {
   13232 			outrel.r_addend = 0;
   13233 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   13234 			if (tls_type == (TLS_TLS | TLS_GD))
   13235 			  {
   13236 			    loc = relgot->contents;
   13237 			    loc += (relgot->reloc_count++
   13238 				    * sizeof (Elf64_External_Rela));
   13239 			    bfd_elf64_swap_reloca_out (output_bfd,
   13240 						       &outrel, loc);
   13241 			    outrel.r_offset += 8;
   13242 			    outrel.r_addend = addend;
   13243 			    outrel.r_info
   13244 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   13245 			  }
   13246 		      }
   13247 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   13248 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   13249 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   13250 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   13251 		    else if (indx != 0)
   13252 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   13253 		    else
   13254 		      {
   13255 			if (ifunc)
   13256 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   13257 			else
   13258 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   13259 
   13260 			/* Write the .got section contents for the sake
   13261 			   of prelink.  */
   13262 			loc = got->contents + off;
   13263 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   13264 				    loc);
   13265 		      }
   13266 
   13267 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   13268 		      {
   13269 			outrel.r_addend += relocation;
   13270 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   13271 			  outrel.r_addend -= htab->elf.tls_sec->vma;
   13272 		      }
   13273 		    loc = relgot->contents;
   13274 		    loc += (relgot->reloc_count++
   13275 			    * sizeof (Elf64_External_Rela));
   13276 		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   13277 		  }
   13278 
   13279 		/* Init the .got section contents here if we're not
   13280 		   emitting a reloc.  */
   13281 		else
   13282 		  {
   13283 		    relocation += addend;
   13284 		    if (tls_type == (TLS_TLS | TLS_LD))
   13285 		      relocation = 1;
   13286 		    else if (tls_type != 0)
   13287 		      {
   13288 			relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   13289 			if (tls_type == (TLS_TLS | TLS_TPREL))
   13290 			  relocation += DTP_OFFSET - TP_OFFSET;
   13291 
   13292 			if (tls_type == (TLS_TLS | TLS_GD))
   13293 			  {
   13294 			    bfd_put_64 (output_bfd, relocation,
   13295 					got->contents + off + 8);
   13296 			    relocation = 1;
   13297 			  }
   13298 		      }
   13299 
   13300 		    bfd_put_64 (output_bfd, relocation,
   13301 				got->contents + off);
   13302 		  }
   13303 	      }
   13304 
   13305 	    if (off >= (bfd_vma) -2)
   13306 	      abort ();
   13307 
   13308 	    relocation = got->output_section->vma + got->output_offset + off;
   13309 	    addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
   13310 	  }
   13311 	  break;
   13312 
   13313 	case R_PPC64_PLT16_HA:
   13314 	case R_PPC64_PLT16_HI:
   13315 	case R_PPC64_PLT16_LO:
   13316 	case R_PPC64_PLT32:
   13317 	case R_PPC64_PLT64:
   13318 	  /* Relocation is to the entry for this symbol in the
   13319 	     procedure linkage table.  */
   13320 
   13321 	  /* Resolve a PLT reloc against a local symbol directly,
   13322 	     without using the procedure linkage table.  */
   13323 	  if (h == NULL)
   13324 	    break;
   13325 
   13326 	  /* It's possible that we didn't make a PLT entry for this
   13327 	     symbol.  This happens when statically linking PIC code,
   13328 	     or when using -Bsymbolic.  Go find a match if there is a
   13329 	     PLT entry.  */
   13330 	  if (htab->plt != NULL)
   13331 	    {
   13332 	      struct plt_entry *ent;
   13333 	      for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
   13334 		if (ent->addend == orig_rel.r_addend
   13335 		    && ent->plt.offset != (bfd_vma) -1)
   13336 		  {
   13337 		    relocation = (htab->plt->output_section->vma
   13338 				  + htab->plt->output_offset
   13339 				  + ent->plt.offset);
   13340 		    unresolved_reloc = FALSE;
   13341 		  }
   13342 	    }
   13343 	  break;
   13344 
   13345 	case R_PPC64_TOC:
   13346 	  /* Relocation value is TOC base.  */
   13347 	  relocation = TOCstart;
   13348 	  if (r_symndx == STN_UNDEF)
   13349 	    relocation += htab->stub_group[input_section->id].toc_off;
   13350 	  else if (unresolved_reloc)
   13351 	    ;
   13352 	  else if (sec != NULL && sec->id <= htab->top_id)
   13353 	    relocation += htab->stub_group[sec->id].toc_off;
   13354 	  else
   13355 	    unresolved_reloc = TRUE;
   13356 	  goto dodyn;
   13357 
   13358 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   13359 	     which is the address of the start of the TOC plus 0x8000.
   13360 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   13361 	     in this order.  */
   13362 	case R_PPC64_TOC16:
   13363 	case R_PPC64_TOC16_LO:
   13364 	case R_PPC64_TOC16_HI:
   13365 	case R_PPC64_TOC16_DS:
   13366 	case R_PPC64_TOC16_LO_DS:
   13367 	case R_PPC64_TOC16_HA:
   13368 	  addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
   13369 	  break;
   13370 
   13371 	  /* Relocate against the beginning of the section.  */
   13372 	case R_PPC64_SECTOFF:
   13373 	case R_PPC64_SECTOFF_LO:
   13374 	case R_PPC64_SECTOFF_HI:
   13375 	case R_PPC64_SECTOFF_DS:
   13376 	case R_PPC64_SECTOFF_LO_DS:
   13377 	case R_PPC64_SECTOFF_HA:
   13378 	  if (sec != NULL)
   13379 	    addend -= sec->output_section->vma;
   13380 	  break;
   13381 
   13382 	case R_PPC64_REL16:
   13383 	case R_PPC64_REL16_LO:
   13384 	case R_PPC64_REL16_HI:
   13385 	case R_PPC64_REL16_HA:
   13386 	  break;
   13387 
   13388 	case R_PPC64_REL14:
   13389 	case R_PPC64_REL14_BRNTAKEN:
   13390 	case R_PPC64_REL14_BRTAKEN:
   13391 	case R_PPC64_REL24:
   13392 	  break;
   13393 
   13394 	case R_PPC64_TPREL16:
   13395 	case R_PPC64_TPREL16_LO:
   13396 	case R_PPC64_TPREL16_HI:
   13397 	case R_PPC64_TPREL16_HA:
   13398 	case R_PPC64_TPREL16_DS:
   13399 	case R_PPC64_TPREL16_LO_DS:
   13400 	case R_PPC64_TPREL16_HIGHER:
   13401 	case R_PPC64_TPREL16_HIGHERA:
   13402 	case R_PPC64_TPREL16_HIGHEST:
   13403 	case R_PPC64_TPREL16_HIGHESTA:
   13404 	  if (h != NULL
   13405 	      && h->elf.root.type == bfd_link_hash_undefweak
   13406 	      && h->elf.dynindx == -1)
   13407 	    {
   13408 	      /* Make this relocation against an undefined weak symbol
   13409 		 resolve to zero.  This is really just a tweak, since
   13410 		 code using weak externs ought to check that they are
   13411 		 defined before using them.  */
   13412 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   13413 
   13414 	      insn = bfd_get_32 (output_bfd, p);
   13415 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   13416 	      if (insn != 0)
   13417 		bfd_put_32 (output_bfd, insn, p);
   13418 	      break;
   13419 	    }
   13420 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   13421 	  if (info->shared)
   13422 	    /* The TPREL16 relocs shouldn't really be used in shared
   13423 	       libs as they will result in DT_TEXTREL being set, but
   13424 	       support them anyway.  */
   13425 	    goto dodyn;
   13426 	  break;
   13427 
   13428 	case R_PPC64_DTPREL16:
   13429 	case R_PPC64_DTPREL16_LO:
   13430 	case R_PPC64_DTPREL16_HI:
   13431 	case R_PPC64_DTPREL16_HA:
   13432 	case R_PPC64_DTPREL16_DS:
   13433 	case R_PPC64_DTPREL16_LO_DS:
   13434 	case R_PPC64_DTPREL16_HIGHER:
   13435 	case R_PPC64_DTPREL16_HIGHERA:
   13436 	case R_PPC64_DTPREL16_HIGHEST:
   13437 	case R_PPC64_DTPREL16_HIGHESTA:
   13438 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   13439 	  break;
   13440 
   13441 	case R_PPC64_DTPMOD64:
   13442 	  relocation = 1;
   13443 	  addend = 0;
   13444 	  goto dodyn;
   13445 
   13446 	case R_PPC64_TPREL64:
   13447 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   13448 	  goto dodyn;
   13449 
   13450 	case R_PPC64_DTPREL64:
   13451 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   13452 	  /* Fall thru */
   13453 
   13454 	  /* Relocations that may need to be propagated if this is a
   13455 	     dynamic object.  */
   13456 	case R_PPC64_REL30:
   13457 	case R_PPC64_REL32:
   13458 	case R_PPC64_REL64:
   13459 	case R_PPC64_ADDR14:
   13460 	case R_PPC64_ADDR14_BRNTAKEN:
   13461 	case R_PPC64_ADDR14_BRTAKEN:
   13462 	case R_PPC64_ADDR16:
   13463 	case R_PPC64_ADDR16_DS:
   13464 	case R_PPC64_ADDR16_HA:
   13465 	case R_PPC64_ADDR16_HI:
   13466 	case R_PPC64_ADDR16_HIGHER:
   13467 	case R_PPC64_ADDR16_HIGHERA:
   13468 	case R_PPC64_ADDR16_HIGHEST:
   13469 	case R_PPC64_ADDR16_HIGHESTA:
   13470 	case R_PPC64_ADDR16_LO:
   13471 	case R_PPC64_ADDR16_LO_DS:
   13472 	case R_PPC64_ADDR24:
   13473 	case R_PPC64_ADDR32:
   13474 	case R_PPC64_ADDR64:
   13475 	case R_PPC64_UADDR16:
   13476 	case R_PPC64_UADDR32:
   13477 	case R_PPC64_UADDR64:
   13478 	dodyn:
   13479 	  if ((input_section->flags & SEC_ALLOC) == 0)
   13480 	    break;
   13481 
   13482 	  if (NO_OPD_RELOCS && is_opd)
   13483 	    break;
   13484 
   13485 	  if ((info->shared
   13486 	       && (h == NULL
   13487 		   || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   13488 		   || h->elf.root.type != bfd_link_hash_undefweak)
   13489 	       && (must_be_dyn_reloc (info, r_type)
   13490 		   || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
   13491 	      || (ELIMINATE_COPY_RELOCS
   13492 		  && !info->shared
   13493 		  && h != NULL
   13494 		  && h->elf.dynindx != -1
   13495 		  && !h->elf.non_got_ref
   13496 		  && !h->elf.def_regular)
   13497 	      || (!info->shared
   13498 		  && (h != NULL
   13499 		      ? h->elf.type == STT_GNU_IFUNC
   13500 		      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
   13501 	    {
   13502 	      bfd_boolean skip, relocate;
   13503 	      asection *sreloc;
   13504 	      bfd_vma out_off;
   13505 
   13506 	      /* When generating a dynamic object, these relocations
   13507 		 are copied into the output file to be resolved at run
   13508 		 time.  */
   13509 
   13510 	      skip = FALSE;
   13511 	      relocate = FALSE;
   13512 
   13513 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   13514 						 input_section, rel->r_offset);
   13515 	      if (out_off == (bfd_vma) -1)
   13516 		skip = TRUE;
   13517 	      else if (out_off == (bfd_vma) -2)
   13518 		skip = TRUE, relocate = TRUE;
   13519 	      out_off += (input_section->output_section->vma
   13520 			  + input_section->output_offset);
   13521 	      outrel.r_offset = out_off;
   13522 	      outrel.r_addend = rel->r_addend;
   13523 
   13524 	      /* Optimize unaligned reloc use.  */
   13525 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   13526 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   13527 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   13528 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   13529 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   13530 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   13531 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   13532 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   13533 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   13534 
   13535 	      if (skip)
   13536 		memset (&outrel, 0, sizeof outrel);
   13537 	      else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
   13538 		       && !is_opd
   13539 		       && r_type != R_PPC64_TOC)
   13540 		{
   13541 		  BFD_ASSERT (h->elf.dynindx != -1);
   13542 		  outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
   13543 		}
   13544 	      else
   13545 		{
   13546 		  /* This symbol is local, or marked to become local,
   13547 		     or this is an opd section reloc which must point
   13548 		     at a local function.  */
   13549 		  outrel.r_addend += relocation;
   13550 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   13551 		    {
   13552 		      if (is_opd && h != NULL)
   13553 			{
   13554 			  /* Lie about opd entries.  This case occurs
   13555 			     when building shared libraries and we
   13556 			     reference a function in another shared
   13557 			     lib.  The same thing happens for a weak
   13558 			     definition in an application that's
   13559 			     overridden by a strong definition in a
   13560 			     shared lib.  (I believe this is a generic
   13561 			     bug in binutils handling of weak syms.)
   13562 			     In these cases we won't use the opd
   13563 			     entry in this lib.  */
   13564 			  unresolved_reloc = FALSE;
   13565 			}
   13566 		      if (!is_opd
   13567 			  && r_type == R_PPC64_ADDR64
   13568 			  && (h != NULL
   13569 			      ? h->elf.type == STT_GNU_IFUNC
   13570 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   13571 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   13572 		      else
   13573 			{
   13574 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   13575 
   13576 			  /* We need to relocate .opd contents for ld.so.
   13577 			     Prelink also wants simple and consistent rules
   13578 			     for relocs.  This make all RELATIVE relocs have
   13579 			     *r_offset equal to r_addend.  */
   13580 			  relocate = TRUE;
   13581 			}
   13582 		    }
   13583 		  else
   13584 		    {
   13585 		      long indx = 0;
   13586 
   13587 		      if (h != NULL
   13588 			  ? h->elf.type == STT_GNU_IFUNC
   13589 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   13590 			{
   13591 			  info->callbacks->einfo
   13592 			    (_("%P: %H: %s for indirect "
   13593 			       "function `%T' unsupported\n"),
   13594 			     input_bfd, input_section, rel->r_offset,
   13595 			     ppc64_elf_howto_table[r_type]->name,
   13596 			     sym_name);
   13597 			  ret = FALSE;
   13598 			}
   13599 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   13600 			;
   13601 		      else if (sec == NULL || sec->owner == NULL)
   13602 			{
   13603 			  bfd_set_error (bfd_error_bad_value);
   13604 			  return FALSE;
   13605 			}
   13606 		      else
   13607 			{
   13608 			  asection *osec;
   13609 
   13610 			  osec = sec->output_section;
   13611 			  indx = elf_section_data (osec)->dynindx;
   13612 
   13613 			  if (indx == 0)
   13614 			    {
   13615 			      if ((osec->flags & SEC_READONLY) == 0
   13616 				  && htab->elf.data_index_section != NULL)
   13617 				osec = htab->elf.data_index_section;
   13618 			      else
   13619 				osec = htab->elf.text_index_section;
   13620 			      indx = elf_section_data (osec)->dynindx;
   13621 			    }
   13622 			  BFD_ASSERT (indx != 0);
   13623 
   13624 			  /* We are turning this relocation into one
   13625 			     against a section symbol, so subtract out
   13626 			     the output section's address but not the
   13627 			     offset of the input section in the output
   13628 			     section.  */
   13629 			  outrel.r_addend -= osec->vma;
   13630 			}
   13631 
   13632 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   13633 		    }
   13634 		}
   13635 
   13636 	      sreloc = elf_section_data (input_section)->sreloc;
   13637 	      if (!htab->elf.dynamic_sections_created)
   13638 		sreloc = htab->reliplt;
   13639 	      if (sreloc == NULL)
   13640 		abort ();
   13641 
   13642 	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   13643 		  >= sreloc->size)
   13644 		abort ();
   13645 	      loc = sreloc->contents;
   13646 	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   13647 	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   13648 
   13649 	      /* If this reloc is against an external symbol, it will
   13650 		 be computed at runtime, so there's no need to do
   13651 		 anything now.  However, for the sake of prelink ensure
   13652 		 that the section contents are a known value.  */
   13653 	      if (! relocate)
   13654 		{
   13655 		  unresolved_reloc = FALSE;
   13656 		  /* The value chosen here is quite arbitrary as ld.so
   13657 		     ignores section contents except for the special
   13658 		     case of .opd where the contents might be accessed
   13659 		     before relocation.  Choose zero, as that won't
   13660 		     cause reloc overflow.  */
   13661 		  relocation = 0;
   13662 		  addend = 0;
   13663 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   13664 		     to improve backward compatibility with older
   13665 		     versions of ld.  */
   13666 		  if (r_type == R_PPC64_ADDR64)
   13667 		    addend = outrel.r_addend;
   13668 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   13669 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   13670 		    addend = (input_section->output_section->vma
   13671 			      + input_section->output_offset
   13672 			      + rel->r_offset);
   13673 		}
   13674 	    }
   13675 	  break;
   13676 
   13677 	case R_PPC64_COPY:
   13678 	case R_PPC64_GLOB_DAT:
   13679 	case R_PPC64_JMP_SLOT:
   13680 	case R_PPC64_JMP_IREL:
   13681 	case R_PPC64_RELATIVE:
   13682 	  /* We shouldn't ever see these dynamic relocs in relocatable
   13683 	     files.  */
   13684 	  /* Fall through.  */
   13685 
   13686 	case R_PPC64_PLTGOT16:
   13687 	case R_PPC64_PLTGOT16_DS:
   13688 	case R_PPC64_PLTGOT16_HA:
   13689 	case R_PPC64_PLTGOT16_HI:
   13690 	case R_PPC64_PLTGOT16_LO:
   13691 	case R_PPC64_PLTGOT16_LO_DS:
   13692 	case R_PPC64_PLTREL32:
   13693 	case R_PPC64_PLTREL64:
   13694 	  /* These ones haven't been implemented yet.  */
   13695 
   13696 	  info->callbacks->einfo
   13697 	    (_("%P: %B: %s is not supported for `%T'\n"),
   13698 	     input_bfd,
   13699 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   13700 
   13701 	  bfd_set_error (bfd_error_invalid_operation);
   13702 	  ret = FALSE;
   13703 	  continue;
   13704 	}
   13705 
   13706       /* Multi-instruction sequences that access the TOC can be
   13707 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   13708 	 to             nop;           addi rb,r2,x;  */
   13709       switch (r_type)
   13710 	{
   13711 	default:
   13712 	  break;
   13713 
   13714 	case R_PPC64_GOT_TLSLD16_HI:
   13715 	case R_PPC64_GOT_TLSGD16_HI:
   13716 	case R_PPC64_GOT_TPREL16_HI:
   13717 	case R_PPC64_GOT_DTPREL16_HI:
   13718 	case R_PPC64_GOT16_HI:
   13719 	case R_PPC64_TOC16_HI:
   13720 	  /* These relocs would only be useful if building up an
   13721 	     offset to later add to r2, perhaps in an indexed
   13722 	     addressing mode instruction.  Don't try to optimize.
   13723 	     Unfortunately, the possibility of someone building up an
   13724 	     offset like this or even with the HA relocs, means that
   13725 	     we need to check the high insn when optimizing the low
   13726 	     insn.  */
   13727 	  break;
   13728 
   13729 	case R_PPC64_GOT_TLSLD16_HA:
   13730 	case R_PPC64_GOT_TLSGD16_HA:
   13731 	case R_PPC64_GOT_TPREL16_HA:
   13732 	case R_PPC64_GOT_DTPREL16_HA:
   13733 	case R_PPC64_GOT16_HA:
   13734 	case R_PPC64_TOC16_HA:
   13735 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   13736 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   13737 	    {
   13738 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   13739 	      bfd_put_32 (input_bfd, NOP, p);
   13740 	    }
   13741 	  break;
   13742 
   13743 	case R_PPC64_GOT_TLSLD16_LO:
   13744 	case R_PPC64_GOT_TLSGD16_LO:
   13745 	case R_PPC64_GOT_TPREL16_LO_DS:
   13746 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13747 	case R_PPC64_GOT16_LO:
   13748 	case R_PPC64_GOT16_LO_DS:
   13749 	case R_PPC64_TOC16_LO:
   13750 	case R_PPC64_TOC16_LO_DS:
   13751 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   13752 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   13753 	    {
   13754 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   13755 	      insn = bfd_get_32 (input_bfd, p);
   13756 	      if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
   13757 		{
   13758 		  /* Transform addic to addi when we change reg.  */
   13759 		  insn &= ~((0x3f << 26) | (0x1f << 16));
   13760 		  insn |= (14u << 26) | (2 << 16);
   13761 		}
   13762 	      else
   13763 		{
   13764 		  insn &= ~(0x1f << 16);
   13765 		  insn |= 2 << 16;
   13766 		}
   13767 	      bfd_put_32 (input_bfd, insn, p);
   13768 	    }
   13769 	  break;
   13770 	}
   13771 
   13772       /* Do any further special processing.  */
   13773       switch (r_type)
   13774 	{
   13775 	default:
   13776 	  break;
   13777 
   13778 	case R_PPC64_ADDR16_HA:
   13779 	case R_PPC64_REL16_HA:
   13780 	case R_PPC64_ADDR16_HIGHERA:
   13781 	case R_PPC64_ADDR16_HIGHESTA:
   13782 	case R_PPC64_TOC16_HA:
   13783 	case R_PPC64_SECTOFF_HA:
   13784 	case R_PPC64_TPREL16_HA:
   13785 	case R_PPC64_DTPREL16_HA:
   13786 	case R_PPC64_TPREL16_HIGHER:
   13787 	case R_PPC64_TPREL16_HIGHERA:
   13788 	case R_PPC64_TPREL16_HIGHEST:
   13789 	case R_PPC64_TPREL16_HIGHESTA:
   13790 	case R_PPC64_DTPREL16_HIGHER:
   13791 	case R_PPC64_DTPREL16_HIGHERA:
   13792 	case R_PPC64_DTPREL16_HIGHEST:
   13793 	case R_PPC64_DTPREL16_HIGHESTA:
   13794 	  /* It's just possible that this symbol is a weak symbol
   13795 	     that's not actually defined anywhere. In that case,
   13796 	     'sec' would be NULL, and we should leave the symbol
   13797 	     alone (it will be set to zero elsewhere in the link).  */
   13798 	  if (sec == NULL)
   13799 	    break;
   13800 	  /* Fall thru */
   13801 
   13802 	case R_PPC64_GOT16_HA:
   13803 	case R_PPC64_PLTGOT16_HA:
   13804 	case R_PPC64_PLT16_HA:
   13805 	case R_PPC64_GOT_TLSGD16_HA:
   13806 	case R_PPC64_GOT_TLSLD16_HA:
   13807 	case R_PPC64_GOT_TPREL16_HA:
   13808 	case R_PPC64_GOT_DTPREL16_HA:
   13809 	  /* Add 0x10000 if sign bit in 0:15 is set.
   13810 	     Bits 0:15 are not used.  */
   13811 	  addend += 0x8000;
   13812 	  break;
   13813 
   13814 	case R_PPC64_ADDR16_DS:
   13815 	case R_PPC64_ADDR16_LO_DS:
   13816 	case R_PPC64_GOT16_DS:
   13817 	case R_PPC64_GOT16_LO_DS:
   13818 	case R_PPC64_PLT16_LO_DS:
   13819 	case R_PPC64_SECTOFF_DS:
   13820 	case R_PPC64_SECTOFF_LO_DS:
   13821 	case R_PPC64_TOC16_DS:
   13822 	case R_PPC64_TOC16_LO_DS:
   13823 	case R_PPC64_PLTGOT16_DS:
   13824 	case R_PPC64_PLTGOT16_LO_DS:
   13825 	case R_PPC64_GOT_TPREL16_DS:
   13826 	case R_PPC64_GOT_TPREL16_LO_DS:
   13827 	case R_PPC64_GOT_DTPREL16_DS:
   13828 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13829 	case R_PPC64_TPREL16_DS:
   13830 	case R_PPC64_TPREL16_LO_DS:
   13831 	case R_PPC64_DTPREL16_DS:
   13832 	case R_PPC64_DTPREL16_LO_DS:
   13833 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   13834 	  mask = 3;
   13835 	  /* If this reloc is against an lq insn, then the value must be
   13836 	     a multiple of 16.  This is somewhat of a hack, but the
   13837 	     "correct" way to do this by defining _DQ forms of all the
   13838 	     _DS relocs bloats all reloc switches in this file.  It
   13839 	     doesn't seem to make much sense to use any of these relocs
   13840 	     in data, so testing the insn should be safe.  */
   13841 	  if ((insn & (0x3f << 26)) == (56u << 26))
   13842 	    mask = 15;
   13843 	  if (((relocation + addend) & mask) != 0)
   13844 	    {
   13845 	      info->callbacks->einfo
   13846 		(_("%P: %H: error: %s not a multiple of %u\n"),
   13847 		 input_bfd, input_section, rel->r_offset,
   13848 		 ppc64_elf_howto_table[r_type]->name,
   13849 		 mask + 1);
   13850 	      bfd_set_error (bfd_error_bad_value);
   13851 	      ret = FALSE;
   13852 	      continue;
   13853 	    }
   13854 	  break;
   13855 	}
   13856 
   13857       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   13858 	 because such sections are not SEC_ALLOC and thus ld.so will
   13859 	 not process them.  */
   13860       if (unresolved_reloc
   13861 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   13862 	       && h->elf.def_dynamic)
   13863 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   13864 				      rel->r_offset) != (bfd_vma) -1)
   13865 	{
   13866 	  info->callbacks->einfo
   13867 	    (_("%P: %H: unresolvable %s against `%T'\n"),
   13868 	     input_bfd, input_section, rel->r_offset,
   13869 	     ppc64_elf_howto_table[(int) r_type]->name,
   13870 	     h->elf.root.root.string);
   13871 	  ret = FALSE;
   13872 	}
   13873 
   13874       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
   13875 				    input_bfd,
   13876 				    input_section,
   13877 				    contents,
   13878 				    rel->r_offset,
   13879 				    relocation,
   13880 				    addend);
   13881 
   13882       if (r != bfd_reloc_ok)
   13883 	{
   13884 	  char *more_info = NULL;
   13885 	  const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
   13886 
   13887 	  if (reloc_dest != DEST_NORMAL)
   13888 	    {
   13889 	      more_info = bfd_malloc (strlen (reloc_name) + 8);
   13890 	      if (more_info != NULL)
   13891 		{
   13892 		  strcpy (more_info, reloc_name);
   13893 		  strcat (more_info, (reloc_dest == DEST_OPD
   13894 				      ? " (OPD)" : " (stub)"));
   13895 		  reloc_name = more_info;
   13896 		}
   13897 	    }
   13898 
   13899 	  if (r == bfd_reloc_overflow)
   13900 	    {
   13901 	      if (warned)
   13902 		continue;
   13903 	      if (h != NULL
   13904 		  && h->elf.root.type == bfd_link_hash_undefweak
   13905 		  && ppc64_elf_howto_table[r_type]->pc_relative)
   13906 		{
   13907 		  /* Assume this is a call protected by other code that
   13908 		     detects the symbol is undefined.  If this is the case,
   13909 		     we can safely ignore the overflow.  If not, the
   13910 		     program is hosed anyway, and a little warning isn't
   13911 		     going to help.  */
   13912 
   13913 		  continue;
   13914 		}
   13915 
   13916 	      if (!((*info->callbacks->reloc_overflow)
   13917 		    (info, &h->elf.root, sym_name,
   13918 		     reloc_name, orig_rel.r_addend,
   13919 		     input_bfd, input_section, rel->r_offset)))
   13920 		return FALSE;
   13921 	    }
   13922 	  else
   13923 	    {
   13924 	      info->callbacks->einfo
   13925 		(_("%P: %H: %s against `%T': error %d\n"),
   13926 		 input_bfd, input_section, rel->r_offset,
   13927 		 reloc_name, sym_name, (int) r);
   13928 	      ret = FALSE;
   13929 	    }
   13930 	  if (more_info != NULL)
   13931 	    free (more_info);
   13932 	}
   13933     }
   13934 
   13935   /* If we're emitting relocations, then shortly after this function
   13936      returns, reloc offsets and addends for this section will be
   13937      adjusted.  Worse, reloc symbol indices will be for the output
   13938      file rather than the input.  Save a copy of the relocs for
   13939      opd_entry_value.  */
   13940   if (is_opd && (info->emitrelocations || info->relocatable))
   13941     {
   13942       bfd_size_type amt;
   13943       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   13944       rel = bfd_alloc (input_bfd, amt);
   13945       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
   13946       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
   13947       if (rel == NULL)
   13948 	return FALSE;
   13949       memcpy (rel, relocs, amt);
   13950     }
   13951   return ret;
   13952 }
   13953 
   13954 /* Adjust the value of any local symbols in opd sections.  */
   13955 
   13956 static int
   13957 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   13958 			      const char *name ATTRIBUTE_UNUSED,
   13959 			      Elf_Internal_Sym *elfsym,
   13960 			      asection *input_sec,
   13961 			      struct elf_link_hash_entry *h)
   13962 {
   13963   struct _opd_sec_data *opd;
   13964   long adjust;
   13965   bfd_vma value;
   13966 
   13967   if (h != NULL)
   13968     return 1;
   13969 
   13970   opd = get_opd_info (input_sec);
   13971   if (opd == NULL || opd->adjust == NULL)
   13972     return 1;
   13973 
   13974   value = elfsym->st_value - input_sec->output_offset;
   13975   if (!info->relocatable)
   13976     value -= input_sec->output_section->vma;
   13977 
   13978   adjust = opd->adjust[value / 8];
   13979   if (adjust == -1)
   13980     return 2;
   13981 
   13982   elfsym->st_value += adjust;
   13983   return 1;
   13984 }
   13985 
   13986 /* Finish up dynamic symbol handling.  We set the contents of various
   13987    dynamic sections here.  */
   13988 
   13989 static bfd_boolean
   13990 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   13991 				 struct bfd_link_info *info,
   13992 				 struct elf_link_hash_entry *h,
   13993 				 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
   13994 {
   13995   struct ppc_link_hash_table *htab;
   13996   struct plt_entry *ent;
   13997   Elf_Internal_Rela rela;
   13998   bfd_byte *loc;
   13999 
   14000   htab = ppc_hash_table (info);
   14001   if (htab == NULL)
   14002     return FALSE;
   14003 
   14004   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   14005     if (ent->plt.offset != (bfd_vma) -1)
   14006       {
   14007 	/* This symbol has an entry in the procedure linkage
   14008 	   table.  Set it up.  */
   14009 	if (!htab->elf.dynamic_sections_created
   14010 	    || h->dynindx == -1)
   14011 	  {
   14012 	    BFD_ASSERT (h->type == STT_GNU_IFUNC
   14013 			&& h->def_regular
   14014 			&& (h->root.type == bfd_link_hash_defined
   14015 			    || h->root.type == bfd_link_hash_defweak));
   14016 	    rela.r_offset = (htab->iplt->output_section->vma
   14017 			     + htab->iplt->output_offset
   14018 			     + ent->plt.offset);
   14019 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   14020 	    rela.r_addend = (h->root.u.def.value
   14021 			     + h->root.u.def.section->output_offset
   14022 			     + h->root.u.def.section->output_section->vma
   14023 			     + ent->addend);
   14024 	    loc = (htab->reliplt->contents
   14025 		   + (htab->reliplt->reloc_count++
   14026 		      * sizeof (Elf64_External_Rela)));
   14027 	  }
   14028 	else
   14029 	  {
   14030 	    rela.r_offset = (htab->plt->output_section->vma
   14031 			     + htab->plt->output_offset
   14032 			     + ent->plt.offset);
   14033 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   14034 	    rela.r_addend = ent->addend;
   14035 	    loc = (htab->relplt->contents
   14036 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
   14037 		      / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
   14038 	  }
   14039 	bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   14040       }
   14041 
   14042   if (h->needs_copy)
   14043     {
   14044       /* This symbol needs a copy reloc.  Set it up.  */
   14045 
   14046       if (h->dynindx == -1
   14047 	  || (h->root.type != bfd_link_hash_defined
   14048 	      && h->root.type != bfd_link_hash_defweak)
   14049 	  || htab->relbss == NULL)
   14050 	abort ();
   14051 
   14052       rela.r_offset = (h->root.u.def.value
   14053 		       + h->root.u.def.section->output_section->vma
   14054 		       + h->root.u.def.section->output_offset);
   14055       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   14056       rela.r_addend = 0;
   14057       loc = htab->relbss->contents;
   14058       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
   14059       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   14060     }
   14061 
   14062   return TRUE;
   14063 }
   14064 
   14065 /* Used to decide how to sort relocs in an optimal manner for the
   14066    dynamic linker, before writing them out.  */
   14067 
   14068 static enum elf_reloc_type_class
   14069 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
   14070 {
   14071   enum elf_ppc64_reloc_type r_type;
   14072 
   14073   r_type = ELF64_R_TYPE (rela->r_info);
   14074   switch (r_type)
   14075     {
   14076     case R_PPC64_RELATIVE:
   14077       return reloc_class_relative;
   14078     case R_PPC64_JMP_SLOT:
   14079       return reloc_class_plt;
   14080     case R_PPC64_COPY:
   14081       return reloc_class_copy;
   14082     default:
   14083       return reloc_class_normal;
   14084     }
   14085 }
   14086 
   14087 /* Finish up the dynamic sections.  */
   14088 
   14089 static bfd_boolean
   14090 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   14091 				   struct bfd_link_info *info)
   14092 {
   14093   struct ppc_link_hash_table *htab;
   14094   bfd *dynobj;
   14095   asection *sdyn;
   14096 
   14097   htab = ppc_hash_table (info);
   14098   if (htab == NULL)
   14099     return FALSE;
   14100 
   14101   dynobj = htab->elf.dynobj;
   14102   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   14103 
   14104   if (htab->elf.dynamic_sections_created)
   14105     {
   14106       Elf64_External_Dyn *dyncon, *dynconend;
   14107 
   14108       if (sdyn == NULL || htab->got == NULL)
   14109 	abort ();
   14110 
   14111       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   14112       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   14113       for (; dyncon < dynconend; dyncon++)
   14114 	{
   14115 	  Elf_Internal_Dyn dyn;
   14116 	  asection *s;
   14117 
   14118 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   14119 
   14120 	  switch (dyn.d_tag)
   14121 	    {
   14122 	    default:
   14123 	      continue;
   14124 
   14125 	    case DT_PPC64_GLINK:
   14126 	      s = htab->glink;
   14127 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   14128 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   14129 		 of glink rather than the first entry point, which is
   14130 		 what ld.so needs, and now have a bigger stub to
   14131 		 support automatic multiple TOCs.  */
   14132 	      dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
   14133 	      break;
   14134 
   14135 	    case DT_PPC64_OPD:
   14136 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   14137 	      if (s == NULL)
   14138 		continue;
   14139 	      dyn.d_un.d_ptr = s->vma;
   14140 	      break;
   14141 
   14142 	    case DT_PPC64_OPDSZ:
   14143 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   14144 	      if (s == NULL)
   14145 		continue;
   14146 	      dyn.d_un.d_val = s->size;
   14147 	      break;
   14148 
   14149 	    case DT_PLTGOT:
   14150 	      s = htab->plt;
   14151 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   14152 	      break;
   14153 
   14154 	    case DT_JMPREL:
   14155 	      s = htab->relplt;
   14156 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   14157 	      break;
   14158 
   14159 	    case DT_PLTRELSZ:
   14160 	      dyn.d_un.d_val = htab->relplt->size;
   14161 	      break;
   14162 
   14163 	    case DT_RELASZ:
   14164 	      /* Don't count procedure linkage table relocs in the
   14165 		 overall reloc count.  */
   14166 	      s = htab->relplt;
   14167 	      if (s == NULL)
   14168 		continue;
   14169 	      dyn.d_un.d_val -= s->size;
   14170 	      break;
   14171 
   14172 	    case DT_RELA:
   14173 	      /* We may not be using the standard ELF linker script.
   14174 		 If .rela.plt is the first .rela section, we adjust
   14175 		 DT_RELA to not include it.  */
   14176 	      s = htab->relplt;
   14177 	      if (s == NULL)
   14178 		continue;
   14179 	      if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
   14180 		continue;
   14181 	      dyn.d_un.d_ptr += s->size;
   14182 	      break;
   14183 	    }
   14184 
   14185 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   14186 	}
   14187     }
   14188 
   14189   if (htab->got != NULL && htab->got->size != 0)
   14190     {
   14191       /* Fill in the first entry in the global offset table.
   14192 	 We use it to hold the link-time TOCbase.  */
   14193       bfd_put_64 (output_bfd,
   14194 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   14195 		  htab->got->contents);
   14196 
   14197       /* Set .got entry size.  */
   14198       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
   14199     }
   14200 
   14201   if (htab->plt != NULL && htab->plt->size != 0)
   14202     {
   14203       /* Set .plt entry size.  */
   14204       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
   14205 	= PLT_ENTRY_SIZE;
   14206     }
   14207 
   14208   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   14209      brlt ourselves if emitrelocations.  */
   14210   if (htab->brlt != NULL
   14211       && htab->brlt->reloc_count != 0
   14212       && !_bfd_elf_link_output_relocs (output_bfd,
   14213 				       htab->brlt,
   14214 				       elf_section_data (htab->brlt)->rela.hdr,
   14215 				       elf_section_data (htab->brlt)->relocs,
   14216 				       NULL))
   14217     return FALSE;
   14218 
   14219   if (htab->glink != NULL
   14220       && htab->glink->reloc_count != 0
   14221       && !_bfd_elf_link_output_relocs (output_bfd,
   14222 				       htab->glink,
   14223 				       elf_section_data (htab->glink)->rela.hdr,
   14224 				       elf_section_data (htab->glink)->relocs,
   14225 				       NULL))
   14226     return FALSE;
   14227 
   14228 
   14229   if (htab->glink_eh_frame != NULL
   14230       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   14231       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
   14232 					   htab->glink_eh_frame,
   14233 					   htab->glink_eh_frame->contents))
   14234     return FALSE;
   14235 
   14236   /* We need to handle writing out multiple GOT sections ourselves,
   14237      since we didn't add them to DYNOBJ.  We know dynobj is the first
   14238      bfd.  */
   14239   while ((dynobj = dynobj->link_next) != NULL)
   14240     {
   14241       asection *s;
   14242 
   14243       if (!is_ppc64_elf (dynobj))
   14244 	continue;
   14245 
   14246       s = ppc64_elf_tdata (dynobj)->got;
   14247       if (s != NULL
   14248 	  && s->size != 0
   14249 	  && s->output_section != bfd_abs_section_ptr
   14250 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   14251 					s->contents, s->output_offset,
   14252 					s->size))
   14253 	return FALSE;
   14254       s = ppc64_elf_tdata (dynobj)->relgot;
   14255       if (s != NULL
   14256 	  && s->size != 0
   14257 	  && s->output_section != bfd_abs_section_ptr
   14258 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   14259 					s->contents, s->output_offset,
   14260 					s->size))
   14261 	return FALSE;
   14262     }
   14263 
   14264   return TRUE;
   14265 }
   14266 
   14267 #include "elf64-target.h"
   14268 
   14269 /* FreeBSD support */
   14270 
   14271 #undef  TARGET_LITTLE_SYM
   14272 #undef  TARGET_LITTLE_NAME
   14273 
   14274 #undef  TARGET_BIG_SYM
   14275 #define TARGET_BIG_SYM	bfd_elf64_powerpc_freebsd_vec
   14276 #undef  TARGET_BIG_NAME
   14277 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
   14278 
   14279 #undef  ELF_OSABI
   14280 #define	ELF_OSABI       ELFOSABI_FREEBSD
   14281 
   14282 #undef  elf64_bed
   14283 #define elf64_bed	elf64_powerpc_fbsd_bed
   14284 
   14285 #include "elf64-target.h"
   14286 
   14287