Home | History | Annotate | Line # | Download | only in bfd
elf64-ppc.c revision 1.1
      1  1.1  christos /* PowerPC64-specific support for 64-bit ELF.
      2  1.1  christos    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
      3  1.1  christos    2009, 2010, 2011 Free Software Foundation, Inc.
      4  1.1  christos    Written by Linus Nordberg, Swox AB <info (at) swox.com>,
      5  1.1  christos    based on elf32-ppc.c by Ian Lance Taylor.
      6  1.1  christos    Largely rewritten by Alan Modra.
      7  1.1  christos 
      8  1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      9  1.1  christos 
     10  1.1  christos    This program is free software; you can redistribute it and/or modify
     11  1.1  christos    it under the terms of the GNU General Public License as published by
     12  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     13  1.1  christos    (at your option) any later version.
     14  1.1  christos 
     15  1.1  christos    This program is distributed in the hope that it will be useful,
     16  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  1.1  christos    GNU General Public License for more details.
     19  1.1  christos 
     20  1.1  christos    You should have received a copy of the GNU General Public License along
     21  1.1  christos    with this program; if not, write to the Free Software Foundation, Inc.,
     22  1.1  christos    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     23  1.1  christos 
     24  1.1  christos 
     25  1.1  christos /* The 64-bit PowerPC ELF ABI may be found at
     26  1.1  christos    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
     27  1.1  christos    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
     28  1.1  christos 
     29  1.1  christos #include "sysdep.h"
     30  1.1  christos #include <stdarg.h>
     31  1.1  christos #include "bfd.h"
     32  1.1  christos #include "bfdlink.h"
     33  1.1  christos #include "libbfd.h"
     34  1.1  christos #include "elf-bfd.h"
     35  1.1  christos #include "elf/ppc64.h"
     36  1.1  christos #include "elf64-ppc.h"
     37  1.1  christos 
     38  1.1  christos static bfd_reloc_status_type ppc64_elf_ha_reloc
     39  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     40  1.1  christos static bfd_reloc_status_type ppc64_elf_branch_reloc
     41  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     42  1.1  christos static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     43  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     44  1.1  christos static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     45  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     46  1.1  christos static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     47  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     48  1.1  christos static bfd_reloc_status_type ppc64_elf_toc_reloc
     49  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     50  1.1  christos static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     51  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     52  1.1  christos static bfd_reloc_status_type ppc64_elf_toc64_reloc
     53  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     54  1.1  christos static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     55  1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     56  1.1  christos static bfd_vma opd_entry_value
     57  1.1  christos   (asection *, bfd_vma, asection **, bfd_vma *);
     58  1.1  christos 
     59  1.1  christos #define TARGET_LITTLE_SYM	bfd_elf64_powerpcle_vec
     60  1.1  christos #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     61  1.1  christos #define TARGET_BIG_SYM		bfd_elf64_powerpc_vec
     62  1.1  christos #define TARGET_BIG_NAME		"elf64-powerpc"
     63  1.1  christos #define ELF_ARCH		bfd_arch_powerpc
     64  1.1  christos #define ELF_TARGET_ID		PPC64_ELF_DATA
     65  1.1  christos #define ELF_MACHINE_CODE	EM_PPC64
     66  1.1  christos #define ELF_MAXPAGESIZE		0x10000
     67  1.1  christos #define ELF_COMMONPAGESIZE	0x1000
     68  1.1  christos #define elf_info_to_howto	ppc64_elf_info_to_howto
     69  1.1  christos 
     70  1.1  christos #define elf_backend_want_got_sym 0
     71  1.1  christos #define elf_backend_want_plt_sym 0
     72  1.1  christos #define elf_backend_plt_alignment 3
     73  1.1  christos #define elf_backend_plt_not_loaded 1
     74  1.1  christos #define elf_backend_got_header_size 8
     75  1.1  christos #define elf_backend_can_gc_sections 1
     76  1.1  christos #define elf_backend_can_refcount 1
     77  1.1  christos #define elf_backend_rela_normal 1
     78  1.1  christos #define elf_backend_default_execstack 0
     79  1.1  christos 
     80  1.1  christos #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     81  1.1  christos #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     82  1.1  christos #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     83  1.1  christos #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
     84  1.1  christos #define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
     85  1.1  christos #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
     86  1.1  christos #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
     87  1.1  christos #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
     88  1.1  christos #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
     89  1.1  christos 
     90  1.1  christos #define elf_backend_object_p		      ppc64_elf_object_p
     91  1.1  christos #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
     92  1.1  christos #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
     93  1.1  christos #define elf_backend_write_core_note	      ppc64_elf_write_core_note
     94  1.1  christos #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
     95  1.1  christos #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
     96  1.1  christos #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
     97  1.1  christos #define elf_backend_check_directives	      ppc64_elf_process_dot_syms
     98  1.1  christos #define elf_backend_as_needed_cleanup	      ppc64_elf_as_needed_cleanup
     99  1.1  christos #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    100  1.1  christos #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    101  1.1  christos #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    102  1.1  christos #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    103  1.1  christos #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    104  1.1  christos #define elf_backend_gc_sweep_hook	      ppc64_elf_gc_sweep_hook
    105  1.1  christos #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    106  1.1  christos #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    107  1.1  christos #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
    108  1.1  christos #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    109  1.1  christos #define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
    110  1.1  christos #define elf_backend_action_discarded	      ppc64_elf_action_discarded
    111  1.1  christos #define elf_backend_relocate_section	      ppc64_elf_relocate_section
    112  1.1  christos #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
    113  1.1  christos #define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
    114  1.1  christos #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
    115  1.1  christos #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
    116  1.1  christos #define elf_backend_special_sections	      ppc64_elf_special_sections
    117  1.1  christos #define elf_backend_post_process_headers      _bfd_elf_set_osabi
    118  1.1  christos 
    119  1.1  christos /* The name of the dynamic interpreter.  This is put in the .interp
    120  1.1  christos    section.  */
    121  1.1  christos #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    122  1.1  christos 
    123  1.1  christos /* The size in bytes of an entry in the procedure linkage table.  */
    124  1.1  christos #define PLT_ENTRY_SIZE 24
    125  1.1  christos 
    126  1.1  christos /* The initial size of the plt reserved for the dynamic linker.  */
    127  1.1  christos #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
    128  1.1  christos 
    129  1.1  christos /* TOC base pointers offset from start of TOC.  */
    130  1.1  christos #define TOC_BASE_OFF	0x8000
    131  1.1  christos 
    132  1.1  christos /* Offset of tp and dtp pointers from start of TLS block.  */
    133  1.1  christos #define TP_OFFSET	0x7000
    134  1.1  christos #define DTP_OFFSET	0x8000
    135  1.1  christos 
    136  1.1  christos /* .plt call stub instructions.  The normal stub is like this, but
    137  1.1  christos    sometimes the .plt entry crosses a 64k boundary and we need to
    138  1.1  christos    insert an addi to adjust r12.  */
    139  1.1  christos #define PLT_CALL_STUB_SIZE (7*4)
    140  1.1  christos #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha     */
    141  1.1  christos #define STD_R2_40R1	0xf8410028	/* std	 %r2,40(%r1)	     */
    142  1.1  christos #define LD_R11_0R12	0xe96c0000	/* ld	 %r11,xxx+0@l(%r12)  */
    143  1.1  christos #define MTCTR_R11	0x7d6903a6	/* mtctr %r11		     */
    144  1.1  christos #define LD_R2_0R12	0xe84c0000	/* ld	 %r2,xxx+8@l(%r12)   */
    145  1.1  christos 					/* ld	 %r11,xxx+16@l(%r12) */
    146  1.1  christos #define BCTR		0x4e800420	/* bctr			     */
    147  1.1  christos 
    148  1.1  christos 
    149  1.1  christos #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,off@ha  */
    150  1.1  christos #define ADDI_R12_R12	0x398c0000	/* addi %r12,%r12,off@l  */
    151  1.1  christos #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha  */
    152  1.1  christos #define ADDI_R2_R2	0x38420000	/* addi  %r2,%r2,off@l   */
    153  1.1  christos 
    154  1.1  christos #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    155  1.1  christos #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)  */
    156  1.1  christos 
    157  1.1  christos #define LD_R2_40R1	0xe8410028	/* ld    %r2,40(%r1)     */
    158  1.1  christos 
    159  1.1  christos /* glink call stub instructions.  We enter with the index in R0.  */
    160  1.1  christos #define GLINK_CALL_STUB_SIZE (16*4)
    161  1.1  christos 					/* 0:				*/
    162  1.1  christos 					/*  .quad plt0-1f		*/
    163  1.1  christos 					/* __glink:			*/
    164  1.1  christos #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    165  1.1  christos #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    166  1.1  christos 					/* 1:				*/
    167  1.1  christos #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    168  1.1  christos #define LD_R2_M16R11	0xe84bfff0	/*  ld %2,(0b-1b)(%11)		*/
    169  1.1  christos #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    170  1.1  christos #define ADD_R12_R2_R11	0x7d825a14	/*  add %12,%2,%11		*/
    171  1.1  christos 					/*  ld %11,0(%12)		*/
    172  1.1  christos 					/*  ld %2,8(%12)		*/
    173  1.1  christos 					/*  mtctr %11			*/
    174  1.1  christos 					/*  ld %11,16(%12)		*/
    175  1.1  christos 					/*  bctr			*/
    176  1.1  christos 
    177  1.1  christos /* Pad with this.  */
    178  1.1  christos #define NOP		0x60000000
    179  1.1  christos 
    180  1.1  christos /* Some other nops.  */
    181  1.1  christos #define CROR_151515	0x4def7b82
    182  1.1  christos #define CROR_313131	0x4ffffb82
    183  1.1  christos 
    184  1.1  christos /* .glink entries for the first 32k functions are two instructions.  */
    185  1.1  christos #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    186  1.1  christos #define B_DOT		0x48000000	/* b     .		*/
    187  1.1  christos 
    188  1.1  christos /* After that, we need two instructions to load the index, followed by
    189  1.1  christos    a branch.  */
    190  1.1  christos #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    191  1.1  christos #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    192  1.1  christos 
    193  1.1  christos /* Instructions used by the save and restore reg functions.  */
    194  1.1  christos #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    195  1.1  christos #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    196  1.1  christos #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    197  1.1  christos #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    198  1.1  christos #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    199  1.1  christos #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    200  1.1  christos #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    201  1.1  christos #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    202  1.1  christos #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    203  1.1  christos #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    204  1.1  christos #define BLR		0x4e800020	/* blr			*/
    205  1.1  christos 
    206  1.1  christos /* Since .opd is an array of descriptors and each entry will end up
    207  1.1  christos    with identical R_PPC64_RELATIVE relocs, there is really no need to
    208  1.1  christos    propagate .opd relocs;  The dynamic linker should be taught to
    209  1.1  christos    relocate .opd without reloc entries.  */
    210  1.1  christos #ifndef NO_OPD_RELOCS
    211  1.1  christos #define NO_OPD_RELOCS 0
    212  1.1  christos #endif
    213  1.1  christos 
    214  1.1  christos #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
    216  1.1  christos 
    217  1.1  christos /* Relocation HOWTO's.  */
    218  1.1  christos static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    219  1.1  christos 
    220  1.1  christos static reloc_howto_type ppc64_elf_howto_raw[] = {
    221  1.1  christos   /* This reloc does nothing.  */
    222  1.1  christos   HOWTO (R_PPC64_NONE,		/* type */
    223  1.1  christos 	 0,			/* rightshift */
    224  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    225  1.1  christos 	 32,			/* bitsize */
    226  1.1  christos 	 FALSE,			/* pc_relative */
    227  1.1  christos 	 0,			/* bitpos */
    228  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    229  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    230  1.1  christos 	 "R_PPC64_NONE",	/* name */
    231  1.1  christos 	 FALSE,			/* partial_inplace */
    232  1.1  christos 	 0,			/* src_mask */
    233  1.1  christos 	 0,			/* dst_mask */
    234  1.1  christos 	 FALSE),		/* pcrel_offset */
    235  1.1  christos 
    236  1.1  christos   /* A standard 32 bit relocation.  */
    237  1.1  christos   HOWTO (R_PPC64_ADDR32,	/* type */
    238  1.1  christos 	 0,			/* rightshift */
    239  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    240  1.1  christos 	 32,			/* bitsize */
    241  1.1  christos 	 FALSE,			/* pc_relative */
    242  1.1  christos 	 0,			/* bitpos */
    243  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    244  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    245  1.1  christos 	 "R_PPC64_ADDR32",	/* name */
    246  1.1  christos 	 FALSE,			/* partial_inplace */
    247  1.1  christos 	 0,			/* src_mask */
    248  1.1  christos 	 0xffffffff,		/* dst_mask */
    249  1.1  christos 	 FALSE),		/* pcrel_offset */
    250  1.1  christos 
    251  1.1  christos   /* An absolute 26 bit branch; the lower two bits must be zero.
    252  1.1  christos      FIXME: we don't check that, we just clear them.  */
    253  1.1  christos   HOWTO (R_PPC64_ADDR24,	/* type */
    254  1.1  christos 	 0,			/* rightshift */
    255  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    256  1.1  christos 	 26,			/* bitsize */
    257  1.1  christos 	 FALSE,			/* pc_relative */
    258  1.1  christos 	 0,			/* bitpos */
    259  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    260  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    261  1.1  christos 	 "R_PPC64_ADDR24",	/* name */
    262  1.1  christos 	 FALSE,			/* partial_inplace */
    263  1.1  christos 	 0,			/* src_mask */
    264  1.1  christos 	 0x03fffffc,		/* dst_mask */
    265  1.1  christos 	 FALSE),		/* pcrel_offset */
    266  1.1  christos 
    267  1.1  christos   /* A standard 16 bit relocation.  */
    268  1.1  christos   HOWTO (R_PPC64_ADDR16,	/* type */
    269  1.1  christos 	 0,			/* rightshift */
    270  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    271  1.1  christos 	 16,			/* bitsize */
    272  1.1  christos 	 FALSE,			/* pc_relative */
    273  1.1  christos 	 0,			/* bitpos */
    274  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    275  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    276  1.1  christos 	 "R_PPC64_ADDR16",	/* name */
    277  1.1  christos 	 FALSE,			/* partial_inplace */
    278  1.1  christos 	 0,			/* src_mask */
    279  1.1  christos 	 0xffff,		/* dst_mask */
    280  1.1  christos 	 FALSE),		/* pcrel_offset */
    281  1.1  christos 
    282  1.1  christos   /* A 16 bit relocation without overflow.  */
    283  1.1  christos   HOWTO (R_PPC64_ADDR16_LO,	/* type */
    284  1.1  christos 	 0,			/* rightshift */
    285  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    286  1.1  christos 	 16,			/* bitsize */
    287  1.1  christos 	 FALSE,			/* pc_relative */
    288  1.1  christos 	 0,			/* bitpos */
    289  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
    290  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    291  1.1  christos 	 "R_PPC64_ADDR16_LO",	/* name */
    292  1.1  christos 	 FALSE,			/* partial_inplace */
    293  1.1  christos 	 0,			/* src_mask */
    294  1.1  christos 	 0xffff,		/* dst_mask */
    295  1.1  christos 	 FALSE),		/* pcrel_offset */
    296  1.1  christos 
    297  1.1  christos   /* Bits 16-31 of an address.  */
    298  1.1  christos   HOWTO (R_PPC64_ADDR16_HI,	/* type */
    299  1.1  christos 	 16,			/* rightshift */
    300  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    301  1.1  christos 	 16,			/* bitsize */
    302  1.1  christos 	 FALSE,			/* pc_relative */
    303  1.1  christos 	 0,			/* bitpos */
    304  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    305  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    306  1.1  christos 	 "R_PPC64_ADDR16_HI",	/* name */
    307  1.1  christos 	 FALSE,			/* partial_inplace */
    308  1.1  christos 	 0,			/* src_mask */
    309  1.1  christos 	 0xffff,		/* dst_mask */
    310  1.1  christos 	 FALSE),		/* pcrel_offset */
    311  1.1  christos 
    312  1.1  christos   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    313  1.1  christos      bits, treated as a signed number, is negative.  */
    314  1.1  christos   HOWTO (R_PPC64_ADDR16_HA,	/* type */
    315  1.1  christos 	 16,			/* rightshift */
    316  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    317  1.1  christos 	 16,			/* bitsize */
    318  1.1  christos 	 FALSE,			/* pc_relative */
    319  1.1  christos 	 0,			/* bitpos */
    320  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    321  1.1  christos 	 ppc64_elf_ha_reloc,	/* special_function */
    322  1.1  christos 	 "R_PPC64_ADDR16_HA",	/* name */
    323  1.1  christos 	 FALSE,			/* partial_inplace */
    324  1.1  christos 	 0,			/* src_mask */
    325  1.1  christos 	 0xffff,		/* dst_mask */
    326  1.1  christos 	 FALSE),		/* pcrel_offset */
    327  1.1  christos 
    328  1.1  christos   /* An absolute 16 bit branch; the lower two bits must be zero.
    329  1.1  christos      FIXME: we don't check that, we just clear them.  */
    330  1.1  christos   HOWTO (R_PPC64_ADDR14,	/* type */
    331  1.1  christos 	 0,			/* rightshift */
    332  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    333  1.1  christos 	 16,			/* bitsize */
    334  1.1  christos 	 FALSE,			/* pc_relative */
    335  1.1  christos 	 0,			/* bitpos */
    336  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    337  1.1  christos 	 ppc64_elf_branch_reloc, /* special_function */
    338  1.1  christos 	 "R_PPC64_ADDR14",	/* name */
    339  1.1  christos 	 FALSE,			/* partial_inplace */
    340  1.1  christos 	 0,			/* src_mask */
    341  1.1  christos 	 0x0000fffc,		/* dst_mask */
    342  1.1  christos 	 FALSE),		/* pcrel_offset */
    343  1.1  christos 
    344  1.1  christos   /* An absolute 16 bit branch, for which bit 10 should be set to
    345  1.1  christos      indicate that the branch is expected to be taken.  The lower two
    346  1.1  christos      bits must be zero.  */
    347  1.1  christos   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
    348  1.1  christos 	 0,			/* rightshift */
    349  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    350  1.1  christos 	 16,			/* bitsize */
    351  1.1  christos 	 FALSE,			/* pc_relative */
    352  1.1  christos 	 0,			/* bitpos */
    353  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    354  1.1  christos 	 ppc64_elf_brtaken_reloc, /* special_function */
    355  1.1  christos 	 "R_PPC64_ADDR14_BRTAKEN",/* name */
    356  1.1  christos 	 FALSE,			/* partial_inplace */
    357  1.1  christos 	 0,			/* src_mask */
    358  1.1  christos 	 0x0000fffc,		/* dst_mask */
    359  1.1  christos 	 FALSE),		/* pcrel_offset */
    360  1.1  christos 
    361  1.1  christos   /* An absolute 16 bit branch, for which bit 10 should be set to
    362  1.1  christos      indicate that the branch is not expected to be taken.  The lower
    363  1.1  christos      two bits must be zero.  */
    364  1.1  christos   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
    365  1.1  christos 	 0,			/* rightshift */
    366  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    367  1.1  christos 	 16,			/* bitsize */
    368  1.1  christos 	 FALSE,			/* pc_relative */
    369  1.1  christos 	 0,			/* bitpos */
    370  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    371  1.1  christos 	 ppc64_elf_brtaken_reloc, /* special_function */
    372  1.1  christos 	 "R_PPC64_ADDR14_BRNTAKEN",/* name */
    373  1.1  christos 	 FALSE,			/* partial_inplace */
    374  1.1  christos 	 0,			/* src_mask */
    375  1.1  christos 	 0x0000fffc,		/* dst_mask */
    376  1.1  christos 	 FALSE),		/* pcrel_offset */
    377  1.1  christos 
    378  1.1  christos   /* A relative 26 bit branch; the lower two bits must be zero.  */
    379  1.1  christos   HOWTO (R_PPC64_REL24,		/* type */
    380  1.1  christos 	 0,			/* rightshift */
    381  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    382  1.1  christos 	 26,			/* bitsize */
    383  1.1  christos 	 TRUE,			/* pc_relative */
    384  1.1  christos 	 0,			/* bitpos */
    385  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    386  1.1  christos 	 ppc64_elf_branch_reloc, /* special_function */
    387  1.1  christos 	 "R_PPC64_REL24",	/* name */
    388  1.1  christos 	 FALSE,			/* partial_inplace */
    389  1.1  christos 	 0,			/* src_mask */
    390  1.1  christos 	 0x03fffffc,		/* dst_mask */
    391  1.1  christos 	 TRUE),			/* pcrel_offset */
    392  1.1  christos 
    393  1.1  christos   /* A relative 16 bit branch; the lower two bits must be zero.  */
    394  1.1  christos   HOWTO (R_PPC64_REL14,		/* type */
    395  1.1  christos 	 0,			/* rightshift */
    396  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    397  1.1  christos 	 16,			/* bitsize */
    398  1.1  christos 	 TRUE,			/* pc_relative */
    399  1.1  christos 	 0,			/* bitpos */
    400  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    401  1.1  christos 	 ppc64_elf_branch_reloc, /* special_function */
    402  1.1  christos 	 "R_PPC64_REL14",	/* name */
    403  1.1  christos 	 FALSE,			/* partial_inplace */
    404  1.1  christos 	 0,			/* src_mask */
    405  1.1  christos 	 0x0000fffc,		/* dst_mask */
    406  1.1  christos 	 TRUE),			/* pcrel_offset */
    407  1.1  christos 
    408  1.1  christos   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    409  1.1  christos      the branch is expected to be taken.  The lower two bits must be
    410  1.1  christos      zero.  */
    411  1.1  christos   HOWTO (R_PPC64_REL14_BRTAKEN,	/* type */
    412  1.1  christos 	 0,			/* rightshift */
    413  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    414  1.1  christos 	 16,			/* bitsize */
    415  1.1  christos 	 TRUE,			/* pc_relative */
    416  1.1  christos 	 0,			/* bitpos */
    417  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    418  1.1  christos 	 ppc64_elf_brtaken_reloc, /* special_function */
    419  1.1  christos 	 "R_PPC64_REL14_BRTAKEN", /* name */
    420  1.1  christos 	 FALSE,			/* partial_inplace */
    421  1.1  christos 	 0,			/* src_mask */
    422  1.1  christos 	 0x0000fffc,		/* dst_mask */
    423  1.1  christos 	 TRUE),			/* pcrel_offset */
    424  1.1  christos 
    425  1.1  christos   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    426  1.1  christos      the branch is not expected to be taken.  The lower two bits must
    427  1.1  christos      be zero.  */
    428  1.1  christos   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
    429  1.1  christos 	 0,			/* rightshift */
    430  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    431  1.1  christos 	 16,			/* bitsize */
    432  1.1  christos 	 TRUE,			/* pc_relative */
    433  1.1  christos 	 0,			/* bitpos */
    434  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    435  1.1  christos 	 ppc64_elf_brtaken_reloc, /* special_function */
    436  1.1  christos 	 "R_PPC64_REL14_BRNTAKEN",/* name */
    437  1.1  christos 	 FALSE,			/* partial_inplace */
    438  1.1  christos 	 0,			/* src_mask */
    439  1.1  christos 	 0x0000fffc,		/* dst_mask */
    440  1.1  christos 	 TRUE),			/* pcrel_offset */
    441  1.1  christos 
    442  1.1  christos   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    443  1.1  christos      symbol.  */
    444  1.1  christos   HOWTO (R_PPC64_GOT16,		/* type */
    445  1.1  christos 	 0,			/* rightshift */
    446  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    447  1.1  christos 	 16,			/* bitsize */
    448  1.1  christos 	 FALSE,			/* pc_relative */
    449  1.1  christos 	 0,			/* bitpos */
    450  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    451  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    452  1.1  christos 	 "R_PPC64_GOT16",	/* name */
    453  1.1  christos 	 FALSE,			/* partial_inplace */
    454  1.1  christos 	 0,			/* src_mask */
    455  1.1  christos 	 0xffff,		/* dst_mask */
    456  1.1  christos 	 FALSE),		/* pcrel_offset */
    457  1.1  christos 
    458  1.1  christos   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    459  1.1  christos      the symbol.  */
    460  1.1  christos   HOWTO (R_PPC64_GOT16_LO,	/* type */
    461  1.1  christos 	 0,			/* rightshift */
    462  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    463  1.1  christos 	 16,			/* bitsize */
    464  1.1  christos 	 FALSE,			/* pc_relative */
    465  1.1  christos 	 0,			/* bitpos */
    466  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    467  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    468  1.1  christos 	 "R_PPC64_GOT16_LO",	/* name */
    469  1.1  christos 	 FALSE,			/* partial_inplace */
    470  1.1  christos 	 0,			/* src_mask */
    471  1.1  christos 	 0xffff,		/* dst_mask */
    472  1.1  christos 	 FALSE),		/* pcrel_offset */
    473  1.1  christos 
    474  1.1  christos   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    475  1.1  christos      the symbol.  */
    476  1.1  christos   HOWTO (R_PPC64_GOT16_HI,	/* type */
    477  1.1  christos 	 16,			/* rightshift */
    478  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    479  1.1  christos 	 16,			/* bitsize */
    480  1.1  christos 	 FALSE,			/* pc_relative */
    481  1.1  christos 	 0,			/* bitpos */
    482  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
    483  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    484  1.1  christos 	 "R_PPC64_GOT16_HI",	/* name */
    485  1.1  christos 	 FALSE,			/* partial_inplace */
    486  1.1  christos 	 0,			/* src_mask */
    487  1.1  christos 	 0xffff,		/* dst_mask */
    488  1.1  christos 	 FALSE),		/* pcrel_offset */
    489  1.1  christos 
    490  1.1  christos   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    491  1.1  christos      the symbol.  */
    492  1.1  christos   HOWTO (R_PPC64_GOT16_HA,	/* type */
    493  1.1  christos 	 16,			/* rightshift */
    494  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    495  1.1  christos 	 16,			/* bitsize */
    496  1.1  christos 	 FALSE,			/* pc_relative */
    497  1.1  christos 	 0,			/* bitpos */
    498  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
    499  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    500  1.1  christos 	 "R_PPC64_GOT16_HA",	/* name */
    501  1.1  christos 	 FALSE,			/* partial_inplace */
    502  1.1  christos 	 0,			/* src_mask */
    503  1.1  christos 	 0xffff,		/* dst_mask */
    504  1.1  christos 	 FALSE),		/* pcrel_offset */
    505  1.1  christos 
    506  1.1  christos   /* This is used only by the dynamic linker.  The symbol should exist
    507  1.1  christos      both in the object being run and in some shared library.  The
    508  1.1  christos      dynamic linker copies the data addressed by the symbol from the
    509  1.1  christos      shared library into the object, because the object being
    510  1.1  christos      run has to have the data at some particular address.  */
    511  1.1  christos   HOWTO (R_PPC64_COPY,		/* type */
    512  1.1  christos 	 0,			/* rightshift */
    513  1.1  christos 	 0,			/* this one is variable size */
    514  1.1  christos 	 0,			/* bitsize */
    515  1.1  christos 	 FALSE,			/* pc_relative */
    516  1.1  christos 	 0,			/* bitpos */
    517  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    518  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    519  1.1  christos 	 "R_PPC64_COPY",	/* name */
    520  1.1  christos 	 FALSE,			/* partial_inplace */
    521  1.1  christos 	 0,			/* src_mask */
    522  1.1  christos 	 0,			/* dst_mask */
    523  1.1  christos 	 FALSE),		/* pcrel_offset */
    524  1.1  christos 
    525  1.1  christos   /* Like R_PPC64_ADDR64, but used when setting global offset table
    526  1.1  christos      entries.  */
    527  1.1  christos   HOWTO (R_PPC64_GLOB_DAT,	/* type */
    528  1.1  christos 	 0,			/* rightshift */
    529  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    530  1.1  christos 	 64,			/* bitsize */
    531  1.1  christos 	 FALSE,			/* pc_relative */
    532  1.1  christos 	 0,			/* bitpos */
    533  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    534  1.1  christos 	 ppc64_elf_unhandled_reloc,  /* special_function */
    535  1.1  christos 	 "R_PPC64_GLOB_DAT",	/* name */
    536  1.1  christos 	 FALSE,			/* partial_inplace */
    537  1.1  christos 	 0,			/* src_mask */
    538  1.1  christos 	 ONES (64),		/* dst_mask */
    539  1.1  christos 	 FALSE),		/* pcrel_offset */
    540  1.1  christos 
    541  1.1  christos   /* Created by the link editor.  Marks a procedure linkage table
    542  1.1  christos      entry for a symbol.  */
    543  1.1  christos   HOWTO (R_PPC64_JMP_SLOT,	/* type */
    544  1.1  christos 	 0,			/* rightshift */
    545  1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    546  1.1  christos 	 0,			/* bitsize */
    547  1.1  christos 	 FALSE,			/* pc_relative */
    548  1.1  christos 	 0,			/* bitpos */
    549  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    550  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    551  1.1  christos 	 "R_PPC64_JMP_SLOT",	/* name */
    552  1.1  christos 	 FALSE,			/* partial_inplace */
    553  1.1  christos 	 0,			/* src_mask */
    554  1.1  christos 	 0,			/* dst_mask */
    555  1.1  christos 	 FALSE),		/* pcrel_offset */
    556  1.1  christos 
    557  1.1  christos   /* Used only by the dynamic linker.  When the object is run, this
    558  1.1  christos      doubleword64 is set to the load address of the object, plus the
    559  1.1  christos      addend.  */
    560  1.1  christos   HOWTO (R_PPC64_RELATIVE,	/* type */
    561  1.1  christos 	 0,			/* rightshift */
    562  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    563  1.1  christos 	 64,			/* bitsize */
    564  1.1  christos 	 FALSE,			/* pc_relative */
    565  1.1  christos 	 0,			/* bitpos */
    566  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    567  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    568  1.1  christos 	 "R_PPC64_RELATIVE",	/* name */
    569  1.1  christos 	 FALSE,			/* partial_inplace */
    570  1.1  christos 	 0,			/* src_mask */
    571  1.1  christos 	 ONES (64),		/* dst_mask */
    572  1.1  christos 	 FALSE),		/* pcrel_offset */
    573  1.1  christos 
    574  1.1  christos   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    575  1.1  christos   HOWTO (R_PPC64_UADDR32,	/* type */
    576  1.1  christos 	 0,			/* rightshift */
    577  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    578  1.1  christos 	 32,			/* bitsize */
    579  1.1  christos 	 FALSE,			/* pc_relative */
    580  1.1  christos 	 0,			/* bitpos */
    581  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    582  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    583  1.1  christos 	 "R_PPC64_UADDR32",	/* name */
    584  1.1  christos 	 FALSE,			/* partial_inplace */
    585  1.1  christos 	 0,			/* src_mask */
    586  1.1  christos 	 0xffffffff,		/* dst_mask */
    587  1.1  christos 	 FALSE),		/* pcrel_offset */
    588  1.1  christos 
    589  1.1  christos   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    590  1.1  christos   HOWTO (R_PPC64_UADDR16,	/* type */
    591  1.1  christos 	 0,			/* rightshift */
    592  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    593  1.1  christos 	 16,			/* bitsize */
    594  1.1  christos 	 FALSE,			/* pc_relative */
    595  1.1  christos 	 0,			/* bitpos */
    596  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    597  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    598  1.1  christos 	 "R_PPC64_UADDR16",	/* name */
    599  1.1  christos 	 FALSE,			/* partial_inplace */
    600  1.1  christos 	 0,			/* src_mask */
    601  1.1  christos 	 0xffff,		/* dst_mask */
    602  1.1  christos 	 FALSE),		/* pcrel_offset */
    603  1.1  christos 
    604  1.1  christos   /* 32-bit PC relative.  */
    605  1.1  christos   HOWTO (R_PPC64_REL32,		/* type */
    606  1.1  christos 	 0,			/* rightshift */
    607  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    608  1.1  christos 	 32,			/* bitsize */
    609  1.1  christos 	 TRUE,			/* pc_relative */
    610  1.1  christos 	 0,			/* bitpos */
    611  1.1  christos 	 /* FIXME: Verify.  Was complain_overflow_bitfield.  */
    612  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    613  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    614  1.1  christos 	 "R_PPC64_REL32",	/* name */
    615  1.1  christos 	 FALSE,			/* partial_inplace */
    616  1.1  christos 	 0,			/* src_mask */
    617  1.1  christos 	 0xffffffff,		/* dst_mask */
    618  1.1  christos 	 TRUE),			/* pcrel_offset */
    619  1.1  christos 
    620  1.1  christos   /* 32-bit relocation to the symbol's procedure linkage table.  */
    621  1.1  christos   HOWTO (R_PPC64_PLT32,		/* type */
    622  1.1  christos 	 0,			/* rightshift */
    623  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    624  1.1  christos 	 32,			/* bitsize */
    625  1.1  christos 	 FALSE,			/* pc_relative */
    626  1.1  christos 	 0,			/* bitpos */
    627  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    628  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    629  1.1  christos 	 "R_PPC64_PLT32",	/* name */
    630  1.1  christos 	 FALSE,			/* partial_inplace */
    631  1.1  christos 	 0,			/* src_mask */
    632  1.1  christos 	 0xffffffff,		/* dst_mask */
    633  1.1  christos 	 FALSE),		/* pcrel_offset */
    634  1.1  christos 
    635  1.1  christos   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    636  1.1  christos      FIXME: R_PPC64_PLTREL32 not supported.  */
    637  1.1  christos   HOWTO (R_PPC64_PLTREL32,	/* type */
    638  1.1  christos 	 0,			/* rightshift */
    639  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    640  1.1  christos 	 32,			/* bitsize */
    641  1.1  christos 	 TRUE,			/* pc_relative */
    642  1.1  christos 	 0,			/* bitpos */
    643  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    644  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    645  1.1  christos 	 "R_PPC64_PLTREL32",	/* name */
    646  1.1  christos 	 FALSE,			/* partial_inplace */
    647  1.1  christos 	 0,			/* src_mask */
    648  1.1  christos 	 0xffffffff,		/* dst_mask */
    649  1.1  christos 	 TRUE),			/* pcrel_offset */
    650  1.1  christos 
    651  1.1  christos   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    652  1.1  christos      the symbol.  */
    653  1.1  christos   HOWTO (R_PPC64_PLT16_LO,	/* type */
    654  1.1  christos 	 0,			/* rightshift */
    655  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    656  1.1  christos 	 16,			/* bitsize */
    657  1.1  christos 	 FALSE,			/* pc_relative */
    658  1.1  christos 	 0,			/* bitpos */
    659  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    660  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    661  1.1  christos 	 "R_PPC64_PLT16_LO",	/* name */
    662  1.1  christos 	 FALSE,			/* partial_inplace */
    663  1.1  christos 	 0,			/* src_mask */
    664  1.1  christos 	 0xffff,		/* dst_mask */
    665  1.1  christos 	 FALSE),		/* pcrel_offset */
    666  1.1  christos 
    667  1.1  christos   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    668  1.1  christos      the symbol.  */
    669  1.1  christos   HOWTO (R_PPC64_PLT16_HI,	/* type */
    670  1.1  christos 	 16,			/* rightshift */
    671  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    672  1.1  christos 	 16,			/* bitsize */
    673  1.1  christos 	 FALSE,			/* pc_relative */
    674  1.1  christos 	 0,			/* bitpos */
    675  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    676  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    677  1.1  christos 	 "R_PPC64_PLT16_HI",	/* name */
    678  1.1  christos 	 FALSE,			/* partial_inplace */
    679  1.1  christos 	 0,			/* src_mask */
    680  1.1  christos 	 0xffff,		/* dst_mask */
    681  1.1  christos 	 FALSE),		/* pcrel_offset */
    682  1.1  christos 
    683  1.1  christos   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    684  1.1  christos      the symbol.  */
    685  1.1  christos   HOWTO (R_PPC64_PLT16_HA,	/* type */
    686  1.1  christos 	 16,			/* rightshift */
    687  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    688  1.1  christos 	 16,			/* bitsize */
    689  1.1  christos 	 FALSE,			/* pc_relative */
    690  1.1  christos 	 0,			/* bitpos */
    691  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    692  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    693  1.1  christos 	 "R_PPC64_PLT16_HA",	/* name */
    694  1.1  christos 	 FALSE,			/* partial_inplace */
    695  1.1  christos 	 0,			/* src_mask */
    696  1.1  christos 	 0xffff,		/* dst_mask */
    697  1.1  christos 	 FALSE),		/* pcrel_offset */
    698  1.1  christos 
    699  1.1  christos   /* 16-bit section relative relocation.  */
    700  1.1  christos   HOWTO (R_PPC64_SECTOFF,	/* type */
    701  1.1  christos 	 0,			/* rightshift */
    702  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    703  1.1  christos 	 16,			/* bitsize */
    704  1.1  christos 	 FALSE,			/* pc_relative */
    705  1.1  christos 	 0,			/* bitpos */
    706  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    707  1.1  christos 	 ppc64_elf_sectoff_reloc, /* special_function */
    708  1.1  christos 	 "R_PPC64_SECTOFF",	/* name */
    709  1.1  christos 	 FALSE,			/* partial_inplace */
    710  1.1  christos 	 0,			/* src_mask */
    711  1.1  christos 	 0xffff,		/* dst_mask */
    712  1.1  christos 	 FALSE),		/* pcrel_offset */
    713  1.1  christos 
    714  1.1  christos   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    715  1.1  christos   HOWTO (R_PPC64_SECTOFF_LO,	/* type */
    716  1.1  christos 	 0,			/* rightshift */
    717  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    718  1.1  christos 	 16,			/* bitsize */
    719  1.1  christos 	 FALSE,			/* pc_relative */
    720  1.1  christos 	 0,			/* bitpos */
    721  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    722  1.1  christos 	 ppc64_elf_sectoff_reloc, /* special_function */
    723  1.1  christos 	 "R_PPC64_SECTOFF_LO",	/* name */
    724  1.1  christos 	 FALSE,			/* partial_inplace */
    725  1.1  christos 	 0,			/* src_mask */
    726  1.1  christos 	 0xffff,		/* dst_mask */
    727  1.1  christos 	 FALSE),		/* pcrel_offset */
    728  1.1  christos 
    729  1.1  christos   /* 16-bit upper half section relative relocation.  */
    730  1.1  christos   HOWTO (R_PPC64_SECTOFF_HI,	/* type */
    731  1.1  christos 	 16,			/* rightshift */
    732  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    733  1.1  christos 	 16,			/* bitsize */
    734  1.1  christos 	 FALSE,			/* pc_relative */
    735  1.1  christos 	 0,			/* bitpos */
    736  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    737  1.1  christos 	 ppc64_elf_sectoff_reloc, /* special_function */
    738  1.1  christos 	 "R_PPC64_SECTOFF_HI",	/* name */
    739  1.1  christos 	 FALSE,			/* partial_inplace */
    740  1.1  christos 	 0,			/* src_mask */
    741  1.1  christos 	 0xffff,		/* dst_mask */
    742  1.1  christos 	 FALSE),		/* pcrel_offset */
    743  1.1  christos 
    744  1.1  christos   /* 16-bit upper half adjusted section relative relocation.  */
    745  1.1  christos   HOWTO (R_PPC64_SECTOFF_HA,	/* type */
    746  1.1  christos 	 16,			/* rightshift */
    747  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    748  1.1  christos 	 16,			/* bitsize */
    749  1.1  christos 	 FALSE,			/* pc_relative */
    750  1.1  christos 	 0,			/* bitpos */
    751  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    752  1.1  christos 	 ppc64_elf_sectoff_ha_reloc, /* special_function */
    753  1.1  christos 	 "R_PPC64_SECTOFF_HA",	/* name */
    754  1.1  christos 	 FALSE,			/* partial_inplace */
    755  1.1  christos 	 0,			/* src_mask */
    756  1.1  christos 	 0xffff,		/* dst_mask */
    757  1.1  christos 	 FALSE),		/* pcrel_offset */
    758  1.1  christos 
    759  1.1  christos   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    760  1.1  christos   HOWTO (R_PPC64_REL30,		/* type */
    761  1.1  christos 	 2,			/* rightshift */
    762  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    763  1.1  christos 	 30,			/* bitsize */
    764  1.1  christos 	 TRUE,			/* pc_relative */
    765  1.1  christos 	 0,			/* bitpos */
    766  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    767  1.1  christos 	 bfd_elf_generic_reloc, /* special_function */
    768  1.1  christos 	 "R_PPC64_REL30",	/* name */
    769  1.1  christos 	 FALSE,			/* partial_inplace */
    770  1.1  christos 	 0,			/* src_mask */
    771  1.1  christos 	 0xfffffffc,		/* dst_mask */
    772  1.1  christos 	 TRUE),			/* pcrel_offset */
    773  1.1  christos 
    774  1.1  christos   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    775  1.1  christos 
    776  1.1  christos   /* A standard 64-bit relocation.  */
    777  1.1  christos   HOWTO (R_PPC64_ADDR64,	/* type */
    778  1.1  christos 	 0,			/* rightshift */
    779  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    780  1.1  christos 	 64,			/* bitsize */
    781  1.1  christos 	 FALSE,			/* pc_relative */
    782  1.1  christos 	 0,			/* bitpos */
    783  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    784  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    785  1.1  christos 	 "R_PPC64_ADDR64",	/* name */
    786  1.1  christos 	 FALSE,			/* partial_inplace */
    787  1.1  christos 	 0,			/* src_mask */
    788  1.1  christos 	 ONES (64),		/* dst_mask */
    789  1.1  christos 	 FALSE),		/* pcrel_offset */
    790  1.1  christos 
    791  1.1  christos   /* The bits 32-47 of an address.  */
    792  1.1  christos   HOWTO (R_PPC64_ADDR16_HIGHER,	/* type */
    793  1.1  christos 	 32,			/* rightshift */
    794  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    795  1.1  christos 	 16,			/* bitsize */
    796  1.1  christos 	 FALSE,			/* pc_relative */
    797  1.1  christos 	 0,			/* bitpos */
    798  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    799  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    800  1.1  christos 	 "R_PPC64_ADDR16_HIGHER", /* name */
    801  1.1  christos 	 FALSE,			/* partial_inplace */
    802  1.1  christos 	 0,			/* src_mask */
    803  1.1  christos 	 0xffff,		/* dst_mask */
    804  1.1  christos 	 FALSE),		/* pcrel_offset */
    805  1.1  christos 
    806  1.1  christos   /* The bits 32-47 of an address, plus 1 if the contents of the low
    807  1.1  christos      16 bits, treated as a signed number, is negative.  */
    808  1.1  christos   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
    809  1.1  christos 	 32,			/* rightshift */
    810  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    811  1.1  christos 	 16,			/* bitsize */
    812  1.1  christos 	 FALSE,			/* pc_relative */
    813  1.1  christos 	 0,			/* bitpos */
    814  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    815  1.1  christos 	 ppc64_elf_ha_reloc,	/* special_function */
    816  1.1  christos 	 "R_PPC64_ADDR16_HIGHERA", /* name */
    817  1.1  christos 	 FALSE,			/* partial_inplace */
    818  1.1  christos 	 0,			/* src_mask */
    819  1.1  christos 	 0xffff,		/* dst_mask */
    820  1.1  christos 	 FALSE),		/* pcrel_offset */
    821  1.1  christos 
    822  1.1  christos   /* The bits 48-63 of an address.  */
    823  1.1  christos   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
    824  1.1  christos 	 48,			/* rightshift */
    825  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    826  1.1  christos 	 16,			/* bitsize */
    827  1.1  christos 	 FALSE,			/* pc_relative */
    828  1.1  christos 	 0,			/* bitpos */
    829  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    830  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    831  1.1  christos 	 "R_PPC64_ADDR16_HIGHEST", /* name */
    832  1.1  christos 	 FALSE,			/* partial_inplace */
    833  1.1  christos 	 0,			/* src_mask */
    834  1.1  christos 	 0xffff,		/* dst_mask */
    835  1.1  christos 	 FALSE),		/* pcrel_offset */
    836  1.1  christos 
    837  1.1  christos   /* The bits 48-63 of an address, plus 1 if the contents of the low
    838  1.1  christos      16 bits, treated as a signed number, is negative.  */
    839  1.1  christos   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
    840  1.1  christos 	 48,			/* rightshift */
    841  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    842  1.1  christos 	 16,			/* bitsize */
    843  1.1  christos 	 FALSE,			/* pc_relative */
    844  1.1  christos 	 0,			/* bitpos */
    845  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    846  1.1  christos 	 ppc64_elf_ha_reloc,	/* special_function */
    847  1.1  christos 	 "R_PPC64_ADDR16_HIGHESTA", /* name */
    848  1.1  christos 	 FALSE,			/* partial_inplace */
    849  1.1  christos 	 0,			/* src_mask */
    850  1.1  christos 	 0xffff,		/* dst_mask */
    851  1.1  christos 	 FALSE),		/* pcrel_offset */
    852  1.1  christos 
    853  1.1  christos   /* Like ADDR64, but may be unaligned.  */
    854  1.1  christos   HOWTO (R_PPC64_UADDR64,	/* type */
    855  1.1  christos 	 0,			/* rightshift */
    856  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    857  1.1  christos 	 64,			/* bitsize */
    858  1.1  christos 	 FALSE,			/* pc_relative */
    859  1.1  christos 	 0,			/* bitpos */
    860  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    861  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    862  1.1  christos 	 "R_PPC64_UADDR64",	/* name */
    863  1.1  christos 	 FALSE,			/* partial_inplace */
    864  1.1  christos 	 0,			/* src_mask */
    865  1.1  christos 	 ONES (64),		/* dst_mask */
    866  1.1  christos 	 FALSE),		/* pcrel_offset */
    867  1.1  christos 
    868  1.1  christos   /* 64-bit relative relocation.  */
    869  1.1  christos   HOWTO (R_PPC64_REL64,		/* type */
    870  1.1  christos 	 0,			/* rightshift */
    871  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    872  1.1  christos 	 64,			/* bitsize */
    873  1.1  christos 	 TRUE,			/* pc_relative */
    874  1.1  christos 	 0,			/* bitpos */
    875  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    876  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
    877  1.1  christos 	 "R_PPC64_REL64",	/* name */
    878  1.1  christos 	 FALSE,			/* partial_inplace */
    879  1.1  christos 	 0,			/* src_mask */
    880  1.1  christos 	 ONES (64),		/* dst_mask */
    881  1.1  christos 	 TRUE),			/* pcrel_offset */
    882  1.1  christos 
    883  1.1  christos   /* 64-bit relocation to the symbol's procedure linkage table.  */
    884  1.1  christos   HOWTO (R_PPC64_PLT64,		/* type */
    885  1.1  christos 	 0,			/* rightshift */
    886  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    887  1.1  christos 	 64,			/* bitsize */
    888  1.1  christos 	 FALSE,			/* pc_relative */
    889  1.1  christos 	 0,			/* bitpos */
    890  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    891  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    892  1.1  christos 	 "R_PPC64_PLT64",	/* name */
    893  1.1  christos 	 FALSE,			/* partial_inplace */
    894  1.1  christos 	 0,			/* src_mask */
    895  1.1  christos 	 ONES (64),		/* dst_mask */
    896  1.1  christos 	 FALSE),		/* pcrel_offset */
    897  1.1  christos 
    898  1.1  christos   /* 64-bit PC relative relocation to the symbol's procedure linkage
    899  1.1  christos      table.  */
    900  1.1  christos   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    901  1.1  christos   HOWTO (R_PPC64_PLTREL64,	/* type */
    902  1.1  christos 	 0,			/* rightshift */
    903  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    904  1.1  christos 	 64,			/* bitsize */
    905  1.1  christos 	 TRUE,			/* pc_relative */
    906  1.1  christos 	 0,			/* bitpos */
    907  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    908  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
    909  1.1  christos 	 "R_PPC64_PLTREL64",	/* name */
    910  1.1  christos 	 FALSE,			/* partial_inplace */
    911  1.1  christos 	 0,			/* src_mask */
    912  1.1  christos 	 ONES (64),		/* dst_mask */
    913  1.1  christos 	 TRUE),			/* pcrel_offset */
    914  1.1  christos 
    915  1.1  christos   /* 16 bit TOC-relative relocation.  */
    916  1.1  christos 
    917  1.1  christos   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    918  1.1  christos   HOWTO (R_PPC64_TOC16,		/* type */
    919  1.1  christos 	 0,			/* rightshift */
    920  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    921  1.1  christos 	 16,			/* bitsize */
    922  1.1  christos 	 FALSE,			/* pc_relative */
    923  1.1  christos 	 0,			/* bitpos */
    924  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
    925  1.1  christos 	 ppc64_elf_toc_reloc,	/* special_function */
    926  1.1  christos 	 "R_PPC64_TOC16",	/* name */
    927  1.1  christos 	 FALSE,			/* partial_inplace */
    928  1.1  christos 	 0,			/* src_mask */
    929  1.1  christos 	 0xffff,		/* dst_mask */
    930  1.1  christos 	 FALSE),		/* pcrel_offset */
    931  1.1  christos 
    932  1.1  christos   /* 16 bit TOC-relative relocation without overflow.  */
    933  1.1  christos 
    934  1.1  christos   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    935  1.1  christos   HOWTO (R_PPC64_TOC16_LO,	/* type */
    936  1.1  christos 	 0,			/* rightshift */
    937  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    938  1.1  christos 	 16,			/* bitsize */
    939  1.1  christos 	 FALSE,			/* pc_relative */
    940  1.1  christos 	 0,			/* bitpos */
    941  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    942  1.1  christos 	 ppc64_elf_toc_reloc,	/* special_function */
    943  1.1  christos 	 "R_PPC64_TOC16_LO",	/* name */
    944  1.1  christos 	 FALSE,			/* partial_inplace */
    945  1.1  christos 	 0,			/* src_mask */
    946  1.1  christos 	 0xffff,		/* dst_mask */
    947  1.1  christos 	 FALSE),		/* pcrel_offset */
    948  1.1  christos 
    949  1.1  christos   /* 16 bit TOC-relative relocation, high 16 bits.  */
    950  1.1  christos 
    951  1.1  christos   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    952  1.1  christos   HOWTO (R_PPC64_TOC16_HI,	/* type */
    953  1.1  christos 	 16,			/* rightshift */
    954  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    955  1.1  christos 	 16,			/* bitsize */
    956  1.1  christos 	 FALSE,			/* pc_relative */
    957  1.1  christos 	 0,			/* bitpos */
    958  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    959  1.1  christos 	 ppc64_elf_toc_reloc,	/* special_function */
    960  1.1  christos 	 "R_PPC64_TOC16_HI",	/* name */
    961  1.1  christos 	 FALSE,			/* partial_inplace */
    962  1.1  christos 	 0,			/* src_mask */
    963  1.1  christos 	 0xffff,		/* dst_mask */
    964  1.1  christos 	 FALSE),		/* pcrel_offset */
    965  1.1  christos 
    966  1.1  christos   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
    967  1.1  christos      contents of the low 16 bits, treated as a signed number, is
    968  1.1  christos      negative.  */
    969  1.1  christos 
    970  1.1  christos   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
    971  1.1  christos   HOWTO (R_PPC64_TOC16_HA,	/* type */
    972  1.1  christos 	 16,			/* rightshift */
    973  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    974  1.1  christos 	 16,			/* bitsize */
    975  1.1  christos 	 FALSE,			/* pc_relative */
    976  1.1  christos 	 0,			/* bitpos */
    977  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    978  1.1  christos 	 ppc64_elf_toc_ha_reloc, /* special_function */
    979  1.1  christos 	 "R_PPC64_TOC16_HA",	/* name */
    980  1.1  christos 	 FALSE,			/* partial_inplace */
    981  1.1  christos 	 0,			/* src_mask */
    982  1.1  christos 	 0xffff,		/* dst_mask */
    983  1.1  christos 	 FALSE),		/* pcrel_offset */
    984  1.1  christos 
    985  1.1  christos   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
    986  1.1  christos 
    987  1.1  christos   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
    988  1.1  christos   HOWTO (R_PPC64_TOC,		/* type */
    989  1.1  christos 	 0,			/* rightshift */
    990  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    991  1.1  christos 	 64,			/* bitsize */
    992  1.1  christos 	 FALSE,			/* pc_relative */
    993  1.1  christos 	 0,			/* bitpos */
    994  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    995  1.1  christos 	 ppc64_elf_toc64_reloc,	/* special_function */
    996  1.1  christos 	 "R_PPC64_TOC",		/* name */
    997  1.1  christos 	 FALSE,			/* partial_inplace */
    998  1.1  christos 	 0,			/* src_mask */
    999  1.1  christos 	 ONES (64),		/* dst_mask */
   1000  1.1  christos 	 FALSE),		/* pcrel_offset */
   1001  1.1  christos 
   1002  1.1  christos   /* Like R_PPC64_GOT16, but also informs the link editor that the
   1003  1.1  christos      value to relocate may (!) refer to a PLT entry which the link
   1004  1.1  christos      editor (a) may replace with the symbol value.  If the link editor
   1005  1.1  christos      is unable to fully resolve the symbol, it may (b) create a PLT
   1006  1.1  christos      entry and store the address to the new PLT entry in the GOT.
   1007  1.1  christos      This permits lazy resolution of function symbols at run time.
   1008  1.1  christos      The link editor may also skip all of this and just (c) emit a
   1009  1.1  christos      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
   1010  1.1  christos   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
   1011  1.1  christos     HOWTO (R_PPC64_PLTGOT16,	/* type */
   1012  1.1  christos 	 0,			/* rightshift */
   1013  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1014  1.1  christos 	 16,			/* bitsize */
   1015  1.1  christos 	 FALSE,			/* pc_relative */
   1016  1.1  christos 	 0,			/* bitpos */
   1017  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1018  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1019  1.1  christos 	 "R_PPC64_PLTGOT16",	/* name */
   1020  1.1  christos 	 FALSE,			/* partial_inplace */
   1021  1.1  christos 	 0,			/* src_mask */
   1022  1.1  christos 	 0xffff,		/* dst_mask */
   1023  1.1  christos 	 FALSE),		/* pcrel_offset */
   1024  1.1  christos 
   1025  1.1  christos   /* Like R_PPC64_PLTGOT16, but without overflow.  */
   1026  1.1  christos   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1027  1.1  christos   HOWTO (R_PPC64_PLTGOT16_LO,	/* type */
   1028  1.1  christos 	 0,			/* rightshift */
   1029  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1030  1.1  christos 	 16,			/* bitsize */
   1031  1.1  christos 	 FALSE,			/* pc_relative */
   1032  1.1  christos 	 0,			/* bitpos */
   1033  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1034  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1035  1.1  christos 	 "R_PPC64_PLTGOT16_LO",	/* name */
   1036  1.1  christos 	 FALSE,			/* partial_inplace */
   1037  1.1  christos 	 0,			/* src_mask */
   1038  1.1  christos 	 0xffff,		/* dst_mask */
   1039  1.1  christos 	 FALSE),		/* pcrel_offset */
   1040  1.1  christos 
   1041  1.1  christos   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
   1042  1.1  christos   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
   1043  1.1  christos   HOWTO (R_PPC64_PLTGOT16_HI,	/* type */
   1044  1.1  christos 	 16,			/* rightshift */
   1045  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1046  1.1  christos 	 16,			/* bitsize */
   1047  1.1  christos 	 FALSE,			/* pc_relative */
   1048  1.1  christos 	 0,			/* bitpos */
   1049  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1050  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1051  1.1  christos 	 "R_PPC64_PLTGOT16_HI",	/* name */
   1052  1.1  christos 	 FALSE,			/* partial_inplace */
   1053  1.1  christos 	 0,			/* src_mask */
   1054  1.1  christos 	 0xffff,		/* dst_mask */
   1055  1.1  christos 	 FALSE),		/* pcrel_offset */
   1056  1.1  christos 
   1057  1.1  christos   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
   1058  1.1  christos      1 if the contents of the low 16 bits, treated as a signed number,
   1059  1.1  christos      is negative.  */
   1060  1.1  christos   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
   1061  1.1  christos   HOWTO (R_PPC64_PLTGOT16_HA,	/* type */
   1062  1.1  christos 	 16,			/* rightshift */
   1063  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1064  1.1  christos 	 16,			/* bitsize */
   1065  1.1  christos 	 FALSE,			/* pc_relative */
   1066  1.1  christos 	 0,			/* bitpos */
   1067  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
   1068  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1069  1.1  christos 	 "R_PPC64_PLTGOT16_HA",	/* name */
   1070  1.1  christos 	 FALSE,			/* partial_inplace */
   1071  1.1  christos 	 0,			/* src_mask */
   1072  1.1  christos 	 0xffff,		/* dst_mask */
   1073  1.1  christos 	 FALSE),		/* pcrel_offset */
   1074  1.1  christos 
   1075  1.1  christos   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
   1076  1.1  christos   HOWTO (R_PPC64_ADDR16_DS,	/* type */
   1077  1.1  christos 	 0,			/* rightshift */
   1078  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1079  1.1  christos 	 16,			/* bitsize */
   1080  1.1  christos 	 FALSE,			/* pc_relative */
   1081  1.1  christos 	 0,			/* bitpos */
   1082  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
   1083  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1084  1.1  christos 	 "R_PPC64_ADDR16_DS",	/* name */
   1085  1.1  christos 	 FALSE,			/* partial_inplace */
   1086  1.1  christos 	 0,			/* src_mask */
   1087  1.1  christos 	 0xfffc,		/* dst_mask */
   1088  1.1  christos 	 FALSE),		/* pcrel_offset */
   1089  1.1  christos 
   1090  1.1  christos   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
   1091  1.1  christos   HOWTO (R_PPC64_ADDR16_LO_DS,	/* type */
   1092  1.1  christos 	 0,			/* rightshift */
   1093  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1094  1.1  christos 	 16,			/* bitsize */
   1095  1.1  christos 	 FALSE,			/* pc_relative */
   1096  1.1  christos 	 0,			/* bitpos */
   1097  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
   1098  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1099  1.1  christos 	 "R_PPC64_ADDR16_LO_DS",/* name */
   1100  1.1  christos 	 FALSE,			/* partial_inplace */
   1101  1.1  christos 	 0,			/* src_mask */
   1102  1.1  christos 	 0xfffc,		/* dst_mask */
   1103  1.1  christos 	 FALSE),		/* pcrel_offset */
   1104  1.1  christos 
   1105  1.1  christos   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
   1106  1.1  christos   HOWTO (R_PPC64_GOT16_DS,	/* type */
   1107  1.1  christos 	 0,			/* rightshift */
   1108  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1109  1.1  christos 	 16,			/* bitsize */
   1110  1.1  christos 	 FALSE,			/* pc_relative */
   1111  1.1  christos 	 0,			/* bitpos */
   1112  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1113  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1114  1.1  christos 	 "R_PPC64_GOT16_DS",	/* name */
   1115  1.1  christos 	 FALSE,			/* partial_inplace */
   1116  1.1  christos 	 0,			/* src_mask */
   1117  1.1  christos 	 0xfffc,		/* dst_mask */
   1118  1.1  christos 	 FALSE),		/* pcrel_offset */
   1119  1.1  christos 
   1120  1.1  christos   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
   1121  1.1  christos   HOWTO (R_PPC64_GOT16_LO_DS,	/* type */
   1122  1.1  christos 	 0,			/* rightshift */
   1123  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1124  1.1  christos 	 16,			/* bitsize */
   1125  1.1  christos 	 FALSE,			/* pc_relative */
   1126  1.1  christos 	 0,			/* bitpos */
   1127  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1128  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1129  1.1  christos 	 "R_PPC64_GOT16_LO_DS",	/* name */
   1130  1.1  christos 	 FALSE,			/* partial_inplace */
   1131  1.1  christos 	 0,			/* src_mask */
   1132  1.1  christos 	 0xfffc,		/* dst_mask */
   1133  1.1  christos 	 FALSE),		/* pcrel_offset */
   1134  1.1  christos 
   1135  1.1  christos   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
   1136  1.1  christos   HOWTO (R_PPC64_PLT16_LO_DS,	/* type */
   1137  1.1  christos 	 0,			/* rightshift */
   1138  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1139  1.1  christos 	 16,			/* bitsize */
   1140  1.1  christos 	 FALSE,			/* pc_relative */
   1141  1.1  christos 	 0,			/* bitpos */
   1142  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1143  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1144  1.1  christos 	 "R_PPC64_PLT16_LO_DS",	/* name */
   1145  1.1  christos 	 FALSE,			/* partial_inplace */
   1146  1.1  christos 	 0,			/* src_mask */
   1147  1.1  christos 	 0xfffc,		/* dst_mask */
   1148  1.1  christos 	 FALSE),		/* pcrel_offset */
   1149  1.1  christos 
   1150  1.1  christos   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
   1151  1.1  christos   HOWTO (R_PPC64_SECTOFF_DS,	/* type */
   1152  1.1  christos 	 0,			/* rightshift */
   1153  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1154  1.1  christos 	 16,			/* bitsize */
   1155  1.1  christos 	 FALSE,			/* pc_relative */
   1156  1.1  christos 	 0,			/* bitpos */
   1157  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
   1158  1.1  christos 	 ppc64_elf_sectoff_reloc, /* special_function */
   1159  1.1  christos 	 "R_PPC64_SECTOFF_DS",	/* name */
   1160  1.1  christos 	 FALSE,			/* partial_inplace */
   1161  1.1  christos 	 0,			/* src_mask */
   1162  1.1  christos 	 0xfffc,		/* dst_mask */
   1163  1.1  christos 	 FALSE),		/* pcrel_offset */
   1164  1.1  christos 
   1165  1.1  christos   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
   1166  1.1  christos   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
   1167  1.1  christos 	 0,			/* rightshift */
   1168  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1169  1.1  christos 	 16,			/* bitsize */
   1170  1.1  christos 	 FALSE,			/* pc_relative */
   1171  1.1  christos 	 0,			/* bitpos */
   1172  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1173  1.1  christos 	 ppc64_elf_sectoff_reloc, /* special_function */
   1174  1.1  christos 	 "R_PPC64_SECTOFF_LO_DS",/* name */
   1175  1.1  christos 	 FALSE,			/* partial_inplace */
   1176  1.1  christos 	 0,			/* src_mask */
   1177  1.1  christos 	 0xfffc,		/* dst_mask */
   1178  1.1  christos 	 FALSE),		/* pcrel_offset */
   1179  1.1  christos 
   1180  1.1  christos   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
   1181  1.1  christos   HOWTO (R_PPC64_TOC16_DS,	/* type */
   1182  1.1  christos 	 0,			/* rightshift */
   1183  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1184  1.1  christos 	 16,			/* bitsize */
   1185  1.1  christos 	 FALSE,			/* pc_relative */
   1186  1.1  christos 	 0,			/* bitpos */
   1187  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1188  1.1  christos 	 ppc64_elf_toc_reloc,	/* special_function */
   1189  1.1  christos 	 "R_PPC64_TOC16_DS",	/* name */
   1190  1.1  christos 	 FALSE,			/* partial_inplace */
   1191  1.1  christos 	 0,			/* src_mask */
   1192  1.1  christos 	 0xfffc,		/* dst_mask */
   1193  1.1  christos 	 FALSE),		/* pcrel_offset */
   1194  1.1  christos 
   1195  1.1  christos   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
   1196  1.1  christos   HOWTO (R_PPC64_TOC16_LO_DS,	/* type */
   1197  1.1  christos 	 0,			/* rightshift */
   1198  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1199  1.1  christos 	 16,			/* bitsize */
   1200  1.1  christos 	 FALSE,			/* pc_relative */
   1201  1.1  christos 	 0,			/* bitpos */
   1202  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1203  1.1  christos 	 ppc64_elf_toc_reloc,	/* special_function */
   1204  1.1  christos 	 "R_PPC64_TOC16_LO_DS",	/* name */
   1205  1.1  christos 	 FALSE,			/* partial_inplace */
   1206  1.1  christos 	 0,			/* src_mask */
   1207  1.1  christos 	 0xfffc,		/* dst_mask */
   1208  1.1  christos 	 FALSE),		/* pcrel_offset */
   1209  1.1  christos 
   1210  1.1  christos   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
   1211  1.1  christos   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
   1212  1.1  christos   HOWTO (R_PPC64_PLTGOT16_DS,	/* type */
   1213  1.1  christos 	 0,			/* rightshift */
   1214  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1215  1.1  christos 	 16,			/* bitsize */
   1216  1.1  christos 	 FALSE,			/* pc_relative */
   1217  1.1  christos 	 0,			/* bitpos */
   1218  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1219  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1220  1.1  christos 	 "R_PPC64_PLTGOT16_DS",	/* name */
   1221  1.1  christos 	 FALSE,			/* partial_inplace */
   1222  1.1  christos 	 0,			/* src_mask */
   1223  1.1  christos 	 0xfffc,		/* dst_mask */
   1224  1.1  christos 	 FALSE),		/* pcrel_offset */
   1225  1.1  christos 
   1226  1.1  christos   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
   1227  1.1  christos   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1228  1.1  christos   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
   1229  1.1  christos 	 0,			/* rightshift */
   1230  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1231  1.1  christos 	 16,			/* bitsize */
   1232  1.1  christos 	 FALSE,			/* pc_relative */
   1233  1.1  christos 	 0,			/* bitpos */
   1234  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1235  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1236  1.1  christos 	 "R_PPC64_PLTGOT16_LO_DS",/* name */
   1237  1.1  christos 	 FALSE,			/* partial_inplace */
   1238  1.1  christos 	 0,			/* src_mask */
   1239  1.1  christos 	 0xfffc,		/* dst_mask */
   1240  1.1  christos 	 FALSE),		/* pcrel_offset */
   1241  1.1  christos 
   1242  1.1  christos   /* Marker relocs for TLS.  */
   1243  1.1  christos   HOWTO (R_PPC64_TLS,
   1244  1.1  christos 	 0,			/* rightshift */
   1245  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1246  1.1  christos 	 32,			/* bitsize */
   1247  1.1  christos 	 FALSE,			/* pc_relative */
   1248  1.1  christos 	 0,			/* bitpos */
   1249  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1250  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1251  1.1  christos 	 "R_PPC64_TLS",		/* name */
   1252  1.1  christos 	 FALSE,			/* partial_inplace */
   1253  1.1  christos 	 0,			/* src_mask */
   1254  1.1  christos 	 0,			/* dst_mask */
   1255  1.1  christos 	 FALSE),		/* pcrel_offset */
   1256  1.1  christos 
   1257  1.1  christos   HOWTO (R_PPC64_TLSGD,
   1258  1.1  christos 	 0,			/* rightshift */
   1259  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1260  1.1  christos 	 32,			/* bitsize */
   1261  1.1  christos 	 FALSE,			/* pc_relative */
   1262  1.1  christos 	 0,			/* bitpos */
   1263  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1264  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1265  1.1  christos 	 "R_PPC64_TLSGD",	/* name */
   1266  1.1  christos 	 FALSE,			/* partial_inplace */
   1267  1.1  christos 	 0,			/* src_mask */
   1268  1.1  christos 	 0,			/* dst_mask */
   1269  1.1  christos 	 FALSE),		/* pcrel_offset */
   1270  1.1  christos 
   1271  1.1  christos   HOWTO (R_PPC64_TLSLD,
   1272  1.1  christos 	 0,			/* rightshift */
   1273  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1274  1.1  christos 	 32,			/* bitsize */
   1275  1.1  christos 	 FALSE,			/* pc_relative */
   1276  1.1  christos 	 0,			/* bitpos */
   1277  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1278  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1279  1.1  christos 	 "R_PPC64_TLSLD",	/* name */
   1280  1.1  christos 	 FALSE,			/* partial_inplace */
   1281  1.1  christos 	 0,			/* src_mask */
   1282  1.1  christos 	 0,			/* dst_mask */
   1283  1.1  christos 	 FALSE),		/* pcrel_offset */
   1284  1.1  christos 
   1285  1.1  christos   /* Computes the load module index of the load module that contains the
   1286  1.1  christos      definition of its TLS sym.  */
   1287  1.1  christos   HOWTO (R_PPC64_DTPMOD64,
   1288  1.1  christos 	 0,			/* rightshift */
   1289  1.1  christos 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1290  1.1  christos 	 64,			/* bitsize */
   1291  1.1  christos 	 FALSE,			/* pc_relative */
   1292  1.1  christos 	 0,			/* bitpos */
   1293  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1294  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1295  1.1  christos 	 "R_PPC64_DTPMOD64",	/* name */
   1296  1.1  christos 	 FALSE,			/* partial_inplace */
   1297  1.1  christos 	 0,			/* src_mask */
   1298  1.1  christos 	 ONES (64),		/* dst_mask */
   1299  1.1  christos 	 FALSE),		/* pcrel_offset */
   1300  1.1  christos 
   1301  1.1  christos   /* Computes a dtv-relative displacement, the difference between the value
   1302  1.1  christos      of sym+add and the base address of the thread-local storage block that
   1303  1.1  christos      contains the definition of sym, minus 0x8000.  */
   1304  1.1  christos   HOWTO (R_PPC64_DTPREL64,
   1305  1.1  christos 	 0,			/* rightshift */
   1306  1.1  christos 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1307  1.1  christos 	 64,			/* bitsize */
   1308  1.1  christos 	 FALSE,			/* pc_relative */
   1309  1.1  christos 	 0,			/* bitpos */
   1310  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1311  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1312  1.1  christos 	 "R_PPC64_DTPREL64",	/* name */
   1313  1.1  christos 	 FALSE,			/* partial_inplace */
   1314  1.1  christos 	 0,			/* src_mask */
   1315  1.1  christos 	 ONES (64),		/* dst_mask */
   1316  1.1  christos 	 FALSE),		/* pcrel_offset */
   1317  1.1  christos 
   1318  1.1  christos   /* A 16 bit dtprel reloc.  */
   1319  1.1  christos   HOWTO (R_PPC64_DTPREL16,
   1320  1.1  christos 	 0,			/* rightshift */
   1321  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1322  1.1  christos 	 16,			/* bitsize */
   1323  1.1  christos 	 FALSE,			/* pc_relative */
   1324  1.1  christos 	 0,			/* bitpos */
   1325  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1326  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1327  1.1  christos 	 "R_PPC64_DTPREL16",	/* name */
   1328  1.1  christos 	 FALSE,			/* partial_inplace */
   1329  1.1  christos 	 0,			/* src_mask */
   1330  1.1  christos 	 0xffff,		/* dst_mask */
   1331  1.1  christos 	 FALSE),		/* pcrel_offset */
   1332  1.1  christos 
   1333  1.1  christos   /* Like DTPREL16, but no overflow.  */
   1334  1.1  christos   HOWTO (R_PPC64_DTPREL16_LO,
   1335  1.1  christos 	 0,			/* rightshift */
   1336  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1337  1.1  christos 	 16,			/* bitsize */
   1338  1.1  christos 	 FALSE,			/* pc_relative */
   1339  1.1  christos 	 0,			/* bitpos */
   1340  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1341  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1342  1.1  christos 	 "R_PPC64_DTPREL16_LO",	/* name */
   1343  1.1  christos 	 FALSE,			/* partial_inplace */
   1344  1.1  christos 	 0,			/* src_mask */
   1345  1.1  christos 	 0xffff,		/* dst_mask */
   1346  1.1  christos 	 FALSE),		/* pcrel_offset */
   1347  1.1  christos 
   1348  1.1  christos   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
   1349  1.1  christos   HOWTO (R_PPC64_DTPREL16_HI,
   1350  1.1  christos 	 16,			/* rightshift */
   1351  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1352  1.1  christos 	 16,			/* bitsize */
   1353  1.1  christos 	 FALSE,			/* pc_relative */
   1354  1.1  christos 	 0,			/* bitpos */
   1355  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1356  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1357  1.1  christos 	 "R_PPC64_DTPREL16_HI",	/* name */
   1358  1.1  christos 	 FALSE,			/* partial_inplace */
   1359  1.1  christos 	 0,			/* src_mask */
   1360  1.1  christos 	 0xffff,		/* dst_mask */
   1361  1.1  christos 	 FALSE),		/* pcrel_offset */
   1362  1.1  christos 
   1363  1.1  christos   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
   1364  1.1  christos   HOWTO (R_PPC64_DTPREL16_HA,
   1365  1.1  christos 	 16,			/* rightshift */
   1366  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1367  1.1  christos 	 16,			/* bitsize */
   1368  1.1  christos 	 FALSE,			/* pc_relative */
   1369  1.1  christos 	 0,			/* bitpos */
   1370  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1371  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1372  1.1  christos 	 "R_PPC64_DTPREL16_HA",	/* name */
   1373  1.1  christos 	 FALSE,			/* partial_inplace */
   1374  1.1  christos 	 0,			/* src_mask */
   1375  1.1  christos 	 0xffff,		/* dst_mask */
   1376  1.1  christos 	 FALSE),		/* pcrel_offset */
   1377  1.1  christos 
   1378  1.1  christos   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
   1379  1.1  christos   HOWTO (R_PPC64_DTPREL16_HIGHER,
   1380  1.1  christos 	 32,			/* rightshift */
   1381  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1382  1.1  christos 	 16,			/* bitsize */
   1383  1.1  christos 	 FALSE,			/* pc_relative */
   1384  1.1  christos 	 0,			/* bitpos */
   1385  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1386  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1387  1.1  christos 	 "R_PPC64_DTPREL16_HIGHER", /* name */
   1388  1.1  christos 	 FALSE,			/* partial_inplace */
   1389  1.1  christos 	 0,			/* src_mask */
   1390  1.1  christos 	 0xffff,		/* dst_mask */
   1391  1.1  christos 	 FALSE),		/* pcrel_offset */
   1392  1.1  christos 
   1393  1.1  christos   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
   1394  1.1  christos   HOWTO (R_PPC64_DTPREL16_HIGHERA,
   1395  1.1  christos 	 32,			/* rightshift */
   1396  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1397  1.1  christos 	 16,			/* bitsize */
   1398  1.1  christos 	 FALSE,			/* pc_relative */
   1399  1.1  christos 	 0,			/* bitpos */
   1400  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1401  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1402  1.1  christos 	 "R_PPC64_DTPREL16_HIGHERA", /* name */
   1403  1.1  christos 	 FALSE,			/* partial_inplace */
   1404  1.1  christos 	 0,			/* src_mask */
   1405  1.1  christos 	 0xffff,		/* dst_mask */
   1406  1.1  christos 	 FALSE),		/* pcrel_offset */
   1407  1.1  christos 
   1408  1.1  christos   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
   1409  1.1  christos   HOWTO (R_PPC64_DTPREL16_HIGHEST,
   1410  1.1  christos 	 48,			/* rightshift */
   1411  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1412  1.1  christos 	 16,			/* bitsize */
   1413  1.1  christos 	 FALSE,			/* pc_relative */
   1414  1.1  christos 	 0,			/* bitpos */
   1415  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1416  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1417  1.1  christos 	 "R_PPC64_DTPREL16_HIGHEST", /* name */
   1418  1.1  christos 	 FALSE,			/* partial_inplace */
   1419  1.1  christos 	 0,			/* src_mask */
   1420  1.1  christos 	 0xffff,		/* dst_mask */
   1421  1.1  christos 	 FALSE),		/* pcrel_offset */
   1422  1.1  christos 
   1423  1.1  christos   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
   1424  1.1  christos   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
   1425  1.1  christos 	 48,			/* rightshift */
   1426  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1427  1.1  christos 	 16,			/* bitsize */
   1428  1.1  christos 	 FALSE,			/* pc_relative */
   1429  1.1  christos 	 0,			/* bitpos */
   1430  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1431  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1432  1.1  christos 	 "R_PPC64_DTPREL16_HIGHESTA", /* name */
   1433  1.1  christos 	 FALSE,			/* partial_inplace */
   1434  1.1  christos 	 0,			/* src_mask */
   1435  1.1  christos 	 0xffff,		/* dst_mask */
   1436  1.1  christos 	 FALSE),		/* pcrel_offset */
   1437  1.1  christos 
   1438  1.1  christos   /* Like DTPREL16, but for insns with a DS field.  */
   1439  1.1  christos   HOWTO (R_PPC64_DTPREL16_DS,
   1440  1.1  christos 	 0,			/* rightshift */
   1441  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1442  1.1  christos 	 16,			/* bitsize */
   1443  1.1  christos 	 FALSE,			/* pc_relative */
   1444  1.1  christos 	 0,			/* bitpos */
   1445  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1446  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1447  1.1  christos 	 "R_PPC64_DTPREL16_DS",	/* name */
   1448  1.1  christos 	 FALSE,			/* partial_inplace */
   1449  1.1  christos 	 0,			/* src_mask */
   1450  1.1  christos 	 0xfffc,		/* dst_mask */
   1451  1.1  christos 	 FALSE),		/* pcrel_offset */
   1452  1.1  christos 
   1453  1.1  christos   /* Like DTPREL16_DS, but no overflow.  */
   1454  1.1  christos   HOWTO (R_PPC64_DTPREL16_LO_DS,
   1455  1.1  christos 	 0,			/* rightshift */
   1456  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1457  1.1  christos 	 16,			/* bitsize */
   1458  1.1  christos 	 FALSE,			/* pc_relative */
   1459  1.1  christos 	 0,			/* bitpos */
   1460  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1461  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1462  1.1  christos 	 "R_PPC64_DTPREL16_LO_DS", /* name */
   1463  1.1  christos 	 FALSE,			/* partial_inplace */
   1464  1.1  christos 	 0,			/* src_mask */
   1465  1.1  christos 	 0xfffc,		/* dst_mask */
   1466  1.1  christos 	 FALSE),		/* pcrel_offset */
   1467  1.1  christos 
   1468  1.1  christos   /* Computes a tp-relative displacement, the difference between the value of
   1469  1.1  christos      sym+add and the value of the thread pointer (r13).  */
   1470  1.1  christos   HOWTO (R_PPC64_TPREL64,
   1471  1.1  christos 	 0,			/* rightshift */
   1472  1.1  christos 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1473  1.1  christos 	 64,			/* bitsize */
   1474  1.1  christos 	 FALSE,			/* pc_relative */
   1475  1.1  christos 	 0,			/* bitpos */
   1476  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1477  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1478  1.1  christos 	 "R_PPC64_TPREL64",	/* name */
   1479  1.1  christos 	 FALSE,			/* partial_inplace */
   1480  1.1  christos 	 0,			/* src_mask */
   1481  1.1  christos 	 ONES (64),		/* dst_mask */
   1482  1.1  christos 	 FALSE),		/* pcrel_offset */
   1483  1.1  christos 
   1484  1.1  christos   /* A 16 bit tprel reloc.  */
   1485  1.1  christos   HOWTO (R_PPC64_TPREL16,
   1486  1.1  christos 	 0,			/* rightshift */
   1487  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1488  1.1  christos 	 16,			/* bitsize */
   1489  1.1  christos 	 FALSE,			/* pc_relative */
   1490  1.1  christos 	 0,			/* bitpos */
   1491  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1492  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1493  1.1  christos 	 "R_PPC64_TPREL16",	/* name */
   1494  1.1  christos 	 FALSE,			/* partial_inplace */
   1495  1.1  christos 	 0,			/* src_mask */
   1496  1.1  christos 	 0xffff,		/* dst_mask */
   1497  1.1  christos 	 FALSE),		/* pcrel_offset */
   1498  1.1  christos 
   1499  1.1  christos   /* Like TPREL16, but no overflow.  */
   1500  1.1  christos   HOWTO (R_PPC64_TPREL16_LO,
   1501  1.1  christos 	 0,			/* rightshift */
   1502  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1503  1.1  christos 	 16,			/* bitsize */
   1504  1.1  christos 	 FALSE,			/* pc_relative */
   1505  1.1  christos 	 0,			/* bitpos */
   1506  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1507  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1508  1.1  christos 	 "R_PPC64_TPREL16_LO",	/* name */
   1509  1.1  christos 	 FALSE,			/* partial_inplace */
   1510  1.1  christos 	 0,			/* src_mask */
   1511  1.1  christos 	 0xffff,		/* dst_mask */
   1512  1.1  christos 	 FALSE),		/* pcrel_offset */
   1513  1.1  christos 
   1514  1.1  christos   /* Like TPREL16_LO, but next higher group of 16 bits.  */
   1515  1.1  christos   HOWTO (R_PPC64_TPREL16_HI,
   1516  1.1  christos 	 16,			/* rightshift */
   1517  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1518  1.1  christos 	 16,			/* bitsize */
   1519  1.1  christos 	 FALSE,			/* pc_relative */
   1520  1.1  christos 	 0,			/* bitpos */
   1521  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1522  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1523  1.1  christos 	 "R_PPC64_TPREL16_HI",	/* name */
   1524  1.1  christos 	 FALSE,			/* partial_inplace */
   1525  1.1  christos 	 0,			/* src_mask */
   1526  1.1  christos 	 0xffff,		/* dst_mask */
   1527  1.1  christos 	 FALSE),		/* pcrel_offset */
   1528  1.1  christos 
   1529  1.1  christos   /* Like TPREL16_HI, but adjust for low 16 bits.  */
   1530  1.1  christos   HOWTO (R_PPC64_TPREL16_HA,
   1531  1.1  christos 	 16,			/* rightshift */
   1532  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1533  1.1  christos 	 16,			/* bitsize */
   1534  1.1  christos 	 FALSE,			/* pc_relative */
   1535  1.1  christos 	 0,			/* bitpos */
   1536  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1537  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1538  1.1  christos 	 "R_PPC64_TPREL16_HA",	/* name */
   1539  1.1  christos 	 FALSE,			/* partial_inplace */
   1540  1.1  christos 	 0,			/* src_mask */
   1541  1.1  christos 	 0xffff,		/* dst_mask */
   1542  1.1  christos 	 FALSE),		/* pcrel_offset */
   1543  1.1  christos 
   1544  1.1  christos   /* Like TPREL16_HI, but next higher group of 16 bits.  */
   1545  1.1  christos   HOWTO (R_PPC64_TPREL16_HIGHER,
   1546  1.1  christos 	 32,			/* rightshift */
   1547  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1548  1.1  christos 	 16,			/* bitsize */
   1549  1.1  christos 	 FALSE,			/* pc_relative */
   1550  1.1  christos 	 0,			/* bitpos */
   1551  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1552  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1553  1.1  christos 	 "R_PPC64_TPREL16_HIGHER",	/* name */
   1554  1.1  christos 	 FALSE,			/* partial_inplace */
   1555  1.1  christos 	 0,			/* src_mask */
   1556  1.1  christos 	 0xffff,		/* dst_mask */
   1557  1.1  christos 	 FALSE),		/* pcrel_offset */
   1558  1.1  christos 
   1559  1.1  christos   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
   1560  1.1  christos   HOWTO (R_PPC64_TPREL16_HIGHERA,
   1561  1.1  christos 	 32,			/* rightshift */
   1562  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1563  1.1  christos 	 16,			/* bitsize */
   1564  1.1  christos 	 FALSE,			/* pc_relative */
   1565  1.1  christos 	 0,			/* bitpos */
   1566  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1567  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1568  1.1  christos 	 "R_PPC64_TPREL16_HIGHERA", /* name */
   1569  1.1  christos 	 FALSE,			/* partial_inplace */
   1570  1.1  christos 	 0,			/* src_mask */
   1571  1.1  christos 	 0xffff,		/* dst_mask */
   1572  1.1  christos 	 FALSE),		/* pcrel_offset */
   1573  1.1  christos 
   1574  1.1  christos   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
   1575  1.1  christos   HOWTO (R_PPC64_TPREL16_HIGHEST,
   1576  1.1  christos 	 48,			/* rightshift */
   1577  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1578  1.1  christos 	 16,			/* bitsize */
   1579  1.1  christos 	 FALSE,			/* pc_relative */
   1580  1.1  christos 	 0,			/* bitpos */
   1581  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1582  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1583  1.1  christos 	 "R_PPC64_TPREL16_HIGHEST", /* name */
   1584  1.1  christos 	 FALSE,			/* partial_inplace */
   1585  1.1  christos 	 0,			/* src_mask */
   1586  1.1  christos 	 0xffff,		/* dst_mask */
   1587  1.1  christos 	 FALSE),		/* pcrel_offset */
   1588  1.1  christos 
   1589  1.1  christos   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
   1590  1.1  christos   HOWTO (R_PPC64_TPREL16_HIGHESTA,
   1591  1.1  christos 	 48,			/* rightshift */
   1592  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1593  1.1  christos 	 16,			/* bitsize */
   1594  1.1  christos 	 FALSE,			/* pc_relative */
   1595  1.1  christos 	 0,			/* bitpos */
   1596  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1597  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1598  1.1  christos 	 "R_PPC64_TPREL16_HIGHESTA", /* name */
   1599  1.1  christos 	 FALSE,			/* partial_inplace */
   1600  1.1  christos 	 0,			/* src_mask */
   1601  1.1  christos 	 0xffff,		/* dst_mask */
   1602  1.1  christos 	 FALSE),		/* pcrel_offset */
   1603  1.1  christos 
   1604  1.1  christos   /* Like TPREL16, but for insns with a DS field.  */
   1605  1.1  christos   HOWTO (R_PPC64_TPREL16_DS,
   1606  1.1  christos 	 0,			/* rightshift */
   1607  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1608  1.1  christos 	 16,			/* bitsize */
   1609  1.1  christos 	 FALSE,			/* pc_relative */
   1610  1.1  christos 	 0,			/* bitpos */
   1611  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1612  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1613  1.1  christos 	 "R_PPC64_TPREL16_DS",	/* name */
   1614  1.1  christos 	 FALSE,			/* partial_inplace */
   1615  1.1  christos 	 0,			/* src_mask */
   1616  1.1  christos 	 0xfffc,		/* dst_mask */
   1617  1.1  christos 	 FALSE),		/* pcrel_offset */
   1618  1.1  christos 
   1619  1.1  christos   /* Like TPREL16_DS, but no overflow.  */
   1620  1.1  christos   HOWTO (R_PPC64_TPREL16_LO_DS,
   1621  1.1  christos 	 0,			/* rightshift */
   1622  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1623  1.1  christos 	 16,			/* bitsize */
   1624  1.1  christos 	 FALSE,			/* pc_relative */
   1625  1.1  christos 	 0,			/* bitpos */
   1626  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1627  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1628  1.1  christos 	 "R_PPC64_TPREL16_LO_DS", /* name */
   1629  1.1  christos 	 FALSE,			/* partial_inplace */
   1630  1.1  christos 	 0,			/* src_mask */
   1631  1.1  christos 	 0xfffc,		/* dst_mask */
   1632  1.1  christos 	 FALSE),		/* pcrel_offset */
   1633  1.1  christos 
   1634  1.1  christos   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1635  1.1  christos      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
   1636  1.1  christos      to the first entry relative to the TOC base (r2).  */
   1637  1.1  christos   HOWTO (R_PPC64_GOT_TLSGD16,
   1638  1.1  christos 	 0,			/* rightshift */
   1639  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1640  1.1  christos 	 16,			/* bitsize */
   1641  1.1  christos 	 FALSE,			/* pc_relative */
   1642  1.1  christos 	 0,			/* bitpos */
   1643  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1644  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1645  1.1  christos 	 "R_PPC64_GOT_TLSGD16",	/* name */
   1646  1.1  christos 	 FALSE,			/* partial_inplace */
   1647  1.1  christos 	 0,			/* src_mask */
   1648  1.1  christos 	 0xffff,		/* dst_mask */
   1649  1.1  christos 	 FALSE),		/* pcrel_offset */
   1650  1.1  christos 
   1651  1.1  christos   /* Like GOT_TLSGD16, but no overflow.  */
   1652  1.1  christos   HOWTO (R_PPC64_GOT_TLSGD16_LO,
   1653  1.1  christos 	 0,			/* rightshift */
   1654  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1655  1.1  christos 	 16,			/* bitsize */
   1656  1.1  christos 	 FALSE,			/* pc_relative */
   1657  1.1  christos 	 0,			/* bitpos */
   1658  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1659  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1660  1.1  christos 	 "R_PPC64_GOT_TLSGD16_LO", /* name */
   1661  1.1  christos 	 FALSE,			/* partial_inplace */
   1662  1.1  christos 	 0,			/* src_mask */
   1663  1.1  christos 	 0xffff,		/* dst_mask */
   1664  1.1  christos 	 FALSE),		/* pcrel_offset */
   1665  1.1  christos 
   1666  1.1  christos   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
   1667  1.1  christos   HOWTO (R_PPC64_GOT_TLSGD16_HI,
   1668  1.1  christos 	 16,			/* rightshift */
   1669  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1670  1.1  christos 	 16,			/* bitsize */
   1671  1.1  christos 	 FALSE,			/* pc_relative */
   1672  1.1  christos 	 0,			/* bitpos */
   1673  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1674  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1675  1.1  christos 	 "R_PPC64_GOT_TLSGD16_HI", /* name */
   1676  1.1  christos 	 FALSE,			/* partial_inplace */
   1677  1.1  christos 	 0,			/* src_mask */
   1678  1.1  christos 	 0xffff,		/* dst_mask */
   1679  1.1  christos 	 FALSE),		/* pcrel_offset */
   1680  1.1  christos 
   1681  1.1  christos   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
   1682  1.1  christos   HOWTO (R_PPC64_GOT_TLSGD16_HA,
   1683  1.1  christos 	 16,			/* rightshift */
   1684  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1685  1.1  christos 	 16,			/* bitsize */
   1686  1.1  christos 	 FALSE,			/* pc_relative */
   1687  1.1  christos 	 0,			/* bitpos */
   1688  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1689  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1690  1.1  christos 	 "R_PPC64_GOT_TLSGD16_HA", /* name */
   1691  1.1  christos 	 FALSE,			/* partial_inplace */
   1692  1.1  christos 	 0,			/* src_mask */
   1693  1.1  christos 	 0xffff,		/* dst_mask */
   1694  1.1  christos 	 FALSE),		/* pcrel_offset */
   1695  1.1  christos 
   1696  1.1  christos   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1697  1.1  christos      with values (sym+add)@dtpmod and zero, and computes the offset to the
   1698  1.1  christos      first entry relative to the TOC base (r2).  */
   1699  1.1  christos   HOWTO (R_PPC64_GOT_TLSLD16,
   1700  1.1  christos 	 0,			/* rightshift */
   1701  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1702  1.1  christos 	 16,			/* bitsize */
   1703  1.1  christos 	 FALSE,			/* pc_relative */
   1704  1.1  christos 	 0,			/* bitpos */
   1705  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1706  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1707  1.1  christos 	 "R_PPC64_GOT_TLSLD16",	/* name */
   1708  1.1  christos 	 FALSE,			/* partial_inplace */
   1709  1.1  christos 	 0,			/* src_mask */
   1710  1.1  christos 	 0xffff,		/* dst_mask */
   1711  1.1  christos 	 FALSE),		/* pcrel_offset */
   1712  1.1  christos 
   1713  1.1  christos   /* Like GOT_TLSLD16, but no overflow.  */
   1714  1.1  christos   HOWTO (R_PPC64_GOT_TLSLD16_LO,
   1715  1.1  christos 	 0,			/* rightshift */
   1716  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1717  1.1  christos 	 16,			/* bitsize */
   1718  1.1  christos 	 FALSE,			/* pc_relative */
   1719  1.1  christos 	 0,			/* bitpos */
   1720  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1721  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1722  1.1  christos 	 "R_PPC64_GOT_TLSLD16_LO", /* name */
   1723  1.1  christos 	 FALSE,			/* partial_inplace */
   1724  1.1  christos 	 0,			/* src_mask */
   1725  1.1  christos 	 0xffff,		/* dst_mask */
   1726  1.1  christos 	 FALSE),		/* pcrel_offset */
   1727  1.1  christos 
   1728  1.1  christos   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
   1729  1.1  christos   HOWTO (R_PPC64_GOT_TLSLD16_HI,
   1730  1.1  christos 	 16,			/* rightshift */
   1731  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1732  1.1  christos 	 16,			/* bitsize */
   1733  1.1  christos 	 FALSE,			/* pc_relative */
   1734  1.1  christos 	 0,			/* bitpos */
   1735  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1736  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1737  1.1  christos 	 "R_PPC64_GOT_TLSLD16_HI", /* name */
   1738  1.1  christos 	 FALSE,			/* partial_inplace */
   1739  1.1  christos 	 0,			/* src_mask */
   1740  1.1  christos 	 0xffff,		/* dst_mask */
   1741  1.1  christos 	 FALSE),		/* pcrel_offset */
   1742  1.1  christos 
   1743  1.1  christos   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
   1744  1.1  christos   HOWTO (R_PPC64_GOT_TLSLD16_HA,
   1745  1.1  christos 	 16,			/* rightshift */
   1746  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1747  1.1  christos 	 16,			/* bitsize */
   1748  1.1  christos 	 FALSE,			/* pc_relative */
   1749  1.1  christos 	 0,			/* bitpos */
   1750  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1751  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1752  1.1  christos 	 "R_PPC64_GOT_TLSLD16_HA", /* name */
   1753  1.1  christos 	 FALSE,			/* partial_inplace */
   1754  1.1  christos 	 0,			/* src_mask */
   1755  1.1  christos 	 0xffff,		/* dst_mask */
   1756  1.1  christos 	 FALSE),		/* pcrel_offset */
   1757  1.1  christos 
   1758  1.1  christos   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
   1759  1.1  christos      the offset to the entry relative to the TOC base (r2).  */
   1760  1.1  christos   HOWTO (R_PPC64_GOT_DTPREL16_DS,
   1761  1.1  christos 	 0,			/* rightshift */
   1762  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1763  1.1  christos 	 16,			/* bitsize */
   1764  1.1  christos 	 FALSE,			/* pc_relative */
   1765  1.1  christos 	 0,			/* bitpos */
   1766  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1767  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1768  1.1  christos 	 "R_PPC64_GOT_DTPREL16_DS", /* name */
   1769  1.1  christos 	 FALSE,			/* partial_inplace */
   1770  1.1  christos 	 0,			/* src_mask */
   1771  1.1  christos 	 0xfffc,		/* dst_mask */
   1772  1.1  christos 	 FALSE),		/* pcrel_offset */
   1773  1.1  christos 
   1774  1.1  christos   /* Like GOT_DTPREL16_DS, but no overflow.  */
   1775  1.1  christos   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
   1776  1.1  christos 	 0,			/* rightshift */
   1777  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1778  1.1  christos 	 16,			/* bitsize */
   1779  1.1  christos 	 FALSE,			/* pc_relative */
   1780  1.1  christos 	 0,			/* bitpos */
   1781  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1782  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1783  1.1  christos 	 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
   1784  1.1  christos 	 FALSE,			/* partial_inplace */
   1785  1.1  christos 	 0,			/* src_mask */
   1786  1.1  christos 	 0xfffc,		/* dst_mask */
   1787  1.1  christos 	 FALSE),		/* pcrel_offset */
   1788  1.1  christos 
   1789  1.1  christos   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
   1790  1.1  christos   HOWTO (R_PPC64_GOT_DTPREL16_HI,
   1791  1.1  christos 	 16,			/* rightshift */
   1792  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1793  1.1  christos 	 16,			/* bitsize */
   1794  1.1  christos 	 FALSE,			/* pc_relative */
   1795  1.1  christos 	 0,			/* bitpos */
   1796  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1797  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1798  1.1  christos 	 "R_PPC64_GOT_DTPREL16_HI", /* name */
   1799  1.1  christos 	 FALSE,			/* partial_inplace */
   1800  1.1  christos 	 0,			/* src_mask */
   1801  1.1  christos 	 0xffff,		/* dst_mask */
   1802  1.1  christos 	 FALSE),		/* pcrel_offset */
   1803  1.1  christos 
   1804  1.1  christos   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
   1805  1.1  christos   HOWTO (R_PPC64_GOT_DTPREL16_HA,
   1806  1.1  christos 	 16,			/* rightshift */
   1807  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1808  1.1  christos 	 16,			/* bitsize */
   1809  1.1  christos 	 FALSE,			/* pc_relative */
   1810  1.1  christos 	 0,			/* bitpos */
   1811  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1812  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1813  1.1  christos 	 "R_PPC64_GOT_DTPREL16_HA", /* name */
   1814  1.1  christos 	 FALSE,			/* partial_inplace */
   1815  1.1  christos 	 0,			/* src_mask */
   1816  1.1  christos 	 0xffff,		/* dst_mask */
   1817  1.1  christos 	 FALSE),		/* pcrel_offset */
   1818  1.1  christos 
   1819  1.1  christos   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
   1820  1.1  christos      offset to the entry relative to the TOC base (r2).  */
   1821  1.1  christos   HOWTO (R_PPC64_GOT_TPREL16_DS,
   1822  1.1  christos 	 0,			/* rightshift */
   1823  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1824  1.1  christos 	 16,			/* bitsize */
   1825  1.1  christos 	 FALSE,			/* pc_relative */
   1826  1.1  christos 	 0,			/* bitpos */
   1827  1.1  christos 	 complain_overflow_signed, /* complain_on_overflow */
   1828  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1829  1.1  christos 	 "R_PPC64_GOT_TPREL16_DS", /* name */
   1830  1.1  christos 	 FALSE,			/* partial_inplace */
   1831  1.1  christos 	 0,			/* src_mask */
   1832  1.1  christos 	 0xfffc,		/* dst_mask */
   1833  1.1  christos 	 FALSE),		/* pcrel_offset */
   1834  1.1  christos 
   1835  1.1  christos   /* Like GOT_TPREL16_DS, but no overflow.  */
   1836  1.1  christos   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
   1837  1.1  christos 	 0,			/* rightshift */
   1838  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1839  1.1  christos 	 16,			/* bitsize */
   1840  1.1  christos 	 FALSE,			/* pc_relative */
   1841  1.1  christos 	 0,			/* bitpos */
   1842  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1843  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1844  1.1  christos 	 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
   1845  1.1  christos 	 FALSE,			/* partial_inplace */
   1846  1.1  christos 	 0,			/* src_mask */
   1847  1.1  christos 	 0xfffc,		/* dst_mask */
   1848  1.1  christos 	 FALSE),		/* pcrel_offset */
   1849  1.1  christos 
   1850  1.1  christos   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
   1851  1.1  christos   HOWTO (R_PPC64_GOT_TPREL16_HI,
   1852  1.1  christos 	 16,			/* rightshift */
   1853  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1854  1.1  christos 	 16,			/* bitsize */
   1855  1.1  christos 	 FALSE,			/* pc_relative */
   1856  1.1  christos 	 0,			/* bitpos */
   1857  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1858  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1859  1.1  christos 	 "R_PPC64_GOT_TPREL16_HI", /* name */
   1860  1.1  christos 	 FALSE,			/* partial_inplace */
   1861  1.1  christos 	 0,			/* src_mask */
   1862  1.1  christos 	 0xffff,		/* dst_mask */
   1863  1.1  christos 	 FALSE),		/* pcrel_offset */
   1864  1.1  christos 
   1865  1.1  christos   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
   1866  1.1  christos   HOWTO (R_PPC64_GOT_TPREL16_HA,
   1867  1.1  christos 	 16,			/* rightshift */
   1868  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1869  1.1  christos 	 16,			/* bitsize */
   1870  1.1  christos 	 FALSE,			/* pc_relative */
   1871  1.1  christos 	 0,			/* bitpos */
   1872  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1873  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1874  1.1  christos 	 "R_PPC64_GOT_TPREL16_HA", /* name */
   1875  1.1  christos 	 FALSE,			/* partial_inplace */
   1876  1.1  christos 	 0,			/* src_mask */
   1877  1.1  christos 	 0xffff,		/* dst_mask */
   1878  1.1  christos 	 FALSE),		/* pcrel_offset */
   1879  1.1  christos 
   1880  1.1  christos   HOWTO (R_PPC64_JMP_IREL,	/* type */
   1881  1.1  christos 	 0,			/* rightshift */
   1882  1.1  christos 	 0,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1883  1.1  christos 	 0,			/* bitsize */
   1884  1.1  christos 	 FALSE,			/* pc_relative */
   1885  1.1  christos 	 0,			/* bitpos */
   1886  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1887  1.1  christos 	 ppc64_elf_unhandled_reloc, /* special_function */
   1888  1.1  christos 	 "R_PPC64_JMP_IREL",	/* name */
   1889  1.1  christos 	 FALSE,			/* partial_inplace */
   1890  1.1  christos 	 0,			/* src_mask */
   1891  1.1  christos 	 0,			/* dst_mask */
   1892  1.1  christos 	 FALSE),		/* pcrel_offset */
   1893  1.1  christos 
   1894  1.1  christos   HOWTO (R_PPC64_IRELATIVE,	/* type */
   1895  1.1  christos 	 0,			/* rightshift */
   1896  1.1  christos 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1897  1.1  christos 	 64,			/* bitsize */
   1898  1.1  christos 	 FALSE,			/* pc_relative */
   1899  1.1  christos 	 0,			/* bitpos */
   1900  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1901  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1902  1.1  christos 	 "R_PPC64_IRELATIVE",	/* name */
   1903  1.1  christos 	 FALSE,			/* partial_inplace */
   1904  1.1  christos 	 0,			/* src_mask */
   1905  1.1  christos 	 ONES (64),		/* dst_mask */
   1906  1.1  christos 	 FALSE),		/* pcrel_offset */
   1907  1.1  christos 
   1908  1.1  christos   /* A 16 bit relative relocation.  */
   1909  1.1  christos   HOWTO (R_PPC64_REL16,		/* type */
   1910  1.1  christos 	 0,			/* rightshift */
   1911  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1912  1.1  christos 	 16,			/* bitsize */
   1913  1.1  christos 	 TRUE,			/* pc_relative */
   1914  1.1  christos 	 0,			/* bitpos */
   1915  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
   1916  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1917  1.1  christos 	 "R_PPC64_REL16",	/* name */
   1918  1.1  christos 	 FALSE,			/* partial_inplace */
   1919  1.1  christos 	 0,			/* src_mask */
   1920  1.1  christos 	 0xffff,		/* dst_mask */
   1921  1.1  christos 	 TRUE),			/* pcrel_offset */
   1922  1.1  christos 
   1923  1.1  christos   /* A 16 bit relative relocation without overflow.  */
   1924  1.1  christos   HOWTO (R_PPC64_REL16_LO,	/* type */
   1925  1.1  christos 	 0,			/* rightshift */
   1926  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1927  1.1  christos 	 16,			/* bitsize */
   1928  1.1  christos 	 TRUE,			/* pc_relative */
   1929  1.1  christos 	 0,			/* bitpos */
   1930  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
   1931  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1932  1.1  christos 	 "R_PPC64_REL16_LO",	/* name */
   1933  1.1  christos 	 FALSE,			/* partial_inplace */
   1934  1.1  christos 	 0,			/* src_mask */
   1935  1.1  christos 	 0xffff,		/* dst_mask */
   1936  1.1  christos 	 TRUE),			/* pcrel_offset */
   1937  1.1  christos 
   1938  1.1  christos   /* The high order 16 bits of a relative address.  */
   1939  1.1  christos   HOWTO (R_PPC64_REL16_HI,	/* type */
   1940  1.1  christos 	 16,			/* rightshift */
   1941  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1942  1.1  christos 	 16,			/* bitsize */
   1943  1.1  christos 	 TRUE,			/* pc_relative */
   1944  1.1  christos 	 0,			/* bitpos */
   1945  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1946  1.1  christos 	 bfd_elf_generic_reloc,	/* special_function */
   1947  1.1  christos 	 "R_PPC64_REL16_HI",	/* name */
   1948  1.1  christos 	 FALSE,			/* partial_inplace */
   1949  1.1  christos 	 0,			/* src_mask */
   1950  1.1  christos 	 0xffff,		/* dst_mask */
   1951  1.1  christos 	 TRUE),			/* pcrel_offset */
   1952  1.1  christos 
   1953  1.1  christos   /* The high order 16 bits of a relative address, plus 1 if the contents of
   1954  1.1  christos      the low 16 bits, treated as a signed number, is negative.  */
   1955  1.1  christos   HOWTO (R_PPC64_REL16_HA,	/* type */
   1956  1.1  christos 	 16,			/* rightshift */
   1957  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1958  1.1  christos 	 16,			/* bitsize */
   1959  1.1  christos 	 TRUE,			/* pc_relative */
   1960  1.1  christos 	 0,			/* bitpos */
   1961  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1962  1.1  christos 	 ppc64_elf_ha_reloc,	/* special_function */
   1963  1.1  christos 	 "R_PPC64_REL16_HA",	/* name */
   1964  1.1  christos 	 FALSE,			/* partial_inplace */
   1965  1.1  christos 	 0,			/* src_mask */
   1966  1.1  christos 	 0xffff,		/* dst_mask */
   1967  1.1  christos 	 TRUE),			/* pcrel_offset */
   1968  1.1  christos 
   1969  1.1  christos   /* GNU extension to record C++ vtable hierarchy.  */
   1970  1.1  christos   HOWTO (R_PPC64_GNU_VTINHERIT,	/* type */
   1971  1.1  christos 	 0,			/* rightshift */
   1972  1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1973  1.1  christos 	 0,			/* bitsize */
   1974  1.1  christos 	 FALSE,			/* pc_relative */
   1975  1.1  christos 	 0,			/* bitpos */
   1976  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1977  1.1  christos 	 NULL,			/* special_function */
   1978  1.1  christos 	 "R_PPC64_GNU_VTINHERIT", /* name */
   1979  1.1  christos 	 FALSE,			/* partial_inplace */
   1980  1.1  christos 	 0,			/* src_mask */
   1981  1.1  christos 	 0,			/* dst_mask */
   1982  1.1  christos 	 FALSE),		/* pcrel_offset */
   1983  1.1  christos 
   1984  1.1  christos   /* GNU extension to record C++ vtable member usage.  */
   1985  1.1  christos   HOWTO (R_PPC64_GNU_VTENTRY,	/* type */
   1986  1.1  christos 	 0,			/* rightshift */
   1987  1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1988  1.1  christos 	 0,			/* bitsize */
   1989  1.1  christos 	 FALSE,			/* pc_relative */
   1990  1.1  christos 	 0,			/* bitpos */
   1991  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
   1992  1.1  christos 	 NULL,			/* special_function */
   1993  1.1  christos 	 "R_PPC64_GNU_VTENTRY",	/* name */
   1994  1.1  christos 	 FALSE,			/* partial_inplace */
   1995  1.1  christos 	 0,			/* src_mask */
   1996  1.1  christos 	 0,			/* dst_mask */
   1997  1.1  christos 	 FALSE),		/* pcrel_offset */
   1998  1.1  christos };
   1999  1.1  christos 
   2000  1.1  christos 
   2001  1.1  christos /* Initialize the ppc64_elf_howto_table, so that linear accesses can
   2003  1.1  christos    be done.  */
   2004  1.1  christos 
   2005  1.1  christos static void
   2006  1.1  christos ppc_howto_init (void)
   2007  1.1  christos {
   2008  1.1  christos   unsigned int i, type;
   2009  1.1  christos 
   2010  1.1  christos   for (i = 0;
   2011  1.1  christos        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2012  1.1  christos        i++)
   2013  1.1  christos     {
   2014  1.1  christos       type = ppc64_elf_howto_raw[i].type;
   2015  1.1  christos       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
   2016  1.1  christos 			  / sizeof (ppc64_elf_howto_table[0])));
   2017  1.1  christos       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   2018  1.1  christos     }
   2019  1.1  christos }
   2020  1.1  christos 
   2021  1.1  christos static reloc_howto_type *
   2022  1.1  christos ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2023  1.1  christos 			     bfd_reloc_code_real_type code)
   2024  1.1  christos {
   2025  1.1  christos   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   2026  1.1  christos 
   2027  1.1  christos   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2028  1.1  christos     /* Initialize howto table if needed.  */
   2029  1.1  christos     ppc_howto_init ();
   2030  1.1  christos 
   2031  1.1  christos   switch (code)
   2032  1.1  christos     {
   2033  1.1  christos     default:
   2034  1.1  christos       return NULL;
   2035  1.1  christos 
   2036  1.1  christos     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   2037  1.1  christos       break;
   2038  1.1  christos     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   2039  1.1  christos       break;
   2040  1.1  christos     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   2041  1.1  christos       break;
   2042  1.1  christos     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   2043  1.1  christos       break;
   2044  1.1  christos     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   2045  1.1  christos       break;
   2046  1.1  christos     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   2047  1.1  christos       break;
   2048  1.1  christos     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   2049  1.1  christos       break;
   2050  1.1  christos     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   2051  1.1  christos       break;
   2052  1.1  christos     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   2053  1.1  christos       break;
   2054  1.1  christos     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   2055  1.1  christos       break;
   2056  1.1  christos     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   2057  1.1  christos       break;
   2058  1.1  christos     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   2059  1.1  christos       break;
   2060  1.1  christos     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   2061  1.1  christos       break;
   2062  1.1  christos     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   2063  1.1  christos       break;
   2064  1.1  christos     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   2065  1.1  christos       break;
   2066  1.1  christos     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   2067  1.1  christos       break;
   2068  1.1  christos     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   2069  1.1  christos       break;
   2070  1.1  christos     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   2071  1.1  christos       break;
   2072  1.1  christos     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   2073  1.1  christos       break;
   2074  1.1  christos     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   2075  1.1  christos       break;
   2076  1.1  christos     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   2077  1.1  christos       break;
   2078  1.1  christos     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   2079  1.1  christos       break;
   2080  1.1  christos     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   2081  1.1  christos       break;
   2082  1.1  christos     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   2083  1.1  christos       break;
   2084  1.1  christos     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   2085  1.1  christos       break;
   2086  1.1  christos     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   2087  1.1  christos       break;
   2088  1.1  christos     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   2089  1.1  christos       break;
   2090  1.1  christos     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   2091  1.1  christos       break;
   2092  1.1  christos     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   2093  1.1  christos       break;
   2094  1.1  christos     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   2095  1.1  christos       break;
   2096  1.1  christos     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   2097  1.1  christos       break;
   2098  1.1  christos     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   2099  1.1  christos       break;
   2100  1.1  christos     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   2101  1.1  christos       break;
   2102  1.1  christos     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   2103  1.1  christos       break;
   2104  1.1  christos     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   2105  1.1  christos       break;
   2106  1.1  christos     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   2107  1.1  christos       break;
   2108  1.1  christos     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   2109  1.1  christos       break;
   2110  1.1  christos     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   2111  1.1  christos       break;
   2112  1.1  christos     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   2113  1.1  christos       break;
   2114  1.1  christos     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   2115  1.1  christos       break;
   2116  1.1  christos     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   2117  1.1  christos       break;
   2118  1.1  christos     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   2119  1.1  christos       break;
   2120  1.1  christos     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   2121  1.1  christos       break;
   2122  1.1  christos     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   2123  1.1  christos       break;
   2124  1.1  christos     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   2125  1.1  christos       break;
   2126  1.1  christos     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   2127  1.1  christos       break;
   2128  1.1  christos     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   2129  1.1  christos       break;
   2130  1.1  christos     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   2131  1.1  christos       break;
   2132  1.1  christos     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   2133  1.1  christos       break;
   2134  1.1  christos     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   2135  1.1  christos       break;
   2136  1.1  christos     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   2137  1.1  christos       break;
   2138  1.1  christos     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   2139  1.1  christos       break;
   2140  1.1  christos     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   2141  1.1  christos       break;
   2142  1.1  christos     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   2143  1.1  christos       break;
   2144  1.1  christos     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   2145  1.1  christos       break;
   2146  1.1  christos     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   2147  1.1  christos       break;
   2148  1.1  christos     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   2149  1.1  christos       break;
   2150  1.1  christos     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   2151  1.1  christos       break;
   2152  1.1  christos     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   2153  1.1  christos       break;
   2154  1.1  christos     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   2155  1.1  christos       break;
   2156  1.1  christos     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   2157  1.1  christos       break;
   2158  1.1  christos     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   2159  1.1  christos       break;
   2160  1.1  christos     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   2161  1.1  christos       break;
   2162  1.1  christos     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   2163  1.1  christos       break;
   2164  1.1  christos     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   2165  1.1  christos       break;
   2166  1.1  christos     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   2167  1.1  christos       break;
   2168  1.1  christos     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   2169  1.1  christos       break;
   2170  1.1  christos     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   2171  1.1  christos       break;
   2172  1.1  christos     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   2173  1.1  christos       break;
   2174  1.1  christos     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   2175  1.1  christos       break;
   2176  1.1  christos     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   2177  1.1  christos       break;
   2178  1.1  christos     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   2179  1.1  christos       break;
   2180  1.1  christos     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   2181  1.1  christos       break;
   2182  1.1  christos     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   2183  1.1  christos       break;
   2184  1.1  christos     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   2185  1.1  christos       break;
   2186  1.1  christos     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   2187  1.1  christos       break;
   2188  1.1  christos     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   2189  1.1  christos       break;
   2190  1.1  christos     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   2191  1.1  christos       break;
   2192  1.1  christos     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   2193  1.1  christos       break;
   2194  1.1  christos     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   2195  1.1  christos       break;
   2196  1.1  christos     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   2197  1.1  christos       break;
   2198  1.1  christos     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   2199  1.1  christos       break;
   2200  1.1  christos     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   2201  1.1  christos       break;
   2202  1.1  christos     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   2203  1.1  christos       break;
   2204  1.1  christos     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   2205  1.1  christos       break;
   2206  1.1  christos     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   2207  1.1  christos       break;
   2208  1.1  christos     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   2209  1.1  christos       break;
   2210  1.1  christos     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   2211  1.1  christos       break;
   2212  1.1  christos     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   2213  1.1  christos       break;
   2214  1.1  christos     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   2215  1.1  christos       break;
   2216  1.1  christos     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   2217  1.1  christos       break;
   2218  1.1  christos     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   2219  1.1  christos       break;
   2220  1.1  christos     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   2221  1.1  christos       break;
   2222  1.1  christos     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   2223  1.1  christos       break;
   2224  1.1  christos     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   2225  1.1  christos       break;
   2226  1.1  christos     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   2227  1.1  christos       break;
   2228  1.1  christos     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   2229  1.1  christos       break;
   2230  1.1  christos     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   2231  1.1  christos       break;
   2232  1.1  christos     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   2233  1.1  christos       break;
   2234  1.1  christos     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   2235  1.1  christos       break;
   2236  1.1  christos     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   2237  1.1  christos       break;
   2238  1.1  christos     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   2239  1.1  christos       break;
   2240  1.1  christos     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   2241  1.1  christos       break;
   2242  1.1  christos     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   2243  1.1  christos       break;
   2244  1.1  christos     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   2245  1.1  christos       break;
   2246  1.1  christos     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   2247  1.1  christos       break;
   2248  1.1  christos     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   2249  1.1  christos       break;
   2250  1.1  christos     }
   2251  1.1  christos 
   2252  1.1  christos   return ppc64_elf_howto_table[r];
   2253  1.1  christos };
   2254  1.1  christos 
   2255  1.1  christos static reloc_howto_type *
   2256  1.1  christos ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2257  1.1  christos 			     const char *r_name)
   2258  1.1  christos {
   2259  1.1  christos   unsigned int i;
   2260  1.1  christos 
   2261  1.1  christos   for (i = 0;
   2262  1.1  christos        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2263  1.1  christos        i++)
   2264  1.1  christos     if (ppc64_elf_howto_raw[i].name != NULL
   2265  1.1  christos 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   2266  1.1  christos       return &ppc64_elf_howto_raw[i];
   2267  1.1  christos 
   2268  1.1  christos   return NULL;
   2269  1.1  christos }
   2270  1.1  christos 
   2271  1.1  christos /* Set the howto pointer for a PowerPC ELF reloc.  */
   2272  1.1  christos 
   2273  1.1  christos static void
   2274  1.1  christos ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2275  1.1  christos 			 Elf_Internal_Rela *dst)
   2276  1.1  christos {
   2277  1.1  christos   unsigned int type;
   2278  1.1  christos 
   2279  1.1  christos   /* Initialize howto table if needed.  */
   2280  1.1  christos   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2281  1.1  christos     ppc_howto_init ();
   2282  1.1  christos 
   2283  1.1  christos   type = ELF64_R_TYPE (dst->r_info);
   2284  1.1  christos   if (type >= (sizeof (ppc64_elf_howto_table)
   2285  1.1  christos 	       / sizeof (ppc64_elf_howto_table[0])))
   2286  1.1  christos     {
   2287  1.1  christos       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
   2288  1.1  christos 			     abfd, (int) type);
   2289  1.1  christos       type = R_PPC64_NONE;
   2290  1.1  christos     }
   2291  1.1  christos   cache_ptr->howto = ppc64_elf_howto_table[type];
   2292  1.1  christos }
   2293  1.1  christos 
   2294  1.1  christos /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   2295  1.1  christos 
   2296  1.1  christos static bfd_reloc_status_type
   2297  1.1  christos ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2298  1.1  christos 		    void *data, asection *input_section,
   2299  1.1  christos 		    bfd *output_bfd, char **error_message)
   2300  1.1  christos {
   2301  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2302  1.1  christos      call the generic function.  Any adjustment will be done at final
   2303  1.1  christos      link time.  */
   2304  1.1  christos   if (output_bfd != NULL)
   2305  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2306  1.1  christos 				  input_section, output_bfd, error_message);
   2307  1.1  christos 
   2308  1.1  christos   /* Adjust the addend for sign extension of the low 16 bits.
   2309  1.1  christos      We won't actually be using the low 16 bits, so trashing them
   2310  1.1  christos      doesn't matter.  */
   2311  1.1  christos   reloc_entry->addend += 0x8000;
   2312  1.1  christos   return bfd_reloc_continue;
   2313  1.1  christos }
   2314  1.1  christos 
   2315  1.1  christos static bfd_reloc_status_type
   2316  1.1  christos ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2317  1.1  christos 			void *data, asection *input_section,
   2318  1.1  christos 			bfd *output_bfd, char **error_message)
   2319  1.1  christos {
   2320  1.1  christos   if (output_bfd != NULL)
   2321  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2322  1.1  christos 				  input_section, output_bfd, error_message);
   2323  1.1  christos 
   2324  1.1  christos   if (strcmp (symbol->section->name, ".opd") == 0
   2325  1.1  christos       && (symbol->section->owner->flags & DYNAMIC) == 0)
   2326  1.1  christos     {
   2327  1.1  christos       bfd_vma dest = opd_entry_value (symbol->section,
   2328  1.1  christos 				      symbol->value + reloc_entry->addend,
   2329  1.1  christos 				      NULL, NULL);
   2330  1.1  christos       if (dest != (bfd_vma) -1)
   2331  1.1  christos 	reloc_entry->addend = dest - (symbol->value
   2332  1.1  christos 				      + symbol->section->output_section->vma
   2333  1.1  christos 				      + symbol->section->output_offset);
   2334  1.1  christos     }
   2335  1.1  christos   return bfd_reloc_continue;
   2336  1.1  christos }
   2337  1.1  christos 
   2338  1.1  christos static bfd_reloc_status_type
   2339  1.1  christos ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2340  1.1  christos 			 void *data, asection *input_section,
   2341  1.1  christos 			 bfd *output_bfd, char **error_message)
   2342  1.1  christos {
   2343  1.1  christos   long insn;
   2344  1.1  christos   enum elf_ppc64_reloc_type r_type;
   2345  1.1  christos   bfd_size_type octets;
   2346  1.1  christos   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
   2347  1.1  christos   bfd_boolean is_power4 = FALSE;
   2348  1.1  christos 
   2349  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2350  1.1  christos      call the generic function.  Any adjustment will be done at final
   2351  1.1  christos      link time.  */
   2352  1.1  christos   if (output_bfd != NULL)
   2353  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2354  1.1  christos 				  input_section, output_bfd, error_message);
   2355  1.1  christos 
   2356  1.1  christos   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2357  1.1  christos   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   2358  1.1  christos   insn &= ~(0x01 << 21);
   2359  1.1  christos   r_type = reloc_entry->howto->type;
   2360  1.1  christos   if (r_type == R_PPC64_ADDR14_BRTAKEN
   2361  1.1  christos       || r_type == R_PPC64_REL14_BRTAKEN)
   2362  1.1  christos     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   2363  1.1  christos 
   2364  1.1  christos   if (is_power4)
   2365  1.1  christos     {
   2366  1.1  christos       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   2367  1.1  christos 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   2368  1.1  christos 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   2369  1.1  christos       if ((insn & (0x14 << 21)) == (0x04 << 21))
   2370  1.1  christos 	insn |= 0x02 << 21;
   2371  1.1  christos       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   2372  1.1  christos 	insn |= 0x08 << 21;
   2373  1.1  christos       else
   2374  1.1  christos 	goto out;
   2375  1.1  christos     }
   2376  1.1  christos   else
   2377  1.1  christos     {
   2378  1.1  christos       bfd_vma target = 0;
   2379  1.1  christos       bfd_vma from;
   2380  1.1  christos 
   2381  1.1  christos       if (!bfd_is_com_section (symbol->section))
   2382  1.1  christos 	target = symbol->value;
   2383  1.1  christos       target += symbol->section->output_section->vma;
   2384  1.1  christos       target += symbol->section->output_offset;
   2385  1.1  christos       target += reloc_entry->addend;
   2386  1.1  christos 
   2387  1.1  christos       from = (reloc_entry->address
   2388  1.1  christos 	      + input_section->output_offset
   2389  1.1  christos 	      + input_section->output_section->vma);
   2390  1.1  christos 
   2391  1.1  christos       /* Invert 'y' bit if not the default.  */
   2392  1.1  christos       if ((bfd_signed_vma) (target - from) < 0)
   2393  1.1  christos 	insn ^= 0x01 << 21;
   2394  1.1  christos     }
   2395  1.1  christos   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   2396  1.1  christos  out:
   2397  1.1  christos   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   2398  1.1  christos 				 input_section, output_bfd, error_message);
   2399  1.1  christos }
   2400  1.1  christos 
   2401  1.1  christos static bfd_reloc_status_type
   2402  1.1  christos ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2403  1.1  christos 			 void *data, asection *input_section,
   2404  1.1  christos 			 bfd *output_bfd, char **error_message)
   2405  1.1  christos {
   2406  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2407  1.1  christos      call the generic function.  Any adjustment will be done at final
   2408  1.1  christos      link time.  */
   2409  1.1  christos   if (output_bfd != NULL)
   2410  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2411  1.1  christos 				  input_section, output_bfd, error_message);
   2412  1.1  christos 
   2413  1.1  christos   /* Subtract the symbol section base address.  */
   2414  1.1  christos   reloc_entry->addend -= symbol->section->output_section->vma;
   2415  1.1  christos   return bfd_reloc_continue;
   2416  1.1  christos }
   2417  1.1  christos 
   2418  1.1  christos static bfd_reloc_status_type
   2419  1.1  christos ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2420  1.1  christos 			    void *data, asection *input_section,
   2421  1.1  christos 			    bfd *output_bfd, char **error_message)
   2422  1.1  christos {
   2423  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2424  1.1  christos      call the generic function.  Any adjustment will be done at final
   2425  1.1  christos      link time.  */
   2426  1.1  christos   if (output_bfd != NULL)
   2427  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2428  1.1  christos 				  input_section, output_bfd, error_message);
   2429  1.1  christos 
   2430  1.1  christos   /* Subtract the symbol section base address.  */
   2431  1.1  christos   reloc_entry->addend -= symbol->section->output_section->vma;
   2432  1.1  christos 
   2433  1.1  christos   /* Adjust the addend for sign extension of the low 16 bits.  */
   2434  1.1  christos   reloc_entry->addend += 0x8000;
   2435  1.1  christos   return bfd_reloc_continue;
   2436  1.1  christos }
   2437  1.1  christos 
   2438  1.1  christos static bfd_reloc_status_type
   2439  1.1  christos ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2440  1.1  christos 		     void *data, asection *input_section,
   2441  1.1  christos 		     bfd *output_bfd, char **error_message)
   2442  1.1  christos {
   2443  1.1  christos   bfd_vma TOCstart;
   2444  1.1  christos 
   2445  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2446  1.1  christos      call the generic function.  Any adjustment will be done at final
   2447  1.1  christos      link time.  */
   2448  1.1  christos   if (output_bfd != NULL)
   2449  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2450  1.1  christos 				  input_section, output_bfd, error_message);
   2451  1.1  christos 
   2452  1.1  christos   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2453  1.1  christos   if (TOCstart == 0)
   2454  1.1  christos     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2455  1.1  christos 
   2456  1.1  christos   /* Subtract the TOC base address.  */
   2457  1.1  christos   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2458  1.1  christos   return bfd_reloc_continue;
   2459  1.1  christos }
   2460  1.1  christos 
   2461  1.1  christos static bfd_reloc_status_type
   2462  1.1  christos ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2463  1.1  christos 			void *data, asection *input_section,
   2464  1.1  christos 			bfd *output_bfd, char **error_message)
   2465  1.1  christos {
   2466  1.1  christos   bfd_vma TOCstart;
   2467  1.1  christos 
   2468  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2469  1.1  christos      call the generic function.  Any adjustment will be done at final
   2470  1.1  christos      link time.  */
   2471  1.1  christos   if (output_bfd != NULL)
   2472  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2473  1.1  christos 				  input_section, output_bfd, error_message);
   2474  1.1  christos 
   2475  1.1  christos   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2476  1.1  christos   if (TOCstart == 0)
   2477  1.1  christos     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2478  1.1  christos 
   2479  1.1  christos   /* Subtract the TOC base address.  */
   2480  1.1  christos   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2481  1.1  christos 
   2482  1.1  christos   /* Adjust the addend for sign extension of the low 16 bits.  */
   2483  1.1  christos   reloc_entry->addend += 0x8000;
   2484  1.1  christos   return bfd_reloc_continue;
   2485  1.1  christos }
   2486  1.1  christos 
   2487  1.1  christos static bfd_reloc_status_type
   2488  1.1  christos ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2489  1.1  christos 		       void *data, asection *input_section,
   2490  1.1  christos 		       bfd *output_bfd, char **error_message)
   2491  1.1  christos {
   2492  1.1  christos   bfd_vma TOCstart;
   2493  1.1  christos   bfd_size_type octets;
   2494  1.1  christos 
   2495  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2496  1.1  christos      call the generic function.  Any adjustment will be done at final
   2497  1.1  christos      link time.  */
   2498  1.1  christos   if (output_bfd != NULL)
   2499  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2500  1.1  christos 				  input_section, output_bfd, error_message);
   2501  1.1  christos 
   2502  1.1  christos   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2503  1.1  christos   if (TOCstart == 0)
   2504  1.1  christos     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2505  1.1  christos 
   2506  1.1  christos   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2507  1.1  christos   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   2508  1.1  christos   return bfd_reloc_ok;
   2509  1.1  christos }
   2510  1.1  christos 
   2511  1.1  christos static bfd_reloc_status_type
   2512  1.1  christos ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2513  1.1  christos 			   void *data, asection *input_section,
   2514  1.1  christos 			   bfd *output_bfd, char **error_message)
   2515  1.1  christos {
   2516  1.1  christos   /* If this is a relocatable link (output_bfd test tells us), just
   2517  1.1  christos      call the generic function.  Any adjustment will be done at final
   2518  1.1  christos      link time.  */
   2519  1.1  christos   if (output_bfd != NULL)
   2520  1.1  christos     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2521  1.1  christos 				  input_section, output_bfd, error_message);
   2522  1.1  christos 
   2523  1.1  christos   if (error_message != NULL)
   2524  1.1  christos     {
   2525  1.1  christos       static char buf[60];
   2526  1.1  christos       sprintf (buf, "generic linker can't handle %s",
   2527  1.1  christos 	       reloc_entry->howto->name);
   2528  1.1  christos       *error_message = buf;
   2529  1.1  christos     }
   2530  1.1  christos   return bfd_reloc_dangerous;
   2531  1.1  christos }
   2532  1.1  christos 
   2533  1.1  christos /* Track GOT entries needed for a given symbol.  We might need more
   2534  1.1  christos    than one got entry per symbol.  */
   2535  1.1  christos struct got_entry
   2536  1.1  christos {
   2537  1.1  christos   struct got_entry *next;
   2538  1.1  christos 
   2539  1.1  christos   /* The symbol addend that we'll be placing in the GOT.  */
   2540  1.1  christos   bfd_vma addend;
   2541  1.1  christos 
   2542  1.1  christos   /* Unlike other ELF targets, we use separate GOT entries for the same
   2543  1.1  christos      symbol referenced from different input files.  This is to support
   2544  1.1  christos      automatic multiple TOC/GOT sections, where the TOC base can vary
   2545  1.1  christos      from one input file to another.  After partitioning into TOC groups
   2546  1.1  christos      we merge entries within the group.
   2547  1.1  christos 
   2548  1.1  christos      Point to the BFD owning this GOT entry.  */
   2549  1.1  christos   bfd *owner;
   2550  1.1  christos 
   2551  1.1  christos   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   2552  1.1  christos      TLS_TPREL or TLS_DTPREL for tls entries.  */
   2553  1.1  christos   unsigned char tls_type;
   2554  1.1  christos 
   2555  1.1  christos   /* Non-zero if got.ent points to real entry.  */
   2556  1.1  christos   unsigned char is_indirect;
   2557  1.1  christos 
   2558  1.1  christos   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   2559  1.1  christos   union
   2560  1.1  christos     {
   2561  1.1  christos       bfd_signed_vma refcount;
   2562  1.1  christos       bfd_vma offset;
   2563  1.1  christos       struct got_entry *ent;
   2564  1.1  christos     } got;
   2565  1.1  christos };
   2566  1.1  christos 
   2567  1.1  christos /* The same for PLT.  */
   2568  1.1  christos struct plt_entry
   2569  1.1  christos {
   2570  1.1  christos   struct plt_entry *next;
   2571  1.1  christos 
   2572  1.1  christos   bfd_vma addend;
   2573  1.1  christos 
   2574  1.1  christos   union
   2575  1.1  christos     {
   2576  1.1  christos       bfd_signed_vma refcount;
   2577  1.1  christos       bfd_vma offset;
   2578  1.1  christos     } plt;
   2579  1.1  christos };
   2580  1.1  christos 
   2581  1.1  christos struct ppc64_elf_obj_tdata
   2582  1.1  christos {
   2583  1.1  christos   struct elf_obj_tdata elf;
   2584  1.1  christos 
   2585  1.1  christos   /* Shortcuts to dynamic linker sections.  */
   2586  1.1  christos   asection *got;
   2587  1.1  christos   asection *relgot;
   2588  1.1  christos 
   2589  1.1  christos   /* Used during garbage collection.  We attach global symbols defined
   2590  1.1  christos      on removed .opd entries to this section so that the sym is removed.  */
   2591  1.1  christos   asection *deleted_section;
   2592  1.1  christos 
   2593  1.1  christos   /* TLS local dynamic got entry handling.  Support for multiple GOT
   2594  1.1  christos      sections means we potentially need one of these for each input bfd.  */
   2595  1.1  christos   struct got_entry tlsld_got;
   2596  1.1  christos 
   2597  1.1  christos   /* A copy of relocs before they are modified for --emit-relocs.  */
   2598  1.1  christos   Elf_Internal_Rela *opd_relocs;
   2599  1.1  christos 
   2600  1.1  christos   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   2601  1.1  christos      the reloc to be in the range -32768 to 32767.  */
   2602  1.1  christos   unsigned int has_small_toc_reloc;
   2603  1.1  christos };
   2604  1.1  christos 
   2605  1.1  christos #define ppc64_elf_tdata(bfd) \
   2606  1.1  christos   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   2607  1.1  christos 
   2608  1.1  christos #define ppc64_tlsld_got(bfd) \
   2609  1.1  christos   (&ppc64_elf_tdata (bfd)->tlsld_got)
   2610  1.1  christos 
   2611  1.1  christos #define is_ppc64_elf(bfd) \
   2612  1.1  christos   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   2613  1.1  christos    && elf_object_id (bfd) == PPC64_ELF_DATA)
   2614  1.1  christos 
   2615  1.1  christos /* Override the generic function because we store some extras.  */
   2616  1.1  christos 
   2617  1.1  christos static bfd_boolean
   2618  1.1  christos ppc64_elf_mkobject (bfd *abfd)
   2619  1.1  christos {
   2620  1.1  christos   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   2621  1.1  christos 				  PPC64_ELF_DATA);
   2622  1.1  christos }
   2623  1.1  christos 
   2624  1.1  christos /* Fix bad default arch selected for a 64 bit input bfd when the
   2625  1.1  christos    default is 32 bit.  */
   2626  1.1  christos 
   2627  1.1  christos static bfd_boolean
   2628  1.1  christos ppc64_elf_object_p (bfd *abfd)
   2629  1.1  christos {
   2630  1.1  christos   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
   2631  1.1  christos     {
   2632  1.1  christos       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   2633  1.1  christos 
   2634  1.1  christos       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   2635  1.1  christos 	{
   2636  1.1  christos 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   2637  1.1  christos 	  abfd->arch_info = abfd->arch_info->next;
   2638  1.1  christos 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   2639  1.1  christos 	}
   2640  1.1  christos     }
   2641  1.1  christos   return TRUE;
   2642  1.1  christos }
   2643  1.1  christos 
   2644  1.1  christos /* Support for core dump NOTE sections.  */
   2645  1.1  christos 
   2646  1.1  christos static bfd_boolean
   2647  1.1  christos ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   2648  1.1  christos {
   2649  1.1  christos   size_t offset, size;
   2650  1.1  christos 
   2651  1.1  christos   if (note->descsz != 504)
   2652  1.1  christos     return FALSE;
   2653  1.1  christos 
   2654  1.1  christos   /* pr_cursig */
   2655  1.1  christos   elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
   2656  1.1  christos 
   2657  1.1  christos   /* pr_pid */
   2658  1.1  christos   elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
   2659  1.1  christos 
   2660  1.1  christos   /* pr_reg */
   2661  1.1  christos   offset = 112;
   2662  1.1  christos   size = 384;
   2663  1.1  christos 
   2664  1.1  christos   /* Make a ".reg/999" section.  */
   2665  1.1  christos   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   2666  1.1  christos 					  size, note->descpos + offset);
   2667  1.1  christos }
   2668  1.1  christos 
   2669  1.1  christos static bfd_boolean
   2670  1.1  christos ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   2671  1.1  christos {
   2672  1.1  christos   if (note->descsz != 136)
   2673  1.1  christos     return FALSE;
   2674  1.1  christos 
   2675  1.1  christos   elf_tdata (abfd)->core_program
   2676  1.1  christos     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   2677  1.1  christos   elf_tdata (abfd)->core_command
   2678  1.1  christos     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   2679  1.1  christos 
   2680  1.1  christos   return TRUE;
   2681  1.1  christos }
   2682  1.1  christos 
   2683  1.1  christos static char *
   2684  1.1  christos ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   2685  1.1  christos 			   ...)
   2686  1.1  christos {
   2687  1.1  christos   switch (note_type)
   2688  1.1  christos     {
   2689  1.1  christos     default:
   2690  1.1  christos       return NULL;
   2691  1.1  christos 
   2692  1.1  christos     case NT_PRPSINFO:
   2693  1.1  christos       {
   2694  1.1  christos 	char data[136];
   2695  1.1  christos 	va_list ap;
   2696  1.1  christos 
   2697  1.1  christos 	va_start (ap, note_type);
   2698  1.1  christos 	memset (data, 0, 40);
   2699  1.1  christos 	strncpy (data + 40, va_arg (ap, const char *), 16);
   2700  1.1  christos 	strncpy (data + 56, va_arg (ap, const char *), 80);
   2701  1.1  christos 	va_end (ap);
   2702  1.1  christos 	return elfcore_write_note (abfd, buf, bufsiz,
   2703  1.1  christos 				   "CORE", note_type, data, sizeof (data));
   2704  1.1  christos       }
   2705  1.1  christos 
   2706  1.1  christos     case NT_PRSTATUS:
   2707  1.1  christos       {
   2708  1.1  christos 	char data[504];
   2709  1.1  christos 	va_list ap;
   2710  1.1  christos 	long pid;
   2711  1.1  christos 	int cursig;
   2712  1.1  christos 	const void *greg;
   2713  1.1  christos 
   2714  1.1  christos 	va_start (ap, note_type);
   2715  1.1  christos 	memset (data, 0, 112);
   2716  1.1  christos 	pid = va_arg (ap, long);
   2717  1.1  christos 	bfd_put_32 (abfd, pid, data + 32);
   2718  1.1  christos 	cursig = va_arg (ap, int);
   2719  1.1  christos 	bfd_put_16 (abfd, cursig, data + 12);
   2720  1.1  christos 	greg = va_arg (ap, const void *);
   2721  1.1  christos 	memcpy (data + 112, greg, 384);
   2722  1.1  christos 	memset (data + 496, 0, 8);
   2723  1.1  christos 	va_end (ap);
   2724  1.1  christos 	return elfcore_write_note (abfd, buf, bufsiz,
   2725  1.1  christos 				   "CORE", note_type, data, sizeof (data));
   2726  1.1  christos       }
   2727  1.1  christos     }
   2728  1.1  christos }
   2729  1.1  christos 
   2730  1.1  christos /* Merge backend specific data from an object file to the output
   2731  1.1  christos    object file when linking.  */
   2732  1.1  christos 
   2733  1.1  christos static bfd_boolean
   2734  1.1  christos ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   2735  1.1  christos {
   2736  1.1  christos   /* Check if we have the same endianess.  */
   2737  1.1  christos   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
   2738  1.1  christos       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
   2739  1.1  christos       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
   2740  1.1  christos     {
   2741  1.1  christos       const char *msg;
   2742  1.1  christos 
   2743  1.1  christos       if (bfd_big_endian (ibfd))
   2744  1.1  christos 	msg = _("%B: compiled for a big endian system "
   2745  1.1  christos 		"and target is little endian");
   2746  1.1  christos       else
   2747  1.1  christos 	msg = _("%B: compiled for a little endian system "
   2748  1.1  christos 		"and target is big endian");
   2749  1.1  christos 
   2750  1.1  christos       (*_bfd_error_handler) (msg, ibfd);
   2751  1.1  christos 
   2752  1.1  christos       bfd_set_error (bfd_error_wrong_format);
   2753  1.1  christos       return FALSE;
   2754  1.1  christos     }
   2755  1.1  christos 
   2756  1.1  christos   return TRUE;
   2757  1.1  christos }
   2758  1.1  christos 
   2759  1.1  christos /* Add extra PPC sections.  */
   2760  1.1  christos 
   2761  1.1  christos static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
   2762  1.1  christos {
   2763  1.1  christos   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
   2764  1.1  christos   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2765  1.1  christos   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2766  1.1  christos   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2767  1.1  christos   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2768  1.1  christos   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2769  1.1  christos   { NULL,                     0,  0, 0,            0 }
   2770  1.1  christos };
   2771  1.1  christos 
   2772  1.1  christos enum _ppc64_sec_type {
   2773  1.1  christos   sec_normal = 0,
   2774  1.1  christos   sec_opd = 1,
   2775  1.1  christos   sec_toc = 2
   2776  1.1  christos };
   2777  1.1  christos 
   2778  1.1  christos struct _ppc64_elf_section_data
   2779  1.1  christos {
   2780  1.1  christos   struct bfd_elf_section_data elf;
   2781  1.1  christos 
   2782  1.1  christos   union
   2783  1.1  christos   {
   2784  1.1  christos     /* An array with one entry for each opd function descriptor.  */
   2785  1.1  christos     struct _opd_sec_data
   2786  1.1  christos     {
   2787  1.1  christos       /* Points to the function code section for local opd entries.  */
   2788  1.1  christos       asection **func_sec;
   2789  1.1  christos 
   2790  1.1  christos       /* After editing .opd, adjust references to opd local syms.  */
   2791  1.1  christos       long *adjust;
   2792  1.1  christos     } opd;
   2793  1.1  christos 
   2794  1.1  christos     /* An array for toc sections, indexed by offset/8.  */
   2795  1.1  christos     struct _toc_sec_data
   2796  1.1  christos     {
   2797  1.1  christos       /* Specifies the relocation symbol index used at a given toc offset.  */
   2798  1.1  christos       unsigned *symndx;
   2799  1.1  christos 
   2800  1.1  christos       /* And the relocation addend.  */
   2801  1.1  christos       bfd_vma *add;
   2802  1.1  christos     } toc;
   2803  1.1  christos   } u;
   2804  1.1  christos 
   2805  1.1  christos   enum _ppc64_sec_type sec_type:2;
   2806  1.1  christos 
   2807  1.1  christos   /* Flag set when small branches are detected.  Used to
   2808  1.1  christos      select suitable defaults for the stub group size.  */
   2809  1.1  christos   unsigned int has_14bit_branch:1;
   2810  1.1  christos };
   2811  1.1  christos 
   2812  1.1  christos #define ppc64_elf_section_data(sec) \
   2813  1.1  christos   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
   2814  1.1  christos 
   2815  1.1  christos static bfd_boolean
   2816  1.1  christos ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
   2817  1.1  christos {
   2818  1.1  christos   if (!sec->used_by_bfd)
   2819  1.1  christos     {
   2820  1.1  christos       struct _ppc64_elf_section_data *sdata;
   2821  1.1  christos       bfd_size_type amt = sizeof (*sdata);
   2822  1.1  christos 
   2823  1.1  christos       sdata = bfd_zalloc (abfd, amt);
   2824  1.1  christos       if (sdata == NULL)
   2825  1.1  christos 	return FALSE;
   2826  1.1  christos       sec->used_by_bfd = sdata;
   2827  1.1  christos     }
   2828  1.1  christos 
   2829  1.1  christos   return _bfd_elf_new_section_hook (abfd, sec);
   2830  1.1  christos }
   2831  1.1  christos 
   2832  1.1  christos static struct _opd_sec_data *
   2833  1.1  christos get_opd_info (asection * sec)
   2834  1.1  christos {
   2835  1.1  christos   if (sec != NULL
   2836  1.1  christos       && ppc64_elf_section_data (sec) != NULL
   2837  1.1  christos       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   2838  1.1  christos     return &ppc64_elf_section_data (sec)->u.opd;
   2839  1.1  christos   return NULL;
   2840  1.1  christos }
   2841  1.1  christos 
   2842  1.1  christos /* Parameters for the qsort hook.  */
   2844  1.1  christos static bfd_boolean synthetic_relocatable;
   2845  1.1  christos 
   2846  1.1  christos /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
   2847  1.1  christos 
   2848  1.1  christos static int
   2849  1.1  christos compare_symbols (const void *ap, const void *bp)
   2850  1.1  christos {
   2851  1.1  christos   const asymbol *a = * (const asymbol **) ap;
   2852  1.1  christos   const asymbol *b = * (const asymbol **) bp;
   2853  1.1  christos 
   2854  1.1  christos   /* Section symbols first.  */
   2855  1.1  christos   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
   2856  1.1  christos     return -1;
   2857  1.1  christos   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
   2858  1.1  christos     return 1;
   2859  1.1  christos 
   2860  1.1  christos   /* then .opd symbols.  */
   2861  1.1  christos   if (strcmp (a->section->name, ".opd") == 0
   2862  1.1  christos       && strcmp (b->section->name, ".opd") != 0)
   2863  1.1  christos     return -1;
   2864  1.1  christos   if (strcmp (a->section->name, ".opd") != 0
   2865  1.1  christos       && strcmp (b->section->name, ".opd") == 0)
   2866  1.1  christos     return 1;
   2867  1.1  christos 
   2868  1.1  christos   /* then other code symbols.  */
   2869  1.1  christos   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2870  1.1  christos       == (SEC_CODE | SEC_ALLOC)
   2871  1.1  christos       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2872  1.1  christos 	 != (SEC_CODE | SEC_ALLOC))
   2873  1.1  christos     return -1;
   2874  1.1  christos 
   2875  1.1  christos   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2876  1.1  christos       != (SEC_CODE | SEC_ALLOC)
   2877  1.1  christos       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2878  1.1  christos 	 == (SEC_CODE | SEC_ALLOC))
   2879  1.1  christos     return 1;
   2880  1.1  christos 
   2881  1.1  christos   if (synthetic_relocatable)
   2882  1.1  christos     {
   2883  1.1  christos       if (a->section->id < b->section->id)
   2884  1.1  christos 	return -1;
   2885  1.1  christos 
   2886  1.1  christos       if (a->section->id > b->section->id)
   2887  1.1  christos 	return 1;
   2888  1.1  christos     }
   2889  1.1  christos 
   2890  1.1  christos   if (a->value + a->section->vma < b->value + b->section->vma)
   2891  1.1  christos     return -1;
   2892  1.1  christos 
   2893  1.1  christos   if (a->value + a->section->vma > b->value + b->section->vma)
   2894  1.1  christos     return 1;
   2895  1.1  christos 
   2896  1.1  christos   /* For syms with the same value, prefer strong dynamic global function
   2897  1.1  christos      syms over other syms.  */
   2898  1.1  christos   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
   2899  1.1  christos     return -1;
   2900  1.1  christos 
   2901  1.1  christos   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
   2902  1.1  christos     return 1;
   2903  1.1  christos 
   2904  1.1  christos   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
   2905  1.1  christos     return -1;
   2906  1.1  christos 
   2907  1.1  christos   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
   2908  1.1  christos     return 1;
   2909  1.1  christos 
   2910  1.1  christos   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
   2911  1.1  christos     return -1;
   2912  1.1  christos 
   2913  1.1  christos   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
   2914  1.1  christos     return 1;
   2915  1.1  christos 
   2916  1.1  christos   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
   2917  1.1  christos     return -1;
   2918  1.1  christos 
   2919  1.1  christos   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
   2920  1.1  christos     return 1;
   2921  1.1  christos 
   2922  1.1  christos   return 0;
   2923  1.1  christos }
   2924  1.1  christos 
   2925  1.1  christos /* Search SYMS for a symbol of the given VALUE.  */
   2926  1.1  christos 
   2927  1.1  christos static asymbol *
   2928  1.1  christos sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
   2929  1.1  christos {
   2930  1.1  christos   long mid;
   2931  1.1  christos 
   2932  1.1  christos   if (id == -1)
   2933  1.1  christos     {
   2934  1.1  christos       while (lo < hi)
   2935  1.1  christos 	{
   2936  1.1  christos 	  mid = (lo + hi) >> 1;
   2937  1.1  christos 	  if (syms[mid]->value + syms[mid]->section->vma < value)
   2938  1.1  christos 	    lo = mid + 1;
   2939  1.1  christos 	  else if (syms[mid]->value + syms[mid]->section->vma > value)
   2940  1.1  christos 	    hi = mid;
   2941  1.1  christos 	  else
   2942  1.1  christos 	    return syms[mid];
   2943  1.1  christos 	}
   2944  1.1  christos     }
   2945  1.1  christos   else
   2946  1.1  christos     {
   2947  1.1  christos       while (lo < hi)
   2948  1.1  christos 	{
   2949  1.1  christos 	  mid = (lo + hi) >> 1;
   2950  1.1  christos 	  if (syms[mid]->section->id < id)
   2951  1.1  christos 	    lo = mid + 1;
   2952  1.1  christos 	  else if (syms[mid]->section->id > id)
   2953  1.1  christos 	    hi = mid;
   2954  1.1  christos 	  else if (syms[mid]->value < value)
   2955  1.1  christos 	    lo = mid + 1;
   2956  1.1  christos 	  else if (syms[mid]->value > value)
   2957  1.1  christos 	    hi = mid;
   2958  1.1  christos 	  else
   2959  1.1  christos 	    return syms[mid];
   2960  1.1  christos 	}
   2961  1.1  christos     }
   2962  1.1  christos   return NULL;
   2963  1.1  christos }
   2964  1.1  christos 
   2965  1.1  christos static bfd_boolean
   2966  1.1  christos section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   2967  1.1  christos {
   2968  1.1  christos   bfd_vma vma = *(bfd_vma *) ptr;
   2969  1.1  christos   return ((section->flags & SEC_ALLOC) != 0
   2970  1.1  christos 	  && section->vma <= vma
   2971  1.1  christos 	  && vma < section->vma + section->size);
   2972  1.1  christos }
   2973  1.1  christos 
   2974  1.1  christos /* Create synthetic symbols, effectively restoring "dot-symbol" function
   2975  1.1  christos    entry syms.  Also generate @plt symbols for the glink branch table.  */
   2976  1.1  christos 
   2977  1.1  christos static long
   2978  1.1  christos ppc64_elf_get_synthetic_symtab (bfd *abfd,
   2979  1.1  christos 				long static_count, asymbol **static_syms,
   2980  1.1  christos 				long dyn_count, asymbol **dyn_syms,
   2981  1.1  christos 				asymbol **ret)
   2982  1.1  christos {
   2983  1.1  christos   asymbol *s;
   2984  1.1  christos   long i;
   2985  1.1  christos   long count;
   2986  1.1  christos   char *names;
   2987  1.1  christos   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
   2988  1.1  christos   asection *opd;
   2989  1.1  christos   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   2990  1.1  christos   asymbol **syms;
   2991  1.1  christos 
   2992  1.1  christos   *ret = NULL;
   2993  1.1  christos 
   2994  1.1  christos   opd = bfd_get_section_by_name (abfd, ".opd");
   2995  1.1  christos   if (opd == NULL)
   2996  1.1  christos     return 0;
   2997  1.1  christos 
   2998  1.1  christos   symcount = static_count;
   2999  1.1  christos   if (!relocatable)
   3000  1.1  christos     symcount += dyn_count;
   3001  1.1  christos   if (symcount == 0)
   3002  1.1  christos     return 0;
   3003  1.1  christos 
   3004  1.1  christos   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
   3005  1.1  christos   if (syms == NULL)
   3006  1.1  christos     return -1;
   3007  1.1  christos 
   3008  1.1  christos   if (!relocatable && static_count != 0 && dyn_count != 0)
   3009  1.1  christos     {
   3010  1.1  christos       /* Use both symbol tables.  */
   3011  1.1  christos       memcpy (syms, static_syms, static_count * sizeof (*syms));
   3012  1.1  christos       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
   3013  1.1  christos     }
   3014  1.1  christos   else if (!relocatable && static_count == 0)
   3015  1.1  christos     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
   3016  1.1  christos   else
   3017  1.1  christos     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
   3018  1.1  christos 
   3019  1.1  christos   synthetic_relocatable = relocatable;
   3020  1.1  christos   qsort (syms, symcount, sizeof (*syms), compare_symbols);
   3021  1.1  christos 
   3022  1.1  christos   if (!relocatable && symcount > 1)
   3023  1.1  christos     {
   3024  1.1  christos       long j;
   3025  1.1  christos       /* Trim duplicate syms, since we may have merged the normal and
   3026  1.1  christos 	 dynamic symbols.  Actually, we only care about syms that have
   3027  1.1  christos 	 different values, so trim any with the same value.  */
   3028  1.1  christos       for (i = 1, j = 1; i < symcount; ++i)
   3029  1.1  christos 	if (syms[i - 1]->value + syms[i - 1]->section->vma
   3030  1.1  christos 	    != syms[i]->value + syms[i]->section->vma)
   3031  1.1  christos 	  syms[j++] = syms[i];
   3032  1.1  christos       symcount = j;
   3033  1.1  christos     }
   3034  1.1  christos 
   3035  1.1  christos   i = 0;
   3036  1.1  christos   if (strcmp (syms[i]->section->name, ".opd") == 0)
   3037  1.1  christos     ++i;
   3038  1.1  christos   codesecsym = i;
   3039  1.1  christos 
   3040  1.1  christos   for (; i < symcount; ++i)
   3041  1.1  christos     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3042  1.1  christos 	 != (SEC_CODE | SEC_ALLOC))
   3043  1.1  christos 	|| (syms[i]->flags & BSF_SECTION_SYM) == 0)
   3044  1.1  christos       break;
   3045  1.1  christos   codesecsymend = i;
   3046  1.1  christos 
   3047  1.1  christos   for (; i < symcount; ++i)
   3048  1.1  christos     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
   3049  1.1  christos       break;
   3050  1.1  christos   secsymend = i;
   3051  1.1  christos 
   3052  1.1  christos   for (; i < symcount; ++i)
   3053  1.1  christos     if (strcmp (syms[i]->section->name, ".opd") != 0)
   3054  1.1  christos       break;
   3055  1.1  christos   opdsymend = i;
   3056  1.1  christos 
   3057  1.1  christos   for (; i < symcount; ++i)
   3058  1.1  christos     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3059  1.1  christos 	!= (SEC_CODE | SEC_ALLOC))
   3060  1.1  christos       break;
   3061  1.1  christos   symcount = i;
   3062  1.1  christos 
   3063  1.1  christos   count = 0;
   3064  1.1  christos 
   3065  1.1  christos   if (relocatable)
   3066  1.1  christos     {
   3067  1.1  christos       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3068  1.1  christos       arelent *r;
   3069  1.1  christos       size_t size;
   3070  1.1  christos       long relcount;
   3071  1.1  christos 
   3072  1.1  christos       if (opdsymend == secsymend)
   3073  1.1  christos 	goto done;
   3074  1.1  christos 
   3075  1.1  christos       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3076  1.1  christos       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
   3077  1.1  christos       if (relcount == 0)
   3078  1.1  christos 	goto done;
   3079  1.1  christos 
   3080  1.1  christos       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
   3081  1.1  christos 	{
   3082  1.1  christos 	  count = -1;
   3083  1.1  christos 	  goto done;
   3084  1.1  christos 	}
   3085  1.1  christos 
   3086  1.1  christos       size = 0;
   3087  1.1  christos       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3088  1.1  christos 	{
   3089  1.1  christos 	  asymbol *sym;
   3090  1.1  christos 
   3091  1.1  christos 	  while (r < opd->relocation + relcount
   3092  1.1  christos 		 && r->address < syms[i]->value + opd->vma)
   3093  1.1  christos 	    ++r;
   3094  1.1  christos 
   3095  1.1  christos 	  if (r == opd->relocation + relcount)
   3096  1.1  christos 	    break;
   3097  1.1  christos 
   3098  1.1  christos 	  if (r->address != syms[i]->value + opd->vma)
   3099  1.1  christos 	    continue;
   3100  1.1  christos 
   3101  1.1  christos 	  if (r->howto->type != R_PPC64_ADDR64)
   3102  1.1  christos 	    continue;
   3103  1.1  christos 
   3104  1.1  christos 	  sym = *r->sym_ptr_ptr;
   3105  1.1  christos 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3106  1.1  christos 			      sym->section->id, sym->value + r->addend))
   3107  1.1  christos 	    {
   3108  1.1  christos 	      ++count;
   3109  1.1  christos 	      size += sizeof (asymbol);
   3110  1.1  christos 	      size += strlen (syms[i]->name) + 2;
   3111  1.1  christos 	    }
   3112  1.1  christos 	}
   3113  1.1  christos 
   3114  1.1  christos       s = *ret = bfd_malloc (size);
   3115  1.1  christos       if (s == NULL)
   3116  1.1  christos 	{
   3117  1.1  christos 	  count = -1;
   3118  1.1  christos 	  goto done;
   3119  1.1  christos 	}
   3120  1.1  christos 
   3121  1.1  christos       names = (char *) (s + count);
   3122  1.1  christos 
   3123  1.1  christos       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3124  1.1  christos 	{
   3125  1.1  christos 	  asymbol *sym;
   3126  1.1  christos 
   3127  1.1  christos 	  while (r < opd->relocation + relcount
   3128  1.1  christos 		 && r->address < syms[i]->value + opd->vma)
   3129  1.1  christos 	    ++r;
   3130  1.1  christos 
   3131  1.1  christos 	  if (r == opd->relocation + relcount)
   3132  1.1  christos 	    break;
   3133  1.1  christos 
   3134  1.1  christos 	  if (r->address != syms[i]->value + opd->vma)
   3135  1.1  christos 	    continue;
   3136  1.1  christos 
   3137  1.1  christos 	  if (r->howto->type != R_PPC64_ADDR64)
   3138  1.1  christos 	    continue;
   3139  1.1  christos 
   3140  1.1  christos 	  sym = *r->sym_ptr_ptr;
   3141  1.1  christos 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3142  1.1  christos 			      sym->section->id, sym->value + r->addend))
   3143  1.1  christos 	    {
   3144  1.1  christos 	      size_t len;
   3145  1.1  christos 
   3146  1.1  christos 	      *s = *syms[i];
   3147  1.1  christos 	      s->flags |= BSF_SYNTHETIC;
   3148  1.1  christos 	      s->section = sym->section;
   3149  1.1  christos 	      s->value = sym->value + r->addend;
   3150  1.1  christos 	      s->name = names;
   3151  1.1  christos 	      *names++ = '.';
   3152  1.1  christos 	      len = strlen (syms[i]->name);
   3153  1.1  christos 	      memcpy (names, syms[i]->name, len + 1);
   3154  1.1  christos 	      names += len + 1;
   3155  1.1  christos 	      /* Have udata.p point back to the original symbol this
   3156  1.1  christos 		 synthetic symbol was derived from.  */
   3157  1.1  christos 	      s->udata.p = syms[i];
   3158  1.1  christos 	      s++;
   3159  1.1  christos 	    }
   3160  1.1  christos 	}
   3161  1.1  christos     }
   3162  1.1  christos   else
   3163  1.1  christos     {
   3164  1.1  christos       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3165  1.1  christos       bfd_byte *contents;
   3166  1.1  christos       size_t size;
   3167  1.1  christos       long plt_count = 0;
   3168  1.1  christos       bfd_vma glink_vma = 0, resolv_vma = 0;
   3169  1.1  christos       asection *dynamic, *glink = NULL, *relplt = NULL;
   3170  1.1  christos       arelent *p;
   3171  1.1  christos 
   3172  1.1  christos       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
   3173  1.1  christos 	{
   3174  1.1  christos 	  if (contents)
   3175  1.1  christos 	    {
   3176  1.1  christos 	    free_contents_and_exit:
   3177  1.1  christos 	      free (contents);
   3178  1.1  christos 	    }
   3179  1.1  christos 	  count = -1;
   3180  1.1  christos 	  goto done;
   3181  1.1  christos 	}
   3182  1.1  christos 
   3183  1.1  christos       size = 0;
   3184  1.1  christos       for (i = secsymend; i < opdsymend; ++i)
   3185  1.1  christos 	{
   3186  1.1  christos 	  bfd_vma ent;
   3187  1.1  christos 
   3188  1.1  christos 	  /* Ignore bogus symbols.  */
   3189  1.1  christos 	  if (syms[i]->value > opd->size - 8)
   3190  1.1  christos 	    continue;
   3191  1.1  christos 
   3192  1.1  christos 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3193  1.1  christos 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3194  1.1  christos 	    {
   3195  1.1  christos 	      ++count;
   3196  1.1  christos 	      size += sizeof (asymbol);
   3197  1.1  christos 	      size += strlen (syms[i]->name) + 2;
   3198  1.1  christos 	    }
   3199  1.1  christos 	}
   3200  1.1  christos 
   3201  1.1  christos       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
   3202  1.1  christos       if (dyn_count != 0
   3203  1.1  christos 	  && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
   3204  1.1  christos 	{
   3205  1.1  christos 	  bfd_byte *dynbuf, *extdyn, *extdynend;
   3206  1.1  christos 	  size_t extdynsize;
   3207  1.1  christos 	  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3208  1.1  christos 
   3209  1.1  christos 	  if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   3210  1.1  christos 	    goto free_contents_and_exit;
   3211  1.1  christos 
   3212  1.1  christos 	  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   3213  1.1  christos 	  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   3214  1.1  christos 
   3215  1.1  christos 	  extdyn = dynbuf;
   3216  1.1  christos 	  extdynend = extdyn + dynamic->size;
   3217  1.1  christos 	  for (; extdyn < extdynend; extdyn += extdynsize)
   3218  1.1  christos 	    {
   3219  1.1  christos 	      Elf_Internal_Dyn dyn;
   3220  1.1  christos 	      (*swap_dyn_in) (abfd, extdyn, &dyn);
   3221  1.1  christos 
   3222  1.1  christos 	      if (dyn.d_tag == DT_NULL)
   3223  1.1  christos 		break;
   3224  1.1  christos 
   3225  1.1  christos 	      if (dyn.d_tag == DT_PPC64_GLINK)
   3226  1.1  christos 		{
   3227  1.1  christos 		  /* The first glink stub starts at offset 32; see comment in
   3228  1.1  christos 		     ppc64_elf_finish_dynamic_sections. */
   3229  1.1  christos 		  glink_vma = dyn.d_un.d_val + 32;
   3230  1.1  christos 		  /* The .glink section usually does not survive the final
   3231  1.1  christos 		     link; search for the section (usually .text) where the
   3232  1.1  christos 		     glink stubs now reside.  */
   3233  1.1  christos 		  glink = bfd_sections_find_if (abfd, section_covers_vma,
   3234  1.1  christos 						&glink_vma);
   3235  1.1  christos 		  break;
   3236  1.1  christos 		}
   3237  1.1  christos 	    }
   3238  1.1  christos 
   3239  1.1  christos 	  free (dynbuf);
   3240  1.1  christos 	}
   3241  1.1  christos 
   3242  1.1  christos       if (glink != NULL)
   3243  1.1  christos 	{
   3244  1.1  christos 	  /* Determine __glink trampoline by reading the relative branch
   3245  1.1  christos 	     from the first glink stub.  */
   3246  1.1  christos 	  bfd_byte buf[4];
   3247  1.1  christos 	  if (bfd_get_section_contents (abfd, glink, buf,
   3248  1.1  christos 					glink_vma + 4 - glink->vma, 4))
   3249  1.1  christos 	    {
   3250  1.1  christos 	      unsigned int insn = bfd_get_32 (abfd, buf);
   3251  1.1  christos 	      insn ^= B_DOT;
   3252  1.1  christos 	      if ((insn & ~0x3fffffc) == 0)
   3253  1.1  christos 		resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
   3254  1.1  christos 	    }
   3255  1.1  christos 
   3256  1.1  christos 	  if (resolv_vma)
   3257  1.1  christos 	    size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   3258  1.1  christos 
   3259  1.1  christos 	  relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   3260  1.1  christos 	  if (relplt != NULL)
   3261  1.1  christos 	    {
   3262  1.1  christos 	      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3263  1.1  christos 	      if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
   3264  1.1  christos 		goto free_contents_and_exit;
   3265  1.1  christos 
   3266  1.1  christos 	      plt_count = relplt->size / sizeof (Elf64_External_Rela);
   3267  1.1  christos 	      size += plt_count * sizeof (asymbol);
   3268  1.1  christos 
   3269  1.1  christos 	      p = relplt->relocation;
   3270  1.1  christos 	      for (i = 0; i < plt_count; i++, p++)
   3271  1.1  christos 		{
   3272  1.1  christos 		  size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   3273  1.1  christos 		  if (p->addend != 0)
   3274  1.1  christos 		    size += sizeof ("+0x") - 1 + 16;
   3275  1.1  christos 		}
   3276  1.1  christos 	    }
   3277  1.1  christos 	}
   3278  1.1  christos 
   3279  1.1  christos       s = *ret = bfd_malloc (size);
   3280  1.1  christos       if (s == NULL)
   3281  1.1  christos 	goto free_contents_and_exit;
   3282  1.1  christos 
   3283  1.1  christos       names = (char *) (s + count + plt_count + (resolv_vma != 0));
   3284  1.1  christos 
   3285  1.1  christos       for (i = secsymend; i < opdsymend; ++i)
   3286  1.1  christos 	{
   3287  1.1  christos 	  bfd_vma ent;
   3288  1.1  christos 
   3289  1.1  christos 	  if (syms[i]->value > opd->size - 8)
   3290  1.1  christos 	    continue;
   3291  1.1  christos 
   3292  1.1  christos 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3293  1.1  christos 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3294  1.1  christos 	    {
   3295  1.1  christos 	      long lo, hi;
   3296  1.1  christos 	      size_t len;
   3297  1.1  christos 	      asection *sec = abfd->sections;
   3298  1.1  christos 
   3299  1.1  christos 	      *s = *syms[i];
   3300  1.1  christos 	      lo = codesecsym;
   3301  1.1  christos 	      hi = codesecsymend;
   3302  1.1  christos 	      while (lo < hi)
   3303  1.1  christos 		{
   3304  1.1  christos 		  long mid = (lo + hi) >> 1;
   3305  1.1  christos 		  if (syms[mid]->section->vma < ent)
   3306  1.1  christos 		    lo = mid + 1;
   3307  1.1  christos 		  else if (syms[mid]->section->vma > ent)
   3308  1.1  christos 		    hi = mid;
   3309  1.1  christos 		  else
   3310  1.1  christos 		    {
   3311  1.1  christos 		      sec = syms[mid]->section;
   3312  1.1  christos 		      break;
   3313  1.1  christos 		    }
   3314  1.1  christos 		}
   3315  1.1  christos 
   3316  1.1  christos 	      if (lo >= hi && lo > codesecsym)
   3317  1.1  christos 		sec = syms[lo - 1]->section;
   3318  1.1  christos 
   3319  1.1  christos 	      for (; sec != NULL; sec = sec->next)
   3320  1.1  christos 		{
   3321  1.1  christos 		  if (sec->vma > ent)
   3322  1.1  christos 		    break;
   3323  1.1  christos 		  if ((sec->flags & SEC_ALLOC) == 0
   3324  1.1  christos 		      || (sec->flags & SEC_LOAD) == 0)
   3325  1.1  christos 		    break;
   3326  1.1  christos 		  if ((sec->flags & SEC_CODE) != 0)
   3327  1.1  christos 		    s->section = sec;
   3328  1.1  christos 		}
   3329  1.1  christos 	      s->flags |= BSF_SYNTHETIC;
   3330  1.1  christos 	      s->value = ent - s->section->vma;
   3331  1.1  christos 	      s->name = names;
   3332  1.1  christos 	      *names++ = '.';
   3333  1.1  christos 	      len = strlen (syms[i]->name);
   3334  1.1  christos 	      memcpy (names, syms[i]->name, len + 1);
   3335  1.1  christos 	      names += len + 1;
   3336  1.1  christos 	      /* Have udata.p point back to the original symbol this
   3337  1.1  christos 		 synthetic symbol was derived from.  */
   3338  1.1  christos 	      s->udata.p = syms[i];
   3339  1.1  christos 	      s++;
   3340  1.1  christos 	    }
   3341  1.1  christos 	}
   3342  1.1  christos       free (contents);
   3343  1.1  christos 
   3344  1.1  christos       if (glink != NULL && relplt != NULL)
   3345  1.1  christos 	{
   3346  1.1  christos 	  if (resolv_vma)
   3347  1.1  christos 	    {
   3348  1.1  christos 	      /* Add a symbol for the main glink trampoline.  */
   3349  1.1  christos 	      memset (s, 0, sizeof *s);
   3350  1.1  christos 	      s->the_bfd = abfd;
   3351  1.1  christos 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   3352  1.1  christos 	      s->section = glink;
   3353  1.1  christos 	      s->value = resolv_vma - glink->vma;
   3354  1.1  christos 	      s->name = names;
   3355  1.1  christos 	      memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   3356  1.1  christos 	      names += sizeof ("__glink_PLTresolve");
   3357  1.1  christos 	      s++;
   3358  1.1  christos 	      count++;
   3359  1.1  christos 	    }
   3360  1.1  christos 
   3361  1.1  christos 	  /* FIXME: It would be very much nicer to put sym@plt on the
   3362  1.1  christos 	     stub rather than on the glink branch table entry.  The
   3363  1.1  christos 	     objdump disassembler would then use a sensible symbol
   3364  1.1  christos 	     name on plt calls.  The difficulty in doing so is
   3365  1.1  christos 	     a) finding the stubs, and,
   3366  1.1  christos 	     b) matching stubs against plt entries, and,
   3367  1.1  christos 	     c) there can be multiple stubs for a given plt entry.
   3368  1.1  christos 
   3369  1.1  christos 	     Solving (a) could be done by code scanning, but older
   3370  1.1  christos 	     ppc64 binaries used different stubs to current code.
   3371  1.1  christos 	     (b) is the tricky one since you need to known the toc
   3372  1.1  christos 	     pointer for at least one function that uses a pic stub to
   3373  1.1  christos 	     be able to calculate the plt address referenced.
   3374  1.1  christos 	     (c) means gdb would need to set multiple breakpoints (or
   3375  1.1  christos 	     find the glink branch itself) when setting breakpoints
   3376  1.1  christos 	     for pending shared library loads.  */
   3377  1.1  christos 	  p = relplt->relocation;
   3378  1.1  christos 	  for (i = 0; i < plt_count; i++, p++)
   3379  1.1  christos 	    {
   3380  1.1  christos 	      size_t len;
   3381  1.1  christos 
   3382  1.1  christos 	      *s = **p->sym_ptr_ptr;
   3383  1.1  christos 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   3384  1.1  christos 		 we are defining a symbol, ensure one of them is set.  */
   3385  1.1  christos 	      if ((s->flags & BSF_LOCAL) == 0)
   3386  1.1  christos 		s->flags |= BSF_GLOBAL;
   3387  1.1  christos 	      s->flags |= BSF_SYNTHETIC;
   3388  1.1  christos 	      s->section = glink;
   3389  1.1  christos 	      s->value = glink_vma - glink->vma;
   3390  1.1  christos 	      s->name = names;
   3391  1.1  christos 	      s->udata.p = NULL;
   3392  1.1  christos 	      len = strlen ((*p->sym_ptr_ptr)->name);
   3393  1.1  christos 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   3394  1.1  christos 	      names += len;
   3395  1.1  christos 	      if (p->addend != 0)
   3396  1.1  christos 		{
   3397  1.1  christos 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   3398  1.1  christos 		  names += sizeof ("+0x") - 1;
   3399  1.1  christos 		  bfd_sprintf_vma (abfd, names, p->addend);
   3400  1.1  christos 		  names += strlen (names);
   3401  1.1  christos 		}
   3402  1.1  christos 	      memcpy (names, "@plt", sizeof ("@plt"));
   3403  1.1  christos 	      names += sizeof ("@plt");
   3404  1.1  christos 	      s++;
   3405  1.1  christos 	      glink_vma += 8;
   3406  1.1  christos 	      if (i >= 0x8000)
   3407  1.1  christos 		glink_vma += 4;
   3408  1.1  christos 	    }
   3409  1.1  christos 	  count += plt_count;
   3410  1.1  christos 	}
   3411  1.1  christos     }
   3412  1.1  christos 
   3413  1.1  christos  done:
   3414  1.1  christos   free (syms);
   3415  1.1  christos   return count;
   3416  1.1  christos }
   3417  1.1  christos 
   3418  1.1  christos /* The following functions are specific to the ELF linker, while
   3420  1.1  christos    functions above are used generally.  Those named ppc64_elf_* are
   3421  1.1  christos    called by the main ELF linker code.  They appear in this file more
   3422  1.1  christos    or less in the order in which they are called.  eg.
   3423  1.1  christos    ppc64_elf_check_relocs is called early in the link process,
   3424  1.1  christos    ppc64_elf_finish_dynamic_sections is one of the last functions
   3425  1.1  christos    called.
   3426  1.1  christos 
   3427  1.1  christos    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   3428  1.1  christos    functions have both a function code symbol and a function descriptor
   3429  1.1  christos    symbol.  A call to foo in a relocatable object file looks like:
   3430  1.1  christos 
   3431  1.1  christos    .		.text
   3432  1.1  christos    .	x:
   3433  1.1  christos    .		bl	.foo
   3434  1.1  christos    .		nop
   3435  1.1  christos 
   3436  1.1  christos    The function definition in another object file might be:
   3437  1.1  christos 
   3438  1.1  christos    .		.section .opd
   3439  1.1  christos    .	foo:	.quad	.foo
   3440  1.1  christos    .		.quad	.TOC.@tocbase
   3441  1.1  christos    .		.quad	0
   3442  1.1  christos    .
   3443  1.1  christos    .		.text
   3444  1.1  christos    .	.foo:	blr
   3445  1.1  christos 
   3446  1.1  christos    When the linker resolves the call during a static link, the branch
   3447  1.1  christos    unsurprisingly just goes to .foo and the .opd information is unused.
   3448  1.1  christos    If the function definition is in a shared library, things are a little
   3449  1.1  christos    different:  The call goes via a plt call stub, the opd information gets
   3450  1.1  christos    copied to the plt, and the linker patches the nop.
   3451  1.1  christos 
   3452  1.1  christos    .	x:
   3453  1.1  christos    .		bl	.foo_stub
   3454  1.1  christos    .		ld	2,40(1)
   3455  1.1  christos    .
   3456  1.1  christos    .
   3457  1.1  christos    .	.foo_stub:
   3458  1.1  christos    .		addis	12,2,Lfoo@toc@ha	# in practice, the call stub
   3459  1.1  christos    .		addi	12,12,Lfoo@toc@l	# is slightly optimized, but
   3460  1.1  christos    .		std	2,40(1)			# this is the general idea
   3461  1.1  christos    .		ld	11,0(12)
   3462  1.1  christos    .		ld	2,8(12)
   3463  1.1  christos    .		mtctr	11
   3464  1.1  christos    .		ld	11,16(12)
   3465  1.1  christos    .		bctr
   3466  1.1  christos    .
   3467  1.1  christos    .		.section .plt
   3468  1.1  christos    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   3469  1.1  christos 
   3470  1.1  christos    The "reloc ()" notation is supposed to indicate that the linker emits
   3471  1.1  christos    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   3472  1.1  christos    copying.
   3473  1.1  christos 
   3474  1.1  christos    What are the difficulties here?  Well, firstly, the relocations
   3475  1.1  christos    examined by the linker in check_relocs are against the function code
   3476  1.1  christos    sym .foo, while the dynamic relocation in the plt is emitted against
   3477  1.1  christos    the function descriptor symbol, foo.  Somewhere along the line, we need
   3478  1.1  christos    to carefully copy dynamic link information from one symbol to the other.
   3479  1.1  christos    Secondly, the generic part of the elf linker will make .foo a dynamic
   3480  1.1  christos    symbol as is normal for most other backends.  We need foo dynamic
   3481  1.1  christos    instead, at least for an application final link.  However, when
   3482  1.1  christos    creating a shared library containing foo, we need to have both symbols
   3483  1.1  christos    dynamic so that references to .foo are satisfied during the early
   3484  1.1  christos    stages of linking.  Otherwise the linker might decide to pull in a
   3485  1.1  christos    definition from some other object, eg. a static library.
   3486  1.1  christos 
   3487  1.1  christos    Update: As of August 2004, we support a new convention.  Function
   3488  1.1  christos    calls may use the function descriptor symbol, ie. "bl foo".  This
   3489  1.1  christos    behaves exactly as "bl .foo".  */
   3490  1.1  christos 
   3491  1.1  christos /* The linker needs to keep track of the number of relocs that it
   3492  1.1  christos    decides to copy as dynamic relocs in check_relocs for each symbol.
   3493  1.1  christos    This is so that it can later discard them if they are found to be
   3494  1.1  christos    unnecessary.  We store the information in a field extending the
   3495  1.1  christos    regular ELF linker hash table.  */
   3496  1.1  christos 
   3497  1.1  christos struct ppc_dyn_relocs
   3498  1.1  christos {
   3499  1.1  christos   struct ppc_dyn_relocs *next;
   3500  1.1  christos 
   3501  1.1  christos   /* The input section of the reloc.  */
   3502  1.1  christos   asection *sec;
   3503  1.1  christos 
   3504  1.1  christos   /* Total number of relocs copied for the input section.  */
   3505  1.1  christos   bfd_size_type count;
   3506  1.1  christos 
   3507  1.1  christos   /* Number of pc-relative relocs copied for the input section.  */
   3508  1.1  christos   bfd_size_type pc_count;
   3509  1.1  christos };
   3510  1.1  christos 
   3511  1.1  christos /* Of those relocs that might be copied as dynamic relocs, this function
   3512  1.1  christos    selects those that must be copied when linking a shared library,
   3513  1.1  christos    even when the symbol is local.  */
   3514  1.1  christos 
   3515  1.1  christos static int
   3516  1.1  christos must_be_dyn_reloc (struct bfd_link_info *info,
   3517  1.1  christos 		   enum elf_ppc64_reloc_type r_type)
   3518  1.1  christos {
   3519  1.1  christos   switch (r_type)
   3520  1.1  christos     {
   3521  1.1  christos     default:
   3522  1.1  christos       return 1;
   3523  1.1  christos 
   3524  1.1  christos     case R_PPC64_REL32:
   3525  1.1  christos     case R_PPC64_REL64:
   3526  1.1  christos     case R_PPC64_REL30:
   3527  1.1  christos       return 0;
   3528  1.1  christos 
   3529  1.1  christos     case R_PPC64_TPREL16:
   3530  1.1  christos     case R_PPC64_TPREL16_LO:
   3531  1.1  christos     case R_PPC64_TPREL16_HI:
   3532  1.1  christos     case R_PPC64_TPREL16_HA:
   3533  1.1  christos     case R_PPC64_TPREL16_DS:
   3534  1.1  christos     case R_PPC64_TPREL16_LO_DS:
   3535  1.1  christos     case R_PPC64_TPREL16_HIGHER:
   3536  1.1  christos     case R_PPC64_TPREL16_HIGHERA:
   3537  1.1  christos     case R_PPC64_TPREL16_HIGHEST:
   3538  1.1  christos     case R_PPC64_TPREL16_HIGHESTA:
   3539  1.1  christos     case R_PPC64_TPREL64:
   3540  1.1  christos       return !info->executable;
   3541  1.1  christos     }
   3542  1.1  christos }
   3543  1.1  christos 
   3544  1.1  christos /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   3545  1.1  christos    copying dynamic variables from a shared lib into an app's dynbss
   3546  1.1  christos    section, and instead use a dynamic relocation to point into the
   3547  1.1  christos    shared lib.  With code that gcc generates, it's vital that this be
   3548  1.1  christos    enabled;  In the PowerPC64 ABI, the address of a function is actually
   3549  1.1  christos    the address of a function descriptor, which resides in the .opd
   3550  1.1  christos    section.  gcc uses the descriptor directly rather than going via the
   3551  1.1  christos    GOT as some other ABI's do, which means that initialized function
   3552  1.1  christos    pointers must reference the descriptor.  Thus, a function pointer
   3553  1.1  christos    initialized to the address of a function in a shared library will
   3554  1.1  christos    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
   3555  1.1  christos    redefines the function descriptor symbol to point to the copy.  This
   3556  1.1  christos    presents a problem as a plt entry for that function is also
   3557  1.1  christos    initialized from the function descriptor symbol and the copy reloc
   3558  1.1  christos    may not be initialized first.  */
   3559  1.1  christos #define ELIMINATE_COPY_RELOCS 1
   3560  1.1  christos 
   3561  1.1  christos /* Section name for stubs is the associated section name plus this
   3562  1.1  christos    string.  */
   3563  1.1  christos #define STUB_SUFFIX ".stub"
   3564  1.1  christos 
   3565  1.1  christos /* Linker stubs.
   3566  1.1  christos    ppc_stub_long_branch:
   3567  1.1  christos    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   3568  1.1  christos    destination, but a 24 bit branch in a stub section will reach.
   3569  1.1  christos    .	b	dest
   3570  1.1  christos 
   3571  1.1  christos    ppc_stub_plt_branch:
   3572  1.1  christos    Similar to the above, but a 24 bit branch in the stub section won't
   3573  1.1  christos    reach its destination.
   3574  1.1  christos    .	addis	%r12,%r2,xxx@toc@ha
   3575  1.1  christos    .	ld	%r11,xxx@toc@l(%r12)
   3576  1.1  christos    .	mtctr	%r11
   3577  1.1  christos    .	bctr
   3578  1.1  christos 
   3579  1.1  christos    ppc_stub_plt_call:
   3580  1.1  christos    Used to call a function in a shared library.  If it so happens that
   3581  1.1  christos    the plt entry referenced crosses a 64k boundary, then an extra
   3582  1.1  christos    "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
   3583  1.1  christos    .	addis	%r12,%r2,xxx@toc@ha
   3584  1.1  christos    .	std	%r2,40(%r1)
   3585  1.1  christos    .	ld	%r11,xxx+0@toc@l(%r12)
   3586  1.1  christos    .	mtctr	%r11
   3587  1.1  christos    .	ld	%r2,xxx+8@toc@l(%r12)
   3588  1.1  christos    .	ld	%r11,xxx+16@toc@l(%r12)
   3589  1.1  christos    .	bctr
   3590  1.1  christos 
   3591  1.1  christos    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   3592  1.1  christos    code to adjust the value and save r2 to support multiple toc sections.
   3593  1.1  christos    A ppc_stub_long_branch with an r2 offset looks like:
   3594  1.1  christos    .	std	%r2,40(%r1)
   3595  1.1  christos    .	addis	%r2,%r2,off@ha
   3596  1.1  christos    .	addi	%r2,%r2,off@l
   3597  1.1  christos    .	b	dest
   3598  1.1  christos 
   3599  1.1  christos    A ppc_stub_plt_branch with an r2 offset looks like:
   3600  1.1  christos    .	std	%r2,40(%r1)
   3601  1.1  christos    .	addis	%r12,%r2,xxx@toc@ha
   3602  1.1  christos    .	ld	%r11,xxx@toc@l(%r12)
   3603  1.1  christos    .	addis	%r2,%r2,off@ha
   3604  1.1  christos    .	addi	%r2,%r2,off@l
   3605  1.1  christos    .	mtctr	%r11
   3606  1.1  christos    .	bctr
   3607  1.1  christos 
   3608  1.1  christos    In cases where the "addis" instruction would add zero, the "addis" is
   3609  1.1  christos    omitted and following instructions modified slightly in some cases.
   3610  1.1  christos */
   3611  1.1  christos 
   3612  1.1  christos enum ppc_stub_type {
   3613  1.1  christos   ppc_stub_none,
   3614  1.1  christos   ppc_stub_long_branch,
   3615  1.1  christos   ppc_stub_long_branch_r2off,
   3616  1.1  christos   ppc_stub_plt_branch,
   3617  1.1  christos   ppc_stub_plt_branch_r2off,
   3618  1.1  christos   ppc_stub_plt_call
   3619  1.1  christos };
   3620  1.1  christos 
   3621  1.1  christos struct ppc_stub_hash_entry {
   3622  1.1  christos 
   3623  1.1  christos   /* Base hash table entry structure.  */
   3624  1.1  christos   struct bfd_hash_entry root;
   3625  1.1  christos 
   3626  1.1  christos   enum ppc_stub_type stub_type;
   3627  1.1  christos 
   3628  1.1  christos   /* The stub section.  */
   3629  1.1  christos   asection *stub_sec;
   3630  1.1  christos 
   3631  1.1  christos   /* Offset within stub_sec of the beginning of this stub.  */
   3632  1.1  christos   bfd_vma stub_offset;
   3633  1.1  christos 
   3634  1.1  christos   /* Given the symbol's value and its section we can determine its final
   3635  1.1  christos      value when building the stubs (so the stub knows where to jump.  */
   3636  1.1  christos   bfd_vma target_value;
   3637  1.1  christos   asection *target_section;
   3638  1.1  christos 
   3639  1.1  christos   /* The symbol table entry, if any, that this was derived from.  */
   3640  1.1  christos   struct ppc_link_hash_entry *h;
   3641  1.1  christos   struct plt_entry *plt_ent;
   3642  1.1  christos 
   3643  1.1  christos   /* And the reloc addend that this was derived from.  */
   3644  1.1  christos   bfd_vma addend;
   3645  1.1  christos 
   3646  1.1  christos   /* Where this stub is being called from, or, in the case of combined
   3647  1.1  christos      stub sections, the first input section in the group.  */
   3648  1.1  christos   asection *id_sec;
   3649  1.1  christos };
   3650  1.1  christos 
   3651  1.1  christos struct ppc_branch_hash_entry {
   3652  1.1  christos 
   3653  1.1  christos   /* Base hash table entry structure.  */
   3654  1.1  christos   struct bfd_hash_entry root;
   3655  1.1  christos 
   3656  1.1  christos   /* Offset within branch lookup table.  */
   3657  1.1  christos   unsigned int offset;
   3658  1.1  christos 
   3659  1.1  christos   /* Generation marker.  */
   3660  1.1  christos   unsigned int iter;
   3661  1.1  christos };
   3662  1.1  christos 
   3663  1.1  christos struct ppc_link_hash_entry
   3664  1.1  christos {
   3665  1.1  christos   struct elf_link_hash_entry elf;
   3666  1.1  christos 
   3667  1.1  christos   union {
   3668  1.1  christos     /* A pointer to the most recently used stub hash entry against this
   3669  1.1  christos        symbol.  */
   3670  1.1  christos     struct ppc_stub_hash_entry *stub_cache;
   3671  1.1  christos 
   3672  1.1  christos     /* A pointer to the next symbol starting with a '.'  */
   3673  1.1  christos     struct ppc_link_hash_entry *next_dot_sym;
   3674  1.1  christos   } u;
   3675  1.1  christos 
   3676  1.1  christos   /* Track dynamic relocs copied for this symbol.  */
   3677  1.1  christos   struct ppc_dyn_relocs *dyn_relocs;
   3678  1.1  christos 
   3679  1.1  christos   /* Link between function code and descriptor symbols.  */
   3680  1.1  christos   struct ppc_link_hash_entry *oh;
   3681  1.1  christos 
   3682  1.1  christos   /* Flag function code and descriptor symbols.  */
   3683  1.1  christos   unsigned int is_func:1;
   3684  1.1  christos   unsigned int is_func_descriptor:1;
   3685  1.1  christos   unsigned int fake:1;
   3686  1.1  christos 
   3687  1.1  christos   /* Whether global opd/toc sym has been adjusted or not.
   3688  1.1  christos      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3689  1.1  christos      should be set for all globals defined in any opd/toc section.  */
   3690  1.1  christos   unsigned int adjust_done:1;
   3691  1.1  christos 
   3692  1.1  christos   /* Set if we twiddled this symbol to weak at some stage.  */
   3693  1.1  christos   unsigned int was_undefined:1;
   3694  1.1  christos 
   3695  1.1  christos   /* Contexts in which symbol is used in the GOT (or TOC).
   3696  1.1  christos      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
   3697  1.1  christos      corresponding relocs are encountered during check_relocs.
   3698  1.1  christos      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
   3699  1.1  christos      indicate the corresponding GOT entry type is not needed.
   3700  1.1  christos      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
   3701  1.1  christos      a TPREL one.  We use a separate flag rather than setting TPREL
   3702  1.1  christos      just for convenience in distinguishing the two cases.  */
   3703  1.1  christos #define TLS_GD		 1	/* GD reloc. */
   3704  1.1  christos #define TLS_LD		 2	/* LD reloc. */
   3705  1.1  christos #define TLS_TPREL	 4	/* TPREL reloc, => IE. */
   3706  1.1  christos #define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
   3707  1.1  christos #define TLS_TLS		16	/* Any TLS reloc.  */
   3708  1.1  christos #define TLS_EXPLICIT	32	/* Marks TOC section TLS relocs. */
   3709  1.1  christos #define TLS_TPRELGD	64	/* TPREL reloc resulting from GD->IE. */
   3710  1.1  christos #define PLT_IFUNC      128	/* STT_GNU_IFUNC.  */
   3711  1.1  christos   unsigned char tls_mask;
   3712  1.1  christos };
   3713  1.1  christos 
   3714  1.1  christos /* ppc64 ELF linker hash table.  */
   3715  1.1  christos 
   3716  1.1  christos struct ppc_link_hash_table
   3717  1.1  christos {
   3718  1.1  christos   struct elf_link_hash_table elf;
   3719  1.1  christos 
   3720  1.1  christos   /* The stub hash table.  */
   3721  1.1  christos   struct bfd_hash_table stub_hash_table;
   3722  1.1  christos 
   3723  1.1  christos   /* Another hash table for plt_branch stubs.  */
   3724  1.1  christos   struct bfd_hash_table branch_hash_table;
   3725  1.1  christos 
   3726  1.1  christos   /* Linker stub bfd.  */
   3727  1.1  christos   bfd *stub_bfd;
   3728  1.1  christos 
   3729  1.1  christos   /* Linker call-backs.  */
   3730  1.1  christos   asection * (*add_stub_section) (const char *, asection *);
   3731  1.1  christos   void (*layout_sections_again) (void);
   3732  1.1  christos 
   3733  1.1  christos   /* Array to keep track of which stub sections have been created, and
   3734  1.1  christos      information on stub grouping.  */
   3735  1.1  christos   struct map_stub {
   3736  1.1  christos     /* This is the section to which stubs in the group will be attached.  */
   3737  1.1  christos     asection *link_sec;
   3738  1.1  christos     /* The stub section.  */
   3739  1.1  christos     asection *stub_sec;
   3740  1.1  christos     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
   3741  1.1  christos     bfd_vma toc_off;
   3742  1.1  christos   } *stub_group;
   3743  1.1  christos 
   3744  1.1  christos   /* Temp used when calculating TOC pointers.  */
   3745  1.1  christos   bfd_vma toc_curr;
   3746  1.1  christos   bfd *toc_bfd;
   3747  1.1  christos   asection *toc_first_sec;
   3748  1.1  christos 
   3749  1.1  christos   /* Highest input section id.  */
   3750  1.1  christos   int top_id;
   3751  1.1  christos 
   3752  1.1  christos   /* Highest output section index.  */
   3753  1.1  christos   int top_index;
   3754  1.1  christos 
   3755  1.1  christos   /* Used when adding symbols.  */
   3756  1.1  christos   struct ppc_link_hash_entry *dot_syms;
   3757  1.1  christos 
   3758  1.1  christos   /* List of input sections for each output section.  */
   3759  1.1  christos   asection **input_list;
   3760  1.1  christos 
   3761  1.1  christos   /* Short-cuts to get to dynamic linker sections.  */
   3762  1.1  christos   asection *got;
   3763  1.1  christos   asection *plt;
   3764  1.1  christos   asection *relplt;
   3765  1.1  christos   asection *iplt;
   3766  1.1  christos   asection *reliplt;
   3767  1.1  christos   asection *dynbss;
   3768  1.1  christos   asection *relbss;
   3769  1.1  christos   asection *glink;
   3770  1.1  christos   asection *sfpr;
   3771  1.1  christos   asection *brlt;
   3772  1.1  christos   asection *relbrlt;
   3773  1.1  christos 
   3774  1.1  christos   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   3775  1.1  christos   struct ppc_link_hash_entry *tls_get_addr;
   3776  1.1  christos   struct ppc_link_hash_entry *tls_get_addr_fd;
   3777  1.1  christos 
   3778  1.1  christos   /* The size of reliplt used by got entry relocs.  */
   3779  1.1  christos   bfd_size_type got_reli_size;
   3780  1.1  christos 
   3781  1.1  christos   /* Statistics.  */
   3782  1.1  christos   unsigned long stub_count[ppc_stub_plt_call];
   3783  1.1  christos 
   3784  1.1  christos   /* Number of stubs against global syms.  */
   3785  1.1  christos   unsigned long stub_globals;
   3786  1.1  christos 
   3787  1.1  christos   /* Set if we should emit symbols for stubs.  */
   3788  1.1  christos   unsigned int emit_stub_syms:1;
   3789  1.1  christos 
   3790  1.1  christos   /* Set if __tls_get_addr optimization should not be done.  */
   3791  1.1  christos   unsigned int no_tls_get_addr_opt:1;
   3792  1.1  christos 
   3793  1.1  christos   /* Support for multiple toc sections.  */
   3794  1.1  christos   unsigned int do_multi_toc:1;
   3795  1.1  christos   unsigned int multi_toc_needed:1;
   3796  1.1  christos   unsigned int second_toc_pass:1;
   3797  1.1  christos   unsigned int do_toc_opt:1;
   3798  1.1  christos 
   3799  1.1  christos   /* Set on error.  */
   3800  1.1  christos   unsigned int stub_error:1;
   3801  1.1  christos 
   3802  1.1  christos   /* Temp used by ppc64_elf_process_dot_syms.  */
   3803  1.1  christos   unsigned int twiddled_syms:1;
   3804  1.1  christos 
   3805  1.1  christos   /* Incremented every time we size stubs.  */
   3806  1.1  christos   unsigned int stub_iteration;
   3807  1.1  christos 
   3808  1.1  christos   /* Small local sym cache.  */
   3809  1.1  christos   struct sym_cache sym_cache;
   3810  1.1  christos };
   3811  1.1  christos 
   3812  1.1  christos /* Rename some of the generic section flags to better document how they
   3813  1.1  christos    are used here.  */
   3814  1.1  christos 
   3815  1.1  christos /* Nonzero if this section has TLS related relocations.  */
   3816  1.1  christos #define has_tls_reloc sec_flg0
   3817  1.1  christos 
   3818  1.1  christos /* Nonzero if this section has a call to __tls_get_addr.  */
   3819  1.1  christos #define has_tls_get_addr_call sec_flg1
   3820  1.1  christos 
   3821  1.1  christos /* Nonzero if this section has any toc or got relocs.  */
   3822  1.1  christos #define has_toc_reloc sec_flg2
   3823  1.1  christos 
   3824  1.1  christos /* Nonzero if this section has a call to another section that uses
   3825  1.1  christos    the toc or got.  */
   3826  1.1  christos #define makes_toc_func_call sec_flg3
   3827  1.1  christos 
   3828  1.1  christos /* Recursion protection when determining above flag.  */
   3829  1.1  christos #define call_check_in_progress sec_flg4
   3830  1.1  christos #define call_check_done sec_flg5
   3831  1.1  christos 
   3832  1.1  christos /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   3833  1.1  christos 
   3834  1.1  christos #define ppc_hash_table(p) \
   3835  1.1  christos   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
   3836  1.1  christos   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
   3837  1.1  christos 
   3838  1.1  christos #define ppc_stub_hash_lookup(table, string, create, copy) \
   3839  1.1  christos   ((struct ppc_stub_hash_entry *) \
   3840  1.1  christos    bfd_hash_lookup ((table), (string), (create), (copy)))
   3841  1.1  christos 
   3842  1.1  christos #define ppc_branch_hash_lookup(table, string, create, copy) \
   3843  1.1  christos   ((struct ppc_branch_hash_entry *) \
   3844  1.1  christos    bfd_hash_lookup ((table), (string), (create), (copy)))
   3845  1.1  christos 
   3846  1.1  christos /* Create an entry in the stub hash table.  */
   3847  1.1  christos 
   3848  1.1  christos static struct bfd_hash_entry *
   3849  1.1  christos stub_hash_newfunc (struct bfd_hash_entry *entry,
   3850  1.1  christos 		   struct bfd_hash_table *table,
   3851  1.1  christos 		   const char *string)
   3852  1.1  christos {
   3853  1.1  christos   /* Allocate the structure if it has not already been allocated by a
   3854  1.1  christos      subclass.  */
   3855  1.1  christos   if (entry == NULL)
   3856  1.1  christos     {
   3857  1.1  christos       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   3858  1.1  christos       if (entry == NULL)
   3859  1.1  christos 	return entry;
   3860  1.1  christos     }
   3861  1.1  christos 
   3862  1.1  christos   /* Call the allocation method of the superclass.  */
   3863  1.1  christos   entry = bfd_hash_newfunc (entry, table, string);
   3864  1.1  christos   if (entry != NULL)
   3865  1.1  christos     {
   3866  1.1  christos       struct ppc_stub_hash_entry *eh;
   3867  1.1  christos 
   3868  1.1  christos       /* Initialize the local fields.  */
   3869  1.1  christos       eh = (struct ppc_stub_hash_entry *) entry;
   3870  1.1  christos       eh->stub_type = ppc_stub_none;
   3871  1.1  christos       eh->stub_sec = NULL;
   3872  1.1  christos       eh->stub_offset = 0;
   3873  1.1  christos       eh->target_value = 0;
   3874  1.1  christos       eh->target_section = NULL;
   3875  1.1  christos       eh->h = NULL;
   3876  1.1  christos       eh->id_sec = NULL;
   3877  1.1  christos     }
   3878  1.1  christos 
   3879  1.1  christos   return entry;
   3880  1.1  christos }
   3881  1.1  christos 
   3882  1.1  christos /* Create an entry in the branch hash table.  */
   3883  1.1  christos 
   3884  1.1  christos static struct bfd_hash_entry *
   3885  1.1  christos branch_hash_newfunc (struct bfd_hash_entry *entry,
   3886  1.1  christos 		     struct bfd_hash_table *table,
   3887  1.1  christos 		     const char *string)
   3888  1.1  christos {
   3889  1.1  christos   /* Allocate the structure if it has not already been allocated by a
   3890  1.1  christos      subclass.  */
   3891  1.1  christos   if (entry == NULL)
   3892  1.1  christos     {
   3893  1.1  christos       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   3894  1.1  christos       if (entry == NULL)
   3895  1.1  christos 	return entry;
   3896  1.1  christos     }
   3897  1.1  christos 
   3898  1.1  christos   /* Call the allocation method of the superclass.  */
   3899  1.1  christos   entry = bfd_hash_newfunc (entry, table, string);
   3900  1.1  christos   if (entry != NULL)
   3901  1.1  christos     {
   3902  1.1  christos       struct ppc_branch_hash_entry *eh;
   3903  1.1  christos 
   3904  1.1  christos       /* Initialize the local fields.  */
   3905  1.1  christos       eh = (struct ppc_branch_hash_entry *) entry;
   3906  1.1  christos       eh->offset = 0;
   3907  1.1  christos       eh->iter = 0;
   3908  1.1  christos     }
   3909  1.1  christos 
   3910  1.1  christos   return entry;
   3911  1.1  christos }
   3912  1.1  christos 
   3913  1.1  christos /* Create an entry in a ppc64 ELF linker hash table.  */
   3914  1.1  christos 
   3915  1.1  christos static struct bfd_hash_entry *
   3916  1.1  christos link_hash_newfunc (struct bfd_hash_entry *entry,
   3917  1.1  christos 		   struct bfd_hash_table *table,
   3918  1.1  christos 		   const char *string)
   3919  1.1  christos {
   3920  1.1  christos   /* Allocate the structure if it has not already been allocated by a
   3921  1.1  christos      subclass.  */
   3922  1.1  christos   if (entry == NULL)
   3923  1.1  christos     {
   3924  1.1  christos       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   3925  1.1  christos       if (entry == NULL)
   3926  1.1  christos 	return entry;
   3927  1.1  christos     }
   3928  1.1  christos 
   3929  1.1  christos   /* Call the allocation method of the superclass.  */
   3930  1.1  christos   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   3931  1.1  christos   if (entry != NULL)
   3932  1.1  christos     {
   3933  1.1  christos       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   3934  1.1  christos 
   3935  1.1  christos       memset (&eh->u.stub_cache, 0,
   3936  1.1  christos 	      (sizeof (struct ppc_link_hash_entry)
   3937  1.1  christos 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   3938  1.1  christos 
   3939  1.1  christos       /* When making function calls, old ABI code references function entry
   3940  1.1  christos 	 points (dot symbols), while new ABI code references the function
   3941  1.1  christos 	 descriptor symbol.  We need to make any combination of reference and
   3942  1.1  christos 	 definition work together, without breaking archive linking.
   3943  1.1  christos 
   3944  1.1  christos 	 For a defined function "foo" and an undefined call to "bar":
   3945  1.1  christos 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   3946  1.1  christos 	 "bar" too).
   3947  1.1  christos 	 A new object defines "foo" and references "bar".
   3948  1.1  christos 
   3949  1.1  christos 	 A new object thus has no problem with its undefined symbols being
   3950  1.1  christos 	 satisfied by definitions in an old object.  On the other hand, the
   3951  1.1  christos 	 old object won't have ".bar" satisfied by a new object.
   3952  1.1  christos 
   3953  1.1  christos 	 Keep a list of newly added dot-symbols.  */
   3954  1.1  christos 
   3955  1.1  christos       if (string[0] == '.')
   3956  1.1  christos 	{
   3957  1.1  christos 	  struct ppc_link_hash_table *htab;
   3958  1.1  christos 
   3959  1.1  christos 	  htab = (struct ppc_link_hash_table *) table;
   3960  1.1  christos 	  eh->u.next_dot_sym = htab->dot_syms;
   3961  1.1  christos 	  htab->dot_syms = eh;
   3962  1.1  christos 	}
   3963  1.1  christos     }
   3964  1.1  christos 
   3965  1.1  christos   return entry;
   3966  1.1  christos }
   3967  1.1  christos 
   3968  1.1  christos /* Create a ppc64 ELF linker hash table.  */
   3969  1.1  christos 
   3970  1.1  christos static struct bfd_link_hash_table *
   3971  1.1  christos ppc64_elf_link_hash_table_create (bfd *abfd)
   3972  1.1  christos {
   3973  1.1  christos   struct ppc_link_hash_table *htab;
   3974  1.1  christos   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
   3975  1.1  christos 
   3976  1.1  christos   htab = bfd_zmalloc (amt);
   3977  1.1  christos   if (htab == NULL)
   3978  1.1  christos     return NULL;
   3979  1.1  christos 
   3980  1.1  christos   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   3981  1.1  christos 				      sizeof (struct ppc_link_hash_entry),
   3982  1.1  christos 				      PPC64_ELF_DATA))
   3983  1.1  christos     {
   3984  1.1  christos       free (htab);
   3985  1.1  christos       return NULL;
   3986  1.1  christos     }
   3987  1.1  christos 
   3988  1.1  christos   /* Init the stub hash table too.  */
   3989  1.1  christos   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   3990  1.1  christos 			    sizeof (struct ppc_stub_hash_entry)))
   3991  1.1  christos     return NULL;
   3992  1.1  christos 
   3993  1.1  christos   /* And the branch hash table.  */
   3994  1.1  christos   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   3995  1.1  christos 			    sizeof (struct ppc_branch_hash_entry)))
   3996  1.1  christos     return NULL;
   3997  1.1  christos 
   3998  1.1  christos   /* Initializing two fields of the union is just cosmetic.  We really
   3999  1.1  christos      only care about glist, but when compiled on a 32-bit host the
   4000  1.1  christos      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   4001  1.1  christos      debugger inspection of these fields look nicer.  */
   4002  1.1  christos   htab->elf.init_got_refcount.refcount = 0;
   4003  1.1  christos   htab->elf.init_got_refcount.glist = NULL;
   4004  1.1  christos   htab->elf.init_plt_refcount.refcount = 0;
   4005  1.1  christos   htab->elf.init_plt_refcount.glist = NULL;
   4006  1.1  christos   htab->elf.init_got_offset.offset = 0;
   4007  1.1  christos   htab->elf.init_got_offset.glist = NULL;
   4008  1.1  christos   htab->elf.init_plt_offset.offset = 0;
   4009  1.1  christos   htab->elf.init_plt_offset.glist = NULL;
   4010  1.1  christos 
   4011  1.1  christos   return &htab->elf.root;
   4012  1.1  christos }
   4013  1.1  christos 
   4014  1.1  christos /* Free the derived linker hash table.  */
   4015  1.1  christos 
   4016  1.1  christos static void
   4017  1.1  christos ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
   4018  1.1  christos {
   4019  1.1  christos   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
   4020  1.1  christos 
   4021  1.1  christos   bfd_hash_table_free (&ret->stub_hash_table);
   4022  1.1  christos   bfd_hash_table_free (&ret->branch_hash_table);
   4023  1.1  christos   _bfd_generic_link_hash_table_free (hash);
   4024  1.1  christos }
   4025  1.1  christos 
   4026  1.1  christos /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   4027  1.1  christos 
   4028  1.1  christos void
   4029  1.1  christos ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
   4030  1.1  christos {
   4031  1.1  christos   struct ppc_link_hash_table *htab;
   4032  1.1  christos 
   4033  1.1  christos   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
   4034  1.1  christos 
   4035  1.1  christos /* Always hook our dynamic sections into the first bfd, which is the
   4036  1.1  christos    linker created stub bfd.  This ensures that the GOT header is at
   4037  1.1  christos    the start of the output TOC section.  */
   4038  1.1  christos   htab = ppc_hash_table (info);
   4039  1.1  christos   if (htab == NULL)
   4040  1.1  christos     return;
   4041  1.1  christos   htab->stub_bfd = abfd;
   4042  1.1  christos   htab->elf.dynobj = abfd;
   4043  1.1  christos }
   4044  1.1  christos 
   4045  1.1  christos /* Build a name for an entry in the stub hash table.  */
   4046  1.1  christos 
   4047  1.1  christos static char *
   4048  1.1  christos ppc_stub_name (const asection *input_section,
   4049  1.1  christos 	       const asection *sym_sec,
   4050  1.1  christos 	       const struct ppc_link_hash_entry *h,
   4051  1.1  christos 	       const Elf_Internal_Rela *rel)
   4052  1.1  christos {
   4053  1.1  christos   char *stub_name;
   4054  1.1  christos   bfd_size_type len;
   4055  1.1  christos 
   4056  1.1  christos   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   4057  1.1  christos      offsets from a sym as a branch target?  In fact, we could
   4058  1.1  christos      probably assume the addend is always zero.  */
   4059  1.1  christos   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   4060  1.1  christos 
   4061  1.1  christos   if (h)
   4062  1.1  christos     {
   4063  1.1  christos       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   4064  1.1  christos       stub_name = bfd_malloc (len);
   4065  1.1  christos       if (stub_name == NULL)
   4066  1.1  christos 	return stub_name;
   4067  1.1  christos 
   4068  1.1  christos       sprintf (stub_name, "%08x.%s+%x",
   4069  1.1  christos 	       input_section->id & 0xffffffff,
   4070  1.1  christos 	       h->elf.root.root.string,
   4071  1.1  christos 	       (int) rel->r_addend & 0xffffffff);
   4072  1.1  christos     }
   4073  1.1  christos   else
   4074  1.1  christos     {
   4075  1.1  christos       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   4076  1.1  christos       stub_name = bfd_malloc (len);
   4077  1.1  christos       if (stub_name == NULL)
   4078  1.1  christos 	return stub_name;
   4079  1.1  christos 
   4080  1.1  christos       sprintf (stub_name, "%08x.%x:%x+%x",
   4081  1.1  christos 	       input_section->id & 0xffffffff,
   4082  1.1  christos 	       sym_sec->id & 0xffffffff,
   4083  1.1  christos 	       (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   4084  1.1  christos 	       (int) rel->r_addend & 0xffffffff);
   4085  1.1  christos     }
   4086  1.1  christos   if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   4087  1.1  christos     stub_name[len - 2] = 0;
   4088  1.1  christos   return stub_name;
   4089  1.1  christos }
   4090  1.1  christos 
   4091  1.1  christos /* Look up an entry in the stub hash.  Stub entries are cached because
   4092  1.1  christos    creating the stub name takes a bit of time.  */
   4093  1.1  christos 
   4094  1.1  christos static struct ppc_stub_hash_entry *
   4095  1.1  christos ppc_get_stub_entry (const asection *input_section,
   4096  1.1  christos 		    const asection *sym_sec,
   4097  1.1  christos 		    struct ppc_link_hash_entry *h,
   4098  1.1  christos 		    const Elf_Internal_Rela *rel,
   4099  1.1  christos 		    struct ppc_link_hash_table *htab)
   4100  1.1  christos {
   4101  1.1  christos   struct ppc_stub_hash_entry *stub_entry;
   4102  1.1  christos   const asection *id_sec;
   4103  1.1  christos 
   4104  1.1  christos   /* If this input section is part of a group of sections sharing one
   4105  1.1  christos      stub section, then use the id of the first section in the group.
   4106  1.1  christos      Stub names need to include a section id, as there may well be
   4107  1.1  christos      more than one stub used to reach say, printf, and we need to
   4108  1.1  christos      distinguish between them.  */
   4109  1.1  christos   id_sec = htab->stub_group[input_section->id].link_sec;
   4110  1.1  christos 
   4111  1.1  christos   if (h != NULL && h->u.stub_cache != NULL
   4112  1.1  christos       && h->u.stub_cache->h == h
   4113  1.1  christos       && h->u.stub_cache->id_sec == id_sec)
   4114  1.1  christos     {
   4115  1.1  christos       stub_entry = h->u.stub_cache;
   4116  1.1  christos     }
   4117  1.1  christos   else
   4118  1.1  christos     {
   4119  1.1  christos       char *stub_name;
   4120  1.1  christos 
   4121  1.1  christos       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
   4122  1.1  christos       if (stub_name == NULL)
   4123  1.1  christos 	return NULL;
   4124  1.1  christos 
   4125  1.1  christos       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   4126  1.1  christos 					 stub_name, FALSE, FALSE);
   4127  1.1  christos       if (h != NULL)
   4128  1.1  christos 	h->u.stub_cache = stub_entry;
   4129  1.1  christos 
   4130  1.1  christos       free (stub_name);
   4131  1.1  christos     }
   4132  1.1  christos 
   4133  1.1  christos   return stub_entry;
   4134  1.1  christos }
   4135  1.1  christos 
   4136  1.1  christos /* Add a new stub entry to the stub hash.  Not all fields of the new
   4137  1.1  christos    stub entry are initialised.  */
   4138  1.1  christos 
   4139  1.1  christos static struct ppc_stub_hash_entry *
   4140  1.1  christos ppc_add_stub (const char *stub_name,
   4141  1.1  christos 	      asection *section,
   4142  1.1  christos 	      struct ppc_link_hash_table *htab)
   4143  1.1  christos {
   4144  1.1  christos   asection *link_sec;
   4145  1.1  christos   asection *stub_sec;
   4146  1.1  christos   struct ppc_stub_hash_entry *stub_entry;
   4147  1.1  christos 
   4148  1.1  christos   link_sec = htab->stub_group[section->id].link_sec;
   4149  1.1  christos   stub_sec = htab->stub_group[section->id].stub_sec;
   4150  1.1  christos   if (stub_sec == NULL)
   4151  1.1  christos     {
   4152  1.1  christos       stub_sec = htab->stub_group[link_sec->id].stub_sec;
   4153  1.1  christos       if (stub_sec == NULL)
   4154  1.1  christos 	{
   4155  1.1  christos 	  size_t namelen;
   4156  1.1  christos 	  bfd_size_type len;
   4157  1.1  christos 	  char *s_name;
   4158  1.1  christos 
   4159  1.1  christos 	  namelen = strlen (link_sec->name);
   4160  1.1  christos 	  len = namelen + sizeof (STUB_SUFFIX);
   4161  1.1  christos 	  s_name = bfd_alloc (htab->stub_bfd, len);
   4162  1.1  christos 	  if (s_name == NULL)
   4163  1.1  christos 	    return NULL;
   4164  1.1  christos 
   4165  1.1  christos 	  memcpy (s_name, link_sec->name, namelen);
   4166  1.1  christos 	  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   4167  1.1  christos 	  stub_sec = (*htab->add_stub_section) (s_name, link_sec);
   4168  1.1  christos 	  if (stub_sec == NULL)
   4169  1.1  christos 	    return NULL;
   4170  1.1  christos 	  htab->stub_group[link_sec->id].stub_sec = stub_sec;
   4171  1.1  christos 	}
   4172  1.1  christos       htab->stub_group[section->id].stub_sec = stub_sec;
   4173  1.1  christos     }
   4174  1.1  christos 
   4175  1.1  christos   /* Enter this entry into the linker stub hash table.  */
   4176  1.1  christos   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   4177  1.1  christos 				     TRUE, FALSE);
   4178  1.1  christos   if (stub_entry == NULL)
   4179  1.1  christos     {
   4180  1.1  christos       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
   4181  1.1  christos 			     section->owner, stub_name);
   4182  1.1  christos       return NULL;
   4183  1.1  christos     }
   4184  1.1  christos 
   4185  1.1  christos   stub_entry->stub_sec = stub_sec;
   4186  1.1  christos   stub_entry->stub_offset = 0;
   4187  1.1  christos   stub_entry->id_sec = link_sec;
   4188  1.1  christos   return stub_entry;
   4189  1.1  christos }
   4190  1.1  christos 
   4191  1.1  christos /* Create sections for linker generated code.  */
   4192  1.1  christos 
   4193  1.1  christos static bfd_boolean
   4194  1.1  christos create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   4195  1.1  christos {
   4196  1.1  christos   struct ppc_link_hash_table *htab;
   4197  1.1  christos   flagword flags;
   4198  1.1  christos 
   4199  1.1  christos   htab = ppc_hash_table (info);
   4200  1.1  christos   if (htab == NULL)
   4201  1.1  christos     return FALSE;
   4202  1.1  christos 
   4203  1.1  christos   /* Create .sfpr for code to save and restore fp regs.  */
   4204  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   4205  1.1  christos 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4206  1.1  christos   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   4207  1.1  christos 						   flags);
   4208  1.1  christos   if (htab->sfpr == NULL
   4209  1.1  christos       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
   4210  1.1  christos     return FALSE;
   4211  1.1  christos 
   4212  1.1  christos   /* Create .glink for lazy dynamic linking support.  */
   4213  1.1  christos   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   4214  1.1  christos 						    flags);
   4215  1.1  christos   if (htab->glink == NULL
   4216  1.1  christos       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
   4217  1.1  christos     return FALSE;
   4218  1.1  christos 
   4219  1.1  christos   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   4220  1.1  christos   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   4221  1.1  christos   if (htab->iplt == NULL
   4222  1.1  christos       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
   4223  1.1  christos     return FALSE;
   4224  1.1  christos 
   4225  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4226  1.1  christos 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4227  1.1  christos   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
   4228  1.1  christos 						      ".rela.iplt",
   4229  1.1  christos 						      flags);
   4230  1.1  christos   if (htab->reliplt == NULL
   4231  1.1  christos       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
   4232  1.1  christos     return FALSE;
   4233  1.1  christos 
   4234  1.1  christos   /* Create branch lookup table for plt_branch stubs.  */
   4235  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD
   4236  1.1  christos 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4237  1.1  christos   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   4238  1.1  christos 						   flags);
   4239  1.1  christos   if (htab->brlt == NULL
   4240  1.1  christos       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
   4241  1.1  christos     return FALSE;
   4242  1.1  christos 
   4243  1.1  christos   if (!info->shared)
   4244  1.1  christos     return TRUE;
   4245  1.1  christos 
   4246  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4247  1.1  christos 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4248  1.1  christos   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
   4249  1.1  christos 						      ".rela.branch_lt",
   4250  1.1  christos 						      flags);
   4251  1.1  christos   if (htab->relbrlt == NULL
   4252  1.1  christos       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
   4253  1.1  christos     return FALSE;
   4254  1.1  christos 
   4255  1.1  christos   return TRUE;
   4256  1.1  christos }
   4257  1.1  christos 
   4258  1.1  christos /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   4259  1.1  christos    not already done.  */
   4260  1.1  christos 
   4261  1.1  christos static bfd_boolean
   4262  1.1  christos create_got_section (bfd *abfd, struct bfd_link_info *info)
   4263  1.1  christos {
   4264  1.1  christos   asection *got, *relgot;
   4265  1.1  christos   flagword flags;
   4266  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4267  1.1  christos 
   4268  1.1  christos   if (!is_ppc64_elf (abfd))
   4269  1.1  christos     return FALSE;
   4270  1.1  christos   if (htab == NULL)
   4271  1.1  christos     return FALSE;
   4272  1.1  christos 
   4273  1.1  christos   if (!htab->got)
   4274  1.1  christos     {
   4275  1.1  christos       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
   4276  1.1  christos 	return FALSE;
   4277  1.1  christos 
   4278  1.1  christos       htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
   4279  1.1  christos       if (!htab->got)
   4280  1.1  christos 	abort ();
   4281  1.1  christos     }
   4282  1.1  christos 
   4283  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4284  1.1  christos 	   | SEC_LINKER_CREATED);
   4285  1.1  christos 
   4286  1.1  christos   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4287  1.1  christos   if (!got
   4288  1.1  christos       || !bfd_set_section_alignment (abfd, got, 3))
   4289  1.1  christos     return FALSE;
   4290  1.1  christos 
   4291  1.1  christos   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   4292  1.1  christos 					       flags | SEC_READONLY);
   4293  1.1  christos   if (!relgot
   4294  1.1  christos       || ! bfd_set_section_alignment (abfd, relgot, 3))
   4295  1.1  christos     return FALSE;
   4296  1.1  christos 
   4297  1.1  christos   ppc64_elf_tdata (abfd)->got = got;
   4298  1.1  christos   ppc64_elf_tdata (abfd)->relgot = relgot;
   4299  1.1  christos   return TRUE;
   4300  1.1  christos }
   4301  1.1  christos 
   4302  1.1  christos /* Create the dynamic sections, and set up shortcuts.  */
   4303  1.1  christos 
   4304  1.1  christos static bfd_boolean
   4305  1.1  christos ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   4306  1.1  christos {
   4307  1.1  christos   struct ppc_link_hash_table *htab;
   4308  1.1  christos 
   4309  1.1  christos   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   4310  1.1  christos     return FALSE;
   4311  1.1  christos 
   4312  1.1  christos   htab = ppc_hash_table (info);
   4313  1.1  christos   if (htab == NULL)
   4314  1.1  christos     return FALSE;
   4315  1.1  christos 
   4316  1.1  christos   if (!htab->got)
   4317  1.1  christos     htab->got = bfd_get_section_by_name (dynobj, ".got");
   4318  1.1  christos   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
   4319  1.1  christos   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
   4320  1.1  christos   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
   4321  1.1  christos   if (!info->shared)
   4322  1.1  christos     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
   4323  1.1  christos 
   4324  1.1  christos   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
   4325  1.1  christos       || (!info->shared && !htab->relbss))
   4326  1.1  christos     abort ();
   4327  1.1  christos 
   4328  1.1  christos   return TRUE;
   4329  1.1  christos }
   4330  1.1  christos 
   4331  1.1  christos /* Follow indirect and warning symbol links.  */
   4332  1.1  christos 
   4333  1.1  christos static inline struct bfd_link_hash_entry *
   4334  1.1  christos follow_link (struct bfd_link_hash_entry *h)
   4335  1.1  christos {
   4336  1.1  christos   while (h->type == bfd_link_hash_indirect
   4337  1.1  christos 	 || h->type == bfd_link_hash_warning)
   4338  1.1  christos     h = h->u.i.link;
   4339  1.1  christos   return h;
   4340  1.1  christos }
   4341  1.1  christos 
   4342  1.1  christos static inline struct elf_link_hash_entry *
   4343  1.1  christos elf_follow_link (struct elf_link_hash_entry *h)
   4344  1.1  christos {
   4345  1.1  christos   return (struct elf_link_hash_entry *) follow_link (&h->root);
   4346  1.1  christos }
   4347  1.1  christos 
   4348  1.1  christos static inline struct ppc_link_hash_entry *
   4349  1.1  christos ppc_follow_link (struct ppc_link_hash_entry *h)
   4350  1.1  christos {
   4351  1.1  christos   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
   4352  1.1  christos }
   4353  1.1  christos 
   4354  1.1  christos /* Merge PLT info on FROM with that on TO.  */
   4355  1.1  christos 
   4356  1.1  christos static void
   4357  1.1  christos move_plt_plist (struct ppc_link_hash_entry *from,
   4358  1.1  christos 		struct ppc_link_hash_entry *to)
   4359  1.1  christos {
   4360  1.1  christos   if (from->elf.plt.plist != NULL)
   4361  1.1  christos     {
   4362  1.1  christos       if (to->elf.plt.plist != NULL)
   4363  1.1  christos 	{
   4364  1.1  christos 	  struct plt_entry **entp;
   4365  1.1  christos 	  struct plt_entry *ent;
   4366  1.1  christos 
   4367  1.1  christos 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   4368  1.1  christos 	    {
   4369  1.1  christos 	      struct plt_entry *dent;
   4370  1.1  christos 
   4371  1.1  christos 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   4372  1.1  christos 		if (dent->addend == ent->addend)
   4373  1.1  christos 		  {
   4374  1.1  christos 		    dent->plt.refcount += ent->plt.refcount;
   4375  1.1  christos 		    *entp = ent->next;
   4376  1.1  christos 		    break;
   4377  1.1  christos 		  }
   4378  1.1  christos 	      if (dent == NULL)
   4379  1.1  christos 		entp = &ent->next;
   4380  1.1  christos 	    }
   4381  1.1  christos 	  *entp = to->elf.plt.plist;
   4382  1.1  christos 	}
   4383  1.1  christos 
   4384  1.1  christos       to->elf.plt.plist = from->elf.plt.plist;
   4385  1.1  christos       from->elf.plt.plist = NULL;
   4386  1.1  christos     }
   4387  1.1  christos }
   4388  1.1  christos 
   4389  1.1  christos /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   4390  1.1  christos 
   4391  1.1  christos static void
   4392  1.1  christos ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   4393  1.1  christos 				struct elf_link_hash_entry *dir,
   4394  1.1  christos 				struct elf_link_hash_entry *ind)
   4395  1.1  christos {
   4396  1.1  christos   struct ppc_link_hash_entry *edir, *eind;
   4397  1.1  christos 
   4398  1.1  christos   edir = (struct ppc_link_hash_entry *) dir;
   4399  1.1  christos   eind = (struct ppc_link_hash_entry *) ind;
   4400  1.1  christos 
   4401  1.1  christos   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   4402  1.1  christos   if (eind->dyn_relocs != NULL)
   4403  1.1  christos     {
   4404  1.1  christos       if (edir->dyn_relocs != NULL)
   4405  1.1  christos 	{
   4406  1.1  christos 	  struct ppc_dyn_relocs **pp;
   4407  1.1  christos 	  struct ppc_dyn_relocs *p;
   4408  1.1  christos 
   4409  1.1  christos 	  /* Add reloc counts against the indirect sym to the direct sym
   4410  1.1  christos 	     list.  Merge any entries against the same section.  */
   4411  1.1  christos 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   4412  1.1  christos 	    {
   4413  1.1  christos 	      struct ppc_dyn_relocs *q;
   4414  1.1  christos 
   4415  1.1  christos 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   4416  1.1  christos 		if (q->sec == p->sec)
   4417  1.1  christos 		  {
   4418  1.1  christos 		    q->pc_count += p->pc_count;
   4419  1.1  christos 		    q->count += p->count;
   4420  1.1  christos 		    *pp = p->next;
   4421  1.1  christos 		    break;
   4422  1.1  christos 		  }
   4423  1.1  christos 	      if (q == NULL)
   4424  1.1  christos 		pp = &p->next;
   4425  1.1  christos 	    }
   4426  1.1  christos 	  *pp = edir->dyn_relocs;
   4427  1.1  christos 	}
   4428  1.1  christos 
   4429  1.1  christos       edir->dyn_relocs = eind->dyn_relocs;
   4430  1.1  christos       eind->dyn_relocs = NULL;
   4431  1.1  christos     }
   4432  1.1  christos 
   4433  1.1  christos   edir->is_func |= eind->is_func;
   4434  1.1  christos   edir->is_func_descriptor |= eind->is_func_descriptor;
   4435  1.1  christos   edir->tls_mask |= eind->tls_mask;
   4436  1.1  christos   if (eind->oh != NULL)
   4437  1.1  christos     edir->oh = ppc_follow_link (eind->oh);
   4438  1.1  christos 
   4439  1.1  christos   /* If called to transfer flags for a weakdef during processing
   4440  1.1  christos      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
   4441  1.1  christos      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
   4442  1.1  christos   if (!(ELIMINATE_COPY_RELOCS
   4443  1.1  christos 	&& eind->elf.root.type != bfd_link_hash_indirect
   4444  1.1  christos 	&& edir->elf.dynamic_adjusted))
   4445  1.1  christos     edir->elf.non_got_ref |= eind->elf.non_got_ref;
   4446  1.1  christos 
   4447  1.1  christos   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   4448  1.1  christos   edir->elf.ref_regular |= eind->elf.ref_regular;
   4449  1.1  christos   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   4450  1.1  christos   edir->elf.needs_plt |= eind->elf.needs_plt;
   4451  1.1  christos 
   4452  1.1  christos   /* If we were called to copy over info for a weak sym, that's all.  */
   4453  1.1  christos   if (eind->elf.root.type != bfd_link_hash_indirect)
   4454  1.1  christos     return;
   4455  1.1  christos 
   4456  1.1  christos   /* Copy over got entries that we may have already seen to the
   4457  1.1  christos      symbol which just became indirect.  */
   4458  1.1  christos   if (eind->elf.got.glist != NULL)
   4459  1.1  christos     {
   4460  1.1  christos       if (edir->elf.got.glist != NULL)
   4461  1.1  christos 	{
   4462  1.1  christos 	  struct got_entry **entp;
   4463  1.1  christos 	  struct got_entry *ent;
   4464  1.1  christos 
   4465  1.1  christos 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   4466  1.1  christos 	    {
   4467  1.1  christos 	      struct got_entry *dent;
   4468  1.1  christos 
   4469  1.1  christos 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4470  1.1  christos 		if (dent->addend == ent->addend
   4471  1.1  christos 		    && dent->owner == ent->owner
   4472  1.1  christos 		    && dent->tls_type == ent->tls_type)
   4473  1.1  christos 		  {
   4474  1.1  christos 		    dent->got.refcount += ent->got.refcount;
   4475  1.1  christos 		    *entp = ent->next;
   4476  1.1  christos 		    break;
   4477  1.1  christos 		  }
   4478  1.1  christos 	      if (dent == NULL)
   4479  1.1  christos 		entp = &ent->next;
   4480  1.1  christos 	    }
   4481  1.1  christos 	  *entp = edir->elf.got.glist;
   4482  1.1  christos 	}
   4483  1.1  christos 
   4484  1.1  christos       edir->elf.got.glist = eind->elf.got.glist;
   4485  1.1  christos       eind->elf.got.glist = NULL;
   4486  1.1  christos     }
   4487  1.1  christos 
   4488  1.1  christos   /* And plt entries.  */
   4489  1.1  christos   move_plt_plist (eind, edir);
   4490  1.1  christos 
   4491  1.1  christos   if (eind->elf.dynindx != -1)
   4492  1.1  christos     {
   4493  1.1  christos       if (edir->elf.dynindx != -1)
   4494  1.1  christos 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4495  1.1  christos 				edir->elf.dynstr_index);
   4496  1.1  christos       edir->elf.dynindx = eind->elf.dynindx;
   4497  1.1  christos       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4498  1.1  christos       eind->elf.dynindx = -1;
   4499  1.1  christos       eind->elf.dynstr_index = 0;
   4500  1.1  christos     }
   4501  1.1  christos }
   4502  1.1  christos 
   4503  1.1  christos /* Find the function descriptor hash entry from the given function code
   4504  1.1  christos    hash entry FH.  Link the entries via their OH fields.  */
   4505  1.1  christos 
   4506  1.1  christos static struct ppc_link_hash_entry *
   4507  1.1  christos lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4508  1.1  christos {
   4509  1.1  christos   struct ppc_link_hash_entry *fdh = fh->oh;
   4510  1.1  christos 
   4511  1.1  christos   if (fdh == NULL)
   4512  1.1  christos     {
   4513  1.1  christos       const char *fd_name = fh->elf.root.root.string + 1;
   4514  1.1  christos 
   4515  1.1  christos       fdh = (struct ppc_link_hash_entry *)
   4516  1.1  christos 	elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
   4517  1.1  christos       if (fdh == NULL)
   4518  1.1  christos 	return fdh;
   4519  1.1  christos 
   4520  1.1  christos       fdh->is_func_descriptor = 1;
   4521  1.1  christos       fdh->oh = fh;
   4522  1.1  christos       fh->is_func = 1;
   4523  1.1  christos       fh->oh = fdh;
   4524  1.1  christos     }
   4525  1.1  christos 
   4526  1.1  christos   return ppc_follow_link (fdh);
   4527  1.1  christos }
   4528  1.1  christos 
   4529  1.1  christos /* Make a fake function descriptor sym for the code sym FH.  */
   4530  1.1  christos 
   4531  1.1  christos static struct ppc_link_hash_entry *
   4532  1.1  christos make_fdh (struct bfd_link_info *info,
   4533  1.1  christos 	  struct ppc_link_hash_entry *fh)
   4534  1.1  christos {
   4535  1.1  christos   bfd *abfd;
   4536  1.1  christos   asymbol *newsym;
   4537  1.1  christos   struct bfd_link_hash_entry *bh;
   4538  1.1  christos   struct ppc_link_hash_entry *fdh;
   4539  1.1  christos 
   4540  1.1  christos   abfd = fh->elf.root.u.undef.abfd;
   4541  1.1  christos   newsym = bfd_make_empty_symbol (abfd);
   4542  1.1  christos   newsym->name = fh->elf.root.root.string + 1;
   4543  1.1  christos   newsym->section = bfd_und_section_ptr;
   4544  1.1  christos   newsym->value = 0;
   4545  1.1  christos   newsym->flags = BSF_WEAK;
   4546  1.1  christos 
   4547  1.1  christos   bh = NULL;
   4548  1.1  christos   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
   4549  1.1  christos 					 newsym->flags, newsym->section,
   4550  1.1  christos 					 newsym->value, NULL, FALSE, FALSE,
   4551  1.1  christos 					 &bh))
   4552  1.1  christos     return NULL;
   4553  1.1  christos 
   4554  1.1  christos   fdh = (struct ppc_link_hash_entry *) bh;
   4555  1.1  christos   fdh->elf.non_elf = 0;
   4556  1.1  christos   fdh->fake = 1;
   4557  1.1  christos   fdh->is_func_descriptor = 1;
   4558  1.1  christos   fdh->oh = fh;
   4559  1.1  christos   fh->is_func = 1;
   4560  1.1  christos   fh->oh = fdh;
   4561  1.1  christos   return fdh;
   4562  1.1  christos }
   4563  1.1  christos 
   4564  1.1  christos /* Fix function descriptor symbols defined in .opd sections to be
   4565  1.1  christos    function type.  */
   4566  1.1  christos 
   4567  1.1  christos static bfd_boolean
   4568  1.1  christos ppc64_elf_add_symbol_hook (bfd *ibfd,
   4569  1.1  christos 			   struct bfd_link_info *info,
   4570  1.1  christos 			   Elf_Internal_Sym *isym,
   4571  1.1  christos 			   const char **name ATTRIBUTE_UNUSED,
   4572  1.1  christos 			   flagword *flags ATTRIBUTE_UNUSED,
   4573  1.1  christos 			   asection **sec,
   4574  1.1  christos 			   bfd_vma *value ATTRIBUTE_UNUSED)
   4575  1.1  christos {
   4576  1.1  christos   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4577  1.1  christos     {
   4578  1.1  christos       if ((ibfd->flags & DYNAMIC) == 0)
   4579  1.1  christos 	elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
   4580  1.1  christos     }
   4581  1.1  christos   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
   4582  1.1  christos     ;
   4583  1.1  christos   else if (*sec != NULL
   4584  1.1  christos 	   && strcmp ((*sec)->name, ".opd") == 0)
   4585  1.1  christos     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4586  1.1  christos 
   4587  1.1  christos   return TRUE;
   4588  1.1  christos }
   4589  1.1  christos 
   4590  1.1  christos /* This function makes an old ABI object reference to ".bar" cause the
   4591  1.1  christos    inclusion of a new ABI object archive that defines "bar".
   4592  1.1  christos    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4593  1.1  christos    table that might be satisfied by the archive symbols.  */
   4594  1.1  christos 
   4595  1.1  christos static struct elf_link_hash_entry *
   4596  1.1  christos ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4597  1.1  christos 				 struct bfd_link_info *info,
   4598  1.1  christos 				 const char *name)
   4599  1.1  christos {
   4600  1.1  christos   struct elf_link_hash_entry *h;
   4601  1.1  christos   char *dot_name;
   4602  1.1  christos   size_t len;
   4603  1.1  christos 
   4604  1.1  christos   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4605  1.1  christos   if (h != NULL
   4606  1.1  christos       /* Don't return this sym if it is a fake function descriptor
   4607  1.1  christos 	 created by add_symbol_adjust.  */
   4608  1.1  christos       && !(h->root.type == bfd_link_hash_undefweak
   4609  1.1  christos 	   && ((struct ppc_link_hash_entry *) h)->fake))
   4610  1.1  christos     return h;
   4611  1.1  christos 
   4612  1.1  christos   if (name[0] == '.')
   4613  1.1  christos     return h;
   4614  1.1  christos 
   4615  1.1  christos   len = strlen (name);
   4616  1.1  christos   dot_name = bfd_alloc (abfd, len + 2);
   4617  1.1  christos   if (dot_name == NULL)
   4618  1.1  christos     return (struct elf_link_hash_entry *) 0 - 1;
   4619  1.1  christos   dot_name[0] = '.';
   4620  1.1  christos   memcpy (dot_name + 1, name, len + 1);
   4621  1.1  christos   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   4622  1.1  christos   bfd_release (abfd, dot_name);
   4623  1.1  christos   return h;
   4624  1.1  christos }
   4625  1.1  christos 
   4626  1.1  christos /* This function satisfies all old ABI object references to ".bar" if a
   4627  1.1  christos    new ABI object defines "bar".  Well, at least, undefined dot symbols
   4628  1.1  christos    are made weak.  This stops later archive searches from including an
   4629  1.1  christos    object if we already have a function descriptor definition.  It also
   4630  1.1  christos    prevents the linker complaining about undefined symbols.
   4631  1.1  christos    We also check and correct mismatched symbol visibility here.  The
   4632  1.1  christos    most restrictive visibility of the function descriptor and the
   4633  1.1  christos    function entry symbol is used.  */
   4634  1.1  christos 
   4635  1.1  christos static bfd_boolean
   4636  1.1  christos add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   4637  1.1  christos {
   4638  1.1  christos   struct ppc_link_hash_table *htab;
   4639  1.1  christos   struct ppc_link_hash_entry *fdh;
   4640  1.1  christos 
   4641  1.1  christos   if (eh->elf.root.type == bfd_link_hash_indirect)
   4642  1.1  christos     return TRUE;
   4643  1.1  christos 
   4644  1.1  christos   if (eh->elf.root.type == bfd_link_hash_warning)
   4645  1.1  christos     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   4646  1.1  christos 
   4647  1.1  christos   if (eh->elf.root.root.string[0] != '.')
   4648  1.1  christos     abort ();
   4649  1.1  christos 
   4650  1.1  christos   htab = ppc_hash_table (info);
   4651  1.1  christos   if (htab == NULL)
   4652  1.1  christos     return FALSE;
   4653  1.1  christos 
   4654  1.1  christos   fdh = lookup_fdh (eh, htab);
   4655  1.1  christos   if (fdh == NULL)
   4656  1.1  christos     {
   4657  1.1  christos       if (!info->relocatable
   4658  1.1  christos 	  && (eh->elf.root.type == bfd_link_hash_undefined
   4659  1.1  christos 	      || eh->elf.root.type == bfd_link_hash_undefweak)
   4660  1.1  christos 	  && eh->elf.ref_regular)
   4661  1.1  christos 	{
   4662  1.1  christos 	  /* Make an undefweak function descriptor sym, which is enough to
   4663  1.1  christos 	     pull in an --as-needed shared lib, but won't cause link
   4664  1.1  christos 	     errors.  Archives are handled elsewhere.  */
   4665  1.1  christos 	  fdh = make_fdh (info, eh);
   4666  1.1  christos 	  if (fdh == NULL)
   4667  1.1  christos 	    return FALSE;
   4668  1.1  christos 	  fdh->elf.ref_regular = 1;
   4669  1.1  christos 	}
   4670  1.1  christos     }
   4671  1.1  christos   else
   4672  1.1  christos     {
   4673  1.1  christos       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   4674  1.1  christos       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   4675  1.1  christos       if (entry_vis < descr_vis)
   4676  1.1  christos 	fdh->elf.other += entry_vis - descr_vis;
   4677  1.1  christos       else if (entry_vis > descr_vis)
   4678  1.1  christos 	eh->elf.other += descr_vis - entry_vis;
   4679  1.1  christos 
   4680  1.1  christos       if ((fdh->elf.root.type == bfd_link_hash_defined
   4681  1.1  christos 	   || fdh->elf.root.type == bfd_link_hash_defweak)
   4682  1.1  christos 	  && eh->elf.root.type == bfd_link_hash_undefined)
   4683  1.1  christos 	{
   4684  1.1  christos 	  eh->elf.root.type = bfd_link_hash_undefweak;
   4685  1.1  christos 	  eh->was_undefined = 1;
   4686  1.1  christos 	  htab->twiddled_syms = 1;
   4687  1.1  christos 	}
   4688  1.1  christos     }
   4689  1.1  christos 
   4690  1.1  christos   return TRUE;
   4691  1.1  christos }
   4692  1.1  christos 
   4693  1.1  christos /* Process list of dot-symbols we made in link_hash_newfunc.  */
   4694  1.1  christos 
   4695  1.1  christos static bfd_boolean
   4696  1.1  christos ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
   4697  1.1  christos {
   4698  1.1  christos   struct ppc_link_hash_table *htab;
   4699  1.1  christos   struct ppc_link_hash_entry **p, *eh;
   4700  1.1  christos 
   4701  1.1  christos   if (!is_ppc64_elf (info->output_bfd))
   4702  1.1  christos     return TRUE;
   4703  1.1  christos   htab = ppc_hash_table (info);
   4704  1.1  christos   if (htab == NULL)
   4705  1.1  christos     return FALSE;
   4706  1.1  christos 
   4707  1.1  christos   if (is_ppc64_elf (ibfd))
   4708  1.1  christos     {
   4709  1.1  christos       p = &htab->dot_syms;
   4710  1.1  christos       while ((eh = *p) != NULL)
   4711  1.1  christos 	{
   4712  1.1  christos 	  *p = NULL;
   4713  1.1  christos 	  if (!add_symbol_adjust (eh, info))
   4714  1.1  christos 	    return FALSE;
   4715  1.1  christos 	  p = &eh->u.next_dot_sym;
   4716  1.1  christos 	}
   4717  1.1  christos     }
   4718  1.1  christos 
   4719  1.1  christos   /* Clear the list for non-ppc64 input files.  */
   4720  1.1  christos   p = &htab->dot_syms;
   4721  1.1  christos   while ((eh = *p) != NULL)
   4722  1.1  christos     {
   4723  1.1  christos       *p = NULL;
   4724  1.1  christos       p = &eh->u.next_dot_sym;
   4725  1.1  christos     }
   4726  1.1  christos 
   4727  1.1  christos   /* We need to fix the undefs list for any syms we have twiddled to
   4728  1.1  christos      undef_weak.  */
   4729  1.1  christos   if (htab->twiddled_syms)
   4730  1.1  christos     {
   4731  1.1  christos       bfd_link_repair_undef_list (&htab->elf.root);
   4732  1.1  christos       htab->twiddled_syms = 0;
   4733  1.1  christos     }
   4734  1.1  christos   return TRUE;
   4735  1.1  christos }
   4736  1.1  christos 
   4737  1.1  christos /* Undo hash table changes when an --as-needed input file is determined
   4738  1.1  christos    not to be needed.  */
   4739  1.1  christos 
   4740  1.1  christos static bfd_boolean
   4741  1.1  christos ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
   4742  1.1  christos 			     struct bfd_link_info *info)
   4743  1.1  christos {
   4744  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4745  1.1  christos 
   4746  1.1  christos   if (htab == NULL)
   4747  1.1  christos     return FALSE;
   4748  1.1  christos 
   4749  1.1  christos   htab->dot_syms = NULL;
   4750  1.1  christos   return TRUE;
   4751  1.1  christos }
   4752  1.1  christos 
   4753  1.1  christos /* If --just-symbols against a final linked binary, then assume we need
   4754  1.1  christos    toc adjusting stubs when calling functions defined there.  */
   4755  1.1  christos 
   4756  1.1  christos static void
   4757  1.1  christos ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   4758  1.1  christos {
   4759  1.1  christos   if ((sec->flags & SEC_CODE) != 0
   4760  1.1  christos       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   4761  1.1  christos       && is_ppc64_elf (sec->owner))
   4762  1.1  christos     {
   4763  1.1  christos       asection *got = bfd_get_section_by_name (sec->owner, ".got");
   4764  1.1  christos       if (got != NULL
   4765  1.1  christos 	  && got->size >= elf_backend_got_header_size
   4766  1.1  christos 	  && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   4767  1.1  christos 	sec->has_toc_reloc = 1;
   4768  1.1  christos     }
   4769  1.1  christos   _bfd_elf_link_just_syms (sec, info);
   4770  1.1  christos }
   4771  1.1  christos 
   4772  1.1  christos static struct plt_entry **
   4773  1.1  christos update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   4774  1.1  christos 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   4775  1.1  christos {
   4776  1.1  christos   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   4777  1.1  christos   struct plt_entry **local_plt;
   4778  1.1  christos   unsigned char *local_got_tls_masks;
   4779  1.1  christos 
   4780  1.1  christos   if (local_got_ents == NULL)
   4781  1.1  christos     {
   4782  1.1  christos       bfd_size_type size = symtab_hdr->sh_info;
   4783  1.1  christos 
   4784  1.1  christos       size *= (sizeof (*local_got_ents)
   4785  1.1  christos 	       + sizeof (*local_plt)
   4786  1.1  christos 	       + sizeof (*local_got_tls_masks));
   4787  1.1  christos       local_got_ents = bfd_zalloc (abfd, size);
   4788  1.1  christos       if (local_got_ents == NULL)
   4789  1.1  christos 	return NULL;
   4790  1.1  christos       elf_local_got_ents (abfd) = local_got_ents;
   4791  1.1  christos     }
   4792  1.1  christos 
   4793  1.1  christos   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
   4794  1.1  christos     {
   4795  1.1  christos       struct got_entry *ent;
   4796  1.1  christos 
   4797  1.1  christos       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   4798  1.1  christos 	if (ent->addend == r_addend
   4799  1.1  christos 	    && ent->owner == abfd
   4800  1.1  christos 	    && ent->tls_type == tls_type)
   4801  1.1  christos 	  break;
   4802  1.1  christos       if (ent == NULL)
   4803  1.1  christos 	{
   4804  1.1  christos 	  bfd_size_type amt = sizeof (*ent);
   4805  1.1  christos 	  ent = bfd_alloc (abfd, amt);
   4806  1.1  christos 	  if (ent == NULL)
   4807  1.1  christos 	    return FALSE;
   4808  1.1  christos 	  ent->next = local_got_ents[r_symndx];
   4809  1.1  christos 	  ent->addend = r_addend;
   4810  1.1  christos 	  ent->owner = abfd;
   4811  1.1  christos 	  ent->tls_type = tls_type;
   4812  1.1  christos 	  ent->is_indirect = FALSE;
   4813  1.1  christos 	  ent->got.refcount = 0;
   4814  1.1  christos 	  local_got_ents[r_symndx] = ent;
   4815  1.1  christos 	}
   4816  1.1  christos       ent->got.refcount += 1;
   4817  1.1  christos     }
   4818  1.1  christos 
   4819  1.1  christos   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   4820  1.1  christos   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   4821  1.1  christos   local_got_tls_masks[r_symndx] |= tls_type;
   4822  1.1  christos 
   4823  1.1  christos   return local_plt + r_symndx;
   4824  1.1  christos }
   4825  1.1  christos 
   4826  1.1  christos static bfd_boolean
   4827  1.1  christos update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   4828  1.1  christos {
   4829  1.1  christos   struct plt_entry *ent;
   4830  1.1  christos 
   4831  1.1  christos   for (ent = *plist; ent != NULL; ent = ent->next)
   4832  1.1  christos     if (ent->addend == addend)
   4833  1.1  christos       break;
   4834  1.1  christos   if (ent == NULL)
   4835  1.1  christos     {
   4836  1.1  christos       bfd_size_type amt = sizeof (*ent);
   4837  1.1  christos       ent = bfd_alloc (abfd, amt);
   4838  1.1  christos       if (ent == NULL)
   4839  1.1  christos 	return FALSE;
   4840  1.1  christos       ent->next = *plist;
   4841  1.1  christos       ent->addend = addend;
   4842  1.1  christos       ent->plt.refcount = 0;
   4843  1.1  christos       *plist = ent;
   4844  1.1  christos     }
   4845  1.1  christos   ent->plt.refcount += 1;
   4846  1.1  christos   return TRUE;
   4847  1.1  christos }
   4848  1.1  christos 
   4849  1.1  christos static bfd_boolean
   4850  1.1  christos is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   4851  1.1  christos {
   4852  1.1  christos   return (r_type == R_PPC64_REL24
   4853  1.1  christos 	  || r_type == R_PPC64_REL14
   4854  1.1  christos 	  || r_type == R_PPC64_REL14_BRTAKEN
   4855  1.1  christos 	  || r_type == R_PPC64_REL14_BRNTAKEN
   4856  1.1  christos 	  || r_type == R_PPC64_ADDR24
   4857  1.1  christos 	  || r_type == R_PPC64_ADDR14
   4858  1.1  christos 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   4859  1.1  christos 	  || r_type == R_PPC64_ADDR14_BRNTAKEN);
   4860  1.1  christos }
   4861  1.1  christos 
   4862  1.1  christos /* Look through the relocs for a section during the first phase, and
   4863  1.1  christos    calculate needed space in the global offset table, procedure
   4864  1.1  christos    linkage table, and dynamic reloc sections.  */
   4865  1.1  christos 
   4866  1.1  christos static bfd_boolean
   4867  1.1  christos ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   4868  1.1  christos 			asection *sec, const Elf_Internal_Rela *relocs)
   4869  1.1  christos {
   4870  1.1  christos   struct ppc_link_hash_table *htab;
   4871  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   4872  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   4873  1.1  christos   const Elf_Internal_Rela *rel;
   4874  1.1  christos   const Elf_Internal_Rela *rel_end;
   4875  1.1  christos   asection *sreloc;
   4876  1.1  christos   asection **opd_sym_map;
   4877  1.1  christos   struct elf_link_hash_entry *tga, *dottga;
   4878  1.1  christos 
   4879  1.1  christos   if (info->relocatable)
   4880  1.1  christos     return TRUE;
   4881  1.1  christos 
   4882  1.1  christos   /* Don't do anything special with non-loaded, non-alloced sections.
   4883  1.1  christos      In particular, any relocs in such sections should not affect GOT
   4884  1.1  christos      and PLT reference counting (ie. we don't allow them to create GOT
   4885  1.1  christos      or PLT entries), there's no possibility or desire to optimize TLS
   4886  1.1  christos      relocs, and there's not much point in propagating relocs to shared
   4887  1.1  christos      libs that the dynamic linker won't relocate.  */
   4888  1.1  christos   if ((sec->flags & SEC_ALLOC) == 0)
   4889  1.1  christos     return TRUE;
   4890  1.1  christos 
   4891  1.1  christos   BFD_ASSERT (is_ppc64_elf (abfd));
   4892  1.1  christos 
   4893  1.1  christos   htab = ppc_hash_table (info);
   4894  1.1  christos   if (htab == NULL)
   4895  1.1  christos     return FALSE;
   4896  1.1  christos 
   4897  1.1  christos   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4898  1.1  christos 			      FALSE, FALSE, TRUE);
   4899  1.1  christos   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   4900  1.1  christos 				 FALSE, FALSE, TRUE);
   4901  1.1  christos   symtab_hdr = &elf_symtab_hdr (abfd);
   4902  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   4903  1.1  christos   sreloc = NULL;
   4904  1.1  christos   opd_sym_map = NULL;
   4905  1.1  christos   if (strcmp (sec->name, ".opd") == 0)
   4906  1.1  christos     {
   4907  1.1  christos       /* Garbage collection needs some extra help with .opd sections.
   4908  1.1  christos 	 We don't want to necessarily keep everything referenced by
   4909  1.1  christos 	 relocs in .opd, as that would keep all functions.  Instead,
   4910  1.1  christos 	 if we reference an .opd symbol (a function descriptor), we
   4911  1.1  christos 	 want to keep the function code symbol's section.  This is
   4912  1.1  christos 	 easy for global symbols, but for local syms we need to keep
   4913  1.1  christos 	 information about the associated function section.  */
   4914  1.1  christos       bfd_size_type amt;
   4915  1.1  christos 
   4916  1.1  christos       amt = sec->size * sizeof (*opd_sym_map) / 8;
   4917  1.1  christos       opd_sym_map = bfd_zalloc (abfd, amt);
   4918  1.1  christos       if (opd_sym_map == NULL)
   4919  1.1  christos 	return FALSE;
   4920  1.1  christos       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
   4921  1.1  christos       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
   4922  1.1  christos       ppc64_elf_section_data (sec)->sec_type = sec_opd;
   4923  1.1  christos     }
   4924  1.1  christos 
   4925  1.1  christos   if (htab->sfpr == NULL
   4926  1.1  christos       && !create_linkage_sections (htab->elf.dynobj, info))
   4927  1.1  christos     return FALSE;
   4928  1.1  christos 
   4929  1.1  christos   rel_end = relocs + sec->reloc_count;
   4930  1.1  christos   for (rel = relocs; rel < rel_end; rel++)
   4931  1.1  christos     {
   4932  1.1  christos       unsigned long r_symndx;
   4933  1.1  christos       struct elf_link_hash_entry *h;
   4934  1.1  christos       enum elf_ppc64_reloc_type r_type;
   4935  1.1  christos       int tls_type;
   4936  1.1  christos       struct _ppc64_elf_section_data *ppc64_sec;
   4937  1.1  christos       struct plt_entry **ifunc;
   4938  1.1  christos 
   4939  1.1  christos       r_symndx = ELF64_R_SYM (rel->r_info);
   4940  1.1  christos       if (r_symndx < symtab_hdr->sh_info)
   4941  1.1  christos 	h = NULL;
   4942  1.1  christos       else
   4943  1.1  christos 	{
   4944  1.1  christos 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4945  1.1  christos 	  h = elf_follow_link (h);
   4946  1.1  christos 	}
   4947  1.1  christos 
   4948  1.1  christos       tls_type = 0;
   4949  1.1  christos       ifunc = NULL;
   4950  1.1  christos       if (h != NULL)
   4951  1.1  christos 	{
   4952  1.1  christos 	  if (h->type == STT_GNU_IFUNC)
   4953  1.1  christos 	    {
   4954  1.1  christos 	      h->needs_plt = 1;
   4955  1.1  christos 	      ifunc = &h->plt.plist;
   4956  1.1  christos 	    }
   4957  1.1  christos 	}
   4958  1.1  christos       else
   4959  1.1  christos 	{
   4960  1.1  christos 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   4961  1.1  christos 							  abfd, r_symndx);
   4962  1.1  christos 	  if (isym == NULL)
   4963  1.1  christos 	    return FALSE;
   4964  1.1  christos 
   4965  1.1  christos 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4966  1.1  christos 	    {
   4967  1.1  christos 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4968  1.1  christos 					     rel->r_addend, PLT_IFUNC);
   4969  1.1  christos 	      if (ifunc == NULL)
   4970  1.1  christos 		return FALSE;
   4971  1.1  christos 	    }
   4972  1.1  christos 	}
   4973  1.1  christos       r_type = ELF64_R_TYPE (rel->r_info);
   4974  1.1  christos       if (is_branch_reloc (r_type))
   4975  1.1  christos 	{
   4976  1.1  christos 	  if (h != NULL && (h == tga || h == dottga))
   4977  1.1  christos 	    {
   4978  1.1  christos 	      if (rel != relocs
   4979  1.1  christos 		  && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   4980  1.1  christos 		      || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   4981  1.1  christos 		/* We have a new-style __tls_get_addr call with a marker
   4982  1.1  christos 		   reloc.  */
   4983  1.1  christos 		;
   4984  1.1  christos 	      else
   4985  1.1  christos 		/* Mark this section as having an old-style call.  */
   4986  1.1  christos 		sec->has_tls_get_addr_call = 1;
   4987  1.1  christos 	    }
   4988  1.1  christos 
   4989  1.1  christos 	  /* STT_GNU_IFUNC symbols must have a PLT entry.  */
   4990  1.1  christos 	  if (ifunc != NULL
   4991  1.1  christos 	      && !update_plt_info (abfd, ifunc, rel->r_addend))
   4992  1.1  christos 	    return FALSE;
   4993  1.1  christos 	}
   4994  1.1  christos 
   4995  1.1  christos       switch (r_type)
   4996  1.1  christos 	{
   4997  1.1  christos 	case R_PPC64_TLSGD:
   4998  1.1  christos 	case R_PPC64_TLSLD:
   4999  1.1  christos 	  /* These special tls relocs tie a call to __tls_get_addr with
   5000  1.1  christos 	     its parameter symbol.  */
   5001  1.1  christos 	  break;
   5002  1.1  christos 
   5003  1.1  christos 	case R_PPC64_GOT_TLSLD16:
   5004  1.1  christos 	case R_PPC64_GOT_TLSLD16_LO:
   5005  1.1  christos 	case R_PPC64_GOT_TLSLD16_HI:
   5006  1.1  christos 	case R_PPC64_GOT_TLSLD16_HA:
   5007  1.1  christos 	  tls_type = TLS_TLS | TLS_LD;
   5008  1.1  christos 	  goto dogottls;
   5009  1.1  christos 
   5010  1.1  christos 	case R_PPC64_GOT_TLSGD16:
   5011  1.1  christos 	case R_PPC64_GOT_TLSGD16_LO:
   5012  1.1  christos 	case R_PPC64_GOT_TLSGD16_HI:
   5013  1.1  christos 	case R_PPC64_GOT_TLSGD16_HA:
   5014  1.1  christos 	  tls_type = TLS_TLS | TLS_GD;
   5015  1.1  christos 	  goto dogottls;
   5016  1.1  christos 
   5017  1.1  christos 	case R_PPC64_GOT_TPREL16_DS:
   5018  1.1  christos 	case R_PPC64_GOT_TPREL16_LO_DS:
   5019  1.1  christos 	case R_PPC64_GOT_TPREL16_HI:
   5020  1.1  christos 	case R_PPC64_GOT_TPREL16_HA:
   5021  1.1  christos 	  if (!info->executable)
   5022  1.1  christos 	    info->flags |= DF_STATIC_TLS;
   5023  1.1  christos 	  tls_type = TLS_TLS | TLS_TPREL;
   5024  1.1  christos 	  goto dogottls;
   5025  1.1  christos 
   5026  1.1  christos 	case R_PPC64_GOT_DTPREL16_DS:
   5027  1.1  christos 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5028  1.1  christos 	case R_PPC64_GOT_DTPREL16_HI:
   5029  1.1  christos 	case R_PPC64_GOT_DTPREL16_HA:
   5030  1.1  christos 	  tls_type = TLS_TLS | TLS_DTPREL;
   5031  1.1  christos 	dogottls:
   5032  1.1  christos 	  sec->has_tls_reloc = 1;
   5033  1.1  christos 	  /* Fall thru */
   5034  1.1  christos 
   5035  1.1  christos 	case R_PPC64_GOT16:
   5036  1.1  christos 	case R_PPC64_GOT16_DS:
   5037  1.1  christos 	case R_PPC64_GOT16_HA:
   5038  1.1  christos 	case R_PPC64_GOT16_HI:
   5039  1.1  christos 	case R_PPC64_GOT16_LO:
   5040  1.1  christos 	case R_PPC64_GOT16_LO_DS:
   5041  1.1  christos 	  /* This symbol requires a global offset table entry.  */
   5042  1.1  christos 	  sec->has_toc_reloc = 1;
   5043  1.1  christos 	  if (r_type == R_PPC64_GOT_TLSLD16
   5044  1.1  christos 	      || r_type == R_PPC64_GOT_TLSGD16
   5045  1.1  christos 	      || r_type == R_PPC64_GOT_TPREL16_DS
   5046  1.1  christos 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   5047  1.1  christos 	      || r_type == R_PPC64_GOT16
   5048  1.1  christos 	      || r_type == R_PPC64_GOT16_DS)
   5049  1.1  christos 	    {
   5050  1.1  christos 	      htab->do_multi_toc = 1;
   5051  1.1  christos 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5052  1.1  christos 	    }
   5053  1.1  christos 
   5054  1.1  christos 	  if (ppc64_elf_tdata (abfd)->got == NULL
   5055  1.1  christos 	      && !create_got_section (abfd, info))
   5056  1.1  christos 	    return FALSE;
   5057  1.1  christos 
   5058  1.1  christos 	  if (h != NULL)
   5059  1.1  christos 	    {
   5060  1.1  christos 	      struct ppc_link_hash_entry *eh;
   5061  1.1  christos 	      struct got_entry *ent;
   5062  1.1  christos 
   5063  1.1  christos 	      eh = (struct ppc_link_hash_entry *) h;
   5064  1.1  christos 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   5065  1.1  christos 		if (ent->addend == rel->r_addend
   5066  1.1  christos 		    && ent->owner == abfd
   5067  1.1  christos 		    && ent->tls_type == tls_type)
   5068  1.1  christos 		  break;
   5069  1.1  christos 	      if (ent == NULL)
   5070  1.1  christos 		{
   5071  1.1  christos 		  bfd_size_type amt = sizeof (*ent);
   5072  1.1  christos 		  ent = bfd_alloc (abfd, amt);
   5073  1.1  christos 		  if (ent == NULL)
   5074  1.1  christos 		    return FALSE;
   5075  1.1  christos 		  ent->next = eh->elf.got.glist;
   5076  1.1  christos 		  ent->addend = rel->r_addend;
   5077  1.1  christos 		  ent->owner = abfd;
   5078  1.1  christos 		  ent->tls_type = tls_type;
   5079  1.1  christos 		  ent->is_indirect = FALSE;
   5080  1.1  christos 		  ent->got.refcount = 0;
   5081  1.1  christos 		  eh->elf.got.glist = ent;
   5082  1.1  christos 		}
   5083  1.1  christos 	      ent->got.refcount += 1;
   5084  1.1  christos 	      eh->tls_mask |= tls_type;
   5085  1.1  christos 	    }
   5086  1.1  christos 	  else
   5087  1.1  christos 	    /* This is a global offset table entry for a local symbol.  */
   5088  1.1  christos 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5089  1.1  christos 					rel->r_addend, tls_type))
   5090  1.1  christos 	      return FALSE;
   5091  1.1  christos 	  break;
   5092  1.1  christos 
   5093  1.1  christos 	case R_PPC64_PLT16_HA:
   5094  1.1  christos 	case R_PPC64_PLT16_HI:
   5095  1.1  christos 	case R_PPC64_PLT16_LO:
   5096  1.1  christos 	case R_PPC64_PLT32:
   5097  1.1  christos 	case R_PPC64_PLT64:
   5098  1.1  christos 	  /* This symbol requires a procedure linkage table entry.  We
   5099  1.1  christos 	     actually build the entry in adjust_dynamic_symbol,
   5100  1.1  christos 	     because this might be a case of linking PIC code without
   5101  1.1  christos 	     linking in any dynamic objects, in which case we don't
   5102  1.1  christos 	     need to generate a procedure linkage table after all.  */
   5103  1.1  christos 	  if (h == NULL)
   5104  1.1  christos 	    {
   5105  1.1  christos 	      /* It does not make sense to have a procedure linkage
   5106  1.1  christos 		 table entry for a local symbol.  */
   5107  1.1  christos 	      bfd_set_error (bfd_error_bad_value);
   5108  1.1  christos 	      return FALSE;
   5109  1.1  christos 	    }
   5110  1.1  christos 	  else
   5111  1.1  christos 	    {
   5112  1.1  christos 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5113  1.1  christos 		return FALSE;
   5114  1.1  christos 	      h->needs_plt = 1;
   5115  1.1  christos 	      if (h->root.root.string[0] == '.'
   5116  1.1  christos 		  && h->root.root.string[1] != '\0')
   5117  1.1  christos 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5118  1.1  christos 	    }
   5119  1.1  christos 	  break;
   5120  1.1  christos 
   5121  1.1  christos 	  /* The following relocations don't need to propagate the
   5122  1.1  christos 	     relocation if linking a shared object since they are
   5123  1.1  christos 	     section relative.  */
   5124  1.1  christos 	case R_PPC64_SECTOFF:
   5125  1.1  christos 	case R_PPC64_SECTOFF_LO:
   5126  1.1  christos 	case R_PPC64_SECTOFF_HI:
   5127  1.1  christos 	case R_PPC64_SECTOFF_HA:
   5128  1.1  christos 	case R_PPC64_SECTOFF_DS:
   5129  1.1  christos 	case R_PPC64_SECTOFF_LO_DS:
   5130  1.1  christos 	case R_PPC64_DTPREL16:
   5131  1.1  christos 	case R_PPC64_DTPREL16_LO:
   5132  1.1  christos 	case R_PPC64_DTPREL16_HI:
   5133  1.1  christos 	case R_PPC64_DTPREL16_HA:
   5134  1.1  christos 	case R_PPC64_DTPREL16_DS:
   5135  1.1  christos 	case R_PPC64_DTPREL16_LO_DS:
   5136  1.1  christos 	case R_PPC64_DTPREL16_HIGHER:
   5137  1.1  christos 	case R_PPC64_DTPREL16_HIGHERA:
   5138  1.1  christos 	case R_PPC64_DTPREL16_HIGHEST:
   5139  1.1  christos 	case R_PPC64_DTPREL16_HIGHESTA:
   5140  1.1  christos 	  break;
   5141  1.1  christos 
   5142  1.1  christos 	  /* Nor do these.  */
   5143  1.1  christos 	case R_PPC64_REL16:
   5144  1.1  christos 	case R_PPC64_REL16_LO:
   5145  1.1  christos 	case R_PPC64_REL16_HI:
   5146  1.1  christos 	case R_PPC64_REL16_HA:
   5147  1.1  christos 	  break;
   5148  1.1  christos 
   5149  1.1  christos 	case R_PPC64_TOC16:
   5150  1.1  christos 	case R_PPC64_TOC16_DS:
   5151  1.1  christos 	  htab->do_multi_toc = 1;
   5152  1.1  christos 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5153  1.1  christos 	case R_PPC64_TOC16_LO:
   5154  1.1  christos 	case R_PPC64_TOC16_HI:
   5155  1.1  christos 	case R_PPC64_TOC16_HA:
   5156  1.1  christos 	case R_PPC64_TOC16_LO_DS:
   5157  1.1  christos 	  sec->has_toc_reloc = 1;
   5158  1.1  christos 	  break;
   5159  1.1  christos 
   5160  1.1  christos 	  /* This relocation describes the C++ object vtable hierarchy.
   5161  1.1  christos 	     Reconstruct it for later use during GC.  */
   5162  1.1  christos 	case R_PPC64_GNU_VTINHERIT:
   5163  1.1  christos 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   5164  1.1  christos 	    return FALSE;
   5165  1.1  christos 	  break;
   5166  1.1  christos 
   5167  1.1  christos 	  /* This relocation describes which C++ vtable entries are actually
   5168  1.1  christos 	     used.  Record for later use during GC.  */
   5169  1.1  christos 	case R_PPC64_GNU_VTENTRY:
   5170  1.1  christos 	  BFD_ASSERT (h != NULL);
   5171  1.1  christos 	  if (h != NULL
   5172  1.1  christos 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   5173  1.1  christos 	    return FALSE;
   5174  1.1  christos 	  break;
   5175  1.1  christos 
   5176  1.1  christos 	case R_PPC64_REL14:
   5177  1.1  christos 	case R_PPC64_REL14_BRTAKEN:
   5178  1.1  christos 	case R_PPC64_REL14_BRNTAKEN:
   5179  1.1  christos 	  {
   5180  1.1  christos 	    asection *dest = NULL;
   5181  1.1  christos 
   5182  1.1  christos 	    /* Heuristic: If jumping outside our section, chances are
   5183  1.1  christos 	       we are going to need a stub.  */
   5184  1.1  christos 	    if (h != NULL)
   5185  1.1  christos 	      {
   5186  1.1  christos 		/* If the sym is weak it may be overridden later, so
   5187  1.1  christos 		   don't assume we know where a weak sym lives.  */
   5188  1.1  christos 		if (h->root.type == bfd_link_hash_defined)
   5189  1.1  christos 		  dest = h->root.u.def.section;
   5190  1.1  christos 	      }
   5191  1.1  christos 	    else
   5192  1.1  christos 	      {
   5193  1.1  christos 		Elf_Internal_Sym *isym;
   5194  1.1  christos 
   5195  1.1  christos 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5196  1.1  christos 					      abfd, r_symndx);
   5197  1.1  christos 		if (isym == NULL)
   5198  1.1  christos 		  return FALSE;
   5199  1.1  christos 
   5200  1.1  christos 		dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5201  1.1  christos 	      }
   5202  1.1  christos 
   5203  1.1  christos 	    if (dest != sec)
   5204  1.1  christos 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   5205  1.1  christos 	  }
   5206  1.1  christos 	  /* Fall through.  */
   5207  1.1  christos 
   5208  1.1  christos 	case R_PPC64_REL24:
   5209  1.1  christos 	  if (h != NULL && ifunc == NULL)
   5210  1.1  christos 	    {
   5211  1.1  christos 	      /* We may need a .plt entry if the function this reloc
   5212  1.1  christos 		 refers to is in a shared lib.  */
   5213  1.1  christos 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5214  1.1  christos 		return FALSE;
   5215  1.1  christos 	      h->needs_plt = 1;
   5216  1.1  christos 	      if (h->root.root.string[0] == '.'
   5217  1.1  christos 		  && h->root.root.string[1] != '\0')
   5218  1.1  christos 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5219  1.1  christos 	      if (h == tga || h == dottga)
   5220  1.1  christos 		sec->has_tls_reloc = 1;
   5221  1.1  christos 	    }
   5222  1.1  christos 	  break;
   5223  1.1  christos 
   5224  1.1  christos 	case R_PPC64_TPREL64:
   5225  1.1  christos 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5226  1.1  christos 	  if (!info->executable)
   5227  1.1  christos 	    info->flags |= DF_STATIC_TLS;
   5228  1.1  christos 	  goto dotlstoc;
   5229  1.1  christos 
   5230  1.1  christos 	case R_PPC64_DTPMOD64:
   5231  1.1  christos 	  if (rel + 1 < rel_end
   5232  1.1  christos 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5233  1.1  christos 	      && rel[1].r_offset == rel->r_offset + 8)
   5234  1.1  christos 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5235  1.1  christos 	  else
   5236  1.1  christos 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5237  1.1  christos 	  goto dotlstoc;
   5238  1.1  christos 
   5239  1.1  christos 	case R_PPC64_DTPREL64:
   5240  1.1  christos 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5241  1.1  christos 	  if (rel != relocs
   5242  1.1  christos 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5243  1.1  christos 	      && rel[-1].r_offset == rel->r_offset - 8)
   5244  1.1  christos 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5245  1.1  christos 	       Don't mark with TLS_DTPREL.  */
   5246  1.1  christos 	    goto dodyn;
   5247  1.1  christos 
   5248  1.1  christos 	dotlstoc:
   5249  1.1  christos 	  sec->has_tls_reloc = 1;
   5250  1.1  christos 	  if (h != NULL)
   5251  1.1  christos 	    {
   5252  1.1  christos 	      struct ppc_link_hash_entry *eh;
   5253  1.1  christos 	      eh = (struct ppc_link_hash_entry *) h;
   5254  1.1  christos 	      eh->tls_mask |= tls_type;
   5255  1.1  christos 	    }
   5256  1.1  christos 	  else
   5257  1.1  christos 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5258  1.1  christos 					rel->r_addend, tls_type))
   5259  1.1  christos 	      return FALSE;
   5260  1.1  christos 
   5261  1.1  christos 	  ppc64_sec = ppc64_elf_section_data (sec);
   5262  1.1  christos 	  if (ppc64_sec->sec_type != sec_toc)
   5263  1.1  christos 	    {
   5264  1.1  christos 	      bfd_size_type amt;
   5265  1.1  christos 
   5266  1.1  christos 	      /* One extra to simplify get_tls_mask.  */
   5267  1.1  christos 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5268  1.1  christos 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5269  1.1  christos 	      if (ppc64_sec->u.toc.symndx == NULL)
   5270  1.1  christos 		return FALSE;
   5271  1.1  christos 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5272  1.1  christos 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5273  1.1  christos 	      if (ppc64_sec->u.toc.add == NULL)
   5274  1.1  christos 		return FALSE;
   5275  1.1  christos 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5276  1.1  christos 	      ppc64_sec->sec_type = sec_toc;
   5277  1.1  christos 	    }
   5278  1.1  christos 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5279  1.1  christos 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5280  1.1  christos 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5281  1.1  christos 
   5282  1.1  christos 	  /* Mark the second slot of a GD or LD entry.
   5283  1.1  christos 	     -1 to indicate GD and -2 to indicate LD.  */
   5284  1.1  christos 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5285  1.1  christos 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5286  1.1  christos 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5287  1.1  christos 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5288  1.1  christos 	  goto dodyn;
   5289  1.1  christos 
   5290  1.1  christos 	case R_PPC64_TPREL16:
   5291  1.1  christos 	case R_PPC64_TPREL16_LO:
   5292  1.1  christos 	case R_PPC64_TPREL16_HI:
   5293  1.1  christos 	case R_PPC64_TPREL16_HA:
   5294  1.1  christos 	case R_PPC64_TPREL16_DS:
   5295  1.1  christos 	case R_PPC64_TPREL16_LO_DS:
   5296  1.1  christos 	case R_PPC64_TPREL16_HIGHER:
   5297  1.1  christos 	case R_PPC64_TPREL16_HIGHERA:
   5298  1.1  christos 	case R_PPC64_TPREL16_HIGHEST:
   5299  1.1  christos 	case R_PPC64_TPREL16_HIGHESTA:
   5300  1.1  christos 	  if (info->shared)
   5301  1.1  christos 	    {
   5302  1.1  christos 	      if (!info->executable)
   5303  1.1  christos 		info->flags |= DF_STATIC_TLS;
   5304  1.1  christos 	      goto dodyn;
   5305  1.1  christos 	    }
   5306  1.1  christos 	  break;
   5307  1.1  christos 
   5308  1.1  christos 	case R_PPC64_ADDR64:
   5309  1.1  christos 	  if (opd_sym_map != NULL
   5310  1.1  christos 	      && rel + 1 < rel_end
   5311  1.1  christos 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5312  1.1  christos 	    {
   5313  1.1  christos 	      if (h != NULL)
   5314  1.1  christos 		{
   5315  1.1  christos 		  if (h->root.root.string[0] == '.'
   5316  1.1  christos 		      && h->root.root.string[1] != 0
   5317  1.1  christos 		      && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
   5318  1.1  christos 		    ;
   5319  1.1  christos 		  else
   5320  1.1  christos 		    ((struct ppc_link_hash_entry *) h)->is_func = 1;
   5321  1.1  christos 		}
   5322  1.1  christos 	      else
   5323  1.1  christos 		{
   5324  1.1  christos 		  asection *s;
   5325  1.1  christos 		  Elf_Internal_Sym *isym;
   5326  1.1  christos 
   5327  1.1  christos 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5328  1.1  christos 						abfd, r_symndx);
   5329  1.1  christos 		  if (isym == NULL)
   5330  1.1  christos 		    return FALSE;
   5331  1.1  christos 
   5332  1.1  christos 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5333  1.1  christos 		  if (s != NULL && s != sec)
   5334  1.1  christos 		    opd_sym_map[rel->r_offset / 8] = s;
   5335  1.1  christos 		}
   5336  1.1  christos 	    }
   5337  1.1  christos 	  /* Fall through.  */
   5338  1.1  christos 
   5339  1.1  christos 	case R_PPC64_REL30:
   5340  1.1  christos 	case R_PPC64_REL32:
   5341  1.1  christos 	case R_PPC64_REL64:
   5342  1.1  christos 	case R_PPC64_ADDR14:
   5343  1.1  christos 	case R_PPC64_ADDR14_BRNTAKEN:
   5344  1.1  christos 	case R_PPC64_ADDR14_BRTAKEN:
   5345  1.1  christos 	case R_PPC64_ADDR16:
   5346  1.1  christos 	case R_PPC64_ADDR16_DS:
   5347  1.1  christos 	case R_PPC64_ADDR16_HA:
   5348  1.1  christos 	case R_PPC64_ADDR16_HI:
   5349  1.1  christos 	case R_PPC64_ADDR16_HIGHER:
   5350  1.1  christos 	case R_PPC64_ADDR16_HIGHERA:
   5351  1.1  christos 	case R_PPC64_ADDR16_HIGHEST:
   5352  1.1  christos 	case R_PPC64_ADDR16_HIGHESTA:
   5353  1.1  christos 	case R_PPC64_ADDR16_LO:
   5354  1.1  christos 	case R_PPC64_ADDR16_LO_DS:
   5355  1.1  christos 	case R_PPC64_ADDR24:
   5356  1.1  christos 	case R_PPC64_ADDR32:
   5357  1.1  christos 	case R_PPC64_UADDR16:
   5358  1.1  christos 	case R_PPC64_UADDR32:
   5359  1.1  christos 	case R_PPC64_UADDR64:
   5360  1.1  christos 	case R_PPC64_TOC:
   5361  1.1  christos 	  if (h != NULL && !info->shared)
   5362  1.1  christos 	    /* We may need a copy reloc.  */
   5363  1.1  christos 	    h->non_got_ref = 1;
   5364  1.1  christos 
   5365  1.1  christos 	  /* Don't propagate .opd relocs.  */
   5366  1.1  christos 	  if (NO_OPD_RELOCS && opd_sym_map != NULL)
   5367  1.1  christos 	    break;
   5368  1.1  christos 
   5369  1.1  christos 	  /* If we are creating a shared library, and this is a reloc
   5370  1.1  christos 	     against a global symbol, or a non PC relative reloc
   5371  1.1  christos 	     against a local symbol, then we need to copy the reloc
   5372  1.1  christos 	     into the shared library.  However, if we are linking with
   5373  1.1  christos 	     -Bsymbolic, we do not need to copy a reloc against a
   5374  1.1  christos 	     global symbol which is defined in an object we are
   5375  1.1  christos 	     including in the link (i.e., DEF_REGULAR is set).  At
   5376  1.1  christos 	     this point we have not seen all the input files, so it is
   5377  1.1  christos 	     possible that DEF_REGULAR is not set now but will be set
   5378  1.1  christos 	     later (it is never cleared).  In case of a weak definition,
   5379  1.1  christos 	     DEF_REGULAR may be cleared later by a strong definition in
   5380  1.1  christos 	     a shared library.  We account for that possibility below by
   5381  1.1  christos 	     storing information in the dyn_relocs field of the hash
   5382  1.1  christos 	     table entry.  A similar situation occurs when creating
   5383  1.1  christos 	     shared libraries and symbol visibility changes render the
   5384  1.1  christos 	     symbol local.
   5385  1.1  christos 
   5386  1.1  christos 	     If on the other hand, we are creating an executable, we
   5387  1.1  christos 	     may need to keep relocations for symbols satisfied by a
   5388  1.1  christos 	     dynamic library if we manage to avoid copy relocs for the
   5389  1.1  christos 	     symbol.  */
   5390  1.1  christos 	dodyn:
   5391  1.1  christos 	  if ((info->shared
   5392  1.1  christos 	       && (must_be_dyn_reloc (info, r_type)
   5393  1.1  christos 		   || (h != NULL
   5394  1.1  christos 		       && (! info->symbolic
   5395  1.1  christos 			   || h->root.type == bfd_link_hash_defweak
   5396  1.1  christos 			   || !h->def_regular))))
   5397  1.1  christos 	      || (ELIMINATE_COPY_RELOCS
   5398  1.1  christos 		  && !info->shared
   5399  1.1  christos 		  && h != NULL
   5400  1.1  christos 		  && (h->root.type == bfd_link_hash_defweak
   5401  1.1  christos 		      || !h->def_regular))
   5402  1.1  christos 	      || (!info->shared
   5403  1.1  christos 		  && ifunc != NULL))
   5404  1.1  christos 	    {
   5405  1.1  christos 	      struct ppc_dyn_relocs *p;
   5406  1.1  christos 	      struct ppc_dyn_relocs **head;
   5407  1.1  christos 
   5408  1.1  christos 	      /* We must copy these reloc types into the output file.
   5409  1.1  christos 		 Create a reloc section in dynobj and make room for
   5410  1.1  christos 		 this reloc.  */
   5411  1.1  christos 	      if (sreloc == NULL)
   5412  1.1  christos 		{
   5413  1.1  christos 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5414  1.1  christos 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
   5415  1.1  christos 
   5416  1.1  christos 		  if (sreloc == NULL)
   5417  1.1  christos 		    return FALSE;
   5418  1.1  christos 		}
   5419  1.1  christos 
   5420  1.1  christos 	      /* If this is a global symbol, we count the number of
   5421  1.1  christos 		 relocations we need for this symbol.  */
   5422  1.1  christos 	      if (h != NULL)
   5423  1.1  christos 		{
   5424  1.1  christos 		  head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   5425  1.1  christos 		}
   5426  1.1  christos 	      else
   5427  1.1  christos 		{
   5428  1.1  christos 		  /* Track dynamic relocs needed for local syms too.
   5429  1.1  christos 		     We really need local syms available to do this
   5430  1.1  christos 		     easily.  Oh well.  */
   5431  1.1  christos 		  asection *s;
   5432  1.1  christos 		  void *vpp;
   5433  1.1  christos 		  Elf_Internal_Sym *isym;
   5434  1.1  christos 
   5435  1.1  christos 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5436  1.1  christos 						abfd, r_symndx);
   5437  1.1  christos 		  if (isym == NULL)
   5438  1.1  christos 		    return FALSE;
   5439  1.1  christos 
   5440  1.1  christos 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5441  1.1  christos 		  if (s == NULL)
   5442  1.1  christos 		    s = sec;
   5443  1.1  christos 
   5444  1.1  christos 		  vpp = &elf_section_data (s)->local_dynrel;
   5445  1.1  christos 		  head = (struct ppc_dyn_relocs **) vpp;
   5446  1.1  christos 		}
   5447  1.1  christos 
   5448  1.1  christos 	      p = *head;
   5449  1.1  christos 	      if (p == NULL || p->sec != sec)
   5450  1.1  christos 		{
   5451  1.1  christos 		  p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5452  1.1  christos 		  if (p == NULL)
   5453  1.1  christos 		    return FALSE;
   5454  1.1  christos 		  p->next = *head;
   5455  1.1  christos 		  *head = p;
   5456  1.1  christos 		  p->sec = sec;
   5457  1.1  christos 		  p->count = 0;
   5458  1.1  christos 		  p->pc_count = 0;
   5459  1.1  christos 		}
   5460  1.1  christos 
   5461  1.1  christos 	      p->count += 1;
   5462  1.1  christos 	      if (!must_be_dyn_reloc (info, r_type))
   5463  1.1  christos 		p->pc_count += 1;
   5464  1.1  christos 	    }
   5465  1.1  christos 	  break;
   5466  1.1  christos 
   5467  1.1  christos 	default:
   5468  1.1  christos 	  break;
   5469  1.1  christos 	}
   5470  1.1  christos     }
   5471  1.1  christos 
   5472  1.1  christos   return TRUE;
   5473  1.1  christos }
   5474  1.1  christos 
   5475  1.1  christos /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   5476  1.1  christos    of the code entry point, and its section.  */
   5477  1.1  christos 
   5478  1.1  christos static bfd_vma
   5479  1.1  christos opd_entry_value (asection *opd_sec,
   5480  1.1  christos 		 bfd_vma offset,
   5481  1.1  christos 		 asection **code_sec,
   5482  1.1  christos 		 bfd_vma *code_off)
   5483  1.1  christos {
   5484  1.1  christos   bfd *opd_bfd = opd_sec->owner;
   5485  1.1  christos   Elf_Internal_Rela *relocs;
   5486  1.1  christos   Elf_Internal_Rela *lo, *hi, *look;
   5487  1.1  christos   bfd_vma val;
   5488  1.1  christos 
   5489  1.1  christos   /* No relocs implies we are linking a --just-symbols object.  */
   5490  1.1  christos   if (opd_sec->reloc_count == 0)
   5491  1.1  christos     {
   5492  1.1  christos       char buf[8];
   5493  1.1  christos 
   5494  1.1  christos       if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
   5495  1.1  christos 	return (bfd_vma) -1;
   5496  1.1  christos 
   5497  1.1  christos       val = bfd_get_64 (opd_bfd, buf);
   5498  1.1  christos       if (code_sec != NULL)
   5499  1.1  christos 	{
   5500  1.1  christos 	  asection *sec, *likely = NULL;
   5501  1.1  christos 	  for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   5502  1.1  christos 	    if (sec->vma <= val
   5503  1.1  christos 		&& (sec->flags & SEC_LOAD) != 0
   5504  1.1  christos 		&& (sec->flags & SEC_ALLOC) != 0)
   5505  1.1  christos 	      likely = sec;
   5506  1.1  christos 	  if (likely != NULL)
   5507  1.1  christos 	    {
   5508  1.1  christos 	      *code_sec = likely;
   5509  1.1  christos 	      if (code_off != NULL)
   5510  1.1  christos 		*code_off = val - likely->vma;
   5511  1.1  christos 	    }
   5512  1.1  christos 	}
   5513  1.1  christos       return val;
   5514  1.1  christos     }
   5515  1.1  christos 
   5516  1.1  christos   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   5517  1.1  christos 
   5518  1.1  christos   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
   5519  1.1  christos   if (relocs == NULL)
   5520  1.1  christos     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
   5521  1.1  christos 
   5522  1.1  christos   /* Go find the opd reloc at the sym address.  */
   5523  1.1  christos   lo = relocs;
   5524  1.1  christos   BFD_ASSERT (lo != NULL);
   5525  1.1  christos   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   5526  1.1  christos   val = (bfd_vma) -1;
   5527  1.1  christos   while (lo < hi)
   5528  1.1  christos     {
   5529  1.1  christos       look = lo + (hi - lo) / 2;
   5530  1.1  christos       if (look->r_offset < offset)
   5531  1.1  christos 	lo = look + 1;
   5532  1.1  christos       else if (look->r_offset > offset)
   5533  1.1  christos 	hi = look;
   5534  1.1  christos       else
   5535  1.1  christos 	{
   5536  1.1  christos 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   5537  1.1  christos 
   5538  1.1  christos 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   5539  1.1  christos 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   5540  1.1  christos 	    {
   5541  1.1  christos 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   5542  1.1  christos 	      asection *sec;
   5543  1.1  christos 
   5544  1.1  christos 	      if (symndx < symtab_hdr->sh_info)
   5545  1.1  christos 		{
   5546  1.1  christos 		  Elf_Internal_Sym *sym;
   5547  1.1  christos 
   5548  1.1  christos 		  sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   5549  1.1  christos 		  if (sym == NULL)
   5550  1.1  christos 		    {
   5551  1.1  christos 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   5552  1.1  christos 						  symtab_hdr->sh_info,
   5553  1.1  christos 						  0, NULL, NULL, NULL);
   5554  1.1  christos 		      if (sym == NULL)
   5555  1.1  christos 			break;
   5556  1.1  christos 		      symtab_hdr->contents = (bfd_byte *) sym;
   5557  1.1  christos 		    }
   5558  1.1  christos 
   5559  1.1  christos 		  sym += symndx;
   5560  1.1  christos 		  val = sym->st_value;
   5561  1.1  christos 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   5562  1.1  christos 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   5563  1.1  christos 		}
   5564  1.1  christos 	      else
   5565  1.1  christos 		{
   5566  1.1  christos 		  struct elf_link_hash_entry **sym_hashes;
   5567  1.1  christos 		  struct elf_link_hash_entry *rh;
   5568  1.1  christos 
   5569  1.1  christos 		  sym_hashes = elf_sym_hashes (opd_bfd);
   5570  1.1  christos 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   5571  1.1  christos 		  rh = elf_follow_link (rh);
   5572  1.1  christos 		  BFD_ASSERT (rh->root.type == bfd_link_hash_defined
   5573  1.1  christos 			      || rh->root.type == bfd_link_hash_defweak);
   5574  1.1  christos 		  val = rh->root.u.def.value;
   5575  1.1  christos 		  sec = rh->root.u.def.section;
   5576  1.1  christos 		}
   5577  1.1  christos 	      val += look->r_addend;
   5578  1.1  christos 	      if (code_off != NULL)
   5579  1.1  christos 		*code_off = val;
   5580  1.1  christos 	      if (code_sec != NULL)
   5581  1.1  christos 		*code_sec = sec;
   5582  1.1  christos 	      if (sec != NULL && sec->output_section != NULL)
   5583  1.1  christos 		val += sec->output_section->vma + sec->output_offset;
   5584  1.1  christos 	    }
   5585  1.1  christos 	  break;
   5586  1.1  christos 	}
   5587  1.1  christos     }
   5588  1.1  christos 
   5589  1.1  christos   return val;
   5590  1.1  christos }
   5591  1.1  christos 
   5592  1.1  christos /* Return true if symbol is defined in a regular object file.  */
   5593  1.1  christos 
   5594  1.1  christos static bfd_boolean
   5595  1.1  christos is_static_defined (struct elf_link_hash_entry *h)
   5596  1.1  christos {
   5597  1.1  christos   return ((h->root.type == bfd_link_hash_defined
   5598  1.1  christos 	   || h->root.type == bfd_link_hash_defweak)
   5599  1.1  christos 	  && h->root.u.def.section != NULL
   5600  1.1  christos 	  && h->root.u.def.section->output_section != NULL);
   5601  1.1  christos }
   5602  1.1  christos 
   5603  1.1  christos /* If FDH is a function descriptor symbol, return the associated code
   5604  1.1  christos    entry symbol if it is defined.  Return NULL otherwise.  */
   5605  1.1  christos 
   5606  1.1  christos static struct ppc_link_hash_entry *
   5607  1.1  christos defined_code_entry (struct ppc_link_hash_entry *fdh)
   5608  1.1  christos {
   5609  1.1  christos   if (fdh->is_func_descriptor)
   5610  1.1  christos     {
   5611  1.1  christos       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   5612  1.1  christos       if (fh->elf.root.type == bfd_link_hash_defined
   5613  1.1  christos 	  || fh->elf.root.type == bfd_link_hash_defweak)
   5614  1.1  christos 	return fh;
   5615  1.1  christos     }
   5616  1.1  christos   return NULL;
   5617  1.1  christos }
   5618  1.1  christos 
   5619  1.1  christos /* If FH is a function code entry symbol, return the associated
   5620  1.1  christos    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   5621  1.1  christos 
   5622  1.1  christos static struct ppc_link_hash_entry *
   5623  1.1  christos defined_func_desc (struct ppc_link_hash_entry *fh)
   5624  1.1  christos {
   5625  1.1  christos   if (fh->oh != NULL
   5626  1.1  christos       && fh->oh->is_func_descriptor)
   5627  1.1  christos     {
   5628  1.1  christos       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   5629  1.1  christos       if (fdh->elf.root.type == bfd_link_hash_defined
   5630  1.1  christos 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   5631  1.1  christos 	return fdh;
   5632  1.1  christos     }
   5633  1.1  christos   return NULL;
   5634  1.1  christos }
   5635  1.1  christos 
   5636  1.1  christos /* Mark all our entry sym sections, both opd and code section.  */
   5637  1.1  christos 
   5638  1.1  christos static void
   5639  1.1  christos ppc64_elf_gc_keep (struct bfd_link_info *info)
   5640  1.1  christos {
   5641  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5642  1.1  christos   struct bfd_sym_chain *sym;
   5643  1.1  christos 
   5644  1.1  christos   if (htab == NULL)
   5645  1.1  christos     return;
   5646  1.1  christos 
   5647  1.1  christos   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   5648  1.1  christos     {
   5649  1.1  christos       struct ppc_link_hash_entry *eh, *fh;
   5650  1.1  christos       asection *sec;
   5651  1.1  christos 
   5652  1.1  christos       eh = (struct ppc_link_hash_entry *)
   5653  1.1  christos 	elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
   5654  1.1  christos       if (eh == NULL)
   5655  1.1  christos 	continue;
   5656  1.1  christos       if (eh->elf.root.type != bfd_link_hash_defined
   5657  1.1  christos 	  && eh->elf.root.type != bfd_link_hash_defweak)
   5658  1.1  christos 	continue;
   5659  1.1  christos 
   5660  1.1  christos       fh = defined_code_entry (eh);
   5661  1.1  christos       if (fh != NULL)
   5662  1.1  christos 	{
   5663  1.1  christos 	  sec = fh->elf.root.u.def.section;
   5664  1.1  christos 	  sec->flags |= SEC_KEEP;
   5665  1.1  christos 	}
   5666  1.1  christos       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5667  1.1  christos 	       && opd_entry_value (eh->elf.root.u.def.section,
   5668  1.1  christos 				   eh->elf.root.u.def.value,
   5669  1.1  christos 				   &sec, NULL) != (bfd_vma) -1)
   5670  1.1  christos 	sec->flags |= SEC_KEEP;
   5671  1.1  christos 
   5672  1.1  christos       sec = eh->elf.root.u.def.section;
   5673  1.1  christos       sec->flags |= SEC_KEEP;
   5674  1.1  christos     }
   5675  1.1  christos }
   5676  1.1  christos 
   5677  1.1  christos /* Mark sections containing dynamically referenced symbols.  When
   5678  1.1  christos    building shared libraries, we must assume that any visible symbol is
   5679  1.1  christos    referenced.  */
   5680  1.1  christos 
   5681  1.1  christos static bfd_boolean
   5682  1.1  christos ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   5683  1.1  christos {
   5684  1.1  christos   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   5685  1.1  christos   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   5686  1.1  christos   struct ppc_link_hash_entry *fdh;
   5687  1.1  christos 
   5688  1.1  christos   if (eh->elf.root.type == bfd_link_hash_warning)
   5689  1.1  christos     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   5690  1.1  christos 
   5691  1.1  christos   /* Dynamic linking info is on the func descriptor sym.  */
   5692  1.1  christos   fdh = defined_func_desc (eh);
   5693  1.1  christos   if (fdh != NULL)
   5694  1.1  christos     eh = fdh;
   5695  1.1  christos 
   5696  1.1  christos   if ((eh->elf.root.type == bfd_link_hash_defined
   5697  1.1  christos        || eh->elf.root.type == bfd_link_hash_defweak)
   5698  1.1  christos       && (eh->elf.ref_dynamic
   5699  1.1  christos 	  || (!info->executable
   5700  1.1  christos 	      && eh->elf.def_regular
   5701  1.1  christos 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   5702  1.1  christos 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
   5703  1.1  christos     {
   5704  1.1  christos       asection *code_sec;
   5705  1.1  christos       struct ppc_link_hash_entry *fh;
   5706  1.1  christos 
   5707  1.1  christos       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   5708  1.1  christos 
   5709  1.1  christos       /* Function descriptor syms cause the associated
   5710  1.1  christos 	 function code sym section to be marked.  */
   5711  1.1  christos       fh = defined_code_entry (eh);
   5712  1.1  christos       if (fh != NULL)
   5713  1.1  christos 	{
   5714  1.1  christos 	  code_sec = fh->elf.root.u.def.section;
   5715  1.1  christos 	  code_sec->flags |= SEC_KEEP;
   5716  1.1  christos 	}
   5717  1.1  christos       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5718  1.1  christos 	       && opd_entry_value (eh->elf.root.u.def.section,
   5719  1.1  christos 				   eh->elf.root.u.def.value,
   5720  1.1  christos 				   &code_sec, NULL) != (bfd_vma) -1)
   5721  1.1  christos 	code_sec->flags |= SEC_KEEP;
   5722  1.1  christos     }
   5723  1.1  christos 
   5724  1.1  christos   return TRUE;
   5725  1.1  christos }
   5726  1.1  christos 
   5727  1.1  christos /* Return the section that should be marked against GC for a given
   5728  1.1  christos    relocation.  */
   5729  1.1  christos 
   5730  1.1  christos static asection *
   5731  1.1  christos ppc64_elf_gc_mark_hook (asection *sec,
   5732  1.1  christos 			struct bfd_link_info *info,
   5733  1.1  christos 			Elf_Internal_Rela *rel,
   5734  1.1  christos 			struct elf_link_hash_entry *h,
   5735  1.1  christos 			Elf_Internal_Sym *sym)
   5736  1.1  christos {
   5737  1.1  christos   asection *rsec;
   5738  1.1  christos 
   5739  1.1  christos   /* Syms return NULL if we're marking .opd, so we avoid marking all
   5740  1.1  christos      function sections, as all functions are referenced in .opd.  */
   5741  1.1  christos   rsec = NULL;
   5742  1.1  christos   if (get_opd_info (sec) != NULL)
   5743  1.1  christos     return rsec;
   5744  1.1  christos 
   5745  1.1  christos   if (h != NULL)
   5746  1.1  christos     {
   5747  1.1  christos       enum elf_ppc64_reloc_type r_type;
   5748  1.1  christos       struct ppc_link_hash_entry *eh, *fh, *fdh;
   5749  1.1  christos 
   5750  1.1  christos       r_type = ELF64_R_TYPE (rel->r_info);
   5751  1.1  christos       switch (r_type)
   5752  1.1  christos 	{
   5753  1.1  christos 	case R_PPC64_GNU_VTINHERIT:
   5754  1.1  christos 	case R_PPC64_GNU_VTENTRY:
   5755  1.1  christos 	  break;
   5756  1.1  christos 
   5757  1.1  christos 	default:
   5758  1.1  christos 	  switch (h->root.type)
   5759  1.1  christos 	    {
   5760  1.1  christos 	    case bfd_link_hash_defined:
   5761  1.1  christos 	    case bfd_link_hash_defweak:
   5762  1.1  christos 	      eh = (struct ppc_link_hash_entry *) h;
   5763  1.1  christos 	      fdh = defined_func_desc (eh);
   5764  1.1  christos 	      if (fdh != NULL)
   5765  1.1  christos 		eh = fdh;
   5766  1.1  christos 
   5767  1.1  christos 	      /* Function descriptor syms cause the associated
   5768  1.1  christos 		 function code sym section to be marked.  */
   5769  1.1  christos 	      fh = defined_code_entry (eh);
   5770  1.1  christos 	      if (fh != NULL)
   5771  1.1  christos 		{
   5772  1.1  christos 		  /* They also mark their opd section.  */
   5773  1.1  christos 		  eh->elf.root.u.def.section->gc_mark = 1;
   5774  1.1  christos 
   5775  1.1  christos 		  rsec = fh->elf.root.u.def.section;
   5776  1.1  christos 		}
   5777  1.1  christos 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5778  1.1  christos 		       && opd_entry_value (eh->elf.root.u.def.section,
   5779  1.1  christos 					   eh->elf.root.u.def.value,
   5780  1.1  christos 					   &rsec, NULL) != (bfd_vma) -1)
   5781  1.1  christos 		eh->elf.root.u.def.section->gc_mark = 1;
   5782  1.1  christos 	      else
   5783  1.1  christos 		rsec = h->root.u.def.section;
   5784  1.1  christos 	      break;
   5785  1.1  christos 
   5786  1.1  christos 	    case bfd_link_hash_common:
   5787  1.1  christos 	      rsec = h->root.u.c.p->section;
   5788  1.1  christos 	      break;
   5789  1.1  christos 
   5790  1.1  christos 	    default:
   5791  1.1  christos 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   5792  1.1  christos 	    }
   5793  1.1  christos 	}
   5794  1.1  christos     }
   5795  1.1  christos   else
   5796  1.1  christos     {
   5797  1.1  christos       struct _opd_sec_data *opd;
   5798  1.1  christos 
   5799  1.1  christos       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   5800  1.1  christos       opd = get_opd_info (rsec);
   5801  1.1  christos       if (opd != NULL && opd->func_sec != NULL)
   5802  1.1  christos 	{
   5803  1.1  christos 	  rsec->gc_mark = 1;
   5804  1.1  christos 
   5805  1.1  christos 	  rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
   5806  1.1  christos 	}
   5807  1.1  christos     }
   5808  1.1  christos 
   5809  1.1  christos   return rsec;
   5810  1.1  christos }
   5811  1.1  christos 
   5812  1.1  christos /* Update the .got, .plt. and dynamic reloc reference counts for the
   5813  1.1  christos    section being removed.  */
   5814  1.1  christos 
   5815  1.1  christos static bfd_boolean
   5816  1.1  christos ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   5817  1.1  christos 			 asection *sec, const Elf_Internal_Rela *relocs)
   5818  1.1  christos {
   5819  1.1  christos   struct ppc_link_hash_table *htab;
   5820  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   5821  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   5822  1.1  christos   struct got_entry **local_got_ents;
   5823  1.1  christos   const Elf_Internal_Rela *rel, *relend;
   5824  1.1  christos 
   5825  1.1  christos   if (info->relocatable)
   5826  1.1  christos     return TRUE;
   5827  1.1  christos 
   5828  1.1  christos   if ((sec->flags & SEC_ALLOC) == 0)
   5829  1.1  christos     return TRUE;
   5830  1.1  christos 
   5831  1.1  christos   elf_section_data (sec)->local_dynrel = NULL;
   5832  1.1  christos 
   5833  1.1  christos   htab = ppc_hash_table (info);
   5834  1.1  christos   if (htab == NULL)
   5835  1.1  christos     return FALSE;
   5836  1.1  christos 
   5837  1.1  christos   symtab_hdr = &elf_symtab_hdr (abfd);
   5838  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   5839  1.1  christos   local_got_ents = elf_local_got_ents (abfd);
   5840  1.1  christos 
   5841  1.1  christos   relend = relocs + sec->reloc_count;
   5842  1.1  christos   for (rel = relocs; rel < relend; rel++)
   5843  1.1  christos     {
   5844  1.1  christos       unsigned long r_symndx;
   5845  1.1  christos       enum elf_ppc64_reloc_type r_type;
   5846  1.1  christos       struct elf_link_hash_entry *h = NULL;
   5847  1.1  christos       unsigned char tls_type = 0;
   5848  1.1  christos 
   5849  1.1  christos       r_symndx = ELF64_R_SYM (rel->r_info);
   5850  1.1  christos       r_type = ELF64_R_TYPE (rel->r_info);
   5851  1.1  christos       if (r_symndx >= symtab_hdr->sh_info)
   5852  1.1  christos 	{
   5853  1.1  christos 	  struct ppc_link_hash_entry *eh;
   5854  1.1  christos 	  struct ppc_dyn_relocs **pp;
   5855  1.1  christos 	  struct ppc_dyn_relocs *p;
   5856  1.1  christos 
   5857  1.1  christos 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5858  1.1  christos 	  h = elf_follow_link (h);
   5859  1.1  christos 	  eh = (struct ppc_link_hash_entry *) h;
   5860  1.1  christos 
   5861  1.1  christos 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   5862  1.1  christos 	    if (p->sec == sec)
   5863  1.1  christos 	      {
   5864  1.1  christos 		/* Everything must go for SEC.  */
   5865  1.1  christos 		*pp = p->next;
   5866  1.1  christos 		break;
   5867  1.1  christos 	      }
   5868  1.1  christos 	}
   5869  1.1  christos 
   5870  1.1  christos       if (is_branch_reloc (r_type))
   5871  1.1  christos 	{
   5872  1.1  christos 	  struct plt_entry **ifunc = NULL;
   5873  1.1  christos 	  if (h != NULL)
   5874  1.1  christos 	    {
   5875  1.1  christos 	      if (h->type == STT_GNU_IFUNC)
   5876  1.1  christos 		ifunc = &h->plt.plist;
   5877  1.1  christos 	    }
   5878  1.1  christos 	  else if (local_got_ents != NULL)
   5879  1.1  christos 	    {
   5880  1.1  christos 	      struct plt_entry **local_plt = (struct plt_entry **)
   5881  1.1  christos 		(local_got_ents + symtab_hdr->sh_info);
   5882  1.1  christos 	      unsigned char *local_got_tls_masks = (unsigned char *)
   5883  1.1  christos 		(local_plt + symtab_hdr->sh_info);
   5884  1.1  christos 	      if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
   5885  1.1  christos 		ifunc = local_plt + r_symndx;
   5886  1.1  christos 	    }
   5887  1.1  christos 	  if (ifunc != NULL)
   5888  1.1  christos 	    {
   5889  1.1  christos 	      struct plt_entry *ent;
   5890  1.1  christos 
   5891  1.1  christos 	      for (ent = *ifunc; ent != NULL; ent = ent->next)
   5892  1.1  christos 		if (ent->addend == rel->r_addend)
   5893  1.1  christos 		  break;
   5894  1.1  christos 	      if (ent == NULL)
   5895  1.1  christos 		abort ();
   5896  1.1  christos 	      if (ent->plt.refcount > 0)
   5897  1.1  christos 		ent->plt.refcount -= 1;
   5898  1.1  christos 	      continue;
   5899  1.1  christos 	    }
   5900  1.1  christos 	}
   5901  1.1  christos 
   5902  1.1  christos       switch (r_type)
   5903  1.1  christos 	{
   5904  1.1  christos 	case R_PPC64_GOT_TLSLD16:
   5905  1.1  christos 	case R_PPC64_GOT_TLSLD16_LO:
   5906  1.1  christos 	case R_PPC64_GOT_TLSLD16_HI:
   5907  1.1  christos 	case R_PPC64_GOT_TLSLD16_HA:
   5908  1.1  christos 	  tls_type = TLS_TLS | TLS_LD;
   5909  1.1  christos 	  goto dogot;
   5910  1.1  christos 
   5911  1.1  christos 	case R_PPC64_GOT_TLSGD16:
   5912  1.1  christos 	case R_PPC64_GOT_TLSGD16_LO:
   5913  1.1  christos 	case R_PPC64_GOT_TLSGD16_HI:
   5914  1.1  christos 	case R_PPC64_GOT_TLSGD16_HA:
   5915  1.1  christos 	  tls_type = TLS_TLS | TLS_GD;
   5916  1.1  christos 	  goto dogot;
   5917  1.1  christos 
   5918  1.1  christos 	case R_PPC64_GOT_TPREL16_DS:
   5919  1.1  christos 	case R_PPC64_GOT_TPREL16_LO_DS:
   5920  1.1  christos 	case R_PPC64_GOT_TPREL16_HI:
   5921  1.1  christos 	case R_PPC64_GOT_TPREL16_HA:
   5922  1.1  christos 	  tls_type = TLS_TLS | TLS_TPREL;
   5923  1.1  christos 	  goto dogot;
   5924  1.1  christos 
   5925  1.1  christos 	case R_PPC64_GOT_DTPREL16_DS:
   5926  1.1  christos 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5927  1.1  christos 	case R_PPC64_GOT_DTPREL16_HI:
   5928  1.1  christos 	case R_PPC64_GOT_DTPREL16_HA:
   5929  1.1  christos 	  tls_type = TLS_TLS | TLS_DTPREL;
   5930  1.1  christos 	  goto dogot;
   5931  1.1  christos 
   5932  1.1  christos 	case R_PPC64_GOT16:
   5933  1.1  christos 	case R_PPC64_GOT16_DS:
   5934  1.1  christos 	case R_PPC64_GOT16_HA:
   5935  1.1  christos 	case R_PPC64_GOT16_HI:
   5936  1.1  christos 	case R_PPC64_GOT16_LO:
   5937  1.1  christos 	case R_PPC64_GOT16_LO_DS:
   5938  1.1  christos 	dogot:
   5939  1.1  christos 	  {
   5940  1.1  christos 	    struct got_entry *ent;
   5941  1.1  christos 
   5942  1.1  christos 	    if (h != NULL)
   5943  1.1  christos 	      ent = h->got.glist;
   5944  1.1  christos 	    else
   5945  1.1  christos 	      ent = local_got_ents[r_symndx];
   5946  1.1  christos 
   5947  1.1  christos 	    for (; ent != NULL; ent = ent->next)
   5948  1.1  christos 	      if (ent->addend == rel->r_addend
   5949  1.1  christos 		  && ent->owner == abfd
   5950  1.1  christos 		  && ent->tls_type == tls_type)
   5951  1.1  christos 		break;
   5952  1.1  christos 	    if (ent == NULL)
   5953  1.1  christos 	      abort ();
   5954  1.1  christos 	    if (ent->got.refcount > 0)
   5955  1.1  christos 	      ent->got.refcount -= 1;
   5956  1.1  christos 	  }
   5957  1.1  christos 	  break;
   5958  1.1  christos 
   5959  1.1  christos 	case R_PPC64_PLT16_HA:
   5960  1.1  christos 	case R_PPC64_PLT16_HI:
   5961  1.1  christos 	case R_PPC64_PLT16_LO:
   5962  1.1  christos 	case R_PPC64_PLT32:
   5963  1.1  christos 	case R_PPC64_PLT64:
   5964  1.1  christos 	case R_PPC64_REL14:
   5965  1.1  christos 	case R_PPC64_REL14_BRNTAKEN:
   5966  1.1  christos 	case R_PPC64_REL14_BRTAKEN:
   5967  1.1  christos 	case R_PPC64_REL24:
   5968  1.1  christos 	  if (h != NULL)
   5969  1.1  christos 	    {
   5970  1.1  christos 	      struct plt_entry *ent;
   5971  1.1  christos 
   5972  1.1  christos 	      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   5973  1.1  christos 		if (ent->addend == rel->r_addend)
   5974  1.1  christos 		  break;
   5975  1.1  christos 	      if (ent != NULL && ent->plt.refcount > 0)
   5976  1.1  christos 		ent->plt.refcount -= 1;
   5977  1.1  christos 	    }
   5978  1.1  christos 	  break;
   5979  1.1  christos 
   5980  1.1  christos 	default:
   5981  1.1  christos 	  break;
   5982  1.1  christos 	}
   5983  1.1  christos     }
   5984  1.1  christos   return TRUE;
   5985  1.1  christos }
   5986  1.1  christos 
   5987  1.1  christos /* The maximum size of .sfpr.  */
   5988  1.1  christos #define SFPR_MAX (218*4)
   5989  1.1  christos 
   5990  1.1  christos struct sfpr_def_parms
   5991  1.1  christos {
   5992  1.1  christos   const char name[12];
   5993  1.1  christos   unsigned char lo, hi;
   5994  1.1  christos   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
   5995  1.1  christos   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
   5996  1.1  christos };
   5997  1.1  christos 
   5998  1.1  christos /* Auto-generate _save*, _rest* functions in .sfpr.  */
   5999  1.1  christos 
   6000  1.1  christos static bfd_boolean
   6001  1.1  christos sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
   6002  1.1  christos {
   6003  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6004  1.1  christos   unsigned int i;
   6005  1.1  christos   size_t len = strlen (parm->name);
   6006  1.1  christos   bfd_boolean writing = FALSE;
   6007  1.1  christos   char sym[16];
   6008  1.1  christos 
   6009  1.1  christos   if (htab == NULL)
   6010  1.1  christos     return FALSE;
   6011  1.1  christos 
   6012  1.1  christos   memcpy (sym, parm->name, len);
   6013  1.1  christos   sym[len + 2] = 0;
   6014  1.1  christos 
   6015  1.1  christos   for (i = parm->lo; i <= parm->hi; i++)
   6016  1.1  christos     {
   6017  1.1  christos       struct elf_link_hash_entry *h;
   6018  1.1  christos 
   6019  1.1  christos       sym[len + 0] = i / 10 + '0';
   6020  1.1  christos       sym[len + 1] = i % 10 + '0';
   6021  1.1  christos       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
   6022  1.1  christos       if (h != NULL
   6023  1.1  christos 	  && !h->def_regular)
   6024  1.1  christos 	{
   6025  1.1  christos 	  h->root.type = bfd_link_hash_defined;
   6026  1.1  christos 	  h->root.u.def.section = htab->sfpr;
   6027  1.1  christos 	  h->root.u.def.value = htab->sfpr->size;
   6028  1.1  christos 	  h->type = STT_FUNC;
   6029  1.1  christos 	  h->def_regular = 1;
   6030  1.1  christos 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   6031  1.1  christos 	  writing = TRUE;
   6032  1.1  christos 	  if (htab->sfpr->contents == NULL)
   6033  1.1  christos 	    {
   6034  1.1  christos 	      htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   6035  1.1  christos 	      if (htab->sfpr->contents == NULL)
   6036  1.1  christos 		return FALSE;
   6037  1.1  christos 	    }
   6038  1.1  christos 	}
   6039  1.1  christos       if (writing)
   6040  1.1  christos 	{
   6041  1.1  christos 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   6042  1.1  christos 	  if (i != parm->hi)
   6043  1.1  christos 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   6044  1.1  christos 	  else
   6045  1.1  christos 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   6046  1.1  christos 	  htab->sfpr->size = p - htab->sfpr->contents;
   6047  1.1  christos 	}
   6048  1.1  christos     }
   6049  1.1  christos 
   6050  1.1  christos   return TRUE;
   6051  1.1  christos }
   6052  1.1  christos 
   6053  1.1  christos static bfd_byte *
   6054  1.1  christos savegpr0 (bfd *abfd, bfd_byte *p, int r)
   6055  1.1  christos {
   6056  1.1  christos   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6057  1.1  christos   return p + 4;
   6058  1.1  christos }
   6059  1.1  christos 
   6060  1.1  christos static bfd_byte *
   6061  1.1  christos savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6062  1.1  christos {
   6063  1.1  christos   p = savegpr0 (abfd, p, r);
   6064  1.1  christos   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6065  1.1  christos   p = p + 4;
   6066  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6067  1.1  christos   return p + 4;
   6068  1.1  christos }
   6069  1.1  christos 
   6070  1.1  christos static bfd_byte *
   6071  1.1  christos restgpr0 (bfd *abfd, bfd_byte *p, int r)
   6072  1.1  christos {
   6073  1.1  christos   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6074  1.1  christos   return p + 4;
   6075  1.1  christos }
   6076  1.1  christos 
   6077  1.1  christos static bfd_byte *
   6078  1.1  christos restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6079  1.1  christos {
   6080  1.1  christos   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6081  1.1  christos   p = p + 4;
   6082  1.1  christos   p = restgpr0 (abfd, p, r);
   6083  1.1  christos   bfd_put_32 (abfd, MTLR_R0, p);
   6084  1.1  christos   p = p + 4;
   6085  1.1  christos   if (r == 29)
   6086  1.1  christos     {
   6087  1.1  christos       p = restgpr0 (abfd, p, 30);
   6088  1.1  christos       p = restgpr0 (abfd, p, 31);
   6089  1.1  christos     }
   6090  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6091  1.1  christos   return p + 4;
   6092  1.1  christos }
   6093  1.1  christos 
   6094  1.1  christos static bfd_byte *
   6095  1.1  christos savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6096  1.1  christos {
   6097  1.1  christos   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6098  1.1  christos   return p + 4;
   6099  1.1  christos }
   6100  1.1  christos 
   6101  1.1  christos static bfd_byte *
   6102  1.1  christos savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6103  1.1  christos {
   6104  1.1  christos   p = savegpr1 (abfd, p, r);
   6105  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6106  1.1  christos   return p + 4;
   6107  1.1  christos }
   6108  1.1  christos 
   6109  1.1  christos static bfd_byte *
   6110  1.1  christos restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6111  1.1  christos {
   6112  1.1  christos   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6113  1.1  christos   return p + 4;
   6114  1.1  christos }
   6115  1.1  christos 
   6116  1.1  christos static bfd_byte *
   6117  1.1  christos restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6118  1.1  christos {
   6119  1.1  christos   p = restgpr1 (abfd, p, r);
   6120  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6121  1.1  christos   return p + 4;
   6122  1.1  christos }
   6123  1.1  christos 
   6124  1.1  christos static bfd_byte *
   6125  1.1  christos savefpr (bfd *abfd, bfd_byte *p, int r)
   6126  1.1  christos {
   6127  1.1  christos   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6128  1.1  christos   return p + 4;
   6129  1.1  christos }
   6130  1.1  christos 
   6131  1.1  christos static bfd_byte *
   6132  1.1  christos savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6133  1.1  christos {
   6134  1.1  christos   p = savefpr (abfd, p, r);
   6135  1.1  christos   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6136  1.1  christos   p = p + 4;
   6137  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6138  1.1  christos   return p + 4;
   6139  1.1  christos }
   6140  1.1  christos 
   6141  1.1  christos static bfd_byte *
   6142  1.1  christos restfpr (bfd *abfd, bfd_byte *p, int r)
   6143  1.1  christos {
   6144  1.1  christos   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6145  1.1  christos   return p + 4;
   6146  1.1  christos }
   6147  1.1  christos 
   6148  1.1  christos static bfd_byte *
   6149  1.1  christos restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6150  1.1  christos {
   6151  1.1  christos   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6152  1.1  christos   p = p + 4;
   6153  1.1  christos   p = restfpr (abfd, p, r);
   6154  1.1  christos   bfd_put_32 (abfd, MTLR_R0, p);
   6155  1.1  christos   p = p + 4;
   6156  1.1  christos   if (r == 29)
   6157  1.1  christos     {
   6158  1.1  christos       p = restfpr (abfd, p, 30);
   6159  1.1  christos       p = restfpr (abfd, p, 31);
   6160  1.1  christos     }
   6161  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6162  1.1  christos   return p + 4;
   6163  1.1  christos }
   6164  1.1  christos 
   6165  1.1  christos static bfd_byte *
   6166  1.1  christos savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6167  1.1  christos {
   6168  1.1  christos   p = savefpr (abfd, p, r);
   6169  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6170  1.1  christos   return p + 4;
   6171  1.1  christos }
   6172  1.1  christos 
   6173  1.1  christos static bfd_byte *
   6174  1.1  christos restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6175  1.1  christos {
   6176  1.1  christos   p = restfpr (abfd, p, r);
   6177  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6178  1.1  christos   return p + 4;
   6179  1.1  christos }
   6180  1.1  christos 
   6181  1.1  christos static bfd_byte *
   6182  1.1  christos savevr (bfd *abfd, bfd_byte *p, int r)
   6183  1.1  christos {
   6184  1.1  christos   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6185  1.1  christos   p = p + 4;
   6186  1.1  christos   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6187  1.1  christos   return p + 4;
   6188  1.1  christos }
   6189  1.1  christos 
   6190  1.1  christos static bfd_byte *
   6191  1.1  christos savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6192  1.1  christos {
   6193  1.1  christos   p = savevr (abfd, p, r);
   6194  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6195  1.1  christos   return p + 4;
   6196  1.1  christos }
   6197  1.1  christos 
   6198  1.1  christos static bfd_byte *
   6199  1.1  christos restvr (bfd *abfd, bfd_byte *p, int r)
   6200  1.1  christos {
   6201  1.1  christos   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6202  1.1  christos   p = p + 4;
   6203  1.1  christos   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6204  1.1  christos   return p + 4;
   6205  1.1  christos }
   6206  1.1  christos 
   6207  1.1  christos static bfd_byte *
   6208  1.1  christos restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6209  1.1  christos {
   6210  1.1  christos   p = restvr (abfd, p, r);
   6211  1.1  christos   bfd_put_32 (abfd, BLR, p);
   6212  1.1  christos   return p + 4;
   6213  1.1  christos }
   6214  1.1  christos 
   6215  1.1  christos /* Called via elf_link_hash_traverse to transfer dynamic linking
   6216  1.1  christos    information on function code symbol entries to their corresponding
   6217  1.1  christos    function descriptor symbol entries.  */
   6218  1.1  christos 
   6219  1.1  christos static bfd_boolean
   6220  1.1  christos func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6221  1.1  christos {
   6222  1.1  christos   struct bfd_link_info *info;
   6223  1.1  christos   struct ppc_link_hash_table *htab;
   6224  1.1  christos   struct plt_entry *ent;
   6225  1.1  christos   struct ppc_link_hash_entry *fh;
   6226  1.1  christos   struct ppc_link_hash_entry *fdh;
   6227  1.1  christos   bfd_boolean force_local;
   6228  1.1  christos 
   6229  1.1  christos   fh = (struct ppc_link_hash_entry *) h;
   6230  1.1  christos   if (fh->elf.root.type == bfd_link_hash_indirect)
   6231  1.1  christos     return TRUE;
   6232  1.1  christos 
   6233  1.1  christos   if (fh->elf.root.type == bfd_link_hash_warning)
   6234  1.1  christos     fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
   6235  1.1  christos 
   6236  1.1  christos   info = inf;
   6237  1.1  christos   htab = ppc_hash_table (info);
   6238  1.1  christos   if (htab == NULL)
   6239  1.1  christos     return FALSE;
   6240  1.1  christos 
   6241  1.1  christos   /* Resolve undefined references to dot-symbols as the value
   6242  1.1  christos      in the function descriptor, if we have one in a regular object.
   6243  1.1  christos      This is to satisfy cases like ".quad .foo".  Calls to functions
   6244  1.1  christos      in dynamic objects are handled elsewhere.  */
   6245  1.1  christos   if (fh->elf.root.type == bfd_link_hash_undefweak
   6246  1.1  christos       && fh->was_undefined
   6247  1.1  christos       && (fdh = defined_func_desc (fh)) != NULL
   6248  1.1  christos       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6249  1.1  christos       && opd_entry_value (fdh->elf.root.u.def.section,
   6250  1.1  christos 			  fdh->elf.root.u.def.value,
   6251  1.1  christos 			  &fh->elf.root.u.def.section,
   6252  1.1  christos 			  &fh->elf.root.u.def.value) != (bfd_vma) -1)
   6253  1.1  christos     {
   6254  1.1  christos       fh->elf.root.type = fdh->elf.root.type;
   6255  1.1  christos       fh->elf.forced_local = 1;
   6256  1.1  christos       fh->elf.def_regular = fdh->elf.def_regular;
   6257  1.1  christos       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6258  1.1  christos     }
   6259  1.1  christos 
   6260  1.1  christos   /* If this is a function code symbol, transfer dynamic linking
   6261  1.1  christos      information to the function descriptor symbol.  */
   6262  1.1  christos   if (!fh->is_func)
   6263  1.1  christos     return TRUE;
   6264  1.1  christos 
   6265  1.1  christos   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6266  1.1  christos     if (ent->plt.refcount > 0)
   6267  1.1  christos       break;
   6268  1.1  christos   if (ent == NULL
   6269  1.1  christos       || fh->elf.root.root.string[0] != '.'
   6270  1.1  christos       || fh->elf.root.root.string[1] == '\0')
   6271  1.1  christos     return TRUE;
   6272  1.1  christos 
   6273  1.1  christos   /* Find the corresponding function descriptor symbol.  Create it
   6274  1.1  christos      as undefined if necessary.  */
   6275  1.1  christos 
   6276  1.1  christos   fdh = lookup_fdh (fh, htab);
   6277  1.1  christos   if (fdh == NULL
   6278  1.1  christos       && !info->executable
   6279  1.1  christos       && (fh->elf.root.type == bfd_link_hash_undefined
   6280  1.1  christos 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6281  1.1  christos     {
   6282  1.1  christos       fdh = make_fdh (info, fh);
   6283  1.1  christos       if (fdh == NULL)
   6284  1.1  christos 	return FALSE;
   6285  1.1  christos     }
   6286  1.1  christos 
   6287  1.1  christos   /* Fake function descriptors are made undefweak.  If the function
   6288  1.1  christos      code symbol is strong undefined, make the fake sym the same.
   6289  1.1  christos      If the function code symbol is defined, then force the fake
   6290  1.1  christos      descriptor local;  We can't support overriding of symbols in a
   6291  1.1  christos      shared library on a fake descriptor.  */
   6292  1.1  christos 
   6293  1.1  christos   if (fdh != NULL
   6294  1.1  christos       && fdh->fake
   6295  1.1  christos       && fdh->elf.root.type == bfd_link_hash_undefweak)
   6296  1.1  christos     {
   6297  1.1  christos       if (fh->elf.root.type == bfd_link_hash_undefined)
   6298  1.1  christos 	{
   6299  1.1  christos 	  fdh->elf.root.type = bfd_link_hash_undefined;
   6300  1.1  christos 	  bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
   6301  1.1  christos 	}
   6302  1.1  christos       else if (fh->elf.root.type == bfd_link_hash_defined
   6303  1.1  christos 	       || fh->elf.root.type == bfd_link_hash_defweak)
   6304  1.1  christos 	{
   6305  1.1  christos 	  _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
   6306  1.1  christos 	}
   6307  1.1  christos     }
   6308  1.1  christos 
   6309  1.1  christos   if (fdh != NULL
   6310  1.1  christos       && !fdh->elf.forced_local
   6311  1.1  christos       && (!info->executable
   6312  1.1  christos 	  || fdh->elf.def_dynamic
   6313  1.1  christos 	  || fdh->elf.ref_dynamic
   6314  1.1  christos 	  || (fdh->elf.root.type == bfd_link_hash_undefweak
   6315  1.1  christos 	      && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
   6316  1.1  christos     {
   6317  1.1  christos       if (fdh->elf.dynindx == -1)
   6318  1.1  christos 	if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   6319  1.1  christos 	  return FALSE;
   6320  1.1  christos       fdh->elf.ref_regular |= fh->elf.ref_regular;
   6321  1.1  christos       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   6322  1.1  christos       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   6323  1.1  christos       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   6324  1.1  christos       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
   6325  1.1  christos 	{
   6326  1.1  christos 	  move_plt_plist (fh, fdh);
   6327  1.1  christos 	  fdh->elf.needs_plt = 1;
   6328  1.1  christos 	}
   6329  1.1  christos       fdh->is_func_descriptor = 1;
   6330  1.1  christos       fdh->oh = fh;
   6331  1.1  christos       fh->oh = fdh;
   6332  1.1  christos     }
   6333  1.1  christos 
   6334  1.1  christos   /* Now that the info is on the function descriptor, clear the
   6335  1.1  christos      function code sym info.  Any function code syms for which we
   6336  1.1  christos      don't have a definition in a regular file, we force local.
   6337  1.1  christos      This prevents a shared library from exporting syms that have
   6338  1.1  christos      been imported from another library.  Function code syms that
   6339  1.1  christos      are really in the library we must leave global to prevent the
   6340  1.1  christos      linker dragging in a definition from a static library.  */
   6341  1.1  christos   force_local = (!fh->elf.def_regular
   6342  1.1  christos 		 || fdh == NULL
   6343  1.1  christos 		 || !fdh->elf.def_regular
   6344  1.1  christos 		 || fdh->elf.forced_local);
   6345  1.1  christos   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6346  1.1  christos 
   6347  1.1  christos   return TRUE;
   6348  1.1  christos }
   6349  1.1  christos 
   6350  1.1  christos /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   6351  1.1  christos    this hook to a) provide some gcc support functions, and b) transfer
   6352  1.1  christos    dynamic linking information gathered so far on function code symbol
   6353  1.1  christos    entries, to their corresponding function descriptor symbol entries.  */
   6354  1.1  christos 
   6355  1.1  christos static bfd_boolean
   6356  1.1  christos ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
   6357  1.1  christos 			    struct bfd_link_info *info)
   6358  1.1  christos {
   6359  1.1  christos   struct ppc_link_hash_table *htab;
   6360  1.1  christos   unsigned int i;
   6361  1.1  christos   const struct sfpr_def_parms funcs[] =
   6362  1.1  christos     {
   6363  1.1  christos       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   6364  1.1  christos       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   6365  1.1  christos       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   6366  1.1  christos       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   6367  1.1  christos       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   6368  1.1  christos       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   6369  1.1  christos       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   6370  1.1  christos       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   6371  1.1  christos       { "._savef", 14, 31, savefpr, savefpr1_tail },
   6372  1.1  christos       { "._restf", 14, 31, restfpr, restfpr1_tail },
   6373  1.1  christos       { "_savevr_", 20, 31, savevr, savevr_tail },
   6374  1.1  christos       { "_restvr_", 20, 31, restvr, restvr_tail }
   6375  1.1  christos     };
   6376  1.1  christos 
   6377  1.1  christos   htab = ppc_hash_table (info);
   6378  1.1  christos   if (htab == NULL)
   6379  1.1  christos     return FALSE;
   6380  1.1  christos 
   6381  1.1  christos   if (htab->sfpr == NULL)
   6382  1.1  christos     /* We don't have any relocs.  */
   6383  1.1  christos     return TRUE;
   6384  1.1  christos 
   6385  1.1  christos   /* Provide any missing _save* and _rest* functions.  */
   6386  1.1  christos   htab->sfpr->size = 0;
   6387  1.1  christos   for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
   6388  1.1  christos     if (!sfpr_define (info, &funcs[i]))
   6389  1.1  christos       return FALSE;
   6390  1.1  christos 
   6391  1.1  christos   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   6392  1.1  christos 
   6393  1.1  christos   if (htab->sfpr->size == 0)
   6394  1.1  christos     htab->sfpr->flags |= SEC_EXCLUDE;
   6395  1.1  christos 
   6396  1.1  christos   return TRUE;
   6397  1.1  christos }
   6398  1.1  christos 
   6399  1.1  christos /* Adjust a symbol defined by a dynamic object and referenced by a
   6400  1.1  christos    regular object.  The current definition is in some section of the
   6401  1.1  christos    dynamic object, but we're not including those sections.  We have to
   6402  1.1  christos    change the definition to something the rest of the link can
   6403  1.1  christos    understand.  */
   6404  1.1  christos 
   6405  1.1  christos static bfd_boolean
   6406  1.1  christos ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   6407  1.1  christos 				 struct elf_link_hash_entry *h)
   6408  1.1  christos {
   6409  1.1  christos   struct ppc_link_hash_table *htab;
   6410  1.1  christos   asection *s;
   6411  1.1  christos 
   6412  1.1  christos   htab = ppc_hash_table (info);
   6413  1.1  christos   if (htab == NULL)
   6414  1.1  christos     return FALSE;
   6415  1.1  christos 
   6416  1.1  christos   /* Deal with function syms.  */
   6417  1.1  christos   if (h->type == STT_FUNC
   6418  1.1  christos       || h->type == STT_GNU_IFUNC
   6419  1.1  christos       || h->needs_plt)
   6420  1.1  christos     {
   6421  1.1  christos       /* Clear procedure linkage table information for any symbol that
   6422  1.1  christos 	 won't need a .plt entry.  */
   6423  1.1  christos       struct plt_entry *ent;
   6424  1.1  christos       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6425  1.1  christos 	if (ent->plt.refcount > 0)
   6426  1.1  christos 	  break;
   6427  1.1  christos       if (ent == NULL
   6428  1.1  christos 	  || (h->type != STT_GNU_IFUNC
   6429  1.1  christos 	      && (SYMBOL_CALLS_LOCAL (info, h)
   6430  1.1  christos 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   6431  1.1  christos 		      && h->root.type == bfd_link_hash_undefweak))))
   6432  1.1  christos 	{
   6433  1.1  christos 	  h->plt.plist = NULL;
   6434  1.1  christos 	  h->needs_plt = 0;
   6435  1.1  christos 	}
   6436  1.1  christos     }
   6437  1.1  christos   else
   6438  1.1  christos     h->plt.plist = NULL;
   6439  1.1  christos 
   6440  1.1  christos   /* If this is a weak symbol, and there is a real definition, the
   6441  1.1  christos      processor independent code will have arranged for us to see the
   6442  1.1  christos      real definition first, and we can just use the same value.  */
   6443  1.1  christos   if (h->u.weakdef != NULL)
   6444  1.1  christos     {
   6445  1.1  christos       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   6446  1.1  christos 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   6447  1.1  christos       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   6448  1.1  christos       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   6449  1.1  christos       if (ELIMINATE_COPY_RELOCS)
   6450  1.1  christos 	h->non_got_ref = h->u.weakdef->non_got_ref;
   6451  1.1  christos       return TRUE;
   6452  1.1  christos     }
   6453  1.1  christos 
   6454  1.1  christos   /* If we are creating a shared library, we must presume that the
   6455  1.1  christos      only references to the symbol are via the global offset table.
   6456  1.1  christos      For such cases we need not do anything here; the relocations will
   6457  1.1  christos      be handled correctly by relocate_section.  */
   6458  1.1  christos   if (info->shared)
   6459  1.1  christos     return TRUE;
   6460  1.1  christos 
   6461  1.1  christos   /* If there are no references to this symbol that do not use the
   6462  1.1  christos      GOT, we don't need to generate a copy reloc.  */
   6463  1.1  christos   if (!h->non_got_ref)
   6464  1.1  christos     return TRUE;
   6465  1.1  christos 
   6466  1.1  christos   /* Don't generate a copy reloc for symbols defined in the executable.  */
   6467  1.1  christos   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
   6468  1.1  christos     return TRUE;
   6469  1.1  christos 
   6470  1.1  christos   if (ELIMINATE_COPY_RELOCS)
   6471  1.1  christos     {
   6472  1.1  christos       struct ppc_link_hash_entry * eh;
   6473  1.1  christos       struct ppc_dyn_relocs *p;
   6474  1.1  christos 
   6475  1.1  christos       eh = (struct ppc_link_hash_entry *) h;
   6476  1.1  christos       for (p = eh->dyn_relocs; p != NULL; p = p->next)
   6477  1.1  christos 	{
   6478  1.1  christos 	  s = p->sec->output_section;
   6479  1.1  christos 	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
   6480  1.1  christos 	    break;
   6481  1.1  christos 	}
   6482  1.1  christos 
   6483  1.1  christos       /* If we didn't find any dynamic relocs in read-only sections, then
   6484  1.1  christos 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   6485  1.1  christos       if (p == NULL)
   6486  1.1  christos 	{
   6487  1.1  christos 	  h->non_got_ref = 0;
   6488  1.1  christos 	  return TRUE;
   6489  1.1  christos 	}
   6490  1.1  christos     }
   6491  1.1  christos 
   6492  1.1  christos   if (h->plt.plist != NULL)
   6493  1.1  christos     {
   6494  1.1  christos       /* We should never get here, but unfortunately there are versions
   6495  1.1  christos 	 of gcc out there that improperly (for this ABI) put initialized
   6496  1.1  christos 	 function pointers, vtable refs and suchlike in read-only
   6497  1.1  christos 	 sections.  Allow them to proceed, but warn that this might
   6498  1.1  christos 	 break at runtime.  */
   6499  1.1  christos       (*_bfd_error_handler)
   6500  1.1  christos 	(_("copy reloc against `%s' requires lazy plt linking; "
   6501  1.1  christos 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
   6502  1.1  christos 	 h->root.root.string);
   6503  1.1  christos     }
   6504  1.1  christos 
   6505  1.1  christos   /* This is a reference to a symbol defined by a dynamic object which
   6506  1.1  christos      is not a function.  */
   6507  1.1  christos 
   6508  1.1  christos   if (h->size == 0)
   6509  1.1  christos     {
   6510  1.1  christos       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
   6511  1.1  christos 			     h->root.root.string);
   6512  1.1  christos       return TRUE;
   6513  1.1  christos     }
   6514  1.1  christos 
   6515  1.1  christos   /* We must allocate the symbol in our .dynbss section, which will
   6516  1.1  christos      become part of the .bss section of the executable.  There will be
   6517  1.1  christos      an entry for this symbol in the .dynsym section.  The dynamic
   6518  1.1  christos      object will contain position independent code, so all references
   6519  1.1  christos      from the dynamic object to this symbol will go through the global
   6520  1.1  christos      offset table.  The dynamic linker will use the .dynsym entry to
   6521  1.1  christos      determine the address it must put in the global offset table, so
   6522  1.1  christos      both the dynamic object and the regular object will refer to the
   6523  1.1  christos      same memory location for the variable.  */
   6524  1.1  christos 
   6525  1.1  christos   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
   6526  1.1  christos      to copy the initial value out of the dynamic object and into the
   6527  1.1  christos      runtime process image.  We need to remember the offset into the
   6528  1.1  christos      .rela.bss section we are going to use.  */
   6529  1.1  christos   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   6530  1.1  christos     {
   6531  1.1  christos       htab->relbss->size += sizeof (Elf64_External_Rela);
   6532  1.1  christos       h->needs_copy = 1;
   6533  1.1  christos     }
   6534  1.1  christos 
   6535  1.1  christos   s = htab->dynbss;
   6536  1.1  christos 
   6537  1.1  christos   return _bfd_elf_adjust_dynamic_copy (h, s);
   6538  1.1  christos }
   6539  1.1  christos 
   6540  1.1  christos /* If given a function descriptor symbol, hide both the function code
   6541  1.1  christos    sym and the descriptor.  */
   6542  1.1  christos static void
   6543  1.1  christos ppc64_elf_hide_symbol (struct bfd_link_info *info,
   6544  1.1  christos 		       struct elf_link_hash_entry *h,
   6545  1.1  christos 		       bfd_boolean force_local)
   6546  1.1  christos {
   6547  1.1  christos   struct ppc_link_hash_entry *eh;
   6548  1.1  christos   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   6549  1.1  christos 
   6550  1.1  christos   eh = (struct ppc_link_hash_entry *) h;
   6551  1.1  christos   if (eh->is_func_descriptor)
   6552  1.1  christos     {
   6553  1.1  christos       struct ppc_link_hash_entry *fh = eh->oh;
   6554  1.1  christos 
   6555  1.1  christos       if (fh == NULL)
   6556  1.1  christos 	{
   6557  1.1  christos 	  const char *p, *q;
   6558  1.1  christos 	  struct ppc_link_hash_table *htab;
   6559  1.1  christos 	  char save;
   6560  1.1  christos 
   6561  1.1  christos 	  /* We aren't supposed to use alloca in BFD because on
   6562  1.1  christos 	     systems which do not have alloca the version in libiberty
   6563  1.1  christos 	     calls xmalloc, which might cause the program to crash
   6564  1.1  christos 	     when it runs out of memory.  This function doesn't have a
   6565  1.1  christos 	     return status, so there's no way to gracefully return an
   6566  1.1  christos 	     error.  So cheat.  We know that string[-1] can be safely
   6567  1.1  christos 	     accessed;  It's either a string in an ELF string table,
   6568  1.1  christos 	     or allocated in an objalloc structure.  */
   6569  1.1  christos 
   6570  1.1  christos 	  p = eh->elf.root.root.string - 1;
   6571  1.1  christos 	  save = *p;
   6572  1.1  christos 	  *(char *) p = '.';
   6573  1.1  christos 	  htab = ppc_hash_table (info);
   6574  1.1  christos 	  if (htab == NULL)
   6575  1.1  christos 	    return;
   6576  1.1  christos 
   6577  1.1  christos 	  fh = (struct ppc_link_hash_entry *)
   6578  1.1  christos 	    elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6579  1.1  christos 	  *(char *) p = save;
   6580  1.1  christos 
   6581  1.1  christos 	  /* Unfortunately, if it so happens that the string we were
   6582  1.1  christos 	     looking for was allocated immediately before this string,
   6583  1.1  christos 	     then we overwrote the string terminator.  That's the only
   6584  1.1  christos 	     reason the lookup should fail.  */
   6585  1.1  christos 	  if (fh == NULL)
   6586  1.1  christos 	    {
   6587  1.1  christos 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   6588  1.1  christos 	      while (q >= eh->elf.root.root.string && *q == *p)
   6589  1.1  christos 		--q, --p;
   6590  1.1  christos 	      if (q < eh->elf.root.root.string && *p == '.')
   6591  1.1  christos 		fh = (struct ppc_link_hash_entry *)
   6592  1.1  christos 		  elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6593  1.1  christos 	    }
   6594  1.1  christos 	  if (fh != NULL)
   6595  1.1  christos 	    {
   6596  1.1  christos 	      eh->oh = fh;
   6597  1.1  christos 	      fh->oh = eh;
   6598  1.1  christos 	    }
   6599  1.1  christos 	}
   6600  1.1  christos       if (fh != NULL)
   6601  1.1  christos 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6602  1.1  christos     }
   6603  1.1  christos }
   6604  1.1  christos 
   6605  1.1  christos static bfd_boolean
   6606  1.1  christos get_sym_h (struct elf_link_hash_entry **hp,
   6607  1.1  christos 	   Elf_Internal_Sym **symp,
   6608  1.1  christos 	   asection **symsecp,
   6609  1.1  christos 	   unsigned char **tls_maskp,
   6610  1.1  christos 	   Elf_Internal_Sym **locsymsp,
   6611  1.1  christos 	   unsigned long r_symndx,
   6612  1.1  christos 	   bfd *ibfd)
   6613  1.1  christos {
   6614  1.1  christos   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   6615  1.1  christos 
   6616  1.1  christos   if (r_symndx >= symtab_hdr->sh_info)
   6617  1.1  christos     {
   6618  1.1  christos       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   6619  1.1  christos       struct elf_link_hash_entry *h;
   6620  1.1  christos 
   6621  1.1  christos       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6622  1.1  christos       h = elf_follow_link (h);
   6623  1.1  christos 
   6624  1.1  christos       if (hp != NULL)
   6625  1.1  christos 	*hp = h;
   6626  1.1  christos 
   6627  1.1  christos       if (symp != NULL)
   6628  1.1  christos 	*symp = NULL;
   6629  1.1  christos 
   6630  1.1  christos       if (symsecp != NULL)
   6631  1.1  christos 	{
   6632  1.1  christos 	  asection *symsec = NULL;
   6633  1.1  christos 	  if (h->root.type == bfd_link_hash_defined
   6634  1.1  christos 	      || h->root.type == bfd_link_hash_defweak)
   6635  1.1  christos 	    symsec = h->root.u.def.section;
   6636  1.1  christos 	  *symsecp = symsec;
   6637  1.1  christos 	}
   6638  1.1  christos 
   6639  1.1  christos       if (tls_maskp != NULL)
   6640  1.1  christos 	{
   6641  1.1  christos 	  struct ppc_link_hash_entry *eh;
   6642  1.1  christos 
   6643  1.1  christos 	  eh = (struct ppc_link_hash_entry *) h;
   6644  1.1  christos 	  *tls_maskp = &eh->tls_mask;
   6645  1.1  christos 	}
   6646  1.1  christos     }
   6647  1.1  christos   else
   6648  1.1  christos     {
   6649  1.1  christos       Elf_Internal_Sym *sym;
   6650  1.1  christos       Elf_Internal_Sym *locsyms = *locsymsp;
   6651  1.1  christos 
   6652  1.1  christos       if (locsyms == NULL)
   6653  1.1  christos 	{
   6654  1.1  christos 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   6655  1.1  christos 	  if (locsyms == NULL)
   6656  1.1  christos 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   6657  1.1  christos 					    symtab_hdr->sh_info,
   6658  1.1  christos 					    0, NULL, NULL, NULL);
   6659  1.1  christos 	  if (locsyms == NULL)
   6660  1.1  christos 	    return FALSE;
   6661  1.1  christos 	  *locsymsp = locsyms;
   6662  1.1  christos 	}
   6663  1.1  christos       sym = locsyms + r_symndx;
   6664  1.1  christos 
   6665  1.1  christos       if (hp != NULL)
   6666  1.1  christos 	*hp = NULL;
   6667  1.1  christos 
   6668  1.1  christos       if (symp != NULL)
   6669  1.1  christos 	*symp = sym;
   6670  1.1  christos 
   6671  1.1  christos       if (symsecp != NULL)
   6672  1.1  christos 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   6673  1.1  christos 
   6674  1.1  christos       if (tls_maskp != NULL)
   6675  1.1  christos 	{
   6676  1.1  christos 	  struct got_entry **lgot_ents;
   6677  1.1  christos 	  unsigned char *tls_mask;
   6678  1.1  christos 
   6679  1.1  christos 	  tls_mask = NULL;
   6680  1.1  christos 	  lgot_ents = elf_local_got_ents (ibfd);
   6681  1.1  christos 	  if (lgot_ents != NULL)
   6682  1.1  christos 	    {
   6683  1.1  christos 	      struct plt_entry **local_plt = (struct plt_entry **)
   6684  1.1  christos 		(lgot_ents + symtab_hdr->sh_info);
   6685  1.1  christos 	      unsigned char *lgot_masks = (unsigned char *)
   6686  1.1  christos 		(local_plt + symtab_hdr->sh_info);
   6687  1.1  christos 	      tls_mask = &lgot_masks[r_symndx];
   6688  1.1  christos 	    }
   6689  1.1  christos 	  *tls_maskp = tls_mask;
   6690  1.1  christos 	}
   6691  1.1  christos     }
   6692  1.1  christos   return TRUE;
   6693  1.1  christos }
   6694  1.1  christos 
   6695  1.1  christos /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   6696  1.1  christos    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   6697  1.1  christos    type suitable for optimization, and 1 otherwise.  */
   6698  1.1  christos 
   6699  1.1  christos static int
   6700  1.1  christos get_tls_mask (unsigned char **tls_maskp,
   6701  1.1  christos 	      unsigned long *toc_symndx,
   6702  1.1  christos 	      bfd_vma *toc_addend,
   6703  1.1  christos 	      Elf_Internal_Sym **locsymsp,
   6704  1.1  christos 	      const Elf_Internal_Rela *rel,
   6705  1.1  christos 	      bfd *ibfd)
   6706  1.1  christos {
   6707  1.1  christos   unsigned long r_symndx;
   6708  1.1  christos   int next_r;
   6709  1.1  christos   struct elf_link_hash_entry *h;
   6710  1.1  christos   Elf_Internal_Sym *sym;
   6711  1.1  christos   asection *sec;
   6712  1.1  christos   bfd_vma off;
   6713  1.1  christos 
   6714  1.1  christos   r_symndx = ELF64_R_SYM (rel->r_info);
   6715  1.1  christos   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6716  1.1  christos     return 0;
   6717  1.1  christos 
   6718  1.1  christos   if ((*tls_maskp != NULL && **tls_maskp != 0)
   6719  1.1  christos       || sec == NULL
   6720  1.1  christos       || ppc64_elf_section_data (sec) == NULL
   6721  1.1  christos       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   6722  1.1  christos     return 1;
   6723  1.1  christos 
   6724  1.1  christos   /* Look inside a TOC section too.  */
   6725  1.1  christos   if (h != NULL)
   6726  1.1  christos     {
   6727  1.1  christos       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   6728  1.1  christos       off = h->root.u.def.value;
   6729  1.1  christos     }
   6730  1.1  christos   else
   6731  1.1  christos     off = sym->st_value;
   6732  1.1  christos   off += rel->r_addend;
   6733  1.1  christos   BFD_ASSERT (off % 8 == 0);
   6734  1.1  christos   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   6735  1.1  christos   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   6736  1.1  christos   if (toc_symndx != NULL)
   6737  1.1  christos     *toc_symndx = r_symndx;
   6738  1.1  christos   if (toc_addend != NULL)
   6739  1.1  christos     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   6740  1.1  christos   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6741  1.1  christos     return 0;
   6742  1.1  christos   if ((h == NULL || is_static_defined (h))
   6743  1.1  christos       && (next_r == -1 || next_r == -2))
   6744  1.1  christos     return 1 - next_r;
   6745  1.1  christos   return 1;
   6746  1.1  christos }
   6747  1.1  christos 
   6748  1.1  christos /* Adjust all global syms defined in opd sections.  In gcc generated
   6749  1.1  christos    code for the old ABI, these will already have been done.  */
   6750  1.1  christos 
   6751  1.1  christos static bfd_boolean
   6752  1.1  christos adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   6753  1.1  christos {
   6754  1.1  christos   struct ppc_link_hash_entry *eh;
   6755  1.1  christos   asection *sym_sec;
   6756  1.1  christos   struct _opd_sec_data *opd;
   6757  1.1  christos 
   6758  1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   6759  1.1  christos     return TRUE;
   6760  1.1  christos 
   6761  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   6762  1.1  christos     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6763  1.1  christos 
   6764  1.1  christos   if (h->root.type != bfd_link_hash_defined
   6765  1.1  christos       && h->root.type != bfd_link_hash_defweak)
   6766  1.1  christos     return TRUE;
   6767  1.1  christos 
   6768  1.1  christos   eh = (struct ppc_link_hash_entry *) h;
   6769  1.1  christos   if (eh->adjust_done)
   6770  1.1  christos     return TRUE;
   6771  1.1  christos 
   6772  1.1  christos   sym_sec = eh->elf.root.u.def.section;
   6773  1.1  christos   opd = get_opd_info (sym_sec);
   6774  1.1  christos   if (opd != NULL && opd->adjust != NULL)
   6775  1.1  christos     {
   6776  1.1  christos       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
   6777  1.1  christos       if (adjust == -1)
   6778  1.1  christos 	{
   6779  1.1  christos 	  /* This entry has been deleted.  */
   6780  1.1  christos 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   6781  1.1  christos 	  if (dsec == NULL)
   6782  1.1  christos 	    {
   6783  1.1  christos 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   6784  1.1  christos 		if (elf_discarded_section (dsec))
   6785  1.1  christos 		  {
   6786  1.1  christos 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   6787  1.1  christos 		    break;
   6788  1.1  christos 		  }
   6789  1.1  christos 	    }
   6790  1.1  christos 	  eh->elf.root.u.def.value = 0;
   6791  1.1  christos 	  eh->elf.root.u.def.section = dsec;
   6792  1.1  christos 	}
   6793  1.1  christos       else
   6794  1.1  christos 	eh->elf.root.u.def.value += adjust;
   6795  1.1  christos       eh->adjust_done = 1;
   6796  1.1  christos     }
   6797  1.1  christos   return TRUE;
   6798  1.1  christos }
   6799  1.1  christos 
   6800  1.1  christos /* Handles decrementing dynamic reloc counts for the reloc specified by
   6801  1.1  christos    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
   6802  1.1  christos    have already been determined.  */
   6803  1.1  christos 
   6804  1.1  christos static bfd_boolean
   6805  1.1  christos dec_dynrel_count (bfd_vma r_info,
   6806  1.1  christos 		  asection *sec,
   6807  1.1  christos 		  struct bfd_link_info *info,
   6808  1.1  christos 		  Elf_Internal_Sym **local_syms,
   6809  1.1  christos 		  struct elf_link_hash_entry *h,
   6810  1.1  christos 		  asection *sym_sec)
   6811  1.1  christos {
   6812  1.1  christos   enum elf_ppc64_reloc_type r_type;
   6813  1.1  christos   struct ppc_dyn_relocs *p;
   6814  1.1  christos   struct ppc_dyn_relocs **pp;
   6815  1.1  christos 
   6816  1.1  christos   /* Can this reloc be dynamic?  This switch, and later tests here
   6817  1.1  christos      should be kept in sync with the code in check_relocs.  */
   6818  1.1  christos   r_type = ELF64_R_TYPE (r_info);
   6819  1.1  christos   switch (r_type)
   6820  1.1  christos     {
   6821  1.1  christos     default:
   6822  1.1  christos       return TRUE;
   6823  1.1  christos 
   6824  1.1  christos     case R_PPC64_TPREL16:
   6825  1.1  christos     case R_PPC64_TPREL16_LO:
   6826  1.1  christos     case R_PPC64_TPREL16_HI:
   6827  1.1  christos     case R_PPC64_TPREL16_HA:
   6828  1.1  christos     case R_PPC64_TPREL16_DS:
   6829  1.1  christos     case R_PPC64_TPREL16_LO_DS:
   6830  1.1  christos     case R_PPC64_TPREL16_HIGHER:
   6831  1.1  christos     case R_PPC64_TPREL16_HIGHERA:
   6832  1.1  christos     case R_PPC64_TPREL16_HIGHEST:
   6833  1.1  christos     case R_PPC64_TPREL16_HIGHESTA:
   6834  1.1  christos       if (!info->shared)
   6835  1.1  christos 	return TRUE;
   6836  1.1  christos 
   6837  1.1  christos     case R_PPC64_TPREL64:
   6838  1.1  christos     case R_PPC64_DTPMOD64:
   6839  1.1  christos     case R_PPC64_DTPREL64:
   6840  1.1  christos     case R_PPC64_ADDR64:
   6841  1.1  christos     case R_PPC64_REL30:
   6842  1.1  christos     case R_PPC64_REL32:
   6843  1.1  christos     case R_PPC64_REL64:
   6844  1.1  christos     case R_PPC64_ADDR14:
   6845  1.1  christos     case R_PPC64_ADDR14_BRNTAKEN:
   6846  1.1  christos     case R_PPC64_ADDR14_BRTAKEN:
   6847  1.1  christos     case R_PPC64_ADDR16:
   6848  1.1  christos     case R_PPC64_ADDR16_DS:
   6849  1.1  christos     case R_PPC64_ADDR16_HA:
   6850  1.1  christos     case R_PPC64_ADDR16_HI:
   6851  1.1  christos     case R_PPC64_ADDR16_HIGHER:
   6852  1.1  christos     case R_PPC64_ADDR16_HIGHERA:
   6853  1.1  christos     case R_PPC64_ADDR16_HIGHEST:
   6854  1.1  christos     case R_PPC64_ADDR16_HIGHESTA:
   6855  1.1  christos     case R_PPC64_ADDR16_LO:
   6856  1.1  christos     case R_PPC64_ADDR16_LO_DS:
   6857  1.1  christos     case R_PPC64_ADDR24:
   6858  1.1  christos     case R_PPC64_ADDR32:
   6859  1.1  christos     case R_PPC64_UADDR16:
   6860  1.1  christos     case R_PPC64_UADDR32:
   6861  1.1  christos     case R_PPC64_UADDR64:
   6862  1.1  christos     case R_PPC64_TOC:
   6863  1.1  christos       break;
   6864  1.1  christos     }
   6865  1.1  christos 
   6866  1.1  christos   if (local_syms != NULL)
   6867  1.1  christos     {
   6868  1.1  christos       unsigned long r_symndx;
   6869  1.1  christos       Elf_Internal_Sym *sym;
   6870  1.1  christos       bfd *ibfd = sec->owner;
   6871  1.1  christos 
   6872  1.1  christos       r_symndx = ELF64_R_SYM (r_info);
   6873  1.1  christos       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   6874  1.1  christos 	return FALSE;
   6875  1.1  christos     }
   6876  1.1  christos 
   6877  1.1  christos   if ((info->shared
   6878  1.1  christos        && (must_be_dyn_reloc (info, r_type)
   6879  1.1  christos 	   || (h != NULL
   6880  1.1  christos 	       && (!info->symbolic
   6881  1.1  christos 		   || h->root.type == bfd_link_hash_defweak
   6882  1.1  christos 		   || !h->def_regular))))
   6883  1.1  christos       || (ELIMINATE_COPY_RELOCS
   6884  1.1  christos 	  && !info->shared
   6885  1.1  christos 	  && h != NULL
   6886  1.1  christos 	  && (h->root.type == bfd_link_hash_defweak
   6887  1.1  christos 	      || !h->def_regular)))
   6888  1.1  christos     ;
   6889  1.1  christos   else
   6890  1.1  christos     return TRUE;
   6891  1.1  christos 
   6892  1.1  christos   if (h != NULL)
   6893  1.1  christos     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   6894  1.1  christos   else
   6895  1.1  christos     {
   6896  1.1  christos       if (sym_sec != NULL)
   6897  1.1  christos 	{
   6898  1.1  christos 	  void *vpp = &elf_section_data (sym_sec)->local_dynrel;
   6899  1.1  christos 	  pp = (struct ppc_dyn_relocs **) vpp;
   6900  1.1  christos 	}
   6901  1.1  christos       else
   6902  1.1  christos 	{
   6903  1.1  christos 	  void *vpp = &elf_section_data (sec)->local_dynrel;
   6904  1.1  christos 	  pp = (struct ppc_dyn_relocs **) vpp;
   6905  1.1  christos 	}
   6906  1.1  christos 
   6907  1.1  christos       /* elf_gc_sweep may have already removed all dyn relocs associated
   6908  1.1  christos 	 with local syms for a given section.  Don't report a dynreloc
   6909  1.1  christos 	 miscount.  */
   6910  1.1  christos       if (*pp == NULL)
   6911  1.1  christos 	return TRUE;
   6912  1.1  christos     }
   6913  1.1  christos 
   6914  1.1  christos   while ((p = *pp) != NULL)
   6915  1.1  christos     {
   6916  1.1  christos       if (p->sec == sec)
   6917  1.1  christos 	{
   6918  1.1  christos 	  if (!must_be_dyn_reloc (info, r_type))
   6919  1.1  christos 	    p->pc_count -= 1;
   6920  1.1  christos 	  p->count -= 1;
   6921  1.1  christos 	  if (p->count == 0)
   6922  1.1  christos 	    *pp = p->next;
   6923  1.1  christos 	  return TRUE;
   6924  1.1  christos 	}
   6925  1.1  christos       pp = &p->next;
   6926  1.1  christos     }
   6927  1.1  christos 
   6928  1.1  christos   (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
   6929  1.1  christos 			   sec->owner, sec);
   6930  1.1  christos   bfd_set_error (bfd_error_bad_value);
   6931  1.1  christos   return FALSE;
   6932  1.1  christos }
   6933  1.1  christos 
   6934  1.1  christos /* Remove unused Official Procedure Descriptor entries.  Currently we
   6935  1.1  christos    only remove those associated with functions in discarded link-once
   6936  1.1  christos    sections, or weakly defined functions that have been overridden.  It
   6937  1.1  christos    would be possible to remove many more entries for statically linked
   6938  1.1  christos    applications.  */
   6939  1.1  christos 
   6940  1.1  christos bfd_boolean
   6941  1.1  christos ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
   6942  1.1  christos {
   6943  1.1  christos   bfd *ibfd;
   6944  1.1  christos   bfd_boolean some_edited = FALSE;
   6945  1.1  christos   asection *need_pad = NULL;
   6946  1.1  christos 
   6947  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   6948  1.1  christos     {
   6949  1.1  christos       asection *sec;
   6950  1.1  christos       Elf_Internal_Rela *relstart, *rel, *relend;
   6951  1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   6952  1.1  christos       Elf_Internal_Sym *local_syms;
   6953  1.1  christos       bfd_vma offset;
   6954  1.1  christos       struct _opd_sec_data *opd;
   6955  1.1  christos       bfd_boolean need_edit, add_aux_fields;
   6956  1.1  christos       bfd_size_type cnt_16b = 0;
   6957  1.1  christos 
   6958  1.1  christos       if (!is_ppc64_elf (ibfd))
   6959  1.1  christos 	continue;
   6960  1.1  christos 
   6961  1.1  christos       sec = bfd_get_section_by_name (ibfd, ".opd");
   6962  1.1  christos       if (sec == NULL || sec->size == 0)
   6963  1.1  christos 	continue;
   6964  1.1  christos 
   6965  1.1  christos       if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
   6966  1.1  christos 	continue;
   6967  1.1  christos 
   6968  1.1  christos       if (sec->output_section == bfd_abs_section_ptr)
   6969  1.1  christos 	continue;
   6970  1.1  christos 
   6971  1.1  christos       /* Look through the section relocs.  */
   6972  1.1  christos       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   6973  1.1  christos 	continue;
   6974  1.1  christos 
   6975  1.1  christos       local_syms = NULL;
   6976  1.1  christos       symtab_hdr = &elf_symtab_hdr (ibfd);
   6977  1.1  christos 
   6978  1.1  christos       /* Read the relocations.  */
   6979  1.1  christos       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   6980  1.1  christos 					    info->keep_memory);
   6981  1.1  christos       if (relstart == NULL)
   6982  1.1  christos 	return FALSE;
   6983  1.1  christos 
   6984  1.1  christos       /* First run through the relocs to check they are sane, and to
   6985  1.1  christos 	 determine whether we need to edit this opd section.  */
   6986  1.1  christos       need_edit = FALSE;
   6987  1.1  christos       need_pad = sec;
   6988  1.1  christos       offset = 0;
   6989  1.1  christos       relend = relstart + sec->reloc_count;
   6990  1.1  christos       for (rel = relstart; rel < relend; )
   6991  1.1  christos 	{
   6992  1.1  christos 	  enum elf_ppc64_reloc_type r_type;
   6993  1.1  christos 	  unsigned long r_symndx;
   6994  1.1  christos 	  asection *sym_sec;
   6995  1.1  christos 	  struct elf_link_hash_entry *h;
   6996  1.1  christos 	  Elf_Internal_Sym *sym;
   6997  1.1  christos 
   6998  1.1  christos 	  /* .opd contains a regular array of 16 or 24 byte entries.  We're
   6999  1.1  christos 	     only interested in the reloc pointing to a function entry
   7000  1.1  christos 	     point.  */
   7001  1.1  christos 	  if (rel->r_offset != offset
   7002  1.1  christos 	      || rel + 1 >= relend
   7003  1.1  christos 	      || (rel + 1)->r_offset != offset + 8)
   7004  1.1  christos 	    {
   7005  1.1  christos 	      /* If someone messes with .opd alignment then after a
   7006  1.1  christos 		 "ld -r" we might have padding in the middle of .opd.
   7007  1.1  christos 		 Also, there's nothing to prevent someone putting
   7008  1.1  christos 		 something silly in .opd with the assembler.  No .opd
   7009  1.1  christos 		 optimization for them!  */
   7010  1.1  christos 	    broken_opd:
   7011  1.1  christos 	      (*_bfd_error_handler)
   7012  1.1  christos 		(_("%B: .opd is not a regular array of opd entries"), ibfd);
   7013  1.1  christos 	      need_edit = FALSE;
   7014  1.1  christos 	      break;
   7015  1.1  christos 	    }
   7016  1.1  christos 
   7017  1.1  christos 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7018  1.1  christos 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7019  1.1  christos 	    {
   7020  1.1  christos 	      (*_bfd_error_handler)
   7021  1.1  christos 		(_("%B: unexpected reloc type %u in .opd section"),
   7022  1.1  christos 		 ibfd, r_type);
   7023  1.1  christos 	      need_edit = FALSE;
   7024  1.1  christos 	      break;
   7025  1.1  christos 	    }
   7026  1.1  christos 
   7027  1.1  christos 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7028  1.1  christos 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7029  1.1  christos 			  r_symndx, ibfd))
   7030  1.1  christos 	    goto error_ret;
   7031  1.1  christos 
   7032  1.1  christos 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7033  1.1  christos 	    {
   7034  1.1  christos 	      const char *sym_name;
   7035  1.1  christos 	      if (h != NULL)
   7036  1.1  christos 		sym_name = h->root.root.string;
   7037  1.1  christos 	      else
   7038  1.1  christos 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7039  1.1  christos 					     sym_sec);
   7040  1.1  christos 
   7041  1.1  christos 	      (*_bfd_error_handler)
   7042  1.1  christos 		(_("%B: undefined sym `%s' in .opd section"),
   7043  1.1  christos 		 ibfd, sym_name);
   7044  1.1  christos 	      need_edit = FALSE;
   7045  1.1  christos 	      break;
   7046  1.1  christos 	    }
   7047  1.1  christos 
   7048  1.1  christos 	  /* opd entries are always for functions defined in the
   7049  1.1  christos 	     current input bfd.  If the symbol isn't defined in the
   7050  1.1  christos 	     input bfd, then we won't be using the function in this
   7051  1.1  christos 	     bfd;  It must be defined in a linkonce section in another
   7052  1.1  christos 	     bfd, or is weak.  It's also possible that we are
   7053  1.1  christos 	     discarding the function due to a linker script /DISCARD/,
   7054  1.1  christos 	     which we test for via the output_section.  */
   7055  1.1  christos 	  if (sym_sec->owner != ibfd
   7056  1.1  christos 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7057  1.1  christos 	    need_edit = TRUE;
   7058  1.1  christos 
   7059  1.1  christos 	  rel += 2;
   7060  1.1  christos 	  if (rel == relend
   7061  1.1  christos 	      || (rel + 1 == relend && rel->r_offset == offset + 16))
   7062  1.1  christos 	    {
   7063  1.1  christos 	      if (sec->size == offset + 24)
   7064  1.1  christos 		{
   7065  1.1  christos 		  need_pad = NULL;
   7066  1.1  christos 		  break;
   7067  1.1  christos 		}
   7068  1.1  christos 	      if (rel == relend && sec->size == offset + 16)
   7069  1.1  christos 		{
   7070  1.1  christos 		  cnt_16b++;
   7071  1.1  christos 		  break;
   7072  1.1  christos 		}
   7073  1.1  christos 	      goto broken_opd;
   7074  1.1  christos 	    }
   7075  1.1  christos 
   7076  1.1  christos 	  if (rel->r_offset == offset + 24)
   7077  1.1  christos 	    offset += 24;
   7078  1.1  christos 	  else if (rel->r_offset != offset + 16)
   7079  1.1  christos 	    goto broken_opd;
   7080  1.1  christos 	  else if (rel + 1 < relend
   7081  1.1  christos 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7082  1.1  christos 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7083  1.1  christos 	    {
   7084  1.1  christos 	      offset += 16;
   7085  1.1  christos 	      cnt_16b++;
   7086  1.1  christos 	    }
   7087  1.1  christos 	  else if (rel + 2 < relend
   7088  1.1  christos 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
   7089  1.1  christos 		   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
   7090  1.1  christos 	    {
   7091  1.1  christos 	      offset += 24;
   7092  1.1  christos 	      rel += 1;
   7093  1.1  christos 	    }
   7094  1.1  christos 	  else
   7095  1.1  christos 	    goto broken_opd;
   7096  1.1  christos 	}
   7097  1.1  christos 
   7098  1.1  christos       add_aux_fields = non_overlapping && cnt_16b > 0;
   7099  1.1  christos 
   7100  1.1  christos       if (need_edit || add_aux_fields)
   7101  1.1  christos 	{
   7102  1.1  christos 	  Elf_Internal_Rela *write_rel;
   7103  1.1  christos 	  Elf_Internal_Shdr *rel_hdr;
   7104  1.1  christos 	  bfd_byte *rptr, *wptr;
   7105  1.1  christos 	  bfd_byte *new_contents;
   7106  1.1  christos 	  bfd_boolean skip;
   7107  1.1  christos 	  long opd_ent_size;
   7108  1.1  christos 	  bfd_size_type amt;
   7109  1.1  christos 
   7110  1.1  christos 	  new_contents = NULL;
   7111  1.1  christos 	  amt = sec->size * sizeof (long) / 8;
   7112  1.1  christos 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7113  1.1  christos 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7114  1.1  christos 	  if (opd->adjust == NULL)
   7115  1.1  christos 	    return FALSE;
   7116  1.1  christos 	  ppc64_elf_section_data (sec)->sec_type = sec_opd;
   7117  1.1  christos 
   7118  1.1  christos 	  /* This seems a waste of time as input .opd sections are all
   7119  1.1  christos 	     zeros as generated by gcc, but I suppose there's no reason
   7120  1.1  christos 	     this will always be so.  We might start putting something in
   7121  1.1  christos 	     the third word of .opd entries.  */
   7122  1.1  christos 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7123  1.1  christos 	    {
   7124  1.1  christos 	      bfd_byte *loc;
   7125  1.1  christos 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7126  1.1  christos 		{
   7127  1.1  christos 		  if (loc != NULL)
   7128  1.1  christos 		    free (loc);
   7129  1.1  christos 		error_ret:
   7130  1.1  christos 		  if (local_syms != NULL
   7131  1.1  christos 		      && symtab_hdr->contents != (unsigned char *) local_syms)
   7132  1.1  christos 		    free (local_syms);
   7133  1.1  christos 		  if (elf_section_data (sec)->relocs != relstart)
   7134  1.1  christos 		    free (relstart);
   7135  1.1  christos 		  return FALSE;
   7136  1.1  christos 		}
   7137  1.1  christos 	      sec->contents = loc;
   7138  1.1  christos 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7139  1.1  christos 	    }
   7140  1.1  christos 
   7141  1.1  christos 	  elf_section_data (sec)->relocs = relstart;
   7142  1.1  christos 
   7143  1.1  christos 	  new_contents = sec->contents;
   7144  1.1  christos 	  if (add_aux_fields)
   7145  1.1  christos 	    {
   7146  1.1  christos 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7147  1.1  christos 	      if (new_contents == NULL)
   7148  1.1  christos 		return FALSE;
   7149  1.1  christos 	      need_pad = FALSE;
   7150  1.1  christos 	    }
   7151  1.1  christos 	  wptr = new_contents;
   7152  1.1  christos 	  rptr = sec->contents;
   7153  1.1  christos 
   7154  1.1  christos 	  write_rel = relstart;
   7155  1.1  christos 	  skip = FALSE;
   7156  1.1  christos 	  offset = 0;
   7157  1.1  christos 	  opd_ent_size = 0;
   7158  1.1  christos 	  for (rel = relstart; rel < relend; rel++)
   7159  1.1  christos 	    {
   7160  1.1  christos 	      unsigned long r_symndx;
   7161  1.1  christos 	      asection *sym_sec;
   7162  1.1  christos 	      struct elf_link_hash_entry *h;
   7163  1.1  christos 	      Elf_Internal_Sym *sym;
   7164  1.1  christos 
   7165  1.1  christos 	      r_symndx = ELF64_R_SYM (rel->r_info);
   7166  1.1  christos 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7167  1.1  christos 			      r_symndx, ibfd))
   7168  1.1  christos 		goto error_ret;
   7169  1.1  christos 
   7170  1.1  christos 	      if (rel->r_offset == offset)
   7171  1.1  christos 		{
   7172  1.1  christos 		  struct ppc_link_hash_entry *fdh = NULL;
   7173  1.1  christos 
   7174  1.1  christos 		  /* See if the .opd entry is full 24 byte or
   7175  1.1  christos 		     16 byte (with fd_aux entry overlapped with next
   7176  1.1  christos 		     fd_func).  */
   7177  1.1  christos 		  opd_ent_size = 24;
   7178  1.1  christos 		  if ((rel + 2 == relend && sec->size == offset + 16)
   7179  1.1  christos 		      || (rel + 3 < relend
   7180  1.1  christos 			  && rel[2].r_offset == offset + 16
   7181  1.1  christos 			  && rel[3].r_offset == offset + 24
   7182  1.1  christos 			  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
   7183  1.1  christos 			  && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
   7184  1.1  christos 		    opd_ent_size = 16;
   7185  1.1  christos 
   7186  1.1  christos 		  if (h != NULL
   7187  1.1  christos 		      && h->root.root.string[0] == '.')
   7188  1.1  christos 		    {
   7189  1.1  christos 		      struct ppc_link_hash_table *htab;
   7190  1.1  christos 
   7191  1.1  christos 		      htab = ppc_hash_table (info);
   7192  1.1  christos 		      if (htab != NULL)
   7193  1.1  christos 			fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
   7194  1.1  christos 					  htab);
   7195  1.1  christos 		      if (fdh != NULL
   7196  1.1  christos 			  && fdh->elf.root.type != bfd_link_hash_defined
   7197  1.1  christos 			  && fdh->elf.root.type != bfd_link_hash_defweak)
   7198  1.1  christos 			fdh = NULL;
   7199  1.1  christos 		    }
   7200  1.1  christos 
   7201  1.1  christos 		  skip = (sym_sec->owner != ibfd
   7202  1.1  christos 			  || sym_sec->output_section == bfd_abs_section_ptr);
   7203  1.1  christos 		  if (skip)
   7204  1.1  christos 		    {
   7205  1.1  christos 		      if (fdh != NULL && sym_sec->owner == ibfd)
   7206  1.1  christos 			{
   7207  1.1  christos 			  /* Arrange for the function descriptor sym
   7208  1.1  christos 			     to be dropped.  */
   7209  1.1  christos 			  fdh->elf.root.u.def.value = 0;
   7210  1.1  christos 			  fdh->elf.root.u.def.section = sym_sec;
   7211  1.1  christos 			}
   7212  1.1  christos 		      opd->adjust[rel->r_offset / 8] = -1;
   7213  1.1  christos 		    }
   7214  1.1  christos 		  else
   7215  1.1  christos 		    {
   7216  1.1  christos 		      /* We'll be keeping this opd entry.  */
   7217  1.1  christos 
   7218  1.1  christos 		      if (fdh != NULL)
   7219  1.1  christos 			{
   7220  1.1  christos 			  /* Redefine the function descriptor symbol to
   7221  1.1  christos 			     this location in the opd section.  It is
   7222  1.1  christos 			     necessary to update the value here rather
   7223  1.1  christos 			     than using an array of adjustments as we do
   7224  1.1  christos 			     for local symbols, because various places
   7225  1.1  christos 			     in the generic ELF code use the value
   7226  1.1  christos 			     stored in u.def.value.  */
   7227  1.1  christos 			  fdh->elf.root.u.def.value = wptr - new_contents;
   7228  1.1  christos 			  fdh->adjust_done = 1;
   7229  1.1  christos 			}
   7230  1.1  christos 
   7231  1.1  christos 		      /* Local syms are a bit tricky.  We could
   7232  1.1  christos 			 tweak them as they can be cached, but
   7233  1.1  christos 			 we'd need to look through the local syms
   7234  1.1  christos 			 for the function descriptor sym which we
   7235  1.1  christos 			 don't have at the moment.  So keep an
   7236  1.1  christos 			 array of adjustments.  */
   7237  1.1  christos 		      opd->adjust[rel->r_offset / 8]
   7238  1.1  christos 			= (wptr - new_contents) - (rptr - sec->contents);
   7239  1.1  christos 
   7240  1.1  christos 		      if (wptr != rptr)
   7241  1.1  christos 			memcpy (wptr, rptr, opd_ent_size);
   7242  1.1  christos 		      wptr += opd_ent_size;
   7243  1.1  christos 		      if (add_aux_fields && opd_ent_size == 16)
   7244  1.1  christos 			{
   7245  1.1  christos 			  memset (wptr, '\0', 8);
   7246  1.1  christos 			  wptr += 8;
   7247  1.1  christos 			}
   7248  1.1  christos 		    }
   7249  1.1  christos 		  rptr += opd_ent_size;
   7250  1.1  christos 		  offset += opd_ent_size;
   7251  1.1  christos 		}
   7252  1.1  christos 
   7253  1.1  christos 	      if (skip)
   7254  1.1  christos 		{
   7255  1.1  christos 		  if (!NO_OPD_RELOCS
   7256  1.1  christos 		      && !info->relocatable
   7257  1.1  christos 		      && !dec_dynrel_count (rel->r_info, sec, info,
   7258  1.1  christos 					    NULL, h, sym_sec))
   7259  1.1  christos 		    goto error_ret;
   7260  1.1  christos 		}
   7261  1.1  christos 	      else
   7262  1.1  christos 		{
   7263  1.1  christos 		  /* We need to adjust any reloc offsets to point to the
   7264  1.1  christos 		     new opd entries.  While we're at it, we may as well
   7265  1.1  christos 		     remove redundant relocs.  */
   7266  1.1  christos 		  rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
   7267  1.1  christos 		  if (write_rel != rel)
   7268  1.1  christos 		    memcpy (write_rel, rel, sizeof (*rel));
   7269  1.1  christos 		  ++write_rel;
   7270  1.1  christos 		}
   7271  1.1  christos 	    }
   7272  1.1  christos 
   7273  1.1  christos 	  sec->size = wptr - new_contents;
   7274  1.1  christos 	  sec->reloc_count = write_rel - relstart;
   7275  1.1  christos 	  if (add_aux_fields)
   7276  1.1  christos 	    {
   7277  1.1  christos 	      free (sec->contents);
   7278  1.1  christos 	      sec->contents = new_contents;
   7279  1.1  christos 	    }
   7280  1.1  christos 
   7281  1.1  christos 	  /* Fudge the header size too, as this is used later in
   7282  1.1  christos 	     elf_bfd_final_link if we are emitting relocs.  */
   7283  1.1  christos 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   7284  1.1  christos 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   7285  1.1  christos 	  some_edited = TRUE;
   7286  1.1  christos 	}
   7287  1.1  christos       else if (elf_section_data (sec)->relocs != relstart)
   7288  1.1  christos 	free (relstart);
   7289  1.1  christos 
   7290  1.1  christos       if (local_syms != NULL
   7291  1.1  christos 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7292  1.1  christos 	{
   7293  1.1  christos 	  if (!info->keep_memory)
   7294  1.1  christos 	    free (local_syms);
   7295  1.1  christos 	  else
   7296  1.1  christos 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7297  1.1  christos 	}
   7298  1.1  christos     }
   7299  1.1  christos 
   7300  1.1  christos   if (some_edited)
   7301  1.1  christos     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   7302  1.1  christos 
   7303  1.1  christos   /* If we are doing a final link and the last .opd entry is just 16 byte
   7304  1.1  christos      long, add a 8 byte padding after it.  */
   7305  1.1  christos   if (need_pad != NULL && !info->relocatable)
   7306  1.1  christos     {
   7307  1.1  christos       bfd_byte *p;
   7308  1.1  christos 
   7309  1.1  christos       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   7310  1.1  christos 	{
   7311  1.1  christos 	  BFD_ASSERT (need_pad->size > 0);
   7312  1.1  christos 
   7313  1.1  christos 	  p = bfd_malloc (need_pad->size + 8);
   7314  1.1  christos 	  if (p == NULL)
   7315  1.1  christos 	    return FALSE;
   7316  1.1  christos 
   7317  1.1  christos 	  if (! bfd_get_section_contents (need_pad->owner, need_pad,
   7318  1.1  christos 					  p, 0, need_pad->size))
   7319  1.1  christos 	    return FALSE;
   7320  1.1  christos 
   7321  1.1  christos 	  need_pad->contents = p;
   7322  1.1  christos 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7323  1.1  christos 	}
   7324  1.1  christos       else
   7325  1.1  christos 	{
   7326  1.1  christos 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   7327  1.1  christos 	  if (p == NULL)
   7328  1.1  christos 	    return FALSE;
   7329  1.1  christos 
   7330  1.1  christos 	  need_pad->contents = p;
   7331  1.1  christos 	}
   7332  1.1  christos 
   7333  1.1  christos       memset (need_pad->contents + need_pad->size, 0, 8);
   7334  1.1  christos       need_pad->size += 8;
   7335  1.1  christos     }
   7336  1.1  christos 
   7337  1.1  christos   return TRUE;
   7338  1.1  christos }
   7339  1.1  christos 
   7340  1.1  christos /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
   7341  1.1  christos 
   7342  1.1  christos asection *
   7343  1.1  christos ppc64_elf_tls_setup (struct bfd_link_info *info,
   7344  1.1  christos 		     int no_tls_get_addr_opt,
   7345  1.1  christos 		     int *no_multi_toc)
   7346  1.1  christos {
   7347  1.1  christos   struct ppc_link_hash_table *htab;
   7348  1.1  christos 
   7349  1.1  christos   htab = ppc_hash_table (info);
   7350  1.1  christos   if (htab == NULL)
   7351  1.1  christos     return NULL;
   7352  1.1  christos 
   7353  1.1  christos   if (*no_multi_toc)
   7354  1.1  christos     htab->do_multi_toc = 0;
   7355  1.1  christos   else if (!htab->do_multi_toc)
   7356  1.1  christos     *no_multi_toc = 1;
   7357  1.1  christos 
   7358  1.1  christos   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
   7359  1.1  christos 			elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   7360  1.1  christos 					      FALSE, FALSE, TRUE));
   7361  1.1  christos   /* Move dynamic linking info to the function descriptor sym.  */
   7362  1.1  christos   if (htab->tls_get_addr != NULL)
   7363  1.1  christos     func_desc_adjust (&htab->tls_get_addr->elf, info);
   7364  1.1  christos   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
   7365  1.1  christos 			   elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   7366  1.1  christos 						 FALSE, FALSE, TRUE));
   7367  1.1  christos   if (!no_tls_get_addr_opt)
   7368  1.1  christos     {
   7369  1.1  christos       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
   7370  1.1  christos 
   7371  1.1  christos       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   7372  1.1  christos 				  FALSE, FALSE, TRUE);
   7373  1.1  christos       if (opt != NULL)
   7374  1.1  christos 	func_desc_adjust (opt, info);
   7375  1.1  christos       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   7376  1.1  christos 				     FALSE, FALSE, TRUE);
   7377  1.1  christos       if (opt_fd != NULL
   7378  1.1  christos 	  && (opt_fd->root.type == bfd_link_hash_defined
   7379  1.1  christos 	      || opt_fd->root.type == bfd_link_hash_defweak))
   7380  1.1  christos 	{
   7381  1.1  christos 	  /* If glibc supports an optimized __tls_get_addr call stub,
   7382  1.1  christos 	     signalled by the presence of __tls_get_addr_opt, and we'll
   7383  1.1  christos 	     be calling __tls_get_addr via a plt call stub, then
   7384  1.1  christos 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   7385  1.1  christos 	  tga_fd = &htab->tls_get_addr_fd->elf;
   7386  1.1  christos 	  if (htab->elf.dynamic_sections_created
   7387  1.1  christos 	      && tga_fd != NULL
   7388  1.1  christos 	      && (tga_fd->type == STT_FUNC
   7389  1.1  christos 		  || tga_fd->needs_plt)
   7390  1.1  christos 	      && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   7391  1.1  christos 		   || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
   7392  1.1  christos 		       && tga_fd->root.type == bfd_link_hash_undefweak)))
   7393  1.1  christos 	    {
   7394  1.1  christos 	      struct plt_entry *ent;
   7395  1.1  christos 
   7396  1.1  christos 	      for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   7397  1.1  christos 		if (ent->plt.refcount > 0)
   7398  1.1  christos 		  break;
   7399  1.1  christos 	      if (ent != NULL)
   7400  1.1  christos 		{
   7401  1.1  christos 		  tga_fd->root.type = bfd_link_hash_indirect;
   7402  1.1  christos 		  tga_fd->root.u.i.link = &opt_fd->root;
   7403  1.1  christos 		  ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   7404  1.1  christos 		  if (opt_fd->dynindx != -1)
   7405  1.1  christos 		    {
   7406  1.1  christos 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   7407  1.1  christos 		      opt_fd->dynindx = -1;
   7408  1.1  christos 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7409  1.1  christos 					      opt_fd->dynstr_index);
   7410  1.1  christos 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   7411  1.1  christos 			return NULL;
   7412  1.1  christos 		    }
   7413  1.1  christos 		  htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
   7414  1.1  christos 		  tga = &htab->tls_get_addr->elf;
   7415  1.1  christos 		  if (opt != NULL && tga != NULL)
   7416  1.1  christos 		    {
   7417  1.1  christos 		      tga->root.type = bfd_link_hash_indirect;
   7418  1.1  christos 		      tga->root.u.i.link = &opt->root;
   7419  1.1  christos 		      ppc64_elf_copy_indirect_symbol (info, opt, tga);
   7420  1.1  christos 		      _bfd_elf_link_hash_hide_symbol (info, opt,
   7421  1.1  christos 						      tga->forced_local);
   7422  1.1  christos 		      htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
   7423  1.1  christos 		    }
   7424  1.1  christos 		  htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   7425  1.1  christos 		  htab->tls_get_addr_fd->is_func_descriptor = 1;
   7426  1.1  christos 		  if (htab->tls_get_addr != NULL)
   7427  1.1  christos 		    {
   7428  1.1  christos 		      htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   7429  1.1  christos 		      htab->tls_get_addr->is_func = 1;
   7430  1.1  christos 		    }
   7431  1.1  christos 		}
   7432  1.1  christos 	    }
   7433  1.1  christos 	}
   7434  1.1  christos       else
   7435  1.1  christos 	no_tls_get_addr_opt = TRUE;
   7436  1.1  christos     }
   7437  1.1  christos   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
   7438  1.1  christos   return _bfd_elf_tls_setup (info->output_bfd, info);
   7439  1.1  christos }
   7440  1.1  christos 
   7441  1.1  christos /* Return TRUE iff REL is a branch reloc with a global symbol matching
   7442  1.1  christos    HASH1 or HASH2.  */
   7443  1.1  christos 
   7444  1.1  christos static bfd_boolean
   7445  1.1  christos branch_reloc_hash_match (const bfd *ibfd,
   7446  1.1  christos 			 const Elf_Internal_Rela *rel,
   7447  1.1  christos 			 const struct ppc_link_hash_entry *hash1,
   7448  1.1  christos 			 const struct ppc_link_hash_entry *hash2)
   7449  1.1  christos {
   7450  1.1  christos   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   7451  1.1  christos   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   7452  1.1  christos   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   7453  1.1  christos 
   7454  1.1  christos   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   7455  1.1  christos     {
   7456  1.1  christos       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   7457  1.1  christos       struct elf_link_hash_entry *h;
   7458  1.1  christos 
   7459  1.1  christos       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   7460  1.1  christos       h = elf_follow_link (h);
   7461  1.1  christos       if (h == &hash1->elf || h == &hash2->elf)
   7462  1.1  christos 	return TRUE;
   7463  1.1  christos     }
   7464  1.1  christos   return FALSE;
   7465  1.1  christos }
   7466  1.1  christos 
   7467  1.1  christos /* Run through all the TLS relocs looking for optimization
   7468  1.1  christos    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   7469  1.1  christos    a preliminary section layout so that we know the TLS segment
   7470  1.1  christos    offsets.  We can't optimize earlier because some optimizations need
   7471  1.1  christos    to know the tp offset, and we need to optimize before allocating
   7472  1.1  christos    dynamic relocations.  */
   7473  1.1  christos 
   7474  1.1  christos bfd_boolean
   7475  1.1  christos ppc64_elf_tls_optimize (struct bfd_link_info *info)
   7476  1.1  christos {
   7477  1.1  christos   bfd *ibfd;
   7478  1.1  christos   asection *sec;
   7479  1.1  christos   struct ppc_link_hash_table *htab;
   7480  1.1  christos   unsigned char *toc_ref;
   7481  1.1  christos   int pass;
   7482  1.1  christos 
   7483  1.1  christos   if (info->relocatable || !info->executable)
   7484  1.1  christos     return TRUE;
   7485  1.1  christos 
   7486  1.1  christos   htab = ppc_hash_table (info);
   7487  1.1  christos   if (htab == NULL)
   7488  1.1  christos     return FALSE;
   7489  1.1  christos 
   7490  1.1  christos   /* Make two passes over the relocs.  On the first pass, mark toc
   7491  1.1  christos      entries involved with tls relocs, and check that tls relocs
   7492  1.1  christos      involved in setting up a tls_get_addr call are indeed followed by
   7493  1.1  christos      such a call.  If they are not, we can't do any tls optimization.
   7494  1.1  christos      On the second pass twiddle tls_mask flags to notify
   7495  1.1  christos      relocate_section that optimization can be done, and adjust got
   7496  1.1  christos      and plt refcounts.  */
   7497  1.1  christos   toc_ref = NULL;
   7498  1.1  christos   for (pass = 0; pass < 2; ++pass)
   7499  1.1  christos     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7500  1.1  christos       {
   7501  1.1  christos 	Elf_Internal_Sym *locsyms = NULL;
   7502  1.1  christos 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   7503  1.1  christos 
   7504  1.1  christos 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7505  1.1  christos 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   7506  1.1  christos 	    {
   7507  1.1  christos 	      Elf_Internal_Rela *relstart, *rel, *relend;
   7508  1.1  christos 	      bfd_boolean found_tls_get_addr_arg = 0;
   7509  1.1  christos 
   7510  1.1  christos 	      /* Read the relocations.  */
   7511  1.1  christos 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7512  1.1  christos 						    info->keep_memory);
   7513  1.1  christos 	      if (relstart == NULL)
   7514  1.1  christos 		return FALSE;
   7515  1.1  christos 
   7516  1.1  christos 	      relend = relstart + sec->reloc_count;
   7517  1.1  christos 	      for (rel = relstart; rel < relend; rel++)
   7518  1.1  christos 		{
   7519  1.1  christos 		  enum elf_ppc64_reloc_type r_type;
   7520  1.1  christos 		  unsigned long r_symndx;
   7521  1.1  christos 		  struct elf_link_hash_entry *h;
   7522  1.1  christos 		  Elf_Internal_Sym *sym;
   7523  1.1  christos 		  asection *sym_sec;
   7524  1.1  christos 		  unsigned char *tls_mask;
   7525  1.1  christos 		  unsigned char tls_set, tls_clear, tls_type = 0;
   7526  1.1  christos 		  bfd_vma value;
   7527  1.1  christos 		  bfd_boolean ok_tprel, is_local;
   7528  1.1  christos 		  long toc_ref_index = 0;
   7529  1.1  christos 		  int expecting_tls_get_addr = 0;
   7530  1.1  christos 		  bfd_boolean ret = FALSE;
   7531  1.1  christos 
   7532  1.1  christos 		  r_symndx = ELF64_R_SYM (rel->r_info);
   7533  1.1  christos 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   7534  1.1  christos 				  r_symndx, ibfd))
   7535  1.1  christos 		    {
   7536  1.1  christos 		    err_free_rel:
   7537  1.1  christos 		      if (elf_section_data (sec)->relocs != relstart)
   7538  1.1  christos 			free (relstart);
   7539  1.1  christos 		      if (toc_ref != NULL)
   7540  1.1  christos 			free (toc_ref);
   7541  1.1  christos 		      if (locsyms != NULL
   7542  1.1  christos 			  && (elf_symtab_hdr (ibfd).contents
   7543  1.1  christos 			      != (unsigned char *) locsyms))
   7544  1.1  christos 			free (locsyms);
   7545  1.1  christos 		      return ret;
   7546  1.1  christos 		    }
   7547  1.1  christos 
   7548  1.1  christos 		  if (h != NULL)
   7549  1.1  christos 		    {
   7550  1.1  christos 		      if (h->root.type == bfd_link_hash_defined
   7551  1.1  christos 			  || h->root.type == bfd_link_hash_defweak)
   7552  1.1  christos 			value = h->root.u.def.value;
   7553  1.1  christos 		      else if (h->root.type == bfd_link_hash_undefweak)
   7554  1.1  christos 			value = 0;
   7555  1.1  christos 		      else
   7556  1.1  christos 			{
   7557  1.1  christos 			  found_tls_get_addr_arg = 0;
   7558  1.1  christos 			  continue;
   7559  1.1  christos 			}
   7560  1.1  christos 		    }
   7561  1.1  christos 		  else
   7562  1.1  christos 		    /* Symbols referenced by TLS relocs must be of type
   7563  1.1  christos 		       STT_TLS.  So no need for .opd local sym adjust.  */
   7564  1.1  christos 		    value = sym->st_value;
   7565  1.1  christos 
   7566  1.1  christos 		  ok_tprel = FALSE;
   7567  1.1  christos 		  is_local = FALSE;
   7568  1.1  christos 		  if (h == NULL
   7569  1.1  christos 		      || !h->def_dynamic)
   7570  1.1  christos 		    {
   7571  1.1  christos 		      is_local = TRUE;
   7572  1.1  christos 		      if (h != NULL
   7573  1.1  christos 			  && h->root.type == bfd_link_hash_undefweak)
   7574  1.1  christos 			ok_tprel = TRUE;
   7575  1.1  christos 		      else
   7576  1.1  christos 			{
   7577  1.1  christos 			  value += sym_sec->output_offset;
   7578  1.1  christos 			  value += sym_sec->output_section->vma;
   7579  1.1  christos 			  value -= htab->elf.tls_sec->vma;
   7580  1.1  christos 			  ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
   7581  1.1  christos 				      < (bfd_vma) 1 << 32);
   7582  1.1  christos 			}
   7583  1.1  christos 		    }
   7584  1.1  christos 
   7585  1.1  christos 		  r_type = ELF64_R_TYPE (rel->r_info);
   7586  1.1  christos 		  /* If this section has old-style __tls_get_addr calls
   7587  1.1  christos 		     without marker relocs, then check that each
   7588  1.1  christos 		     __tls_get_addr call reloc is preceded by a reloc
   7589  1.1  christos 		     that conceivably belongs to the __tls_get_addr arg
   7590  1.1  christos 		     setup insn.  If we don't find matching arg setup
   7591  1.1  christos 		     relocs, don't do any tls optimization.  */
   7592  1.1  christos 		  if (pass == 0
   7593  1.1  christos 		      && sec->has_tls_get_addr_call
   7594  1.1  christos 		      && h != NULL
   7595  1.1  christos 		      && (h == &htab->tls_get_addr->elf
   7596  1.1  christos 			  || h == &htab->tls_get_addr_fd->elf)
   7597  1.1  christos 		      && !found_tls_get_addr_arg
   7598  1.1  christos 		      && is_branch_reloc (r_type))
   7599  1.1  christos 		    {
   7600  1.1  christos 		      info->callbacks->minfo (_("%C __tls_get_addr lost arg, "
   7601  1.1  christos 						"TLS optimization disabled\n"),
   7602  1.1  christos 					      ibfd, sec, rel->r_offset);
   7603  1.1  christos 		      ret = TRUE;
   7604  1.1  christos 		      goto err_free_rel;
   7605  1.1  christos 		    }
   7606  1.1  christos 
   7607  1.1  christos 		  found_tls_get_addr_arg = 0;
   7608  1.1  christos 		  switch (r_type)
   7609  1.1  christos 		    {
   7610  1.1  christos 		    case R_PPC64_GOT_TLSLD16:
   7611  1.1  christos 		    case R_PPC64_GOT_TLSLD16_LO:
   7612  1.1  christos 		      expecting_tls_get_addr = 1;
   7613  1.1  christos 		      found_tls_get_addr_arg = 1;
   7614  1.1  christos 		      /* Fall thru */
   7615  1.1  christos 
   7616  1.1  christos 		    case R_PPC64_GOT_TLSLD16_HI:
   7617  1.1  christos 		    case R_PPC64_GOT_TLSLD16_HA:
   7618  1.1  christos 		      /* These relocs should never be against a symbol
   7619  1.1  christos 			 defined in a shared lib.  Leave them alone if
   7620  1.1  christos 			 that turns out to be the case.  */
   7621  1.1  christos 		      if (!is_local)
   7622  1.1  christos 			continue;
   7623  1.1  christos 
   7624  1.1  christos 		      /* LD -> LE */
   7625  1.1  christos 		      tls_set = 0;
   7626  1.1  christos 		      tls_clear = TLS_LD;
   7627  1.1  christos 		      tls_type = TLS_TLS | TLS_LD;
   7628  1.1  christos 		      break;
   7629  1.1  christos 
   7630  1.1  christos 		    case R_PPC64_GOT_TLSGD16:
   7631  1.1  christos 		    case R_PPC64_GOT_TLSGD16_LO:
   7632  1.1  christos 		      expecting_tls_get_addr = 1;
   7633  1.1  christos 		      found_tls_get_addr_arg = 1;
   7634  1.1  christos 		      /* Fall thru */
   7635  1.1  christos 
   7636  1.1  christos 		    case R_PPC64_GOT_TLSGD16_HI:
   7637  1.1  christos 		    case R_PPC64_GOT_TLSGD16_HA:
   7638  1.1  christos 		      if (ok_tprel)
   7639  1.1  christos 			/* GD -> LE */
   7640  1.1  christos 			tls_set = 0;
   7641  1.1  christos 		      else
   7642  1.1  christos 			/* GD -> IE */
   7643  1.1  christos 			tls_set = TLS_TLS | TLS_TPRELGD;
   7644  1.1  christos 		      tls_clear = TLS_GD;
   7645  1.1  christos 		      tls_type = TLS_TLS | TLS_GD;
   7646  1.1  christos 		      break;
   7647  1.1  christos 
   7648  1.1  christos 		    case R_PPC64_GOT_TPREL16_DS:
   7649  1.1  christos 		    case R_PPC64_GOT_TPREL16_LO_DS:
   7650  1.1  christos 		    case R_PPC64_GOT_TPREL16_HI:
   7651  1.1  christos 		    case R_PPC64_GOT_TPREL16_HA:
   7652  1.1  christos 		      if (ok_tprel)
   7653  1.1  christos 			{
   7654  1.1  christos 			  /* IE -> LE */
   7655  1.1  christos 			  tls_set = 0;
   7656  1.1  christos 			  tls_clear = TLS_TPREL;
   7657  1.1  christos 			  tls_type = TLS_TLS | TLS_TPREL;
   7658  1.1  christos 			  break;
   7659  1.1  christos 			}
   7660  1.1  christos 		      continue;
   7661  1.1  christos 
   7662  1.1  christos 		    case R_PPC64_TLSGD:
   7663  1.1  christos 		    case R_PPC64_TLSLD:
   7664  1.1  christos 		      found_tls_get_addr_arg = 1;
   7665  1.1  christos 		      /* Fall thru */
   7666  1.1  christos 
   7667  1.1  christos 		    case R_PPC64_TLS:
   7668  1.1  christos 		    case R_PPC64_TOC16:
   7669  1.1  christos 		    case R_PPC64_TOC16_LO:
   7670  1.1  christos 		      if (sym_sec == NULL || sym_sec != toc)
   7671  1.1  christos 			continue;
   7672  1.1  christos 
   7673  1.1  christos 		      /* Mark this toc entry as referenced by a TLS
   7674  1.1  christos 			 code sequence.  We can do that now in the
   7675  1.1  christos 			 case of R_PPC64_TLS, and after checking for
   7676  1.1  christos 			 tls_get_addr for the TOC16 relocs.  */
   7677  1.1  christos 		      if (toc_ref == NULL)
   7678  1.1  christos 			toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
   7679  1.1  christos 		      if (toc_ref == NULL)
   7680  1.1  christos 			goto err_free_rel;
   7681  1.1  christos 
   7682  1.1  christos 		      if (h != NULL)
   7683  1.1  christos 			value = h->root.u.def.value;
   7684  1.1  christos 		      else
   7685  1.1  christos 			value = sym->st_value;
   7686  1.1  christos 		      value += rel->r_addend;
   7687  1.1  christos 		      BFD_ASSERT (value < toc->size && value % 8 == 0);
   7688  1.1  christos 		      toc_ref_index = (value + toc->output_offset) / 8;
   7689  1.1  christos 		      if (r_type == R_PPC64_TLS
   7690  1.1  christos 			  || r_type == R_PPC64_TLSGD
   7691  1.1  christos 			  || r_type == R_PPC64_TLSLD)
   7692  1.1  christos 			{
   7693  1.1  christos 			  toc_ref[toc_ref_index] = 1;
   7694  1.1  christos 			  continue;
   7695  1.1  christos 			}
   7696  1.1  christos 
   7697  1.1  christos 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   7698  1.1  christos 			continue;
   7699  1.1  christos 
   7700  1.1  christos 		      tls_set = 0;
   7701  1.1  christos 		      tls_clear = 0;
   7702  1.1  christos 		      expecting_tls_get_addr = 2;
   7703  1.1  christos 		      break;
   7704  1.1  christos 
   7705  1.1  christos 		    case R_PPC64_TPREL64:
   7706  1.1  christos 		      if (pass == 0
   7707  1.1  christos 			  || sec != toc
   7708  1.1  christos 			  || toc_ref == NULL
   7709  1.1  christos 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7710  1.1  christos 			continue;
   7711  1.1  christos 		      if (ok_tprel)
   7712  1.1  christos 			{
   7713  1.1  christos 			  /* IE -> LE */
   7714  1.1  christos 			  tls_set = TLS_EXPLICIT;
   7715  1.1  christos 			  tls_clear = TLS_TPREL;
   7716  1.1  christos 			  break;
   7717  1.1  christos 			}
   7718  1.1  christos 		      continue;
   7719  1.1  christos 
   7720  1.1  christos 		    case R_PPC64_DTPMOD64:
   7721  1.1  christos 		      if (pass == 0
   7722  1.1  christos 			  || sec != toc
   7723  1.1  christos 			  || toc_ref == NULL
   7724  1.1  christos 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7725  1.1  christos 			continue;
   7726  1.1  christos 		      if (rel + 1 < relend
   7727  1.1  christos 			  && (rel[1].r_info
   7728  1.1  christos 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   7729  1.1  christos 			  && rel[1].r_offset == rel->r_offset + 8)
   7730  1.1  christos 			{
   7731  1.1  christos 			  if (ok_tprel)
   7732  1.1  christos 			    /* GD -> LE */
   7733  1.1  christos 			    tls_set = TLS_EXPLICIT | TLS_GD;
   7734  1.1  christos 			  else
   7735  1.1  christos 			    /* GD -> IE */
   7736  1.1  christos 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
   7737  1.1  christos 			  tls_clear = TLS_GD;
   7738  1.1  christos 			}
   7739  1.1  christos 		      else
   7740  1.1  christos 			{
   7741  1.1  christos 			  if (!is_local)
   7742  1.1  christos 			    continue;
   7743  1.1  christos 
   7744  1.1  christos 			  /* LD -> LE */
   7745  1.1  christos 			  tls_set = TLS_EXPLICIT;
   7746  1.1  christos 			  tls_clear = TLS_LD;
   7747  1.1  christos 			}
   7748  1.1  christos 		      break;
   7749  1.1  christos 
   7750  1.1  christos 		    default:
   7751  1.1  christos 		      continue;
   7752  1.1  christos 		    }
   7753  1.1  christos 
   7754  1.1  christos 		  if (pass == 0)
   7755  1.1  christos 		    {
   7756  1.1  christos 		      if (!expecting_tls_get_addr
   7757  1.1  christos 			  || !sec->has_tls_get_addr_call)
   7758  1.1  christos 			continue;
   7759  1.1  christos 
   7760  1.1  christos 		      if (rel + 1 < relend
   7761  1.1  christos 			  && branch_reloc_hash_match (ibfd, rel + 1,
   7762  1.1  christos 						      htab->tls_get_addr,
   7763  1.1  christos 						      htab->tls_get_addr_fd))
   7764  1.1  christos 			{
   7765  1.1  christos 			  if (expecting_tls_get_addr == 2)
   7766  1.1  christos 			    {
   7767  1.1  christos 			      /* Check for toc tls entries.  */
   7768  1.1  christos 			      unsigned char *toc_tls;
   7769  1.1  christos 			      int retval;
   7770  1.1  christos 
   7771  1.1  christos 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   7772  1.1  christos 						     &locsyms,
   7773  1.1  christos 						     rel, ibfd);
   7774  1.1  christos 			      if (retval == 0)
   7775  1.1  christos 				goto err_free_rel;
   7776  1.1  christos 			      if (toc_tls != NULL)
   7777  1.1  christos 				{
   7778  1.1  christos 				  if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
   7779  1.1  christos 				    found_tls_get_addr_arg = 1;
   7780  1.1  christos 				  if (retval > 1)
   7781  1.1  christos 				    toc_ref[toc_ref_index] = 1;
   7782  1.1  christos 				}
   7783  1.1  christos 			    }
   7784  1.1  christos 			  continue;
   7785  1.1  christos 			}
   7786  1.1  christos 
   7787  1.1  christos 		      if (expecting_tls_get_addr != 1)
   7788  1.1  christos 			continue;
   7789  1.1  christos 
   7790  1.1  christos 		      /* Uh oh, we didn't find the expected call.  We
   7791  1.1  christos 			 could just mark this symbol to exclude it
   7792  1.1  christos 			 from tls optimization but it's safer to skip
   7793  1.1  christos 			 the entire optimization.  */
   7794  1.1  christos 		      info->callbacks->minfo (_("%C arg lost __tls_get_addr, "
   7795  1.1  christos 						"TLS optimization disabled\n"),
   7796  1.1  christos 					      ibfd, sec, rel->r_offset);
   7797  1.1  christos 		      ret = TRUE;
   7798  1.1  christos 		      goto err_free_rel;
   7799  1.1  christos 		    }
   7800  1.1  christos 
   7801  1.1  christos 		  if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
   7802  1.1  christos 		    {
   7803  1.1  christos 		      struct plt_entry *ent;
   7804  1.1  christos 		      for (ent = htab->tls_get_addr->elf.plt.plist;
   7805  1.1  christos 			   ent != NULL;
   7806  1.1  christos 			   ent = ent->next)
   7807  1.1  christos 			if (ent->addend == 0)
   7808  1.1  christos 			  {
   7809  1.1  christos 			    if (ent->plt.refcount > 0)
   7810  1.1  christos 			      {
   7811  1.1  christos 				ent->plt.refcount -= 1;
   7812  1.1  christos 				expecting_tls_get_addr = 0;
   7813  1.1  christos 			      }
   7814  1.1  christos 			    break;
   7815  1.1  christos 			  }
   7816  1.1  christos 		    }
   7817  1.1  christos 
   7818  1.1  christos 		  if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
   7819  1.1  christos 		    {
   7820  1.1  christos 		      struct plt_entry *ent;
   7821  1.1  christos 		      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   7822  1.1  christos 			   ent != NULL;
   7823  1.1  christos 			   ent = ent->next)
   7824  1.1  christos 			if (ent->addend == 0)
   7825  1.1  christos 			  {
   7826  1.1  christos 			    if (ent->plt.refcount > 0)
   7827  1.1  christos 			      ent->plt.refcount -= 1;
   7828  1.1  christos 			    break;
   7829  1.1  christos 			  }
   7830  1.1  christos 		    }
   7831  1.1  christos 
   7832  1.1  christos 		  if (tls_clear == 0)
   7833  1.1  christos 		    continue;
   7834  1.1  christos 
   7835  1.1  christos 		  if ((tls_set & TLS_EXPLICIT) == 0)
   7836  1.1  christos 		    {
   7837  1.1  christos 		      struct got_entry *ent;
   7838  1.1  christos 
   7839  1.1  christos 		      /* Adjust got entry for this reloc.  */
   7840  1.1  christos 		      if (h != NULL)
   7841  1.1  christos 			ent = h->got.glist;
   7842  1.1  christos 		      else
   7843  1.1  christos 			ent = elf_local_got_ents (ibfd)[r_symndx];
   7844  1.1  christos 
   7845  1.1  christos 		      for (; ent != NULL; ent = ent->next)
   7846  1.1  christos 			if (ent->addend == rel->r_addend
   7847  1.1  christos 			    && ent->owner == ibfd
   7848  1.1  christos 			    && ent->tls_type == tls_type)
   7849  1.1  christos 			  break;
   7850  1.1  christos 		      if (ent == NULL)
   7851  1.1  christos 			abort ();
   7852  1.1  christos 
   7853  1.1  christos 		      if (tls_set == 0)
   7854  1.1  christos 			{
   7855  1.1  christos 			  /* We managed to get rid of a got entry.  */
   7856  1.1  christos 			  if (ent->got.refcount > 0)
   7857  1.1  christos 			    ent->got.refcount -= 1;
   7858  1.1  christos 			}
   7859  1.1  christos 		    }
   7860  1.1  christos 		  else
   7861  1.1  christos 		    {
   7862  1.1  christos 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   7863  1.1  christos 			 we'll lose one or two dyn relocs.  */
   7864  1.1  christos 		      if (!dec_dynrel_count (rel->r_info, sec, info,
   7865  1.1  christos 					     NULL, h, sym_sec))
   7866  1.1  christos 			return FALSE;
   7867  1.1  christos 
   7868  1.1  christos 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   7869  1.1  christos 			{
   7870  1.1  christos 			  if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
   7871  1.1  christos 						 NULL, h, sym_sec))
   7872  1.1  christos 			    return FALSE;
   7873  1.1  christos 			}
   7874  1.1  christos 		    }
   7875  1.1  christos 
   7876  1.1  christos 		  *tls_mask |= tls_set;
   7877  1.1  christos 		  *tls_mask &= ~tls_clear;
   7878  1.1  christos 		}
   7879  1.1  christos 
   7880  1.1  christos 	      if (elf_section_data (sec)->relocs != relstart)
   7881  1.1  christos 		free (relstart);
   7882  1.1  christos 	    }
   7883  1.1  christos 
   7884  1.1  christos 	if (locsyms != NULL
   7885  1.1  christos 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   7886  1.1  christos 	  {
   7887  1.1  christos 	    if (!info->keep_memory)
   7888  1.1  christos 	      free (locsyms);
   7889  1.1  christos 	    else
   7890  1.1  christos 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   7891  1.1  christos 	  }
   7892  1.1  christos       }
   7893  1.1  christos 
   7894  1.1  christos   if (toc_ref != NULL)
   7895  1.1  christos     free (toc_ref);
   7896  1.1  christos   return TRUE;
   7897  1.1  christos }
   7898  1.1  christos 
   7899  1.1  christos /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   7900  1.1  christos    the values of any global symbols in a toc section that has been
   7901  1.1  christos    edited.  Globals in toc sections should be a rarity, so this function
   7902  1.1  christos    sets a flag if any are found in toc sections other than the one just
   7903  1.1  christos    edited, so that futher hash table traversals can be avoided.  */
   7904  1.1  christos 
   7905  1.1  christos struct adjust_toc_info
   7906  1.1  christos {
   7907  1.1  christos   asection *toc;
   7908  1.1  christos   unsigned long *skip;
   7909  1.1  christos   bfd_boolean global_toc_syms;
   7910  1.1  christos };
   7911  1.1  christos 
   7912  1.1  christos enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   7913  1.1  christos 
   7914  1.1  christos static bfd_boolean
   7915  1.1  christos adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   7916  1.1  christos {
   7917  1.1  christos   struct ppc_link_hash_entry *eh;
   7918  1.1  christos   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   7919  1.1  christos   unsigned long i;
   7920  1.1  christos 
   7921  1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   7922  1.1  christos     return TRUE;
   7923  1.1  christos 
   7924  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   7925  1.1  christos     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   7926  1.1  christos 
   7927  1.1  christos   if (h->root.type != bfd_link_hash_defined
   7928  1.1  christos       && h->root.type != bfd_link_hash_defweak)
   7929  1.1  christos     return TRUE;
   7930  1.1  christos 
   7931  1.1  christos   eh = (struct ppc_link_hash_entry *) h;
   7932  1.1  christos   if (eh->adjust_done)
   7933  1.1  christos     return TRUE;
   7934  1.1  christos 
   7935  1.1  christos   if (eh->elf.root.u.def.section == toc_inf->toc)
   7936  1.1  christos     {
   7937  1.1  christos       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   7938  1.1  christos 	i = toc_inf->toc->rawsize >> 3;
   7939  1.1  christos       else
   7940  1.1  christos 	i = eh->elf.root.u.def.value >> 3;
   7941  1.1  christos 
   7942  1.1  christos       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   7943  1.1  christos 	{
   7944  1.1  christos 	  (*_bfd_error_handler)
   7945  1.1  christos 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   7946  1.1  christos 	  do
   7947  1.1  christos 	    ++i;
   7948  1.1  christos 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   7949  1.1  christos 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   7950  1.1  christos 	}
   7951  1.1  christos 
   7952  1.1  christos       eh->elf.root.u.def.value -= toc_inf->skip[i];
   7953  1.1  christos       eh->adjust_done = 1;
   7954  1.1  christos     }
   7955  1.1  christos   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   7956  1.1  christos     toc_inf->global_toc_syms = TRUE;
   7957  1.1  christos 
   7958  1.1  christos   return TRUE;
   7959  1.1  christos }
   7960  1.1  christos 
   7961  1.1  christos /* Examine all relocs referencing .toc sections in order to remove
   7962  1.1  christos    unused .toc entries.  */
   7963  1.1  christos 
   7964  1.1  christos bfd_boolean
   7965  1.1  christos ppc64_elf_edit_toc (struct bfd_link_info *info)
   7966  1.1  christos {
   7967  1.1  christos   bfd *ibfd;
   7968  1.1  christos   struct adjust_toc_info toc_inf;
   7969  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   7970  1.1  christos 
   7971  1.1  christos   htab->do_toc_opt = 1;
   7972  1.1  christos   toc_inf.global_toc_syms = TRUE;
   7973  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7974  1.1  christos     {
   7975  1.1  christos       asection *toc, *sec;
   7976  1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   7977  1.1  christos       Elf_Internal_Sym *local_syms;
   7978  1.1  christos       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   7979  1.1  christos       unsigned long *skip, *drop;
   7980  1.1  christos       unsigned char *used;
   7981  1.1  christos       unsigned char *keep, last, some_unused;
   7982  1.1  christos 
   7983  1.1  christos       if (!is_ppc64_elf (ibfd))
   7984  1.1  christos 	continue;
   7985  1.1  christos 
   7986  1.1  christos       toc = bfd_get_section_by_name (ibfd, ".toc");
   7987  1.1  christos       if (toc == NULL
   7988  1.1  christos 	  || toc->size == 0
   7989  1.1  christos 	  || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
   7990  1.1  christos 	  || elf_discarded_section (toc))
   7991  1.1  christos 	continue;
   7992  1.1  christos 
   7993  1.1  christos       toc_relocs = NULL;
   7994  1.1  christos       local_syms = NULL;
   7995  1.1  christos       symtab_hdr = &elf_symtab_hdr (ibfd);
   7996  1.1  christos 
   7997  1.1  christos       /* Look at sections dropped from the final link.  */
   7998  1.1  christos       skip = NULL;
   7999  1.1  christos       relstart = NULL;
   8000  1.1  christos       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8001  1.1  christos 	{
   8002  1.1  christos 	  if (sec->reloc_count == 0
   8003  1.1  christos 	      || !elf_discarded_section (sec)
   8004  1.1  christos 	      || get_opd_info (sec)
   8005  1.1  christos 	      || (sec->flags & SEC_ALLOC) == 0
   8006  1.1  christos 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8007  1.1  christos 	    continue;
   8008  1.1  christos 
   8009  1.1  christos 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
   8010  1.1  christos 	  if (relstart == NULL)
   8011  1.1  christos 	    goto error_ret;
   8012  1.1  christos 
   8013  1.1  christos 	  /* Run through the relocs to see which toc entries might be
   8014  1.1  christos 	     unused.  */
   8015  1.1  christos 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8016  1.1  christos 	    {
   8017  1.1  christos 	      enum elf_ppc64_reloc_type r_type;
   8018  1.1  christos 	      unsigned long r_symndx;
   8019  1.1  christos 	      asection *sym_sec;
   8020  1.1  christos 	      struct elf_link_hash_entry *h;
   8021  1.1  christos 	      Elf_Internal_Sym *sym;
   8022  1.1  christos 	      bfd_vma val;
   8023  1.1  christos 
   8024  1.1  christos 	      r_type = ELF64_R_TYPE (rel->r_info);
   8025  1.1  christos 	      switch (r_type)
   8026  1.1  christos 		{
   8027  1.1  christos 		default:
   8028  1.1  christos 		  continue;
   8029  1.1  christos 
   8030  1.1  christos 		case R_PPC64_TOC16:
   8031  1.1  christos 		case R_PPC64_TOC16_LO:
   8032  1.1  christos 		case R_PPC64_TOC16_HI:
   8033  1.1  christos 		case R_PPC64_TOC16_HA:
   8034  1.1  christos 		case R_PPC64_TOC16_DS:
   8035  1.1  christos 		case R_PPC64_TOC16_LO_DS:
   8036  1.1  christos 		  break;
   8037  1.1  christos 		}
   8038  1.1  christos 
   8039  1.1  christos 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8040  1.1  christos 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8041  1.1  christos 			      r_symndx, ibfd))
   8042  1.1  christos 		goto error_ret;
   8043  1.1  christos 
   8044  1.1  christos 	      if (sym_sec != toc)
   8045  1.1  christos 		continue;
   8046  1.1  christos 
   8047  1.1  christos 	      if (h != NULL)
   8048  1.1  christos 		val = h->root.u.def.value;
   8049  1.1  christos 	      else
   8050  1.1  christos 		val = sym->st_value;
   8051  1.1  christos 	      val += rel->r_addend;
   8052  1.1  christos 
   8053  1.1  christos 	      if (val >= toc->size)
   8054  1.1  christos 		continue;
   8055  1.1  christos 
   8056  1.1  christos 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8057  1.1  christos 		 If not, don't mark as unused.  */
   8058  1.1  christos 	      if (val & 7)
   8059  1.1  christos 		continue;
   8060  1.1  christos 
   8061  1.1  christos 	      if (skip == NULL)
   8062  1.1  christos 		{
   8063  1.1  christos 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8064  1.1  christos 		  if (skip == NULL)
   8065  1.1  christos 		    goto error_ret;
   8066  1.1  christos 		}
   8067  1.1  christos 
   8068  1.1  christos 	      skip[val >> 3] = ref_from_discarded;
   8069  1.1  christos 	    }
   8070  1.1  christos 
   8071  1.1  christos 	  if (elf_section_data (sec)->relocs != relstart)
   8072  1.1  christos 	    free (relstart);
   8073  1.1  christos 	}
   8074  1.1  christos 
   8075  1.1  christos       /* For largetoc loads of address constants, we can convert
   8076  1.1  christos 	 .  addis rx,2,addr@got@ha
   8077  1.1  christos 	 .  ld ry,addr@got@l(rx)
   8078  1.1  christos 	 to
   8079  1.1  christos 	 .  addis rx,2,addr@toc@ha
   8080  1.1  christos 	 .  addi ry,rx,addr@toc@l
   8081  1.1  christos 	 when addr is within 2G of the toc pointer.  This then means
   8082  1.1  christos 	 that the word storing "addr" in the toc is no longer needed.  */
   8083  1.1  christos 
   8084  1.1  christos       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   8085  1.1  christos 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   8086  1.1  christos 	  && toc->reloc_count != 0)
   8087  1.1  christos 	{
   8088  1.1  christos 	  /* Read toc relocs.  */
   8089  1.1  christos 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8090  1.1  christos 						  info->keep_memory);
   8091  1.1  christos 	  if (toc_relocs == NULL)
   8092  1.1  christos 	    goto error_ret;
   8093  1.1  christos 
   8094  1.1  christos 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8095  1.1  christos 	    {
   8096  1.1  christos 	      enum elf_ppc64_reloc_type r_type;
   8097  1.1  christos 	      unsigned long r_symndx;
   8098  1.1  christos 	      asection *sym_sec;
   8099  1.1  christos 	      struct elf_link_hash_entry *h;
   8100  1.1  christos 	      Elf_Internal_Sym *sym;
   8101  1.1  christos 	      bfd_vma val, addr;
   8102  1.1  christos 
   8103  1.1  christos 	      r_type = ELF64_R_TYPE (rel->r_info);
   8104  1.1  christos 	      if (r_type != R_PPC64_ADDR64)
   8105  1.1  christos 		continue;
   8106  1.1  christos 
   8107  1.1  christos 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8108  1.1  christos 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8109  1.1  christos 			      r_symndx, ibfd))
   8110  1.1  christos 		goto error_ret;
   8111  1.1  christos 
   8112  1.1  christos 	      if (sym_sec == NULL
   8113  1.1  christos 		  || elf_discarded_section (sym_sec))
   8114  1.1  christos 		continue;
   8115  1.1  christos 
   8116  1.1  christos 	      if (!SYMBOL_CALLS_LOCAL (info, h))
   8117  1.1  christos 		continue;
   8118  1.1  christos 
   8119  1.1  christos 	      if (h != NULL)
   8120  1.1  christos 		{
   8121  1.1  christos 		  if (h->type == STT_GNU_IFUNC)
   8122  1.1  christos 		    continue;
   8123  1.1  christos 		  val = h->root.u.def.value;
   8124  1.1  christos 		}
   8125  1.1  christos 	      else
   8126  1.1  christos 		{
   8127  1.1  christos 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   8128  1.1  christos 		    continue;
   8129  1.1  christos 		  val = sym->st_value;
   8130  1.1  christos 		}
   8131  1.1  christos 	      val += rel->r_addend;
   8132  1.1  christos 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   8133  1.1  christos 
   8134  1.1  christos 	      /* We don't yet know the exact toc pointer value, but we
   8135  1.1  christos 		 know it will be somewhere in the toc section.  Don't
   8136  1.1  christos 		 optimize if the difference from any possible toc
   8137  1.1  christos 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   8138  1.1  christos 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   8139  1.1  christos 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8140  1.1  christos 		continue;
   8141  1.1  christos 
   8142  1.1  christos 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   8143  1.1  christos 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8144  1.1  christos 		continue;
   8145  1.1  christos 
   8146  1.1  christos 	      if (skip == NULL)
   8147  1.1  christos 		{
   8148  1.1  christos 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8149  1.1  christos 		  if (skip == NULL)
   8150  1.1  christos 		    goto error_ret;
   8151  1.1  christos 		}
   8152  1.1  christos 
   8153  1.1  christos 	      skip[rel->r_offset >> 3]
   8154  1.1  christos 		|= can_optimize | ((rel - toc_relocs) << 2);
   8155  1.1  christos 	    }
   8156  1.1  christos 	}
   8157  1.1  christos 
   8158  1.1  christos       if (skip == NULL)
   8159  1.1  christos 	continue;
   8160  1.1  christos 
   8161  1.1  christos       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   8162  1.1  christos       if (used == NULL)
   8163  1.1  christos 	{
   8164  1.1  christos 	error_ret:
   8165  1.1  christos 	  if (local_syms != NULL
   8166  1.1  christos 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   8167  1.1  christos 	    free (local_syms);
   8168  1.1  christos 	  if (sec != NULL
   8169  1.1  christos 	      && relstart != NULL
   8170  1.1  christos 	      && elf_section_data (sec)->relocs != relstart)
   8171  1.1  christos 	    free (relstart);
   8172  1.1  christos 	  if (toc_relocs != NULL
   8173  1.1  christos 	      && elf_section_data (toc)->relocs != toc_relocs)
   8174  1.1  christos 	    free (toc_relocs);
   8175  1.1  christos 	  if (skip != NULL)
   8176  1.1  christos 	    free (skip);
   8177  1.1  christos 	  return FALSE;
   8178  1.1  christos 	}
   8179  1.1  christos 
   8180  1.1  christos       /* Now check all kept sections that might reference the toc.
   8181  1.1  christos 	 Check the toc itself last.  */
   8182  1.1  christos       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   8183  1.1  christos 		  : ibfd->sections);
   8184  1.1  christos 	   sec != NULL;
   8185  1.1  christos 	   sec = (sec == toc ? NULL
   8186  1.1  christos 		  : sec->next == NULL ? toc
   8187  1.1  christos 		  : sec->next == toc && toc->next ? toc->next
   8188  1.1  christos 		  : sec->next))
   8189  1.1  christos 	{
   8190  1.1  christos 	  int repeat;
   8191  1.1  christos 
   8192  1.1  christos 	  if (sec->reloc_count == 0
   8193  1.1  christos 	      || elf_discarded_section (sec)
   8194  1.1  christos 	      || get_opd_info (sec)
   8195  1.1  christos 	      || (sec->flags & SEC_ALLOC) == 0
   8196  1.1  christos 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8197  1.1  christos 	    continue;
   8198  1.1  christos 
   8199  1.1  christos 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8200  1.1  christos 						info->keep_memory);
   8201  1.1  christos 	  if (relstart == NULL)
   8202  1.1  christos 	    goto error_ret;
   8203  1.1  christos 
   8204  1.1  christos 	  /* Mark toc entries referenced as used.  */
   8205  1.1  christos 	  repeat = 0;
   8206  1.1  christos 	  do
   8207  1.1  christos 	    for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8208  1.1  christos 	      {
   8209  1.1  christos 		enum elf_ppc64_reloc_type r_type;
   8210  1.1  christos 		unsigned long r_symndx;
   8211  1.1  christos 		asection *sym_sec;
   8212  1.1  christos 		struct elf_link_hash_entry *h;
   8213  1.1  christos 		Elf_Internal_Sym *sym;
   8214  1.1  christos 		bfd_vma val;
   8215  1.1  christos 
   8216  1.1  christos 		r_type = ELF64_R_TYPE (rel->r_info);
   8217  1.1  christos 		switch (r_type)
   8218  1.1  christos 		  {
   8219  1.1  christos 		  case R_PPC64_TOC16:
   8220  1.1  christos 		  case R_PPC64_TOC16_LO:
   8221  1.1  christos 		  case R_PPC64_TOC16_HI:
   8222  1.1  christos 		  case R_PPC64_TOC16_HA:
   8223  1.1  christos 		  case R_PPC64_TOC16_DS:
   8224  1.1  christos 		  case R_PPC64_TOC16_LO_DS:
   8225  1.1  christos 		    /* In case we're taking addresses of toc entries.  */
   8226  1.1  christos 		  case R_PPC64_ADDR64:
   8227  1.1  christos 		    break;
   8228  1.1  christos 
   8229  1.1  christos 		  default:
   8230  1.1  christos 		    continue;
   8231  1.1  christos 		  }
   8232  1.1  christos 
   8233  1.1  christos 		r_symndx = ELF64_R_SYM (rel->r_info);
   8234  1.1  christos 		if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8235  1.1  christos 				r_symndx, ibfd))
   8236  1.1  christos 		  {
   8237  1.1  christos 		    free (used);
   8238  1.1  christos 		    goto error_ret;
   8239  1.1  christos 		  }
   8240  1.1  christos 
   8241  1.1  christos 		if (sym_sec != toc)
   8242  1.1  christos 		  continue;
   8243  1.1  christos 
   8244  1.1  christos 		if (h != NULL)
   8245  1.1  christos 		  val = h->root.u.def.value;
   8246  1.1  christos 		else
   8247  1.1  christos 		  val = sym->st_value;
   8248  1.1  christos 		val += rel->r_addend;
   8249  1.1  christos 
   8250  1.1  christos 		if (val >= toc->size)
   8251  1.1  christos 		  continue;
   8252  1.1  christos 
   8253  1.1  christos 		if ((skip[val >> 3] & can_optimize) != 0)
   8254  1.1  christos 		  {
   8255  1.1  christos 		    bfd_vma off;
   8256  1.1  christos 		    unsigned char opc;
   8257  1.1  christos 
   8258  1.1  christos 		    switch (r_type)
   8259  1.1  christos 		      {
   8260  1.1  christos 		      case R_PPC64_TOC16_HA:
   8261  1.1  christos 			break;
   8262  1.1  christos 
   8263  1.1  christos 		      case R_PPC64_TOC16_LO_DS:
   8264  1.1  christos 			off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
   8265  1.1  christos 			if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
   8266  1.1  christos 			  return FALSE;
   8267  1.1  christos 			if ((opc & (0x3f << 2)) == (58u << 2))
   8268  1.1  christos 			  break;
   8269  1.1  christos 			/* Fall thru */
   8270  1.1  christos 
   8271  1.1  christos 		      default:
   8272  1.1  christos 			/* Wrong sort of reloc, or not a ld.  We may
   8273  1.1  christos 			   as well clear ref_from_discarded too.  */
   8274  1.1  christos 			skip[val >> 3] = 0;
   8275  1.1  christos 		      }
   8276  1.1  christos 		  }
   8277  1.1  christos 
   8278  1.1  christos 		/* For the toc section, we only mark as used if
   8279  1.1  christos 		   this entry itself isn't unused.  */
   8280  1.1  christos 		if (sec == toc
   8281  1.1  christos 		    && !used[val >> 3]
   8282  1.1  christos 		    && (used[rel->r_offset >> 3]
   8283  1.1  christos 			|| !(skip[rel->r_offset >> 3] & ref_from_discarded)))
   8284  1.1  christos 		  /* Do all the relocs again, to catch reference
   8285  1.1  christos 		     chains.  */
   8286  1.1  christos 		  repeat = 1;
   8287  1.1  christos 
   8288  1.1  christos 		used[val >> 3] = 1;
   8289  1.1  christos 	      }
   8290  1.1  christos 	  while (repeat);
   8291  1.1  christos 
   8292  1.1  christos 	  if (elf_section_data (sec)->relocs != relstart)
   8293  1.1  christos 	    free (relstart);
   8294  1.1  christos 	}
   8295  1.1  christos 
   8296  1.1  christos       /* Merge the used and skip arrays.  Assume that TOC
   8297  1.1  christos 	 doublewords not appearing as either used or unused belong
   8298  1.1  christos 	 to to an entry more than one doubleword in size.  */
   8299  1.1  christos       for (drop = skip, keep = used, last = 0, some_unused = 0;
   8300  1.1  christos 	   drop < skip + (toc->size + 7) / 8;
   8301  1.1  christos 	   ++drop, ++keep)
   8302  1.1  christos 	{
   8303  1.1  christos 	  if (*keep)
   8304  1.1  christos 	    {
   8305  1.1  christos 	      *drop &= ~ref_from_discarded;
   8306  1.1  christos 	      if ((*drop & can_optimize) != 0)
   8307  1.1  christos 		some_unused = 1;
   8308  1.1  christos 	      last = 0;
   8309  1.1  christos 	    }
   8310  1.1  christos 	  else if (*drop)
   8311  1.1  christos 	    {
   8312  1.1  christos 	      some_unused = 1;
   8313  1.1  christos 	      last = ref_from_discarded;
   8314  1.1  christos 	    }
   8315  1.1  christos 	  else
   8316  1.1  christos 	    *drop = last;
   8317  1.1  christos 	}
   8318  1.1  christos 
   8319  1.1  christos       free (used);
   8320  1.1  christos 
   8321  1.1  christos       if (some_unused)
   8322  1.1  christos 	{
   8323  1.1  christos 	  bfd_byte *contents, *src;
   8324  1.1  christos 	  unsigned long off;
   8325  1.1  christos 	  Elf_Internal_Sym *sym;
   8326  1.1  christos 	  bfd_boolean local_toc_syms = FALSE;
   8327  1.1  christos 
   8328  1.1  christos 	  /* Shuffle the toc contents, and at the same time convert the
   8329  1.1  christos 	     skip array from booleans into offsets.  */
   8330  1.1  christos 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   8331  1.1  christos 	    goto error_ret;
   8332  1.1  christos 
   8333  1.1  christos 	  elf_section_data (toc)->this_hdr.contents = contents;
   8334  1.1  christos 
   8335  1.1  christos 	  for (src = contents, off = 0, drop = skip;
   8336  1.1  christos 	       src < contents + toc->size;
   8337  1.1  christos 	       src += 8, ++drop)
   8338  1.1  christos 	    {
   8339  1.1  christos 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   8340  1.1  christos 		off += 8;
   8341  1.1  christos 	      else if (off != 0)
   8342  1.1  christos 		{
   8343  1.1  christos 		  *drop = off;
   8344  1.1  christos 		  memcpy (src - off, src, 8);
   8345  1.1  christos 		}
   8346  1.1  christos 	    }
   8347  1.1  christos 	  *drop = off;
   8348  1.1  christos 	  toc->rawsize = toc->size;
   8349  1.1  christos 	  toc->size = src - contents - off;
   8350  1.1  christos 
   8351  1.1  christos 	  /* Adjust addends for relocs against the toc section sym,
   8352  1.1  christos 	     and optimize any accesses we can.  */
   8353  1.1  christos 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8354  1.1  christos 	    {
   8355  1.1  christos 	      if (sec->reloc_count == 0
   8356  1.1  christos 		  || elf_discarded_section (sec))
   8357  1.1  christos 		continue;
   8358  1.1  christos 
   8359  1.1  christos 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8360  1.1  christos 						    info->keep_memory);
   8361  1.1  christos 	      if (relstart == NULL)
   8362  1.1  christos 		goto error_ret;
   8363  1.1  christos 
   8364  1.1  christos 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8365  1.1  christos 		{
   8366  1.1  christos 		  enum elf_ppc64_reloc_type r_type;
   8367  1.1  christos 		  unsigned long r_symndx;
   8368  1.1  christos 		  asection *sym_sec;
   8369  1.1  christos 		  struct elf_link_hash_entry *h;
   8370  1.1  christos 		  bfd_vma val;
   8371  1.1  christos 
   8372  1.1  christos 		  r_type = ELF64_R_TYPE (rel->r_info);
   8373  1.1  christos 		  switch (r_type)
   8374  1.1  christos 		    {
   8375  1.1  christos 		    default:
   8376  1.1  christos 		      continue;
   8377  1.1  christos 
   8378  1.1  christos 		    case R_PPC64_TOC16:
   8379  1.1  christos 		    case R_PPC64_TOC16_LO:
   8380  1.1  christos 		    case R_PPC64_TOC16_HI:
   8381  1.1  christos 		    case R_PPC64_TOC16_HA:
   8382  1.1  christos 		    case R_PPC64_TOC16_DS:
   8383  1.1  christos 		    case R_PPC64_TOC16_LO_DS:
   8384  1.1  christos 		    case R_PPC64_ADDR64:
   8385  1.1  christos 		      break;
   8386  1.1  christos 		    }
   8387  1.1  christos 
   8388  1.1  christos 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8389  1.1  christos 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8390  1.1  christos 				  r_symndx, ibfd))
   8391  1.1  christos 		    goto error_ret;
   8392  1.1  christos 
   8393  1.1  christos 		  if (sym_sec != toc)
   8394  1.1  christos 		    continue;
   8395  1.1  christos 
   8396  1.1  christos 		  if (h != NULL)
   8397  1.1  christos 		    val = h->root.u.def.value;
   8398  1.1  christos 		  else
   8399  1.1  christos 		    {
   8400  1.1  christos 		      val = sym->st_value;
   8401  1.1  christos 		      if (val != 0)
   8402  1.1  christos 			local_toc_syms = TRUE;
   8403  1.1  christos 		    }
   8404  1.1  christos 
   8405  1.1  christos 		  val += rel->r_addend;
   8406  1.1  christos 
   8407  1.1  christos 		  if (val > toc->rawsize)
   8408  1.1  christos 		    val = toc->rawsize;
   8409  1.1  christos 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   8410  1.1  christos 		    continue;
   8411  1.1  christos 		  else if ((skip[val >> 3] & can_optimize) != 0)
   8412  1.1  christos 		    {
   8413  1.1  christos 		      Elf_Internal_Rela *tocrel
   8414  1.1  christos 			= toc_relocs + (skip[val >> 3] >> 2);
   8415  1.1  christos 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   8416  1.1  christos 
   8417  1.1  christos 		      switch (r_type)
   8418  1.1  christos 			{
   8419  1.1  christos 			case R_PPC64_TOC16_HA:
   8420  1.1  christos 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   8421  1.1  christos 			  break;
   8422  1.1  christos 
   8423  1.1  christos 			case R_PPC64_TOC16_LO_DS:
   8424  1.1  christos 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   8425  1.1  christos 			  break;
   8426  1.1  christos 
   8427  1.1  christos 			default:
   8428  1.1  christos 			  abort ();
   8429  1.1  christos 			}
   8430  1.1  christos 		      rel->r_addend = tocrel->r_addend;
   8431  1.1  christos 		      elf_section_data (sec)->relocs = relstart;
   8432  1.1  christos 		      continue;
   8433  1.1  christos 		    }
   8434  1.1  christos 
   8435  1.1  christos 		  if (h != NULL || sym->st_value != 0)
   8436  1.1  christos 		    continue;
   8437  1.1  christos 
   8438  1.1  christos 		  rel->r_addend -= skip[val >> 3];
   8439  1.1  christos 		  elf_section_data (sec)->relocs = relstart;
   8440  1.1  christos 		}
   8441  1.1  christos 
   8442  1.1  christos 	      if (elf_section_data (sec)->relocs != relstart)
   8443  1.1  christos 		free (relstart);
   8444  1.1  christos 	    }
   8445  1.1  christos 
   8446  1.1  christos 	  /* We shouldn't have local or global symbols defined in the TOC,
   8447  1.1  christos 	     but handle them anyway.  */
   8448  1.1  christos 	  if (local_syms != NULL)
   8449  1.1  christos 	    for (sym = local_syms;
   8450  1.1  christos 		 sym < local_syms + symtab_hdr->sh_info;
   8451  1.1  christos 		 ++sym)
   8452  1.1  christos 	      if (sym->st_value != 0
   8453  1.1  christos 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   8454  1.1  christos 		{
   8455  1.1  christos 		  unsigned long i;
   8456  1.1  christos 
   8457  1.1  christos 		  if (sym->st_value > toc->rawsize)
   8458  1.1  christos 		    i = toc->rawsize >> 3;
   8459  1.1  christos 		  else
   8460  1.1  christos 		    i = sym->st_value >> 3;
   8461  1.1  christos 
   8462  1.1  christos 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8463  1.1  christos 		    {
   8464  1.1  christos 		      if (local_toc_syms)
   8465  1.1  christos 			(*_bfd_error_handler)
   8466  1.1  christos 			  (_("%s defined on removed toc entry"),
   8467  1.1  christos 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   8468  1.1  christos 		      do
   8469  1.1  christos 			++i;
   8470  1.1  christos 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   8471  1.1  christos 		      sym->st_value = (bfd_vma) i << 3;
   8472  1.1  christos 		    }
   8473  1.1  christos 
   8474  1.1  christos 		  sym->st_value -= skip[i];
   8475  1.1  christos 		  symtab_hdr->contents = (unsigned char *) local_syms;
   8476  1.1  christos 		}
   8477  1.1  christos 
   8478  1.1  christos 	  /* Adjust any global syms defined in this toc input section.  */
   8479  1.1  christos 	  if (toc_inf.global_toc_syms)
   8480  1.1  christos 	    {
   8481  1.1  christos 	      toc_inf.toc = toc;
   8482  1.1  christos 	      toc_inf.skip = skip;
   8483  1.1  christos 	      toc_inf.global_toc_syms = FALSE;
   8484  1.1  christos 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   8485  1.1  christos 				      &toc_inf);
   8486  1.1  christos 	    }
   8487  1.1  christos 
   8488  1.1  christos 	  if (toc->reloc_count != 0)
   8489  1.1  christos 	    {
   8490  1.1  christos 	      Elf_Internal_Shdr *rel_hdr;
   8491  1.1  christos 	      Elf_Internal_Rela *wrel;
   8492  1.1  christos 	      bfd_size_type sz;
   8493  1.1  christos 
   8494  1.1  christos 	      /* Remove unused toc relocs, and adjust those we keep.  */
   8495  1.1  christos 	      if (toc_relocs == NULL)
   8496  1.1  christos 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8497  1.1  christos 							info->keep_memory);
   8498  1.1  christos 	      if (toc_relocs == NULL)
   8499  1.1  christos 		goto error_ret;
   8500  1.1  christos 
   8501  1.1  christos 	      wrel = toc_relocs;
   8502  1.1  christos 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8503  1.1  christos 		if ((skip[rel->r_offset >> 3]
   8504  1.1  christos 		     & (ref_from_discarded | can_optimize)) == 0)
   8505  1.1  christos 		  {
   8506  1.1  christos 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   8507  1.1  christos 		    wrel->r_info = rel->r_info;
   8508  1.1  christos 		    wrel->r_addend = rel->r_addend;
   8509  1.1  christos 		    ++wrel;
   8510  1.1  christos 		  }
   8511  1.1  christos 		else if (!dec_dynrel_count (rel->r_info, toc, info,
   8512  1.1  christos 					    &local_syms, NULL, NULL))
   8513  1.1  christos 		  goto error_ret;
   8514  1.1  christos 
   8515  1.1  christos 	      elf_section_data (toc)->relocs = toc_relocs;
   8516  1.1  christos 	      toc->reloc_count = wrel - toc_relocs;
   8517  1.1  christos 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   8518  1.1  christos 	      sz = rel_hdr->sh_entsize;
   8519  1.1  christos 	      rel_hdr->sh_size = toc->reloc_count * sz;
   8520  1.1  christos 	    }
   8521  1.1  christos 	}
   8522  1.1  christos       else if (toc_relocs != NULL
   8523  1.1  christos 	       && elf_section_data (toc)->relocs != toc_relocs)
   8524  1.1  christos 	free (toc_relocs);
   8525  1.1  christos 
   8526  1.1  christos       if (local_syms != NULL
   8527  1.1  christos 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   8528  1.1  christos 	{
   8529  1.1  christos 	  if (!info->keep_memory)
   8530  1.1  christos 	    free (local_syms);
   8531  1.1  christos 	  else
   8532  1.1  christos 	    symtab_hdr->contents = (unsigned char *) local_syms;
   8533  1.1  christos 	}
   8534  1.1  christos       free (skip);
   8535  1.1  christos     }
   8536  1.1  christos 
   8537  1.1  christos   return TRUE;
   8538  1.1  christos }
   8539  1.1  christos 
   8540  1.1  christos /* Return true iff input section I references the TOC using
   8541  1.1  christos    instructions limited to +/-32k offsets.  */
   8542  1.1  christos 
   8543  1.1  christos bfd_boolean
   8544  1.1  christos ppc64_elf_has_small_toc_reloc (asection *i)
   8545  1.1  christos {
   8546  1.1  christos   return (is_ppc64_elf (i->owner)
   8547  1.1  christos 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   8548  1.1  christos }
   8549  1.1  christos 
   8550  1.1  christos /* Allocate space for one GOT entry.  */
   8551  1.1  christos 
   8552  1.1  christos static void
   8553  1.1  christos allocate_got (struct elf_link_hash_entry *h,
   8554  1.1  christos 	      struct bfd_link_info *info,
   8555  1.1  christos 	      struct got_entry *gent)
   8556  1.1  christos {
   8557  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8558  1.1  christos   bfd_boolean dyn;
   8559  1.1  christos   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   8560  1.1  christos   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   8561  1.1  christos 		 ? 16 : 8);
   8562  1.1  christos   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   8563  1.1  christos 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   8564  1.1  christos   asection *got = ppc64_elf_tdata (gent->owner)->got;
   8565  1.1  christos 
   8566  1.1  christos   gent->got.offset = got->size;
   8567  1.1  christos   got->size += entsize;
   8568  1.1  christos 
   8569  1.1  christos   dyn = htab->elf.dynamic_sections_created;
   8570  1.1  christos   if ((info->shared
   8571  1.1  christos        || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
   8572  1.1  christos 	    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   8573  1.1  christos 		|| h->root.type != bfd_link_hash_undefweak))
   8574  1.1  christos     {
   8575  1.1  christos       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   8576  1.1  christos       relgot->size += rentsize;
   8577  1.1  christos     }
   8578  1.1  christos   else if (h->type == STT_GNU_IFUNC)
   8579  1.1  christos     {
   8580  1.1  christos       asection *relgot = htab->reliplt;
   8581  1.1  christos       relgot->size += rentsize;
   8582  1.1  christos       htab->got_reli_size += rentsize;
   8583  1.1  christos     }
   8584  1.1  christos }
   8585  1.1  christos 
   8586  1.1  christos /* This function merges got entries in the same toc group.  */
   8587  1.1  christos 
   8588  1.1  christos static void
   8589  1.1  christos merge_got_entries (struct got_entry **pent)
   8590  1.1  christos {
   8591  1.1  christos   struct got_entry *ent, *ent2;
   8592  1.1  christos 
   8593  1.1  christos   for (ent = *pent; ent != NULL; ent = ent->next)
   8594  1.1  christos     if (!ent->is_indirect)
   8595  1.1  christos       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   8596  1.1  christos 	if (!ent2->is_indirect
   8597  1.1  christos 	    && ent2->addend == ent->addend
   8598  1.1  christos 	    && ent2->tls_type == ent->tls_type
   8599  1.1  christos 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   8600  1.1  christos 	  {
   8601  1.1  christos 	    ent2->is_indirect = TRUE;
   8602  1.1  christos 	    ent2->got.ent = ent;
   8603  1.1  christos 	  }
   8604  1.1  christos }
   8605  1.1  christos 
   8606  1.1  christos /* Allocate space in .plt, .got and associated reloc sections for
   8607  1.1  christos    dynamic relocs.  */
   8608  1.1  christos 
   8609  1.1  christos static bfd_boolean
   8610  1.1  christos allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8611  1.1  christos {
   8612  1.1  christos   struct bfd_link_info *info;
   8613  1.1  christos   struct ppc_link_hash_table *htab;
   8614  1.1  christos   asection *s;
   8615  1.1  christos   struct ppc_link_hash_entry *eh;
   8616  1.1  christos   struct ppc_dyn_relocs *p;
   8617  1.1  christos   struct got_entry **pgent, *gent;
   8618  1.1  christos 
   8619  1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   8620  1.1  christos     return TRUE;
   8621  1.1  christos 
   8622  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   8623  1.1  christos     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8624  1.1  christos 
   8625  1.1  christos   info = (struct bfd_link_info *) inf;
   8626  1.1  christos   htab = ppc_hash_table (info);
   8627  1.1  christos   if (htab == NULL)
   8628  1.1  christos     return FALSE;
   8629  1.1  christos 
   8630  1.1  christos   if ((htab->elf.dynamic_sections_created
   8631  1.1  christos        && h->dynindx != -1
   8632  1.1  christos        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
   8633  1.1  christos       || h->type == STT_GNU_IFUNC)
   8634  1.1  christos     {
   8635  1.1  christos       struct plt_entry *pent;
   8636  1.1  christos       bfd_boolean doneone = FALSE;
   8637  1.1  christos       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   8638  1.1  christos 	if (pent->plt.refcount > 0)
   8639  1.1  christos 	  {
   8640  1.1  christos 	    if (!htab->elf.dynamic_sections_created
   8641  1.1  christos 		|| h->dynindx == -1)
   8642  1.1  christos 	      {
   8643  1.1  christos 		s = htab->iplt;
   8644  1.1  christos 		pent->plt.offset = s->size;
   8645  1.1  christos 		s->size += PLT_ENTRY_SIZE;
   8646  1.1  christos 		s = htab->reliplt;
   8647  1.1  christos 	      }
   8648  1.1  christos 	    else
   8649  1.1  christos 	      {
   8650  1.1  christos 		/* If this is the first .plt entry, make room for the special
   8651  1.1  christos 		   first entry.  */
   8652  1.1  christos 		s = htab->plt;
   8653  1.1  christos 		if (s->size == 0)
   8654  1.1  christos 		  s->size += PLT_INITIAL_ENTRY_SIZE;
   8655  1.1  christos 
   8656  1.1  christos 		pent->plt.offset = s->size;
   8657  1.1  christos 
   8658  1.1  christos 		/* Make room for this entry.  */
   8659  1.1  christos 		s->size += PLT_ENTRY_SIZE;
   8660  1.1  christos 
   8661  1.1  christos 		/* Make room for the .glink code.  */
   8662  1.1  christos 		s = htab->glink;
   8663  1.1  christos 		if (s->size == 0)
   8664  1.1  christos 		  s->size += GLINK_CALL_STUB_SIZE;
   8665  1.1  christos 		/* We need bigger stubs past index 32767.  */
   8666  1.1  christos 		if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
   8667  1.1  christos 		  s->size += 4;
   8668  1.1  christos 		s->size += 2*4;
   8669  1.1  christos 
   8670  1.1  christos 		/* We also need to make an entry in the .rela.plt section.  */
   8671  1.1  christos 		s = htab->relplt;
   8672  1.1  christos 	      }
   8673  1.1  christos 	    s->size += sizeof (Elf64_External_Rela);
   8674  1.1  christos 	    doneone = TRUE;
   8675  1.1  christos 	  }
   8676  1.1  christos 	else
   8677  1.1  christos 	  pent->plt.offset = (bfd_vma) -1;
   8678  1.1  christos       if (!doneone)
   8679  1.1  christos 	{
   8680  1.1  christos 	  h->plt.plist = NULL;
   8681  1.1  christos 	  h->needs_plt = 0;
   8682  1.1  christos 	}
   8683  1.1  christos     }
   8684  1.1  christos   else
   8685  1.1  christos     {
   8686  1.1  christos       h->plt.plist = NULL;
   8687  1.1  christos       h->needs_plt = 0;
   8688  1.1  christos     }
   8689  1.1  christos 
   8690  1.1  christos   eh = (struct ppc_link_hash_entry *) h;
   8691  1.1  christos   /* Run through the TLS GD got entries first if we're changing them
   8692  1.1  christos      to TPREL.  */
   8693  1.1  christos   if ((eh->tls_mask & TLS_TPRELGD) != 0)
   8694  1.1  christos     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8695  1.1  christos       if (gent->got.refcount > 0
   8696  1.1  christos 	  && (gent->tls_type & TLS_GD) != 0)
   8697  1.1  christos 	{
   8698  1.1  christos 	  /* This was a GD entry that has been converted to TPREL.  If
   8699  1.1  christos 	     there happens to be a TPREL entry we can use that one.  */
   8700  1.1  christos 	  struct got_entry *ent;
   8701  1.1  christos 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   8702  1.1  christos 	    if (ent->got.refcount > 0
   8703  1.1  christos 		&& (ent->tls_type & TLS_TPREL) != 0
   8704  1.1  christos 		&& ent->addend == gent->addend
   8705  1.1  christos 		&& ent->owner == gent->owner)
   8706  1.1  christos 	      {
   8707  1.1  christos 		gent->got.refcount = 0;
   8708  1.1  christos 		break;
   8709  1.1  christos 	      }
   8710  1.1  christos 
   8711  1.1  christos 	  /* If not, then we'll be using our own TPREL entry.  */
   8712  1.1  christos 	  if (gent->got.refcount != 0)
   8713  1.1  christos 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   8714  1.1  christos 	}
   8715  1.1  christos 
   8716  1.1  christos   /* Remove any list entry that won't generate a word in the GOT before
   8717  1.1  christos      we call merge_got_entries.  Otherwise we risk merging to empty
   8718  1.1  christos      entries.  */
   8719  1.1  christos   pgent = &h->got.glist;
   8720  1.1  christos   while ((gent = *pgent) != NULL)
   8721  1.1  christos     if (gent->got.refcount > 0)
   8722  1.1  christos       {
   8723  1.1  christos 	if ((gent->tls_type & TLS_LD) != 0
   8724  1.1  christos 	    && !h->def_dynamic)
   8725  1.1  christos 	  {
   8726  1.1  christos 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   8727  1.1  christos 	    *pgent = gent->next;
   8728  1.1  christos 	  }
   8729  1.1  christos 	else
   8730  1.1  christos 	  pgent = &gent->next;
   8731  1.1  christos       }
   8732  1.1  christos     else
   8733  1.1  christos       *pgent = gent->next;
   8734  1.1  christos 
   8735  1.1  christos   if (!htab->do_multi_toc)
   8736  1.1  christos     merge_got_entries (&h->got.glist);
   8737  1.1  christos 
   8738  1.1  christos   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8739  1.1  christos     if (!gent->is_indirect)
   8740  1.1  christos       {
   8741  1.1  christos 	/* Make sure this symbol is output as a dynamic symbol.
   8742  1.1  christos 	   Undefined weak syms won't yet be marked as dynamic,
   8743  1.1  christos 	   nor will all TLS symbols.  */
   8744  1.1  christos 	if (h->dynindx == -1
   8745  1.1  christos 	    && !h->forced_local
   8746  1.1  christos 	    && h->type != STT_GNU_IFUNC
   8747  1.1  christos 	    && htab->elf.dynamic_sections_created)
   8748  1.1  christos 	  {
   8749  1.1  christos 	    if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8750  1.1  christos 	      return FALSE;
   8751  1.1  christos 	  }
   8752  1.1  christos 
   8753  1.1  christos 	if (!is_ppc64_elf (gent->owner))
   8754  1.1  christos 	  abort ();
   8755  1.1  christos 
   8756  1.1  christos 	allocate_got (h, info, gent);
   8757  1.1  christos       }
   8758  1.1  christos 
   8759  1.1  christos   if (eh->dyn_relocs == NULL
   8760  1.1  christos       || (!htab->elf.dynamic_sections_created
   8761  1.1  christos 	  && h->type != STT_GNU_IFUNC))
   8762  1.1  christos     return TRUE;
   8763  1.1  christos 
   8764  1.1  christos   /* In the shared -Bsymbolic case, discard space allocated for
   8765  1.1  christos      dynamic pc-relative relocs against symbols which turn out to be
   8766  1.1  christos      defined in regular objects.  For the normal shared case, discard
   8767  1.1  christos      space for relocs that have become local due to symbol visibility
   8768  1.1  christos      changes.  */
   8769  1.1  christos 
   8770  1.1  christos   if (info->shared)
   8771  1.1  christos     {
   8772  1.1  christos       /* Relocs that use pc_count are those that appear on a call insn,
   8773  1.1  christos 	 or certain REL relocs (see must_be_dyn_reloc) that can be
   8774  1.1  christos 	 generated via assembly.  We want calls to protected symbols to
   8775  1.1  christos 	 resolve directly to the function rather than going via the plt.
   8776  1.1  christos 	 If people want function pointer comparisons to work as expected
   8777  1.1  christos 	 then they should avoid writing weird assembly.  */
   8778  1.1  christos       if (SYMBOL_CALLS_LOCAL (info, h))
   8779  1.1  christos 	{
   8780  1.1  christos 	  struct ppc_dyn_relocs **pp;
   8781  1.1  christos 
   8782  1.1  christos 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   8783  1.1  christos 	    {
   8784  1.1  christos 	      p->count -= p->pc_count;
   8785  1.1  christos 	      p->pc_count = 0;
   8786  1.1  christos 	      if (p->count == 0)
   8787  1.1  christos 		*pp = p->next;
   8788  1.1  christos 	      else
   8789  1.1  christos 		pp = &p->next;
   8790  1.1  christos 	    }
   8791  1.1  christos 	}
   8792  1.1  christos 
   8793  1.1  christos       /* Also discard relocs on undefined weak syms with non-default
   8794  1.1  christos 	 visibility.  */
   8795  1.1  christos       if (eh->dyn_relocs != NULL
   8796  1.1  christos 	  && h->root.type == bfd_link_hash_undefweak)
   8797  1.1  christos 	{
   8798  1.1  christos 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   8799  1.1  christos 	    eh->dyn_relocs = NULL;
   8800  1.1  christos 
   8801  1.1  christos 	  /* Make sure this symbol is output as a dynamic symbol.
   8802  1.1  christos 	     Undefined weak syms won't yet be marked as dynamic.  */
   8803  1.1  christos 	  else if (h->dynindx == -1
   8804  1.1  christos 		   && !h->forced_local)
   8805  1.1  christos 	    {
   8806  1.1  christos 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8807  1.1  christos 		return FALSE;
   8808  1.1  christos 	    }
   8809  1.1  christos 	}
   8810  1.1  christos     }
   8811  1.1  christos   else if (h->type == STT_GNU_IFUNC)
   8812  1.1  christos     {
   8813  1.1  christos       if (!h->non_got_ref)
   8814  1.1  christos 	eh->dyn_relocs = NULL;
   8815  1.1  christos     }
   8816  1.1  christos   else if (ELIMINATE_COPY_RELOCS)
   8817  1.1  christos     {
   8818  1.1  christos       /* For the non-shared case, discard space for relocs against
   8819  1.1  christos 	 symbols which turn out to need copy relocs or are not
   8820  1.1  christos 	 dynamic.  */
   8821  1.1  christos 
   8822  1.1  christos       if (!h->non_got_ref
   8823  1.1  christos 	  && !h->def_regular)
   8824  1.1  christos 	{
   8825  1.1  christos 	  /* Make sure this symbol is output as a dynamic symbol.
   8826  1.1  christos 	     Undefined weak syms won't yet be marked as dynamic.  */
   8827  1.1  christos 	  if (h->dynindx == -1
   8828  1.1  christos 	      && !h->forced_local)
   8829  1.1  christos 	    {
   8830  1.1  christos 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8831  1.1  christos 		return FALSE;
   8832  1.1  christos 	    }
   8833  1.1  christos 
   8834  1.1  christos 	  /* If that succeeded, we know we'll be keeping all the
   8835  1.1  christos 	     relocs.  */
   8836  1.1  christos 	  if (h->dynindx != -1)
   8837  1.1  christos 	    goto keep;
   8838  1.1  christos 	}
   8839  1.1  christos 
   8840  1.1  christos       eh->dyn_relocs = NULL;
   8841  1.1  christos 
   8842  1.1  christos     keep: ;
   8843  1.1  christos     }
   8844  1.1  christos 
   8845  1.1  christos   /* Finally, allocate space.  */
   8846  1.1  christos   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   8847  1.1  christos     {
   8848  1.1  christos       asection *sreloc = elf_section_data (p->sec)->sreloc;
   8849  1.1  christos       if (!htab->elf.dynamic_sections_created)
   8850  1.1  christos 	sreloc = htab->reliplt;
   8851  1.1  christos       sreloc->size += p->count * sizeof (Elf64_External_Rela);
   8852  1.1  christos     }
   8853  1.1  christos 
   8854  1.1  christos   return TRUE;
   8855  1.1  christos }
   8856  1.1  christos 
   8857  1.1  christos /* Find any dynamic relocs that apply to read-only sections.  */
   8858  1.1  christos 
   8859  1.1  christos static bfd_boolean
   8860  1.1  christos readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8861  1.1  christos {
   8862  1.1  christos   struct ppc_link_hash_entry *eh;
   8863  1.1  christos   struct ppc_dyn_relocs *p;
   8864  1.1  christos 
   8865  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   8866  1.1  christos     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8867  1.1  christos 
   8868  1.1  christos   eh = (struct ppc_link_hash_entry *) h;
   8869  1.1  christos   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   8870  1.1  christos     {
   8871  1.1  christos       asection *s = p->sec->output_section;
   8872  1.1  christos 
   8873  1.1  christos       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   8874  1.1  christos 	{
   8875  1.1  christos 	  struct bfd_link_info *info = inf;
   8876  1.1  christos 
   8877  1.1  christos 	  info->flags |= DF_TEXTREL;
   8878  1.1  christos 
   8879  1.1  christos 	  /* Not an error, just cut short the traversal.  */
   8880  1.1  christos 	  return FALSE;
   8881  1.1  christos 	}
   8882  1.1  christos     }
   8883  1.1  christos   return TRUE;
   8884  1.1  christos }
   8885  1.1  christos 
   8886  1.1  christos /* Set the sizes of the dynamic sections.  */
   8887  1.1  christos 
   8888  1.1  christos static bfd_boolean
   8889  1.1  christos ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   8890  1.1  christos 				 struct bfd_link_info *info)
   8891  1.1  christos {
   8892  1.1  christos   struct ppc_link_hash_table *htab;
   8893  1.1  christos   bfd *dynobj;
   8894  1.1  christos   asection *s;
   8895  1.1  christos   bfd_boolean relocs;
   8896  1.1  christos   bfd *ibfd;
   8897  1.1  christos   struct got_entry *first_tlsld;
   8898  1.1  christos 
   8899  1.1  christos   htab = ppc_hash_table (info);
   8900  1.1  christos   if (htab == NULL)
   8901  1.1  christos     return FALSE;
   8902  1.1  christos 
   8903  1.1  christos   dynobj = htab->elf.dynobj;
   8904  1.1  christos   if (dynobj == NULL)
   8905  1.1  christos     abort ();
   8906  1.1  christos 
   8907  1.1  christos   if (htab->elf.dynamic_sections_created)
   8908  1.1  christos     {
   8909  1.1  christos       /* Set the contents of the .interp section to the interpreter.  */
   8910  1.1  christos       if (info->executable)
   8911  1.1  christos 	{
   8912  1.1  christos 	  s = bfd_get_section_by_name (dynobj, ".interp");
   8913  1.1  christos 	  if (s == NULL)
   8914  1.1  christos 	    abort ();
   8915  1.1  christos 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   8916  1.1  christos 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   8917  1.1  christos 	}
   8918  1.1  christos     }
   8919  1.1  christos 
   8920  1.1  christos   /* Set up .got offsets for local syms, and space for local dynamic
   8921  1.1  christos      relocs.  */
   8922  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   8923  1.1  christos     {
   8924  1.1  christos       struct got_entry **lgot_ents;
   8925  1.1  christos       struct got_entry **end_lgot_ents;
   8926  1.1  christos       struct plt_entry **local_plt;
   8927  1.1  christos       struct plt_entry **end_local_plt;
   8928  1.1  christos       unsigned char *lgot_masks;
   8929  1.1  christos       bfd_size_type locsymcount;
   8930  1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   8931  1.1  christos       asection *srel;
   8932  1.1  christos 
   8933  1.1  christos       if (!is_ppc64_elf (ibfd))
   8934  1.1  christos 	continue;
   8935  1.1  christos 
   8936  1.1  christos       for (s = ibfd->sections; s != NULL; s = s->next)
   8937  1.1  christos 	{
   8938  1.1  christos 	  struct ppc_dyn_relocs *p;
   8939  1.1  christos 
   8940  1.1  christos 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   8941  1.1  christos 	    {
   8942  1.1  christos 	      if (!bfd_is_abs_section (p->sec)
   8943  1.1  christos 		  && bfd_is_abs_section (p->sec->output_section))
   8944  1.1  christos 		{
   8945  1.1  christos 		  /* Input section has been discarded, either because
   8946  1.1  christos 		     it is a copy of a linkonce section or due to
   8947  1.1  christos 		     linker script /DISCARD/, so we'll be discarding
   8948  1.1  christos 		     the relocs too.  */
   8949  1.1  christos 		}
   8950  1.1  christos 	      else if (p->count != 0)
   8951  1.1  christos 		{
   8952  1.1  christos 		  srel = elf_section_data (p->sec)->sreloc;
   8953  1.1  christos 		  if (!htab->elf.dynamic_sections_created)
   8954  1.1  christos 		    srel = htab->reliplt;
   8955  1.1  christos 		  srel->size += p->count * sizeof (Elf64_External_Rela);
   8956  1.1  christos 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   8957  1.1  christos 		    info->flags |= DF_TEXTREL;
   8958  1.1  christos 		}
   8959  1.1  christos 	    }
   8960  1.1  christos 	}
   8961  1.1  christos 
   8962  1.1  christos       lgot_ents = elf_local_got_ents (ibfd);
   8963  1.1  christos       if (!lgot_ents)
   8964  1.1  christos 	continue;
   8965  1.1  christos 
   8966  1.1  christos       symtab_hdr = &elf_symtab_hdr (ibfd);
   8967  1.1  christos       locsymcount = symtab_hdr->sh_info;
   8968  1.1  christos       end_lgot_ents = lgot_ents + locsymcount;
   8969  1.1  christos       local_plt = (struct plt_entry **) end_lgot_ents;
   8970  1.1  christos       end_local_plt = local_plt + locsymcount;
   8971  1.1  christos       lgot_masks = (unsigned char *) end_local_plt;
   8972  1.1  christos       s = ppc64_elf_tdata (ibfd)->got;
   8973  1.1  christos       srel = ppc64_elf_tdata (ibfd)->relgot;
   8974  1.1  christos       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   8975  1.1  christos 	{
   8976  1.1  christos 	  struct got_entry **pent, *ent;
   8977  1.1  christos 
   8978  1.1  christos 	  pent = lgot_ents;
   8979  1.1  christos 	  while ((ent = *pent) != NULL)
   8980  1.1  christos 	    if (ent->got.refcount > 0)
   8981  1.1  christos 	      {
   8982  1.1  christos 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   8983  1.1  christos 		  {
   8984  1.1  christos 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   8985  1.1  christos 		    *pent = ent->next;
   8986  1.1  christos 		  }
   8987  1.1  christos 		else
   8988  1.1  christos 		  {
   8989  1.1  christos 		    unsigned int num = 1;
   8990  1.1  christos 		    ent->got.offset = s->size;
   8991  1.1  christos 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   8992  1.1  christos 		      num = 2;
   8993  1.1  christos 		    s->size += num * 8;
   8994  1.1  christos 		    if (info->shared)
   8995  1.1  christos 		      srel->size += num * sizeof (Elf64_External_Rela);
   8996  1.1  christos 		    else if ((*lgot_masks & PLT_IFUNC) != 0)
   8997  1.1  christos 		      {
   8998  1.1  christos 			htab->reliplt->size
   8999  1.1  christos 			  += num * sizeof (Elf64_External_Rela);
   9000  1.1  christos 			htab->got_reli_size
   9001  1.1  christos 			  += num * sizeof (Elf64_External_Rela);
   9002  1.1  christos 		      }
   9003  1.1  christos 		    pent = &ent->next;
   9004  1.1  christos 		  }
   9005  1.1  christos 	      }
   9006  1.1  christos 	    else
   9007  1.1  christos 	      *pent = ent->next;
   9008  1.1  christos 	}
   9009  1.1  christos 
   9010  1.1  christos       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   9011  1.1  christos       for (; local_plt < end_local_plt; ++local_plt)
   9012  1.1  christos 	{
   9013  1.1  christos 	  struct plt_entry *ent;
   9014  1.1  christos 
   9015  1.1  christos 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   9016  1.1  christos 	    if (ent->plt.refcount > 0)
   9017  1.1  christos 	      {
   9018  1.1  christos 		s = htab->iplt;
   9019  1.1  christos 		ent->plt.offset = s->size;
   9020  1.1  christos 		s->size += PLT_ENTRY_SIZE;
   9021  1.1  christos 
   9022  1.1  christos 		htab->reliplt->size += sizeof (Elf64_External_Rela);
   9023  1.1  christos 	      }
   9024  1.1  christos 	    else
   9025  1.1  christos 	      ent->plt.offset = (bfd_vma) -1;
   9026  1.1  christos 	}
   9027  1.1  christos     }
   9028  1.1  christos 
   9029  1.1  christos   /* Allocate global sym .plt and .got entries, and space for global
   9030  1.1  christos      sym dynamic relocs.  */
   9031  1.1  christos   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   9032  1.1  christos 
   9033  1.1  christos   first_tlsld = NULL;
   9034  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9035  1.1  christos     {
   9036  1.1  christos       struct got_entry *ent;
   9037  1.1  christos 
   9038  1.1  christos       if (!is_ppc64_elf (ibfd))
   9039  1.1  christos 	continue;
   9040  1.1  christos 
   9041  1.1  christos       ent = ppc64_tlsld_got (ibfd);
   9042  1.1  christos       if (ent->got.refcount > 0)
   9043  1.1  christos 	{
   9044  1.1  christos 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   9045  1.1  christos 	    {
   9046  1.1  christos 	      ent->is_indirect = TRUE;
   9047  1.1  christos 	      ent->got.ent = first_tlsld;
   9048  1.1  christos 	    }
   9049  1.1  christos 	  else
   9050  1.1  christos 	    {
   9051  1.1  christos 	      if (first_tlsld == NULL)
   9052  1.1  christos 		first_tlsld = ent;
   9053  1.1  christos 	      s = ppc64_elf_tdata (ibfd)->got;
   9054  1.1  christos 	      ent->got.offset = s->size;
   9055  1.1  christos 	      ent->owner = ibfd;
   9056  1.1  christos 	      s->size += 16;
   9057  1.1  christos 	      if (info->shared)
   9058  1.1  christos 		{
   9059  1.1  christos 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   9060  1.1  christos 		  srel->size += sizeof (Elf64_External_Rela);
   9061  1.1  christos 		}
   9062  1.1  christos 	    }
   9063  1.1  christos 	}
   9064  1.1  christos       else
   9065  1.1  christos 	ent->got.offset = (bfd_vma) -1;
   9066  1.1  christos     }
   9067  1.1  christos 
   9068  1.1  christos   /* We now have determined the sizes of the various dynamic sections.
   9069  1.1  christos      Allocate memory for them.  */
   9070  1.1  christos   relocs = FALSE;
   9071  1.1  christos   for (s = dynobj->sections; s != NULL; s = s->next)
   9072  1.1  christos     {
   9073  1.1  christos       if ((s->flags & SEC_LINKER_CREATED) == 0)
   9074  1.1  christos 	continue;
   9075  1.1  christos 
   9076  1.1  christos       if (s == htab->brlt || s == htab->relbrlt)
   9077  1.1  christos 	/* These haven't been allocated yet;  don't strip.  */
   9078  1.1  christos 	continue;
   9079  1.1  christos       else if (s == htab->got
   9080  1.1  christos 	       || s == htab->plt
   9081  1.1  christos 	       || s == htab->iplt
   9082  1.1  christos 	       || s == htab->glink
   9083  1.1  christos 	       || s == htab->dynbss)
   9084  1.1  christos 	{
   9085  1.1  christos 	  /* Strip this section if we don't need it; see the
   9086  1.1  christos 	     comment below.  */
   9087  1.1  christos 	}
   9088  1.1  christos       else if (CONST_STRNEQ (s->name, ".rela"))
   9089  1.1  christos 	{
   9090  1.1  christos 	  if (s->size != 0)
   9091  1.1  christos 	    {
   9092  1.1  christos 	      if (s != htab->relplt)
   9093  1.1  christos 		relocs = TRUE;
   9094  1.1  christos 
   9095  1.1  christos 	      /* We use the reloc_count field as a counter if we need
   9096  1.1  christos 		 to copy relocs into the output file.  */
   9097  1.1  christos 	      s->reloc_count = 0;
   9098  1.1  christos 	    }
   9099  1.1  christos 	}
   9100  1.1  christos       else
   9101  1.1  christos 	{
   9102  1.1  christos 	  /* It's not one of our sections, so don't allocate space.  */
   9103  1.1  christos 	  continue;
   9104  1.1  christos 	}
   9105  1.1  christos 
   9106  1.1  christos       if (s->size == 0)
   9107  1.1  christos 	{
   9108  1.1  christos 	  /* If we don't need this section, strip it from the
   9109  1.1  christos 	     output file.  This is mostly to handle .rela.bss and
   9110  1.1  christos 	     .rela.plt.  We must create both sections in
   9111  1.1  christos 	     create_dynamic_sections, because they must be created
   9112  1.1  christos 	     before the linker maps input sections to output
   9113  1.1  christos 	     sections.  The linker does that before
   9114  1.1  christos 	     adjust_dynamic_symbol is called, and it is that
   9115  1.1  christos 	     function which decides whether anything needs to go
   9116  1.1  christos 	     into these sections.  */
   9117  1.1  christos 	  s->flags |= SEC_EXCLUDE;
   9118  1.1  christos 	  continue;
   9119  1.1  christos 	}
   9120  1.1  christos 
   9121  1.1  christos       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   9122  1.1  christos 	continue;
   9123  1.1  christos 
   9124  1.1  christos       /* Allocate memory for the section contents.  We use bfd_zalloc
   9125  1.1  christos 	 here in case unused entries are not reclaimed before the
   9126  1.1  christos 	 section's contents are written out.  This should not happen,
   9127  1.1  christos 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   9128  1.1  christos 	 sections instead of garbage.
   9129  1.1  christos 	 We also rely on the section contents being zero when writing
   9130  1.1  christos 	 the GOT.  */
   9131  1.1  christos       s->contents = bfd_zalloc (dynobj, s->size);
   9132  1.1  christos       if (s->contents == NULL)
   9133  1.1  christos 	return FALSE;
   9134  1.1  christos     }
   9135  1.1  christos 
   9136  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9137  1.1  christos     {
   9138  1.1  christos       if (!is_ppc64_elf (ibfd))
   9139  1.1  christos 	continue;
   9140  1.1  christos 
   9141  1.1  christos       s = ppc64_elf_tdata (ibfd)->got;
   9142  1.1  christos       if (s != NULL && s != htab->got)
   9143  1.1  christos 	{
   9144  1.1  christos 	  if (s->size == 0)
   9145  1.1  christos 	    s->flags |= SEC_EXCLUDE;
   9146  1.1  christos 	  else
   9147  1.1  christos 	    {
   9148  1.1  christos 	      s->contents = bfd_zalloc (ibfd, s->size);
   9149  1.1  christos 	      if (s->contents == NULL)
   9150  1.1  christos 		return FALSE;
   9151  1.1  christos 	    }
   9152  1.1  christos 	}
   9153  1.1  christos       s = ppc64_elf_tdata (ibfd)->relgot;
   9154  1.1  christos       if (s != NULL)
   9155  1.1  christos 	{
   9156  1.1  christos 	  if (s->size == 0)
   9157  1.1  christos 	    s->flags |= SEC_EXCLUDE;
   9158  1.1  christos 	  else
   9159  1.1  christos 	    {
   9160  1.1  christos 	      s->contents = bfd_zalloc (ibfd, s->size);
   9161  1.1  christos 	      if (s->contents == NULL)
   9162  1.1  christos 		return FALSE;
   9163  1.1  christos 	      relocs = TRUE;
   9164  1.1  christos 	      s->reloc_count = 0;
   9165  1.1  christos 	    }
   9166  1.1  christos 	}
   9167  1.1  christos     }
   9168  1.1  christos 
   9169  1.1  christos   if (htab->elf.dynamic_sections_created)
   9170  1.1  christos     {
   9171  1.1  christos       /* Add some entries to the .dynamic section.  We fill in the
   9172  1.1  christos 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   9173  1.1  christos 	 must add the entries now so that we get the correct size for
   9174  1.1  christos 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   9175  1.1  christos 	 dynamic linker and used by the debugger.  */
   9176  1.1  christos #define add_dynamic_entry(TAG, VAL) \
   9177  1.1  christos   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   9178  1.1  christos 
   9179  1.1  christos       if (info->executable)
   9180  1.1  christos 	{
   9181  1.1  christos 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   9182  1.1  christos 	    return FALSE;
   9183  1.1  christos 	}
   9184  1.1  christos 
   9185  1.1  christos       if (htab->plt != NULL && htab->plt->size != 0)
   9186  1.1  christos 	{
   9187  1.1  christos 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   9188  1.1  christos 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   9189  1.1  christos 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   9190  1.1  christos 	      || !add_dynamic_entry (DT_JMPREL, 0)
   9191  1.1  christos 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   9192  1.1  christos 	    return FALSE;
   9193  1.1  christos 	}
   9194  1.1  christos 
   9195  1.1  christos       if (NO_OPD_RELOCS)
   9196  1.1  christos 	{
   9197  1.1  christos 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   9198  1.1  christos 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   9199  1.1  christos 	    return FALSE;
   9200  1.1  christos 	}
   9201  1.1  christos 
   9202  1.1  christos       if (!htab->no_tls_get_addr_opt
   9203  1.1  christos 	  && htab->tls_get_addr_fd != NULL
   9204  1.1  christos 	  && htab->tls_get_addr_fd->elf.plt.plist != NULL
   9205  1.1  christos 	  && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
   9206  1.1  christos 	return FALSE;
   9207  1.1  christos 
   9208  1.1  christos       if (relocs)
   9209  1.1  christos 	{
   9210  1.1  christos 	  if (!add_dynamic_entry (DT_RELA, 0)
   9211  1.1  christos 	      || !add_dynamic_entry (DT_RELASZ, 0)
   9212  1.1  christos 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   9213  1.1  christos 	    return FALSE;
   9214  1.1  christos 
   9215  1.1  christos 	  /* If any dynamic relocs apply to a read-only section,
   9216  1.1  christos 	     then we need a DT_TEXTREL entry.  */
   9217  1.1  christos 	  if ((info->flags & DF_TEXTREL) == 0)
   9218  1.1  christos 	    elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
   9219  1.1  christos 
   9220  1.1  christos 	  if ((info->flags & DF_TEXTREL) != 0)
   9221  1.1  christos 	    {
   9222  1.1  christos 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   9223  1.1  christos 		return FALSE;
   9224  1.1  christos 	    }
   9225  1.1  christos 	}
   9226  1.1  christos     }
   9227  1.1  christos #undef add_dynamic_entry
   9228  1.1  christos 
   9229  1.1  christos   return TRUE;
   9230  1.1  christos }
   9231  1.1  christos 
   9232  1.1  christos /* Determine the type of stub needed, if any, for a call.  */
   9233  1.1  christos 
   9234  1.1  christos static inline enum ppc_stub_type
   9235  1.1  christos ppc_type_of_stub (asection *input_sec,
   9236  1.1  christos 		  const Elf_Internal_Rela *rel,
   9237  1.1  christos 		  struct ppc_link_hash_entry **hash,
   9238  1.1  christos 		  struct plt_entry **plt_ent,
   9239  1.1  christos 		  bfd_vma destination)
   9240  1.1  christos {
   9241  1.1  christos   struct ppc_link_hash_entry *h = *hash;
   9242  1.1  christos   bfd_vma location;
   9243  1.1  christos   bfd_vma branch_offset;
   9244  1.1  christos   bfd_vma max_branch_offset;
   9245  1.1  christos   enum elf_ppc64_reloc_type r_type;
   9246  1.1  christos 
   9247  1.1  christos   if (h != NULL)
   9248  1.1  christos     {
   9249  1.1  christos       struct plt_entry *ent;
   9250  1.1  christos       struct ppc_link_hash_entry *fdh = h;
   9251  1.1  christos       if (h->oh != NULL
   9252  1.1  christos 	  && h->oh->is_func_descriptor)
   9253  1.1  christos 	{
   9254  1.1  christos 	  fdh = ppc_follow_link (h->oh);
   9255  1.1  christos 	  *hash = fdh;
   9256  1.1  christos 	}
   9257  1.1  christos 
   9258  1.1  christos       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   9259  1.1  christos 	if (ent->addend == rel->r_addend
   9260  1.1  christos 	    && ent->plt.offset != (bfd_vma) -1)
   9261  1.1  christos 	  {
   9262  1.1  christos 	    *plt_ent = ent;
   9263  1.1  christos 	    return ppc_stub_plt_call;
   9264  1.1  christos 	  }
   9265  1.1  christos 
   9266  1.1  christos       /* Here, we know we don't have a plt entry.  If we don't have a
   9267  1.1  christos 	 either a defined function descriptor or a defined entry symbol
   9268  1.1  christos 	 in a regular object file, then it is pointless trying to make
   9269  1.1  christos 	 any other type of stub.  */
   9270  1.1  christos       if (!is_static_defined (&fdh->elf)
   9271  1.1  christos 	  && !is_static_defined (&h->elf))
   9272  1.1  christos 	return ppc_stub_none;
   9273  1.1  christos     }
   9274  1.1  christos   else if (elf_local_got_ents (input_sec->owner) != NULL)
   9275  1.1  christos     {
   9276  1.1  christos       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   9277  1.1  christos       struct plt_entry **local_plt = (struct plt_entry **)
   9278  1.1  christos 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   9279  1.1  christos       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   9280  1.1  christos 
   9281  1.1  christos       if (local_plt[r_symndx] != NULL)
   9282  1.1  christos 	{
   9283  1.1  christos 	  struct plt_entry *ent;
   9284  1.1  christos 
   9285  1.1  christos 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   9286  1.1  christos 	    if (ent->addend == rel->r_addend
   9287  1.1  christos 		&& ent->plt.offset != (bfd_vma) -1)
   9288  1.1  christos 	      {
   9289  1.1  christos 		*plt_ent = ent;
   9290  1.1  christos 		return ppc_stub_plt_call;
   9291  1.1  christos 	      }
   9292  1.1  christos 	}
   9293  1.1  christos     }
   9294  1.1  christos 
   9295  1.1  christos   /* Determine where the call point is.  */
   9296  1.1  christos   location = (input_sec->output_offset
   9297  1.1  christos 	      + input_sec->output_section->vma
   9298  1.1  christos 	      + rel->r_offset);
   9299  1.1  christos 
   9300  1.1  christos   branch_offset = destination - location;
   9301  1.1  christos   r_type = ELF64_R_TYPE (rel->r_info);
   9302  1.1  christos 
   9303  1.1  christos   /* Determine if a long branch stub is needed.  */
   9304  1.1  christos   max_branch_offset = 1 << 25;
   9305  1.1  christos   if (r_type != R_PPC64_REL24)
   9306  1.1  christos     max_branch_offset = 1 << 15;
   9307  1.1  christos 
   9308  1.1  christos   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
   9309  1.1  christos     /* We need a stub.  Figure out whether a long_branch or plt_branch
   9310  1.1  christos        is needed later.  */
   9311  1.1  christos     return ppc_stub_long_branch;
   9312  1.1  christos 
   9313  1.1  christos   return ppc_stub_none;
   9314  1.1  christos }
   9315  1.1  christos 
   9316  1.1  christos /* Build a .plt call stub.  */
   9317  1.1  christos 
   9318  1.1  christos static inline bfd_byte *
   9319  1.1  christos build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
   9320  1.1  christos {
   9321  1.1  christos #define PPC_LO(v) ((v) & 0xffff)
   9322  1.1  christos #define PPC_HI(v) (((v) >> 16) & 0xffff)
   9323  1.1  christos #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   9324  1.1  christos 
   9325  1.1  christos   if (PPC_HA (offset) != 0)
   9326  1.1  christos     {
   9327  1.1  christos       if (r != NULL)
   9328  1.1  christos 	{
   9329  1.1  christos 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   9330  1.1  christos 	  r[1].r_offset = r[0].r_offset + 8;
   9331  1.1  christos 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9332  1.1  christos 	  r[1].r_addend = r[0].r_addend;
   9333  1.1  christos 	  if (PPC_HA (offset + 16) != PPC_HA (offset))
   9334  1.1  christos 	    {
   9335  1.1  christos 	      r[2].r_offset = r[1].r_offset + 4;
   9336  1.1  christos 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   9337  1.1  christos 	      r[2].r_addend = r[0].r_addend;
   9338  1.1  christos 	    }
   9339  1.1  christos 	  else
   9340  1.1  christos 	    {
   9341  1.1  christos 	      r[2].r_offset = r[1].r_offset + 8;
   9342  1.1  christos 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9343  1.1  christos 	      r[2].r_addend = r[0].r_addend + 8;
   9344  1.1  christos 	      r[3].r_offset = r[2].r_offset + 4;
   9345  1.1  christos 	      r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9346  1.1  christos 	      r[3].r_addend = r[0].r_addend + 16;
   9347  1.1  christos 	    }
   9348  1.1  christos 	}
   9349  1.1  christos       bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   9350  1.1  christos       bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9351  1.1  christos       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),	p += 4;
   9352  1.1  christos       if (PPC_HA (offset + 16) != PPC_HA (offset))
   9353  1.1  christos 	{
   9354  1.1  christos 	  bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p),	p += 4;
   9355  1.1  christos 	  offset = 0;
   9356  1.1  christos 	}
   9357  1.1  christos       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9358  1.1  christos       bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p),	p += 4;
   9359  1.1  christos       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p),	p += 4;
   9360  1.1  christos       bfd_put_32 (obfd, BCTR, p),				p += 4;
   9361  1.1  christos     }
   9362  1.1  christos   else
   9363  1.1  christos     {
   9364  1.1  christos       if (r != NULL)
   9365  1.1  christos 	{
   9366  1.1  christos 	  r[0].r_offset += 4;
   9367  1.1  christos 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9368  1.1  christos 	  if (PPC_HA (offset + 16) != PPC_HA (offset))
   9369  1.1  christos 	    {
   9370  1.1  christos 	      r[1].r_offset = r[0].r_offset + 4;
   9371  1.1  christos 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   9372  1.1  christos 	      r[1].r_addend = r[0].r_addend;
   9373  1.1  christos 	    }
   9374  1.1  christos 	  else
   9375  1.1  christos 	    {
   9376  1.1  christos 	      r[1].r_offset = r[0].r_offset + 8;
   9377  1.1  christos 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9378  1.1  christos 	      r[1].r_addend = r[0].r_addend + 16;
   9379  1.1  christos 	      r[2].r_offset = r[1].r_offset + 4;
   9380  1.1  christos 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9381  1.1  christos 	      r[2].r_addend = r[0].r_addend + 8;
   9382  1.1  christos 	    }
   9383  1.1  christos 	}
   9384  1.1  christos       bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9385  1.1  christos       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p),	p += 4;
   9386  1.1  christos       if (PPC_HA (offset + 16) != PPC_HA (offset))
   9387  1.1  christos 	{
   9388  1.1  christos 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   9389  1.1  christos 	  offset = 0;
   9390  1.1  christos 	}
   9391  1.1  christos       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9392  1.1  christos       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p),	p += 4;
   9393  1.1  christos       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),	p += 4;
   9394  1.1  christos       bfd_put_32 (obfd, BCTR, p),				p += 4;
   9395  1.1  christos     }
   9396  1.1  christos   return p;
   9397  1.1  christos }
   9398  1.1  christos 
   9399  1.1  christos /* Build a special .plt call stub for __tls_get_addr.  */
   9400  1.1  christos 
   9401  1.1  christos #define LD_R11_0R3	0xe9630000
   9402  1.1  christos #define LD_R12_0R3	0xe9830000
   9403  1.1  christos #define MR_R0_R3	0x7c601b78
   9404  1.1  christos #define CMPDI_R11_0	0x2c2b0000
   9405  1.1  christos #define ADD_R3_R12_R13	0x7c6c6a14
   9406  1.1  christos #define BEQLR		0x4d820020
   9407  1.1  christos #define MR_R3_R0	0x7c030378
   9408  1.1  christos #define MFLR_R11	0x7d6802a6
   9409  1.1  christos #define STD_R11_0R1	0xf9610000
   9410  1.1  christos #define BCTRL		0x4e800421
   9411  1.1  christos #define LD_R11_0R1	0xe9610000
   9412  1.1  christos #define LD_R2_0R1	0xe8410000
   9413  1.1  christos #define MTLR_R11	0x7d6803a6
   9414  1.1  christos 
   9415  1.1  christos static inline bfd_byte *
   9416  1.1  christos build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
   9417  1.1  christos 			 Elf_Internal_Rela *r)
   9418  1.1  christos {
   9419  1.1  christos   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),		p += 4;
   9420  1.1  christos   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   9421  1.1  christos   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   9422  1.1  christos   bfd_put_32 (obfd, CMPDI_R11_0, p),		p += 4;
   9423  1.1  christos   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   9424  1.1  christos   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   9425  1.1  christos   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   9426  1.1  christos   bfd_put_32 (obfd, MFLR_R11, p),		p += 4;
   9427  1.1  christos   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),	p += 4;
   9428  1.1  christos 
   9429  1.1  christos   if (r != NULL)
   9430  1.1  christos     r[0].r_offset += 9 * 4;
   9431  1.1  christos   p = build_plt_stub (obfd, p, offset, r);
   9432  1.1  christos   bfd_put_32 (obfd, BCTRL, p - 4);
   9433  1.1  christos 
   9434  1.1  christos   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),	p += 4;
   9435  1.1  christos   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),		p += 4;
   9436  1.1  christos   bfd_put_32 (obfd, MTLR_R11, p),		p += 4;
   9437  1.1  christos   bfd_put_32 (obfd, BLR, p),			p += 4;
   9438  1.1  christos 
   9439  1.1  christos   return p;
   9440  1.1  christos }
   9441  1.1  christos 
   9442  1.1  christos static Elf_Internal_Rela *
   9443  1.1  christos get_relocs (asection *sec, int count)
   9444  1.1  christos {
   9445  1.1  christos   Elf_Internal_Rela *relocs;
   9446  1.1  christos   struct bfd_elf_section_data *elfsec_data;
   9447  1.1  christos 
   9448  1.1  christos   elfsec_data = elf_section_data (sec);
   9449  1.1  christos   relocs = elfsec_data->relocs;
   9450  1.1  christos   if (relocs == NULL)
   9451  1.1  christos     {
   9452  1.1  christos       bfd_size_type relsize;
   9453  1.1  christos       relsize = sec->reloc_count * sizeof (*relocs);
   9454  1.1  christos       relocs = bfd_alloc (sec->owner, relsize);
   9455  1.1  christos       if (relocs == NULL)
   9456  1.1  christos 	return NULL;
   9457  1.1  christos       elfsec_data->relocs = relocs;
   9458  1.1  christos       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   9459  1.1  christos 					  sizeof (Elf_Internal_Shdr));
   9460  1.1  christos       if (elfsec_data->rela.hdr == NULL)
   9461  1.1  christos 	return NULL;
   9462  1.1  christos       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   9463  1.1  christos 					* sizeof (Elf64_External_Rela));
   9464  1.1  christos       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   9465  1.1  christos       sec->reloc_count = 0;
   9466  1.1  christos     }
   9467  1.1  christos   relocs += sec->reloc_count;
   9468  1.1  christos   sec->reloc_count += count;
   9469  1.1  christos   return relocs;
   9470  1.1  christos }
   9471  1.1  christos 
   9472  1.1  christos static bfd_vma
   9473  1.1  christos get_r2off (struct ppc_link_hash_table *htab,
   9474  1.1  christos 	   struct ppc_stub_hash_entry *stub_entry)
   9475  1.1  christos {
   9476  1.1  christos   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
   9477  1.1  christos 
   9478  1.1  christos   if (r2off == 0)
   9479  1.1  christos     {
   9480  1.1  christos       /* Support linking -R objects.  Get the toc pointer from the
   9481  1.1  christos 	 opd entry.  */
   9482  1.1  christos       char buf[8];
   9483  1.1  christos       asection *opd = stub_entry->h->elf.root.u.def.section;
   9484  1.1  christos       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   9485  1.1  christos 
   9486  1.1  christos       if (strcmp (opd->name, ".opd") != 0
   9487  1.1  christos 	  || opd->reloc_count != 0)
   9488  1.1  christos 	{
   9489  1.1  christos 	  (*_bfd_error_handler) (_("cannot find opd entry toc for %s"),
   9490  1.1  christos 				 stub_entry->h->elf.root.root.string);
   9491  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   9492  1.1  christos 	  return 0;
   9493  1.1  christos 	}
   9494  1.1  christos       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   9495  1.1  christos 	return 0;
   9496  1.1  christos       r2off = bfd_get_64 (opd->owner, buf);
   9497  1.1  christos       r2off -= elf_gp (stub_entry->id_sec->output_section->owner);
   9498  1.1  christos     }
   9499  1.1  christos   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
   9500  1.1  christos   return r2off;
   9501  1.1  christos }
   9502  1.1  christos 
   9503  1.1  christos static bfd_boolean
   9504  1.1  christos ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   9505  1.1  christos {
   9506  1.1  christos   struct ppc_stub_hash_entry *stub_entry;
   9507  1.1  christos   struct ppc_branch_hash_entry *br_entry;
   9508  1.1  christos   struct bfd_link_info *info;
   9509  1.1  christos   struct ppc_link_hash_table *htab;
   9510  1.1  christos   bfd_byte *loc;
   9511  1.1  christos   bfd_byte *p;
   9512  1.1  christos   bfd_vma dest, off;
   9513  1.1  christos   int size;
   9514  1.1  christos   Elf_Internal_Rela *r;
   9515  1.1  christos   asection *plt;
   9516  1.1  christos 
   9517  1.1  christos   /* Massage our args to the form they really have.  */
   9518  1.1  christos   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   9519  1.1  christos   info = in_arg;
   9520  1.1  christos 
   9521  1.1  christos   htab = ppc_hash_table (info);
   9522  1.1  christos   if (htab == NULL)
   9523  1.1  christos     return FALSE;
   9524  1.1  christos 
   9525  1.1  christos   /* Make a note of the offset within the stubs for this entry.  */
   9526  1.1  christos   stub_entry->stub_offset = stub_entry->stub_sec->size;
   9527  1.1  christos   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
   9528  1.1  christos 
   9529  1.1  christos   htab->stub_count[stub_entry->stub_type - 1] += 1;
   9530  1.1  christos   switch (stub_entry->stub_type)
   9531  1.1  christos     {
   9532  1.1  christos     case ppc_stub_long_branch:
   9533  1.1  christos     case ppc_stub_long_branch_r2off:
   9534  1.1  christos       /* Branches are relative.  This is where we are going to.  */
   9535  1.1  christos       off = dest = (stub_entry->target_value
   9536  1.1  christos 		    + stub_entry->target_section->output_offset
   9537  1.1  christos 		    + stub_entry->target_section->output_section->vma);
   9538  1.1  christos 
   9539  1.1  christos       /* And this is where we are coming from.  */
   9540  1.1  christos       off -= (stub_entry->stub_offset
   9541  1.1  christos 	      + stub_entry->stub_sec->output_offset
   9542  1.1  christos 	      + stub_entry->stub_sec->output_section->vma);
   9543  1.1  christos 
   9544  1.1  christos       size = 4;
   9545  1.1  christos       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   9546  1.1  christos 	{
   9547  1.1  christos 	  bfd_vma r2off = get_r2off (htab, stub_entry);
   9548  1.1  christos 
   9549  1.1  christos 	  if (r2off == 0)
   9550  1.1  christos 	    {
   9551  1.1  christos 	      htab->stub_error = TRUE;
   9552  1.1  christos 	      return FALSE;
   9553  1.1  christos 	    }
   9554  1.1  christos 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   9555  1.1  christos 	  loc += 4;
   9556  1.1  christos 	  size = 12;
   9557  1.1  christos 	  if (PPC_HA (r2off) != 0)
   9558  1.1  christos 	    {
   9559  1.1  christos 	      size = 16;
   9560  1.1  christos 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   9561  1.1  christos 	      loc += 4;
   9562  1.1  christos 	    }
   9563  1.1  christos 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   9564  1.1  christos 	  loc += 4;
   9565  1.1  christos 	  off -= size - 4;
   9566  1.1  christos 	}
   9567  1.1  christos       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
   9568  1.1  christos 
   9569  1.1  christos       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   9570  1.1  christos 	{
   9571  1.1  christos 	  (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
   9572  1.1  christos 				 stub_entry->root.string);
   9573  1.1  christos 	  htab->stub_error = TRUE;
   9574  1.1  christos 	  return FALSE;
   9575  1.1  christos 	}
   9576  1.1  christos 
   9577  1.1  christos       if (info->emitrelocations)
   9578  1.1  christos 	{
   9579  1.1  christos 	  r = get_relocs (stub_entry->stub_sec, 1);
   9580  1.1  christos 	  if (r == NULL)
   9581  1.1  christos 	    return FALSE;
   9582  1.1  christos 	  r->r_offset = loc - stub_entry->stub_sec->contents;
   9583  1.1  christos 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   9584  1.1  christos 	  r->r_addend = dest;
   9585  1.1  christos 	  if (stub_entry->h != NULL)
   9586  1.1  christos 	    {
   9587  1.1  christos 	      struct elf_link_hash_entry **hashes;
   9588  1.1  christos 	      unsigned long symndx;
   9589  1.1  christos 	      struct ppc_link_hash_entry *h;
   9590  1.1  christos 
   9591  1.1  christos 	      hashes = elf_sym_hashes (htab->stub_bfd);
   9592  1.1  christos 	      if (hashes == NULL)
   9593  1.1  christos 		{
   9594  1.1  christos 		  bfd_size_type hsize;
   9595  1.1  christos 
   9596  1.1  christos 		  hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   9597  1.1  christos 		  hashes = bfd_zalloc (htab->stub_bfd, hsize);
   9598  1.1  christos 		  if (hashes == NULL)
   9599  1.1  christos 		    return FALSE;
   9600  1.1  christos 		  elf_sym_hashes (htab->stub_bfd) = hashes;
   9601  1.1  christos 		  htab->stub_globals = 1;
   9602  1.1  christos 		}
   9603  1.1  christos 	      symndx = htab->stub_globals++;
   9604  1.1  christos 	      h = stub_entry->h;
   9605  1.1  christos 	      hashes[symndx] = &h->elf;
   9606  1.1  christos 	      r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
   9607  1.1  christos 	      if (h->oh != NULL && h->oh->is_func)
   9608  1.1  christos 		h = ppc_follow_link (h->oh);
   9609  1.1  christos 	      if (h->elf.root.u.def.section != stub_entry->target_section)
   9610  1.1  christos 		/* H is an opd symbol.  The addend must be zero.  */
   9611  1.1  christos 		r->r_addend = 0;
   9612  1.1  christos 	      else
   9613  1.1  christos 		{
   9614  1.1  christos 		  off = (h->elf.root.u.def.value
   9615  1.1  christos 			 + h->elf.root.u.def.section->output_offset
   9616  1.1  christos 			 + h->elf.root.u.def.section->output_section->vma);
   9617  1.1  christos 		  r->r_addend -= off;
   9618  1.1  christos 		}
   9619  1.1  christos 	    }
   9620  1.1  christos 	}
   9621  1.1  christos       break;
   9622  1.1  christos 
   9623  1.1  christos     case ppc_stub_plt_branch:
   9624  1.1  christos     case ppc_stub_plt_branch_r2off:
   9625  1.1  christos       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   9626  1.1  christos 					 stub_entry->root.string + 9,
   9627  1.1  christos 					 FALSE, FALSE);
   9628  1.1  christos       if (br_entry == NULL)
   9629  1.1  christos 	{
   9630  1.1  christos 	  (*_bfd_error_handler) (_("can't find branch stub `%s'"),
   9631  1.1  christos 				 stub_entry->root.string);
   9632  1.1  christos 	  htab->stub_error = TRUE;
   9633  1.1  christos 	  return FALSE;
   9634  1.1  christos 	}
   9635  1.1  christos 
   9636  1.1  christos       dest = (stub_entry->target_value
   9637  1.1  christos 	      + stub_entry->target_section->output_offset
   9638  1.1  christos 	      + stub_entry->target_section->output_section->vma);
   9639  1.1  christos 
   9640  1.1  christos       bfd_put_64 (htab->brlt->owner, dest,
   9641  1.1  christos 		  htab->brlt->contents + br_entry->offset);
   9642  1.1  christos 
   9643  1.1  christos       if (br_entry->iter == htab->stub_iteration)
   9644  1.1  christos 	{
   9645  1.1  christos 	  br_entry->iter = 0;
   9646  1.1  christos 
   9647  1.1  christos 	  if (htab->relbrlt != NULL)
   9648  1.1  christos 	    {
   9649  1.1  christos 	      /* Create a reloc for the branch lookup table entry.  */
   9650  1.1  christos 	      Elf_Internal_Rela rela;
   9651  1.1  christos 	      bfd_byte *rl;
   9652  1.1  christos 
   9653  1.1  christos 	      rela.r_offset = (br_entry->offset
   9654  1.1  christos 			       + htab->brlt->output_offset
   9655  1.1  christos 			       + htab->brlt->output_section->vma);
   9656  1.1  christos 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   9657  1.1  christos 	      rela.r_addend = dest;
   9658  1.1  christos 
   9659  1.1  christos 	      rl = htab->relbrlt->contents;
   9660  1.1  christos 	      rl += (htab->relbrlt->reloc_count++
   9661  1.1  christos 		     * sizeof (Elf64_External_Rela));
   9662  1.1  christos 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   9663  1.1  christos 	    }
   9664  1.1  christos 	  else if (info->emitrelocations)
   9665  1.1  christos 	    {
   9666  1.1  christos 	      r = get_relocs (htab->brlt, 1);
   9667  1.1  christos 	      if (r == NULL)
   9668  1.1  christos 		return FALSE;
   9669  1.1  christos 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   9670  1.1  christos 		 normal reloc processing.  Symbols and offsets are not
   9671  1.1  christos 		 translated from input file to output file form, so
   9672  1.1  christos 		 set up the offset per the output file.  */
   9673  1.1  christos 	      r->r_offset = (br_entry->offset
   9674  1.1  christos 			     + htab->brlt->output_offset
   9675  1.1  christos 			     + htab->brlt->output_section->vma);
   9676  1.1  christos 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   9677  1.1  christos 	      r->r_addend = dest;
   9678  1.1  christos 	    }
   9679  1.1  christos 	}
   9680  1.1  christos 
   9681  1.1  christos       dest = (br_entry->offset
   9682  1.1  christos 	      + htab->brlt->output_offset
   9683  1.1  christos 	      + htab->brlt->output_section->vma);
   9684  1.1  christos 
   9685  1.1  christos       off = (dest
   9686  1.1  christos 	     - elf_gp (htab->brlt->output_section->owner)
   9687  1.1  christos 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   9688  1.1  christos 
   9689  1.1  christos       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   9690  1.1  christos 	{
   9691  1.1  christos 	  (*_bfd_error_handler)
   9692  1.1  christos 	    (_("linkage table error against `%s'"),
   9693  1.1  christos 	     stub_entry->root.string);
   9694  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   9695  1.1  christos 	  htab->stub_error = TRUE;
   9696  1.1  christos 	  return FALSE;
   9697  1.1  christos 	}
   9698  1.1  christos 
   9699  1.1  christos       if (info->emitrelocations)
   9700  1.1  christos 	{
   9701  1.1  christos 	  r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
   9702  1.1  christos 	  if (r == NULL)
   9703  1.1  christos 	    return FALSE;
   9704  1.1  christos 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   9705  1.1  christos 	  if (bfd_big_endian (info->output_bfd))
   9706  1.1  christos 	    r[0].r_offset += 2;
   9707  1.1  christos 	  if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
   9708  1.1  christos 	    r[0].r_offset += 4;
   9709  1.1  christos 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9710  1.1  christos 	  r[0].r_addend = dest;
   9711  1.1  christos 	  if (PPC_HA (off) != 0)
   9712  1.1  christos 	    {
   9713  1.1  christos 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   9714  1.1  christos 	      r[1].r_offset = r[0].r_offset + 4;
   9715  1.1  christos 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9716  1.1  christos 	      r[1].r_addend = r[0].r_addend;
   9717  1.1  christos 	    }
   9718  1.1  christos 	}
   9719  1.1  christos 
   9720  1.1  christos       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   9721  1.1  christos 	{
   9722  1.1  christos 	  if (PPC_HA (off) != 0)
   9723  1.1  christos 	    {
   9724  1.1  christos 	      size = 16;
   9725  1.1  christos 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   9726  1.1  christos 	      loc += 4;
   9727  1.1  christos 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   9728  1.1  christos 	    }
   9729  1.1  christos 	  else
   9730  1.1  christos 	    {
   9731  1.1  christos 	      size = 12;
   9732  1.1  christos 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   9733  1.1  christos 	    }
   9734  1.1  christos 	}
   9735  1.1  christos       else
   9736  1.1  christos 	{
   9737  1.1  christos 	  bfd_vma r2off = get_r2off (htab, stub_entry);
   9738  1.1  christos 
   9739  1.1  christos 	  if (r2off == 0)
   9740  1.1  christos 	    {
   9741  1.1  christos 	      htab->stub_error = TRUE;
   9742  1.1  christos 	      return FALSE;
   9743  1.1  christos 	    }
   9744  1.1  christos 
   9745  1.1  christos 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   9746  1.1  christos 	  loc += 4;
   9747  1.1  christos 	  size = 20;
   9748  1.1  christos 	  if (PPC_HA (off) != 0)
   9749  1.1  christos 	    {
   9750  1.1  christos 	      size += 4;
   9751  1.1  christos 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   9752  1.1  christos 	      loc += 4;
   9753  1.1  christos 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   9754  1.1  christos 	      loc += 4;
   9755  1.1  christos 	    }
   9756  1.1  christos 	  else
   9757  1.1  christos 	    {
   9758  1.1  christos 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   9759  1.1  christos 	      loc += 4;
   9760  1.1  christos 	    }
   9761  1.1  christos 
   9762  1.1  christos 	  if (PPC_HA (r2off) != 0)
   9763  1.1  christos 	    {
   9764  1.1  christos 	      size += 4;
   9765  1.1  christos 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   9766  1.1  christos 	      loc += 4;
   9767  1.1  christos 	    }
   9768  1.1  christos 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   9769  1.1  christos 	}
   9770  1.1  christos       loc += 4;
   9771  1.1  christos       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
   9772  1.1  christos       loc += 4;
   9773  1.1  christos       bfd_put_32 (htab->stub_bfd, BCTR, loc);
   9774  1.1  christos       break;
   9775  1.1  christos 
   9776  1.1  christos     case ppc_stub_plt_call:
   9777  1.1  christos       if (stub_entry->h != NULL
   9778  1.1  christos 	  && stub_entry->h->is_func_descriptor
   9779  1.1  christos 	  && stub_entry->h->oh != NULL)
   9780  1.1  christos 	{
   9781  1.1  christos 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   9782  1.1  christos 
   9783  1.1  christos 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   9784  1.1  christos 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
   9785  1.1  christos 	     FIXME: We used to define the symbol on one of the call
   9786  1.1  christos 	     stubs instead, which is why we test symbol section id
   9787  1.1  christos 	     against htab->top_id in various places.  Likely all
   9788  1.1  christos 	     these checks could now disappear.  */
   9789  1.1  christos 	  if (fh->elf.root.type == bfd_link_hash_undefined)
   9790  1.1  christos 	    fh->elf.root.type = bfd_link_hash_undefweak;
   9791  1.1  christos 	}
   9792  1.1  christos 
   9793  1.1  christos       /* Now build the stub.  */
   9794  1.1  christos       dest = stub_entry->plt_ent->plt.offset & ~1;
   9795  1.1  christos       if (dest >= (bfd_vma) -2)
   9796  1.1  christos 	abort ();
   9797  1.1  christos 
   9798  1.1  christos       plt = htab->plt;
   9799  1.1  christos       if (!htab->elf.dynamic_sections_created
   9800  1.1  christos 	  || stub_entry->h == NULL
   9801  1.1  christos 	  || stub_entry->h->elf.dynindx == -1)
   9802  1.1  christos 	plt = htab->iplt;
   9803  1.1  christos 
   9804  1.1  christos       dest += plt->output_offset + plt->output_section->vma;
   9805  1.1  christos 
   9806  1.1  christos       if (stub_entry->h == NULL
   9807  1.1  christos 	  && (stub_entry->plt_ent->plt.offset & 1) == 0)
   9808  1.1  christos 	{
   9809  1.1  christos 	  Elf_Internal_Rela rela;
   9810  1.1  christos 	  bfd_byte *rl;
   9811  1.1  christos 
   9812  1.1  christos 	  rela.r_offset = dest;
   9813  1.1  christos 	  rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   9814  1.1  christos 	  rela.r_addend = (stub_entry->target_value
   9815  1.1  christos 			   + stub_entry->target_section->output_offset
   9816  1.1  christos 			   + stub_entry->target_section->output_section->vma);
   9817  1.1  christos 
   9818  1.1  christos 	  rl = (htab->reliplt->contents
   9819  1.1  christos 		+ (htab->reliplt->reloc_count++
   9820  1.1  christos 		   * sizeof (Elf64_External_Rela)));
   9821  1.1  christos 	  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
   9822  1.1  christos 	  stub_entry->plt_ent->plt.offset |= 1;
   9823  1.1  christos 	}
   9824  1.1  christos 
   9825  1.1  christos       off = (dest
   9826  1.1  christos 	     - elf_gp (plt->output_section->owner)
   9827  1.1  christos 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   9828  1.1  christos 
   9829  1.1  christos       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   9830  1.1  christos 	{
   9831  1.1  christos 	  (*_bfd_error_handler)
   9832  1.1  christos 	    (_("linkage table error against `%s'"),
   9833  1.1  christos 	     stub_entry->h != NULL
   9834  1.1  christos 	     ? stub_entry->h->elf.root.root.string
   9835  1.1  christos 	     : "<local sym>");
   9836  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   9837  1.1  christos 	  htab->stub_error = TRUE;
   9838  1.1  christos 	  return FALSE;
   9839  1.1  christos 	}
   9840  1.1  christos 
   9841  1.1  christos       r = NULL;
   9842  1.1  christos       if (info->emitrelocations)
   9843  1.1  christos 	{
   9844  1.1  christos 	  r = get_relocs (stub_entry->stub_sec,
   9845  1.1  christos 			  (2 + (PPC_HA (off) != 0)
   9846  1.1  christos 			   + (PPC_HA (off + 16) == PPC_HA (off))));
   9847  1.1  christos 	  if (r == NULL)
   9848  1.1  christos 	    return FALSE;
   9849  1.1  christos 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   9850  1.1  christos 	  if (bfd_big_endian (info->output_bfd))
   9851  1.1  christos 	    r[0].r_offset += 2;
   9852  1.1  christos 	  r[0].r_addend = dest;
   9853  1.1  christos 	}
   9854  1.1  christos       if (stub_entry->h != NULL
   9855  1.1  christos 	  && (stub_entry->h == htab->tls_get_addr_fd
   9856  1.1  christos 	      || stub_entry->h == htab->tls_get_addr)
   9857  1.1  christos 	  && !htab->no_tls_get_addr_opt)
   9858  1.1  christos 	p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
   9859  1.1  christos       else
   9860  1.1  christos 	p = build_plt_stub (htab->stub_bfd, loc, off, r);
   9861  1.1  christos       size = p - loc;
   9862  1.1  christos       break;
   9863  1.1  christos 
   9864  1.1  christos     default:
   9865  1.1  christos       BFD_FAIL ();
   9866  1.1  christos       return FALSE;
   9867  1.1  christos     }
   9868  1.1  christos 
   9869  1.1  christos   stub_entry->stub_sec->size += size;
   9870  1.1  christos 
   9871  1.1  christos   if (htab->emit_stub_syms)
   9872  1.1  christos     {
   9873  1.1  christos       struct elf_link_hash_entry *h;
   9874  1.1  christos       size_t len1, len2;
   9875  1.1  christos       char *name;
   9876  1.1  christos       const char *const stub_str[] = { "long_branch",
   9877  1.1  christos 				       "long_branch_r2off",
   9878  1.1  christos 				       "plt_branch",
   9879  1.1  christos 				       "plt_branch_r2off",
   9880  1.1  christos 				       "plt_call" };
   9881  1.1  christos 
   9882  1.1  christos       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
   9883  1.1  christos       len2 = strlen (stub_entry->root.string);
   9884  1.1  christos       name = bfd_malloc (len1 + len2 + 2);
   9885  1.1  christos       if (name == NULL)
   9886  1.1  christos 	return FALSE;
   9887  1.1  christos       memcpy (name, stub_entry->root.string, 9);
   9888  1.1  christos       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
   9889  1.1  christos       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   9890  1.1  christos       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   9891  1.1  christos       if (h == NULL)
   9892  1.1  christos 	return FALSE;
   9893  1.1  christos       if (h->root.type == bfd_link_hash_new)
   9894  1.1  christos 	{
   9895  1.1  christos 	  h->root.type = bfd_link_hash_defined;
   9896  1.1  christos 	  h->root.u.def.section = stub_entry->stub_sec;
   9897  1.1  christos 	  h->root.u.def.value = stub_entry->stub_offset;
   9898  1.1  christos 	  h->ref_regular = 1;
   9899  1.1  christos 	  h->def_regular = 1;
   9900  1.1  christos 	  h->ref_regular_nonweak = 1;
   9901  1.1  christos 	  h->forced_local = 1;
   9902  1.1  christos 	  h->non_elf = 0;
   9903  1.1  christos 	}
   9904  1.1  christos     }
   9905  1.1  christos 
   9906  1.1  christos   return TRUE;
   9907  1.1  christos }
   9908  1.1  christos 
   9909  1.1  christos /* As above, but don't actually build the stub.  Just bump offset so
   9910  1.1  christos    we know stub section sizes, and select plt_branch stubs where
   9911  1.1  christos    long_branch stubs won't do.  */
   9912  1.1  christos 
   9913  1.1  christos static bfd_boolean
   9914  1.1  christos ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   9915  1.1  christos {
   9916  1.1  christos   struct ppc_stub_hash_entry *stub_entry;
   9917  1.1  christos   struct bfd_link_info *info;
   9918  1.1  christos   struct ppc_link_hash_table *htab;
   9919  1.1  christos   bfd_vma off;
   9920  1.1  christos   int size;
   9921  1.1  christos 
   9922  1.1  christos   /* Massage our args to the form they really have.  */
   9923  1.1  christos   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   9924  1.1  christos   info = in_arg;
   9925  1.1  christos 
   9926  1.1  christos   htab = ppc_hash_table (info);
   9927  1.1  christos   if (htab == NULL)
   9928  1.1  christos     return FALSE;
   9929  1.1  christos 
   9930  1.1  christos   if (stub_entry->stub_type == ppc_stub_plt_call)
   9931  1.1  christos     {
   9932  1.1  christos       asection *plt;
   9933  1.1  christos       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   9934  1.1  christos       if (off >= (bfd_vma) -2)
   9935  1.1  christos 	abort ();
   9936  1.1  christos       plt = htab->plt;
   9937  1.1  christos       if (!htab->elf.dynamic_sections_created
   9938  1.1  christos 	  || stub_entry->h == NULL
   9939  1.1  christos 	  || stub_entry->h->elf.dynindx == -1)
   9940  1.1  christos 	plt = htab->iplt;
   9941  1.1  christos       off += (plt->output_offset
   9942  1.1  christos 	      + plt->output_section->vma
   9943  1.1  christos 	      - elf_gp (plt->output_section->owner)
   9944  1.1  christos 	      - htab->stub_group[stub_entry->id_sec->id].toc_off);
   9945  1.1  christos 
   9946  1.1  christos       size = PLT_CALL_STUB_SIZE;
   9947  1.1  christos       if (PPC_HA (off) == 0)
   9948  1.1  christos 	size -= 4;
   9949  1.1  christos       if (PPC_HA (off + 16) != PPC_HA (off))
   9950  1.1  christos 	size += 4;
   9951  1.1  christos       if (stub_entry->h != NULL
   9952  1.1  christos 	  && (stub_entry->h == htab->tls_get_addr_fd
   9953  1.1  christos 	      || stub_entry->h == htab->tls_get_addr)
   9954  1.1  christos 	  && !htab->no_tls_get_addr_opt)
   9955  1.1  christos 	size += 13 * 4;
   9956  1.1  christos       if (info->emitrelocations)
   9957  1.1  christos 	{
   9958  1.1  christos 	  stub_entry->stub_sec->reloc_count
   9959  1.1  christos 	    += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
   9960  1.1  christos 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   9961  1.1  christos 	}
   9962  1.1  christos     }
   9963  1.1  christos   else
   9964  1.1  christos     {
   9965  1.1  christos       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
   9966  1.1  christos 	 variants.  */
   9967  1.1  christos       bfd_vma r2off = 0;
   9968  1.1  christos 
   9969  1.1  christos       off = (stub_entry->target_value
   9970  1.1  christos 	     + stub_entry->target_section->output_offset
   9971  1.1  christos 	     + stub_entry->target_section->output_section->vma);
   9972  1.1  christos       off -= (stub_entry->stub_sec->size
   9973  1.1  christos 	      + stub_entry->stub_sec->output_offset
   9974  1.1  christos 	      + stub_entry->stub_sec->output_section->vma);
   9975  1.1  christos 
   9976  1.1  christos       /* Reset the stub type from the plt variant in case we now
   9977  1.1  christos 	 can reach with a shorter stub.  */
   9978  1.1  christos       if (stub_entry->stub_type >= ppc_stub_plt_branch)
   9979  1.1  christos 	stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   9980  1.1  christos 
   9981  1.1  christos       size = 4;
   9982  1.1  christos       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   9983  1.1  christos 	{
   9984  1.1  christos 	  r2off = get_r2off (htab, stub_entry);
   9985  1.1  christos 	  if (r2off == 0)
   9986  1.1  christos 	    {
   9987  1.1  christos 	      htab->stub_error = TRUE;
   9988  1.1  christos 	      return FALSE;
   9989  1.1  christos 	    }
   9990  1.1  christos 	  size = 12;
   9991  1.1  christos 	  if (PPC_HA (r2off) != 0)
   9992  1.1  christos 	    size = 16;
   9993  1.1  christos 	  off -= size - 4;
   9994  1.1  christos 	}
   9995  1.1  christos 
   9996  1.1  christos       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
   9997  1.1  christos       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   9998  1.1  christos 	{
   9999  1.1  christos 	  struct ppc_branch_hash_entry *br_entry;
   10000  1.1  christos 
   10001  1.1  christos 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10002  1.1  christos 					     stub_entry->root.string + 9,
   10003  1.1  christos 					     TRUE, FALSE);
   10004  1.1  christos 	  if (br_entry == NULL)
   10005  1.1  christos 	    {
   10006  1.1  christos 	      (*_bfd_error_handler) (_("can't build branch stub `%s'"),
   10007  1.1  christos 				     stub_entry->root.string);
   10008  1.1  christos 	      htab->stub_error = TRUE;
   10009  1.1  christos 	      return FALSE;
   10010  1.1  christos 	    }
   10011  1.1  christos 
   10012  1.1  christos 	  if (br_entry->iter != htab->stub_iteration)
   10013  1.1  christos 	    {
   10014  1.1  christos 	      br_entry->iter = htab->stub_iteration;
   10015  1.1  christos 	      br_entry->offset = htab->brlt->size;
   10016  1.1  christos 	      htab->brlt->size += 8;
   10017  1.1  christos 
   10018  1.1  christos 	      if (htab->relbrlt != NULL)
   10019  1.1  christos 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   10020  1.1  christos 	      else if (info->emitrelocations)
   10021  1.1  christos 		{
   10022  1.1  christos 		  htab->brlt->reloc_count += 1;
   10023  1.1  christos 		  htab->brlt->flags |= SEC_RELOC;
   10024  1.1  christos 		}
   10025  1.1  christos 	    }
   10026  1.1  christos 
   10027  1.1  christos 	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
   10028  1.1  christos 	  off = (br_entry->offset
   10029  1.1  christos 		 + htab->brlt->output_offset
   10030  1.1  christos 		 + htab->brlt->output_section->vma
   10031  1.1  christos 		 - elf_gp (htab->brlt->output_section->owner)
   10032  1.1  christos 		 - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10033  1.1  christos 
   10034  1.1  christos 	  if (info->emitrelocations)
   10035  1.1  christos 	    {
   10036  1.1  christos 	      stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
   10037  1.1  christos 	      stub_entry->stub_sec->flags |= SEC_RELOC;
   10038  1.1  christos 	    }
   10039  1.1  christos 
   10040  1.1  christos 	  if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10041  1.1  christos 	    {
   10042  1.1  christos 	      size = 12;
   10043  1.1  christos 	      if (PPC_HA (off) != 0)
   10044  1.1  christos 		size = 16;
   10045  1.1  christos 	    }
   10046  1.1  christos 	  else
   10047  1.1  christos 	    {
   10048  1.1  christos 	      size = 20;
   10049  1.1  christos 	      if (PPC_HA (off) != 0)
   10050  1.1  christos 		size += 4;
   10051  1.1  christos 
   10052  1.1  christos 	      if (PPC_HA (r2off) != 0)
   10053  1.1  christos 		size += 4;
   10054  1.1  christos 	    }
   10055  1.1  christos 	}
   10056  1.1  christos       else if (info->emitrelocations)
   10057  1.1  christos 	{
   10058  1.1  christos 	  stub_entry->stub_sec->reloc_count += 1;
   10059  1.1  christos 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   10060  1.1  christos 	}
   10061  1.1  christos     }
   10062  1.1  christos 
   10063  1.1  christos   stub_entry->stub_sec->size += size;
   10064  1.1  christos   return TRUE;
   10065  1.1  christos }
   10066  1.1  christos 
   10067  1.1  christos /* Set up various things so that we can make a list of input sections
   10068  1.1  christos    for each output section included in the link.  Returns -1 on error,
   10069  1.1  christos    0 when no stubs will be needed, and 1 on success.  */
   10070  1.1  christos 
   10071  1.1  christos int
   10072  1.1  christos ppc64_elf_setup_section_lists
   10073  1.1  christos   (struct bfd_link_info *info,
   10074  1.1  christos    asection *(*add_stub_section) (const char *, asection *),
   10075  1.1  christos    void (*layout_sections_again) (void))
   10076  1.1  christos {
   10077  1.1  christos   bfd *input_bfd;
   10078  1.1  christos   int top_id, top_index, id;
   10079  1.1  christos   asection *section;
   10080  1.1  christos   asection **input_list;
   10081  1.1  christos   bfd_size_type amt;
   10082  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10083  1.1  christos 
   10084  1.1  christos   if (htab == NULL)
   10085  1.1  christos     return -1;
   10086  1.1  christos   /* Stash our params away.  */
   10087  1.1  christos   htab->add_stub_section = add_stub_section;
   10088  1.1  christos   htab->layout_sections_again = layout_sections_again;
   10089  1.1  christos 
   10090  1.1  christos   if (htab->brlt == NULL)
   10091  1.1  christos     return 0;
   10092  1.1  christos 
   10093  1.1  christos   /* Find the top input section id.  */
   10094  1.1  christos   for (input_bfd = info->input_bfds, top_id = 3;
   10095  1.1  christos        input_bfd != NULL;
   10096  1.1  christos        input_bfd = input_bfd->link_next)
   10097  1.1  christos     {
   10098  1.1  christos       for (section = input_bfd->sections;
   10099  1.1  christos 	   section != NULL;
   10100  1.1  christos 	   section = section->next)
   10101  1.1  christos 	{
   10102  1.1  christos 	  if (top_id < section->id)
   10103  1.1  christos 	    top_id = section->id;
   10104  1.1  christos 	}
   10105  1.1  christos     }
   10106  1.1  christos 
   10107  1.1  christos   htab->top_id = top_id;
   10108  1.1  christos   amt = sizeof (struct map_stub) * (top_id + 1);
   10109  1.1  christos   htab->stub_group = bfd_zmalloc (amt);
   10110  1.1  christos   if (htab->stub_group == NULL)
   10111  1.1  christos     return -1;
   10112  1.1  christos 
   10113  1.1  christos   /* Set toc_off for com, und, abs and ind sections.  */
   10114  1.1  christos   for (id = 0; id < 3; id++)
   10115  1.1  christos     htab->stub_group[id].toc_off = TOC_BASE_OFF;
   10116  1.1  christos 
   10117  1.1  christos   /* We can't use output_bfd->section_count here to find the top output
   10118  1.1  christos      section index as some sections may have been removed, and
   10119  1.1  christos      strip_excluded_output_sections doesn't renumber the indices.  */
   10120  1.1  christos   for (section = info->output_bfd->sections, top_index = 0;
   10121  1.1  christos        section != NULL;
   10122  1.1  christos        section = section->next)
   10123  1.1  christos     {
   10124  1.1  christos       if (top_index < section->index)
   10125  1.1  christos 	top_index = section->index;
   10126  1.1  christos     }
   10127  1.1  christos 
   10128  1.1  christos   htab->top_index = top_index;
   10129  1.1  christos   amt = sizeof (asection *) * (top_index + 1);
   10130  1.1  christos   input_list = bfd_zmalloc (amt);
   10131  1.1  christos   htab->input_list = input_list;
   10132  1.1  christos   if (input_list == NULL)
   10133  1.1  christos     return -1;
   10134  1.1  christos 
   10135  1.1  christos   return 1;
   10136  1.1  christos }
   10137  1.1  christos 
   10138  1.1  christos /* Set up for first pass at multitoc partitioning.  */
   10139  1.1  christos 
   10140  1.1  christos void
   10141  1.1  christos ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   10142  1.1  christos {
   10143  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10144  1.1  christos 
   10145  1.1  christos   elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
   10146  1.1  christos   htab->toc_curr = elf_gp (info->output_bfd);
   10147  1.1  christos   htab->toc_bfd = NULL;
   10148  1.1  christos   htab->toc_first_sec = NULL;
   10149  1.1  christos }
   10150  1.1  christos 
   10151  1.1  christos /* The linker repeatedly calls this function for each TOC input section
   10152  1.1  christos    and linker generated GOT section.  Group input bfds such that the toc
   10153  1.1  christos    within a group is less than 64k in size.  */
   10154  1.1  christos 
   10155  1.1  christos bfd_boolean
   10156  1.1  christos ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   10157  1.1  christos {
   10158  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10159  1.1  christos   bfd_vma addr, off, limit;
   10160  1.1  christos 
   10161  1.1  christos   if (htab == NULL)
   10162  1.1  christos     return FALSE;
   10163  1.1  christos 
   10164  1.1  christos   if (!htab->second_toc_pass)
   10165  1.1  christos     {
   10166  1.1  christos       /* Keep track of the first .toc or .got section for this input bfd.  */
   10167  1.1  christos       if (htab->toc_bfd != isec->owner)
   10168  1.1  christos 	{
   10169  1.1  christos 	  htab->toc_bfd = isec->owner;
   10170  1.1  christos 	  htab->toc_first_sec = isec;
   10171  1.1  christos 	}
   10172  1.1  christos 
   10173  1.1  christos       addr = isec->output_offset + isec->output_section->vma;
   10174  1.1  christos       off = addr - htab->toc_curr;
   10175  1.1  christos       limit = 0x80008000;
   10176  1.1  christos       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   10177  1.1  christos 	limit = 0x10000;
   10178  1.1  christos       if (off + isec->size > limit)
   10179  1.1  christos 	{
   10180  1.1  christos 	  addr = (htab->toc_first_sec->output_offset
   10181  1.1  christos 		  + htab->toc_first_sec->output_section->vma);
   10182  1.1  christos 	  htab->toc_curr = addr;
   10183  1.1  christos 	}
   10184  1.1  christos 
   10185  1.1  christos       /* toc_curr is the base address of this toc group.  Set elf_gp
   10186  1.1  christos 	 for the input section to be the offset relative to the
   10187  1.1  christos 	 output toc base plus 0x8000.  Making the input elf_gp an
   10188  1.1  christos 	 offset allows us to move the toc as a whole without
   10189  1.1  christos 	 recalculating input elf_gp.  */
   10190  1.1  christos       off = htab->toc_curr - elf_gp (isec->output_section->owner);
   10191  1.1  christos       off += TOC_BASE_OFF;
   10192  1.1  christos 
   10193  1.1  christos       /* Die if someone uses a linker script that doesn't keep input
   10194  1.1  christos 	 file .toc and .got together.  */
   10195  1.1  christos       if (elf_gp (isec->owner) != 0
   10196  1.1  christos 	  && elf_gp (isec->owner) != off)
   10197  1.1  christos 	return FALSE;
   10198  1.1  christos 
   10199  1.1  christos       elf_gp (isec->owner) = off;
   10200  1.1  christos       return TRUE;
   10201  1.1  christos     }
   10202  1.1  christos 
   10203  1.1  christos   /* During the second pass toc_first_sec points to the start of
   10204  1.1  christos      a toc group, and toc_curr is used to track the old elf_gp.
   10205  1.1  christos      We use toc_bfd to ensure we only look at each bfd once.  */
   10206  1.1  christos   if (htab->toc_bfd == isec->owner)
   10207  1.1  christos     return TRUE;
   10208  1.1  christos   htab->toc_bfd = isec->owner;
   10209  1.1  christos 
   10210  1.1  christos   if (htab->toc_first_sec == NULL
   10211  1.1  christos       || htab->toc_curr != elf_gp (isec->owner))
   10212  1.1  christos     {
   10213  1.1  christos       htab->toc_curr = elf_gp (isec->owner);
   10214  1.1  christos       htab->toc_first_sec = isec;
   10215  1.1  christos     }
   10216  1.1  christos   addr = (htab->toc_first_sec->output_offset
   10217  1.1  christos 	  + htab->toc_first_sec->output_section->vma);
   10218  1.1  christos   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
   10219  1.1  christos   elf_gp (isec->owner) = off;
   10220  1.1  christos 
   10221  1.1  christos   return TRUE;
   10222  1.1  christos }
   10223  1.1  christos 
   10224  1.1  christos /* Called via elf_link_hash_traverse to merge GOT entries for global
   10225  1.1  christos    symbol H.  */
   10226  1.1  christos 
   10227  1.1  christos static bfd_boolean
   10228  1.1  christos merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   10229  1.1  christos {
   10230  1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   10231  1.1  christos     return TRUE;
   10232  1.1  christos 
   10233  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   10234  1.1  christos     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10235  1.1  christos 
   10236  1.1  christos   merge_got_entries (&h->got.glist);
   10237  1.1  christos 
   10238  1.1  christos   return TRUE;
   10239  1.1  christos }
   10240  1.1  christos 
   10241  1.1  christos /* Called via elf_link_hash_traverse to allocate GOT entries for global
   10242  1.1  christos    symbol H.  */
   10243  1.1  christos 
   10244  1.1  christos static bfd_boolean
   10245  1.1  christos reallocate_got (struct elf_link_hash_entry *h, void *inf)
   10246  1.1  christos {
   10247  1.1  christos   struct got_entry *gent;
   10248  1.1  christos 
   10249  1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   10250  1.1  christos     return TRUE;
   10251  1.1  christos 
   10252  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   10253  1.1  christos     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10254  1.1  christos 
   10255  1.1  christos   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   10256  1.1  christos     if (!gent->is_indirect)
   10257  1.1  christos       allocate_got (h, (struct bfd_link_info *) inf, gent);
   10258  1.1  christos   return TRUE;
   10259  1.1  christos }
   10260  1.1  christos 
   10261  1.1  christos /* Called on the first multitoc pass after the last call to
   10262  1.1  christos    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   10263  1.1  christos    entries.  */
   10264  1.1  christos 
   10265  1.1  christos bfd_boolean
   10266  1.1  christos ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   10267  1.1  christos {
   10268  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10269  1.1  christos   struct bfd *ibfd, *ibfd2;
   10270  1.1  christos   bfd_boolean done_something;
   10271  1.1  christos 
   10272  1.1  christos   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   10273  1.1  christos 
   10274  1.1  christos   if (!htab->do_multi_toc)
   10275  1.1  christos     return FALSE;
   10276  1.1  christos 
   10277  1.1  christos   /* Merge global sym got entries within a toc group.  */
   10278  1.1  christos   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   10279  1.1  christos 
   10280  1.1  christos   /* And tlsld_got.  */
   10281  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10282  1.1  christos     {
   10283  1.1  christos       struct got_entry *ent, *ent2;
   10284  1.1  christos 
   10285  1.1  christos       if (!is_ppc64_elf (ibfd))
   10286  1.1  christos 	continue;
   10287  1.1  christos 
   10288  1.1  christos       ent = ppc64_tlsld_got (ibfd);
   10289  1.1  christos       if (!ent->is_indirect
   10290  1.1  christos 	  && ent->got.offset != (bfd_vma) -1)
   10291  1.1  christos 	{
   10292  1.1  christos 	  for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
   10293  1.1  christos 	    {
   10294  1.1  christos 	      if (!is_ppc64_elf (ibfd2))
   10295  1.1  christos 		continue;
   10296  1.1  christos 
   10297  1.1  christos 	      ent2 = ppc64_tlsld_got (ibfd2);
   10298  1.1  christos 	      if (!ent2->is_indirect
   10299  1.1  christos 		  && ent2->got.offset != (bfd_vma) -1
   10300  1.1  christos 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   10301  1.1  christos 		{
   10302  1.1  christos 		  ent2->is_indirect = TRUE;
   10303  1.1  christos 		  ent2->got.ent = ent;
   10304  1.1  christos 		}
   10305  1.1  christos 	    }
   10306  1.1  christos 	}
   10307  1.1  christos     }
   10308  1.1  christos 
   10309  1.1  christos   /* Zap sizes of got sections.  */
   10310  1.1  christos   htab->reliplt->rawsize = htab->reliplt->size;
   10311  1.1  christos   htab->reliplt->size -= htab->got_reli_size;
   10312  1.1  christos   htab->got_reli_size = 0;
   10313  1.1  christos 
   10314  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10315  1.1  christos     {
   10316  1.1  christos       asection *got, *relgot;
   10317  1.1  christos 
   10318  1.1  christos       if (!is_ppc64_elf (ibfd))
   10319  1.1  christos 	continue;
   10320  1.1  christos 
   10321  1.1  christos       got = ppc64_elf_tdata (ibfd)->got;
   10322  1.1  christos       if (got != NULL)
   10323  1.1  christos 	{
   10324  1.1  christos 	  got->rawsize = got->size;
   10325  1.1  christos 	  got->size = 0;
   10326  1.1  christos 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   10327  1.1  christos 	  relgot->rawsize = relgot->size;
   10328  1.1  christos 	  relgot->size = 0;
   10329  1.1  christos 	}
   10330  1.1  christos     }
   10331  1.1  christos 
   10332  1.1  christos   /* Now reallocate the got, local syms first.  We don't need to
   10333  1.1  christos      allocate section contents again since we never increase size.  */
   10334  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10335  1.1  christos     {
   10336  1.1  christos       struct got_entry **lgot_ents;
   10337  1.1  christos       struct got_entry **end_lgot_ents;
   10338  1.1  christos       struct plt_entry **local_plt;
   10339  1.1  christos       struct plt_entry **end_local_plt;
   10340  1.1  christos       unsigned char *lgot_masks;
   10341  1.1  christos       bfd_size_type locsymcount;
   10342  1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   10343  1.1  christos       asection *s, *srel;
   10344  1.1  christos 
   10345  1.1  christos       if (!is_ppc64_elf (ibfd))
   10346  1.1  christos 	continue;
   10347  1.1  christos 
   10348  1.1  christos       lgot_ents = elf_local_got_ents (ibfd);
   10349  1.1  christos       if (!lgot_ents)
   10350  1.1  christos 	continue;
   10351  1.1  christos 
   10352  1.1  christos       symtab_hdr = &elf_symtab_hdr (ibfd);
   10353  1.1  christos       locsymcount = symtab_hdr->sh_info;
   10354  1.1  christos       end_lgot_ents = lgot_ents + locsymcount;
   10355  1.1  christos       local_plt = (struct plt_entry **) end_lgot_ents;
   10356  1.1  christos       end_local_plt = local_plt + locsymcount;
   10357  1.1  christos       lgot_masks = (unsigned char *) end_local_plt;
   10358  1.1  christos       s = ppc64_elf_tdata (ibfd)->got;
   10359  1.1  christos       srel = ppc64_elf_tdata (ibfd)->relgot;
   10360  1.1  christos       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   10361  1.1  christos 	{
   10362  1.1  christos 	  struct got_entry *ent;
   10363  1.1  christos 
   10364  1.1  christos 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   10365  1.1  christos 	    {
   10366  1.1  christos 	      unsigned int num = 1;
   10367  1.1  christos 	      ent->got.offset = s->size;
   10368  1.1  christos 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   10369  1.1  christos 		num = 2;
   10370  1.1  christos 	      s->size += num * 8;
   10371  1.1  christos 	      if (info->shared)
   10372  1.1  christos 		srel->size += num * sizeof (Elf64_External_Rela);
   10373  1.1  christos 	      else if ((*lgot_masks & PLT_IFUNC) != 0)
   10374  1.1  christos 		{
   10375  1.1  christos 		  htab->reliplt->size
   10376  1.1  christos 		    += num * sizeof (Elf64_External_Rela);
   10377  1.1  christos 		  htab->got_reli_size
   10378  1.1  christos 		    += num * sizeof (Elf64_External_Rela);
   10379  1.1  christos 		}
   10380  1.1  christos 	    }
   10381  1.1  christos 	}
   10382  1.1  christos     }
   10383  1.1  christos 
   10384  1.1  christos   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   10385  1.1  christos 
   10386  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10387  1.1  christos     {
   10388  1.1  christos       struct got_entry *ent;
   10389  1.1  christos 
   10390  1.1  christos       if (!is_ppc64_elf (ibfd))
   10391  1.1  christos 	continue;
   10392  1.1  christos 
   10393  1.1  christos       ent = ppc64_tlsld_got (ibfd);
   10394  1.1  christos       if (!ent->is_indirect
   10395  1.1  christos 	  && ent->got.offset != (bfd_vma) -1)
   10396  1.1  christos 	{
   10397  1.1  christos 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   10398  1.1  christos 	  ent->got.offset = s->size;
   10399  1.1  christos 	  s->size += 16;
   10400  1.1  christos 	  if (info->shared)
   10401  1.1  christos 	    {
   10402  1.1  christos 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10403  1.1  christos 	      srel->size += sizeof (Elf64_External_Rela);
   10404  1.1  christos 	    }
   10405  1.1  christos 	}
   10406  1.1  christos     }
   10407  1.1  christos 
   10408  1.1  christos   done_something = htab->reliplt->rawsize != htab->reliplt->size;
   10409  1.1  christos   if (!done_something)
   10410  1.1  christos     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10411  1.1  christos       {
   10412  1.1  christos 	asection *got;
   10413  1.1  christos 
   10414  1.1  christos 	if (!is_ppc64_elf (ibfd))
   10415  1.1  christos 	  continue;
   10416  1.1  christos 
   10417  1.1  christos 	got = ppc64_elf_tdata (ibfd)->got;
   10418  1.1  christos 	if (got != NULL)
   10419  1.1  christos 	  {
   10420  1.1  christos 	    done_something = got->rawsize != got->size;
   10421  1.1  christos 	    if (done_something)
   10422  1.1  christos 	      break;
   10423  1.1  christos 	  }
   10424  1.1  christos       }
   10425  1.1  christos 
   10426  1.1  christos   if (done_something)
   10427  1.1  christos     (*htab->layout_sections_again) ();
   10428  1.1  christos 
   10429  1.1  christos   /* Set up for second pass over toc sections to recalculate elf_gp
   10430  1.1  christos      on input sections.  */
   10431  1.1  christos   htab->toc_bfd = NULL;
   10432  1.1  christos   htab->toc_first_sec = NULL;
   10433  1.1  christos   htab->second_toc_pass = TRUE;
   10434  1.1  christos   return done_something;
   10435  1.1  christos }
   10436  1.1  christos 
   10437  1.1  christos /* Called after second pass of multitoc partitioning.  */
   10438  1.1  christos 
   10439  1.1  christos void
   10440  1.1  christos ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   10441  1.1  christos {
   10442  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10443  1.1  christos 
   10444  1.1  christos   /* After the second pass, toc_curr tracks the TOC offset used
   10445  1.1  christos      for code sections below in ppc64_elf_next_input_section.  */
   10446  1.1  christos   htab->toc_curr = TOC_BASE_OFF;
   10447  1.1  christos }
   10448  1.1  christos 
   10449  1.1  christos /* No toc references were found in ISEC.  If the code in ISEC makes no
   10450  1.1  christos    calls, then there's no need to use toc adjusting stubs when branching
   10451  1.1  christos    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   10452  1.1  christos    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   10453  1.1  christos    needed, and 2 if a cyclical call-graph was found but no other reason
   10454  1.1  christos    for a stub was detected.  If called from the top level, a return of
   10455  1.1  christos    2 means the same as a return of 0.  */
   10456  1.1  christos 
   10457  1.1  christos static int
   10458  1.1  christos toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   10459  1.1  christos {
   10460  1.1  christos   int ret;
   10461  1.1  christos 
   10462  1.1  christos   /* Mark this section as checked.  */
   10463  1.1  christos   isec->call_check_done = 1;
   10464  1.1  christos 
   10465  1.1  christos   /* We know none of our code bearing sections will need toc stubs.  */
   10466  1.1  christos   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   10467  1.1  christos     return 0;
   10468  1.1  christos 
   10469  1.1  christos   if (isec->size == 0)
   10470  1.1  christos     return 0;
   10471  1.1  christos 
   10472  1.1  christos   if (isec->output_section == NULL)
   10473  1.1  christos     return 0;
   10474  1.1  christos 
   10475  1.1  christos   ret = 0;
   10476  1.1  christos   if (isec->reloc_count != 0)
   10477  1.1  christos     {
   10478  1.1  christos       Elf_Internal_Rela *relstart, *rel;
   10479  1.1  christos       Elf_Internal_Sym *local_syms;
   10480  1.1  christos       struct ppc_link_hash_table *htab;
   10481  1.1  christos 
   10482  1.1  christos       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   10483  1.1  christos 					    info->keep_memory);
   10484  1.1  christos       if (relstart == NULL)
   10485  1.1  christos 	return -1;
   10486  1.1  christos 
   10487  1.1  christos       /* Look for branches to outside of this section.  */
   10488  1.1  christos       local_syms = NULL;
   10489  1.1  christos       htab = ppc_hash_table (info);
   10490  1.1  christos       if (htab == NULL)
   10491  1.1  christos 	return -1;
   10492  1.1  christos 
   10493  1.1  christos       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   10494  1.1  christos 	{
   10495  1.1  christos 	  enum elf_ppc64_reloc_type r_type;
   10496  1.1  christos 	  unsigned long r_symndx;
   10497  1.1  christos 	  struct elf_link_hash_entry *h;
   10498  1.1  christos 	  struct ppc_link_hash_entry *eh;
   10499  1.1  christos 	  Elf_Internal_Sym *sym;
   10500  1.1  christos 	  asection *sym_sec;
   10501  1.1  christos 	  struct _opd_sec_data *opd;
   10502  1.1  christos 	  bfd_vma sym_value;
   10503  1.1  christos 	  bfd_vma dest;
   10504  1.1  christos 
   10505  1.1  christos 	  r_type = ELF64_R_TYPE (rel->r_info);
   10506  1.1  christos 	  if (r_type != R_PPC64_REL24
   10507  1.1  christos 	      && r_type != R_PPC64_REL14
   10508  1.1  christos 	      && r_type != R_PPC64_REL14_BRTAKEN
   10509  1.1  christos 	      && r_type != R_PPC64_REL14_BRNTAKEN)
   10510  1.1  christos 	    continue;
   10511  1.1  christos 
   10512  1.1  christos 	  r_symndx = ELF64_R_SYM (rel->r_info);
   10513  1.1  christos 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   10514  1.1  christos 			  isec->owner))
   10515  1.1  christos 	    {
   10516  1.1  christos 	      ret = -1;
   10517  1.1  christos 	      break;
   10518  1.1  christos 	    }
   10519  1.1  christos 
   10520  1.1  christos 	  /* Calls to dynamic lib functions go through a plt call stub
   10521  1.1  christos 	     that uses r2.  */
   10522  1.1  christos 	  eh = (struct ppc_link_hash_entry *) h;
   10523  1.1  christos 	  if (eh != NULL
   10524  1.1  christos 	      && (eh->elf.plt.plist != NULL
   10525  1.1  christos 		  || (eh->oh != NULL
   10526  1.1  christos 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   10527  1.1  christos 	    {
   10528  1.1  christos 	      ret = 1;
   10529  1.1  christos 	      break;
   10530  1.1  christos 	    }
   10531  1.1  christos 
   10532  1.1  christos 	  if (sym_sec == NULL)
   10533  1.1  christos 	    /* Ignore other undefined symbols.  */
   10534  1.1  christos 	    continue;
   10535  1.1  christos 
   10536  1.1  christos 	  /* Assume branches to other sections not included in the
   10537  1.1  christos 	     link need stubs too, to cover -R and absolute syms.  */
   10538  1.1  christos 	  if (sym_sec->output_section == NULL)
   10539  1.1  christos 	    {
   10540  1.1  christos 	      ret = 1;
   10541  1.1  christos 	      break;
   10542  1.1  christos 	    }
   10543  1.1  christos 
   10544  1.1  christos 	  if (h == NULL)
   10545  1.1  christos 	    sym_value = sym->st_value;
   10546  1.1  christos 	  else
   10547  1.1  christos 	    {
   10548  1.1  christos 	      if (h->root.type != bfd_link_hash_defined
   10549  1.1  christos 		  && h->root.type != bfd_link_hash_defweak)
   10550  1.1  christos 		abort ();
   10551  1.1  christos 	      sym_value = h->root.u.def.value;
   10552  1.1  christos 	    }
   10553  1.1  christos 	  sym_value += rel->r_addend;
   10554  1.1  christos 
   10555  1.1  christos 	  /* If this branch reloc uses an opd sym, find the code section.  */
   10556  1.1  christos 	  opd = get_opd_info (sym_sec);
   10557  1.1  christos 	  if (opd != NULL)
   10558  1.1  christos 	    {
   10559  1.1  christos 	      if (h == NULL && opd->adjust != NULL)
   10560  1.1  christos 		{
   10561  1.1  christos 		  long adjust;
   10562  1.1  christos 
   10563  1.1  christos 		  adjust = opd->adjust[sym->st_value / 8];
   10564  1.1  christos 		  if (adjust == -1)
   10565  1.1  christos 		    /* Assume deleted functions won't ever be called.  */
   10566  1.1  christos 		    continue;
   10567  1.1  christos 		  sym_value += adjust;
   10568  1.1  christos 		}
   10569  1.1  christos 
   10570  1.1  christos 	      dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
   10571  1.1  christos 	      if (dest == (bfd_vma) -1)
   10572  1.1  christos 		continue;
   10573  1.1  christos 	    }
   10574  1.1  christos 	  else
   10575  1.1  christos 	    dest = (sym_value
   10576  1.1  christos 		    + sym_sec->output_offset
   10577  1.1  christos 		    + sym_sec->output_section->vma);
   10578  1.1  christos 
   10579  1.1  christos 	  /* Ignore branch to self.  */
   10580  1.1  christos 	  if (sym_sec == isec)
   10581  1.1  christos 	    continue;
   10582  1.1  christos 
   10583  1.1  christos 	  /* If the called function uses the toc, we need a stub.  */
   10584  1.1  christos 	  if (sym_sec->has_toc_reloc
   10585  1.1  christos 	      || sym_sec->makes_toc_func_call)
   10586  1.1  christos 	    {
   10587  1.1  christos 	      ret = 1;
   10588  1.1  christos 	      break;
   10589  1.1  christos 	    }
   10590  1.1  christos 
   10591  1.1  christos 	  /* Assume any branch that needs a long branch stub might in fact
   10592  1.1  christos 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   10593  1.1  christos 	  else if (dest - (isec->output_offset
   10594  1.1  christos 			   + isec->output_section->vma
   10595  1.1  christos 			   + rel->r_offset) + (1 << 25) >= (2 << 25))
   10596  1.1  christos 	    {
   10597  1.1  christos 	      ret = 1;
   10598  1.1  christos 	      break;
   10599  1.1  christos 	    }
   10600  1.1  christos 
   10601  1.1  christos 	  /* If calling back to a section in the process of being
   10602  1.1  christos 	     tested, we can't say for sure that no toc adjusting stubs
   10603  1.1  christos 	     are needed, so don't return zero.  */
   10604  1.1  christos 	  else if (sym_sec->call_check_in_progress)
   10605  1.1  christos 	    ret = 2;
   10606  1.1  christos 
   10607  1.1  christos 	  /* Branches to another section that itself doesn't have any TOC
   10608  1.1  christos 	     references are OK.  Recursively call ourselves to check.  */
   10609  1.1  christos 	  else if (!sym_sec->call_check_done)
   10610  1.1  christos 	    {
   10611  1.1  christos 	      int recur;
   10612  1.1  christos 
   10613  1.1  christos 	      /* Mark current section as indeterminate, so that other
   10614  1.1  christos 		 sections that call back to current won't be marked as
   10615  1.1  christos 		 known.  */
   10616  1.1  christos 	      isec->call_check_in_progress = 1;
   10617  1.1  christos 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   10618  1.1  christos 	      isec->call_check_in_progress = 0;
   10619  1.1  christos 
   10620  1.1  christos 	      if (recur != 0)
   10621  1.1  christos 		{
   10622  1.1  christos 		  ret = recur;
   10623  1.1  christos 		  if (recur != 2)
   10624  1.1  christos 		    break;
   10625  1.1  christos 		}
   10626  1.1  christos 	    }
   10627  1.1  christos 	}
   10628  1.1  christos 
   10629  1.1  christos       if (local_syms != NULL
   10630  1.1  christos 	  && (elf_symtab_hdr (isec->owner).contents
   10631  1.1  christos 	      != (unsigned char *) local_syms))
   10632  1.1  christos 	free (local_syms);
   10633  1.1  christos       if (elf_section_data (isec)->relocs != relstart)
   10634  1.1  christos 	free (relstart);
   10635  1.1  christos     }
   10636  1.1  christos 
   10637  1.1  christos   if ((ret & 1) == 0
   10638  1.1  christos       && isec->map_head.s != NULL
   10639  1.1  christos       && (strcmp (isec->output_section->name, ".init") == 0
   10640  1.1  christos 	  || strcmp (isec->output_section->name, ".fini") == 0))
   10641  1.1  christos     {
   10642  1.1  christos       if (isec->map_head.s->has_toc_reloc
   10643  1.1  christos 	  || isec->map_head.s->makes_toc_func_call)
   10644  1.1  christos 	ret = 1;
   10645  1.1  christos       else if (!isec->map_head.s->call_check_done)
   10646  1.1  christos 	{
   10647  1.1  christos 	  int recur;
   10648  1.1  christos 	  isec->call_check_in_progress = 1;
   10649  1.1  christos 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   10650  1.1  christos 	  isec->call_check_in_progress = 0;
   10651  1.1  christos 	  if (recur != 0)
   10652  1.1  christos 	    ret = recur;
   10653  1.1  christos 	}
   10654  1.1  christos     }
   10655  1.1  christos 
   10656  1.1  christos   if (ret == 1)
   10657  1.1  christos     isec->makes_toc_func_call = 1;
   10658  1.1  christos 
   10659  1.1  christos   return ret;
   10660  1.1  christos }
   10661  1.1  christos 
   10662  1.1  christos /* The linker repeatedly calls this function for each input section,
   10663  1.1  christos    in the order that input sections are linked into output sections.
   10664  1.1  christos    Build lists of input sections to determine groupings between which
   10665  1.1  christos    we may insert linker stubs.  */
   10666  1.1  christos 
   10667  1.1  christos bfd_boolean
   10668  1.1  christos ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   10669  1.1  christos {
   10670  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10671  1.1  christos 
   10672  1.1  christos   if (htab == NULL)
   10673  1.1  christos     return FALSE;
   10674  1.1  christos 
   10675  1.1  christos   if ((isec->output_section->flags & SEC_CODE) != 0
   10676  1.1  christos       && isec->output_section->index <= htab->top_index)
   10677  1.1  christos     {
   10678  1.1  christos       asection **list = htab->input_list + isec->output_section->index;
   10679  1.1  christos       /* Steal the link_sec pointer for our list.  */
   10680  1.1  christos #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   10681  1.1  christos       /* This happens to make the list in reverse order,
   10682  1.1  christos 	 which is what we want.  */
   10683  1.1  christos       PREV_SEC (isec) = *list;
   10684  1.1  christos       *list = isec;
   10685  1.1  christos     }
   10686  1.1  christos 
   10687  1.1  christos   if (htab->multi_toc_needed)
   10688  1.1  christos     {
   10689  1.1  christos       /* If a code section has a function that uses the TOC then we need
   10690  1.1  christos 	 to use the right TOC (obviously).  Also, make sure that .opd gets
   10691  1.1  christos 	 the correct TOC value for R_PPC64_TOC relocs that don't have or
   10692  1.1  christos 	 can't find their function symbol (shouldn't ever happen now).
   10693  1.1  christos 	 Also specially treat .fixup for the linux kernel.  .fixup
   10694  1.1  christos 	 contains branches, but only back to the function that hit an
   10695  1.1  christos 	 exception.  */
   10696  1.1  christos       if (isec->has_toc_reloc
   10697  1.1  christos 	  || (isec->flags & SEC_CODE) == 0
   10698  1.1  christos 	  || strcmp (isec->name, ".fixup") == 0)
   10699  1.1  christos 	{
   10700  1.1  christos 	  if (elf_gp (isec->owner) != 0)
   10701  1.1  christos 	    htab->toc_curr = elf_gp (isec->owner);
   10702  1.1  christos 	}
   10703  1.1  christos       else
   10704  1.1  christos 	{
   10705  1.1  christos 	  if (!isec->call_check_done
   10706  1.1  christos 	      && toc_adjusting_stub_needed (info, isec) < 0)
   10707  1.1  christos 	    return FALSE;
   10708  1.1  christos 	  /* If we make a local call from this section, ie. a branch
   10709  1.1  christos 	     without a following nop, then we have no place to put a
   10710  1.1  christos 	     toc restoring insn.  We must use the same toc group as
   10711  1.1  christos 	     the callee.
   10712  1.1  christos 	     Testing makes_toc_func_call actually tests for *any*
   10713  1.1  christos 	     calls to functions that need a good toc pointer.  A more
   10714  1.1  christos 	     precise test would be better, as this one will set
   10715  1.1  christos 	     incorrect values for pasted .init/.fini fragments.
   10716  1.1  christos 	     (Fixed later in check_pasted_section.)  */
   10717  1.1  christos 	  if (isec->makes_toc_func_call
   10718  1.1  christos 	      && elf_gp (isec->owner) != 0)
   10719  1.1  christos 	    htab->toc_curr = elf_gp (isec->owner);
   10720  1.1  christos 	}
   10721  1.1  christos     }
   10722  1.1  christos 
   10723  1.1  christos   /* Functions that don't use the TOC can belong in any TOC group.
   10724  1.1  christos      Use the last TOC base.  */
   10725  1.1  christos   htab->stub_group[isec->id].toc_off = htab->toc_curr;
   10726  1.1  christos   return TRUE;
   10727  1.1  christos }
   10728  1.1  christos 
   10729  1.1  christos /* Check that all .init and .fini sections use the same toc, if they
   10730  1.1  christos    have toc relocs.  */
   10731  1.1  christos 
   10732  1.1  christos static bfd_boolean
   10733  1.1  christos check_pasted_section (struct bfd_link_info *info, const char *name)
   10734  1.1  christos {
   10735  1.1  christos   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   10736  1.1  christos 
   10737  1.1  christos   if (o != NULL)
   10738  1.1  christos     {
   10739  1.1  christos       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10740  1.1  christos       bfd_vma toc_off = 0;
   10741  1.1  christos       asection *i;
   10742  1.1  christos 
   10743  1.1  christos       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   10744  1.1  christos 	if (i->has_toc_reloc)
   10745  1.1  christos 	  {
   10746  1.1  christos 	    if (toc_off == 0)
   10747  1.1  christos 	      toc_off = htab->stub_group[i->id].toc_off;
   10748  1.1  christos 	    else if (toc_off != htab->stub_group[i->id].toc_off)
   10749  1.1  christos 	      return FALSE;
   10750  1.1  christos 	  }
   10751  1.1  christos 
   10752  1.1  christos       if (toc_off == 0)
   10753  1.1  christos 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   10754  1.1  christos 	  if (i->makes_toc_func_call)
   10755  1.1  christos 	    {
   10756  1.1  christos 	      toc_off = htab->stub_group[i->id].toc_off;
   10757  1.1  christos 	      break;
   10758  1.1  christos 	    }
   10759  1.1  christos 
   10760  1.1  christos       /* Make sure the whole pasted function uses the same toc offset.  */
   10761  1.1  christos       if (toc_off != 0)
   10762  1.1  christos 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   10763  1.1  christos 	  htab->stub_group[i->id].toc_off = toc_off;
   10764  1.1  christos     }
   10765  1.1  christos   return TRUE;
   10766  1.1  christos }
   10767  1.1  christos 
   10768  1.1  christos bfd_boolean
   10769  1.1  christos ppc64_elf_check_init_fini (struct bfd_link_info *info)
   10770  1.1  christos {
   10771  1.1  christos   return (check_pasted_section (info, ".init")
   10772  1.1  christos 	  & check_pasted_section (info, ".fini"));
   10773  1.1  christos }
   10774  1.1  christos 
   10775  1.1  christos /* See whether we can group stub sections together.  Grouping stub
   10776  1.1  christos    sections may result in fewer stubs.  More importantly, we need to
   10777  1.1  christos    put all .init* and .fini* stubs at the beginning of the .init or
   10778  1.1  christos    .fini output sections respectively, because glibc splits the
   10779  1.1  christos    _init and _fini functions into multiple parts.  Putting a stub in
   10780  1.1  christos    the middle of a function is not a good idea.  */
   10781  1.1  christos 
   10782  1.1  christos static void
   10783  1.1  christos group_sections (struct ppc_link_hash_table *htab,
   10784  1.1  christos 		bfd_size_type stub_group_size,
   10785  1.1  christos 		bfd_boolean stubs_always_before_branch)
   10786  1.1  christos {
   10787  1.1  christos   asection **list;
   10788  1.1  christos   bfd_size_type stub14_group_size;
   10789  1.1  christos   bfd_boolean suppress_size_errors;
   10790  1.1  christos 
   10791  1.1  christos   suppress_size_errors = FALSE;
   10792  1.1  christos   stub14_group_size = stub_group_size;
   10793  1.1  christos   if (stub_group_size == 1)
   10794  1.1  christos     {
   10795  1.1  christos       /* Default values.  */
   10796  1.1  christos       if (stubs_always_before_branch)
   10797  1.1  christos 	{
   10798  1.1  christos 	  stub_group_size = 0x1e00000;
   10799  1.1  christos 	  stub14_group_size = 0x7800;
   10800  1.1  christos 	}
   10801  1.1  christos       else
   10802  1.1  christos 	{
   10803  1.1  christos 	  stub_group_size = 0x1c00000;
   10804  1.1  christos 	  stub14_group_size = 0x7000;
   10805  1.1  christos 	}
   10806  1.1  christos       suppress_size_errors = TRUE;
   10807  1.1  christos     }
   10808  1.1  christos 
   10809  1.1  christos   list = htab->input_list + htab->top_index;
   10810  1.1  christos   do
   10811  1.1  christos     {
   10812  1.1  christos       asection *tail = *list;
   10813  1.1  christos       while (tail != NULL)
   10814  1.1  christos 	{
   10815  1.1  christos 	  asection *curr;
   10816  1.1  christos 	  asection *prev;
   10817  1.1  christos 	  bfd_size_type total;
   10818  1.1  christos 	  bfd_boolean big_sec;
   10819  1.1  christos 	  bfd_vma curr_toc;
   10820  1.1  christos 
   10821  1.1  christos 	  curr = tail;
   10822  1.1  christos 	  total = tail->size;
   10823  1.1  christos 	  big_sec = total > (ppc64_elf_section_data (tail) != NULL
   10824  1.1  christos 			     && ppc64_elf_section_data (tail)->has_14bit_branch
   10825  1.1  christos 			     ? stub14_group_size : stub_group_size);
   10826  1.1  christos 	  if (big_sec && !suppress_size_errors)
   10827  1.1  christos 	    (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
   10828  1.1  christos 				     tail->owner, tail);
   10829  1.1  christos 	  curr_toc = htab->stub_group[tail->id].toc_off;
   10830  1.1  christos 
   10831  1.1  christos 	  while ((prev = PREV_SEC (curr)) != NULL
   10832  1.1  christos 		 && ((total += curr->output_offset - prev->output_offset)
   10833  1.1  christos 		     < (ppc64_elf_section_data (prev) != NULL
   10834  1.1  christos 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   10835  1.1  christos 			? stub14_group_size : stub_group_size))
   10836  1.1  christos 		 && htab->stub_group[prev->id].toc_off == curr_toc)
   10837  1.1  christos 	    curr = prev;
   10838  1.1  christos 
   10839  1.1  christos 	  /* OK, the size from the start of CURR to the end is less
   10840  1.1  christos 	     than stub_group_size and thus can be handled by one stub
   10841  1.1  christos 	     section.  (or the tail section is itself larger than
   10842  1.1  christos 	     stub_group_size, in which case we may be toast.)  We
   10843  1.1  christos 	     should really be keeping track of the total size of stubs
   10844  1.1  christos 	     added here, as stubs contribute to the final output
   10845  1.1  christos 	     section size.  That's a little tricky, and this way will
   10846  1.1  christos 	     only break if stubs added make the total size more than
   10847  1.1  christos 	     2^25, ie. for the default stub_group_size, if stubs total
   10848  1.1  christos 	     more than 2097152 bytes, or nearly 75000 plt call stubs.  */
   10849  1.1  christos 	  do
   10850  1.1  christos 	    {
   10851  1.1  christos 	      prev = PREV_SEC (tail);
   10852  1.1  christos 	      /* Set up this stub group.  */
   10853  1.1  christos 	      htab->stub_group[tail->id].link_sec = curr;
   10854  1.1  christos 	    }
   10855  1.1  christos 	  while (tail != curr && (tail = prev) != NULL);
   10856  1.1  christos 
   10857  1.1  christos 	  /* But wait, there's more!  Input sections up to stub_group_size
   10858  1.1  christos 	     bytes before the stub section can be handled by it too.
   10859  1.1  christos 	     Don't do this if we have a really large section after the
   10860  1.1  christos 	     stubs, as adding more stubs increases the chance that
   10861  1.1  christos 	     branches may not reach into the stub section.  */
   10862  1.1  christos 	  if (!stubs_always_before_branch && !big_sec)
   10863  1.1  christos 	    {
   10864  1.1  christos 	      total = 0;
   10865  1.1  christos 	      while (prev != NULL
   10866  1.1  christos 		     && ((total += tail->output_offset - prev->output_offset)
   10867  1.1  christos 			 < (ppc64_elf_section_data (prev) != NULL
   10868  1.1  christos 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   10869  1.1  christos 			    ? stub14_group_size : stub_group_size))
   10870  1.1  christos 		     && htab->stub_group[prev->id].toc_off == curr_toc)
   10871  1.1  christos 		{
   10872  1.1  christos 		  tail = prev;
   10873  1.1  christos 		  prev = PREV_SEC (tail);
   10874  1.1  christos 		  htab->stub_group[tail->id].link_sec = curr;
   10875  1.1  christos 		}
   10876  1.1  christos 	    }
   10877  1.1  christos 	  tail = prev;
   10878  1.1  christos 	}
   10879  1.1  christos     }
   10880  1.1  christos   while (list-- != htab->input_list);
   10881  1.1  christos   free (htab->input_list);
   10882  1.1  christos #undef PREV_SEC
   10883  1.1  christos }
   10884  1.1  christos 
   10885  1.1  christos /* Determine and set the size of the stub section for a final link.
   10886  1.1  christos 
   10887  1.1  christos    The basic idea here is to examine all the relocations looking for
   10888  1.1  christos    PC-relative calls to a target that is unreachable with a "bl"
   10889  1.1  christos    instruction.  */
   10890  1.1  christos 
   10891  1.1  christos bfd_boolean
   10892  1.1  christos ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
   10893  1.1  christos {
   10894  1.1  christos   bfd_size_type stub_group_size;
   10895  1.1  christos   bfd_boolean stubs_always_before_branch;
   10896  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10897  1.1  christos 
   10898  1.1  christos   if (htab == NULL)
   10899  1.1  christos     return FALSE;
   10900  1.1  christos 
   10901  1.1  christos   stubs_always_before_branch = group_size < 0;
   10902  1.1  christos   if (group_size < 0)
   10903  1.1  christos     stub_group_size = -group_size;
   10904  1.1  christos   else
   10905  1.1  christos     stub_group_size = group_size;
   10906  1.1  christos 
   10907  1.1  christos   group_sections (htab, stub_group_size, stubs_always_before_branch);
   10908  1.1  christos 
   10909  1.1  christos   while (1)
   10910  1.1  christos     {
   10911  1.1  christos       bfd *input_bfd;
   10912  1.1  christos       unsigned int bfd_indx;
   10913  1.1  christos       asection *stub_sec;
   10914  1.1  christos 
   10915  1.1  christos       htab->stub_iteration += 1;
   10916  1.1  christos 
   10917  1.1  christos       for (input_bfd = info->input_bfds, bfd_indx = 0;
   10918  1.1  christos 	   input_bfd != NULL;
   10919  1.1  christos 	   input_bfd = input_bfd->link_next, bfd_indx++)
   10920  1.1  christos 	{
   10921  1.1  christos 	  Elf_Internal_Shdr *symtab_hdr;
   10922  1.1  christos 	  asection *section;
   10923  1.1  christos 	  Elf_Internal_Sym *local_syms = NULL;
   10924  1.1  christos 
   10925  1.1  christos 	  if (!is_ppc64_elf (input_bfd))
   10926  1.1  christos 	    continue;
   10927  1.1  christos 
   10928  1.1  christos 	  /* We'll need the symbol table in a second.  */
   10929  1.1  christos 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   10930  1.1  christos 	  if (symtab_hdr->sh_info == 0)
   10931  1.1  christos 	    continue;
   10932  1.1  christos 
   10933  1.1  christos 	  /* Walk over each section attached to the input bfd.  */
   10934  1.1  christos 	  for (section = input_bfd->sections;
   10935  1.1  christos 	       section != NULL;
   10936  1.1  christos 	       section = section->next)
   10937  1.1  christos 	    {
   10938  1.1  christos 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   10939  1.1  christos 
   10940  1.1  christos 	      /* If there aren't any relocs, then there's nothing more
   10941  1.1  christos 		 to do.  */
   10942  1.1  christos 	      if ((section->flags & SEC_RELOC) == 0
   10943  1.1  christos 		  || (section->flags & SEC_ALLOC) == 0
   10944  1.1  christos 		  || (section->flags & SEC_LOAD) == 0
   10945  1.1  christos 		  || (section->flags & SEC_CODE) == 0
   10946  1.1  christos 		  || section->reloc_count == 0)
   10947  1.1  christos 		continue;
   10948  1.1  christos 
   10949  1.1  christos 	      /* If this section is a link-once section that will be
   10950  1.1  christos 		 discarded, then don't create any stubs.  */
   10951  1.1  christos 	      if (section->output_section == NULL
   10952  1.1  christos 		  || section->output_section->owner != info->output_bfd)
   10953  1.1  christos 		continue;
   10954  1.1  christos 
   10955  1.1  christos 	      /* Get the relocs.  */
   10956  1.1  christos 	      internal_relocs
   10957  1.1  christos 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   10958  1.1  christos 					     info->keep_memory);
   10959  1.1  christos 	      if (internal_relocs == NULL)
   10960  1.1  christos 		goto error_ret_free_local;
   10961  1.1  christos 
   10962  1.1  christos 	      /* Now examine each relocation.  */
   10963  1.1  christos 	      irela = internal_relocs;
   10964  1.1  christos 	      irelaend = irela + section->reloc_count;
   10965  1.1  christos 	      for (; irela < irelaend; irela++)
   10966  1.1  christos 		{
   10967  1.1  christos 		  enum elf_ppc64_reloc_type r_type;
   10968  1.1  christos 		  unsigned int r_indx;
   10969  1.1  christos 		  enum ppc_stub_type stub_type;
   10970  1.1  christos 		  struct ppc_stub_hash_entry *stub_entry;
   10971  1.1  christos 		  asection *sym_sec, *code_sec;
   10972  1.1  christos 		  bfd_vma sym_value, code_value;
   10973  1.1  christos 		  bfd_vma destination;
   10974  1.1  christos 		  bfd_boolean ok_dest;
   10975  1.1  christos 		  struct ppc_link_hash_entry *hash;
   10976  1.1  christos 		  struct ppc_link_hash_entry *fdh;
   10977  1.1  christos 		  struct elf_link_hash_entry *h;
   10978  1.1  christos 		  Elf_Internal_Sym *sym;
   10979  1.1  christos 		  char *stub_name;
   10980  1.1  christos 		  const asection *id_sec;
   10981  1.1  christos 		  struct _opd_sec_data *opd;
   10982  1.1  christos 		  struct plt_entry *plt_ent;
   10983  1.1  christos 
   10984  1.1  christos 		  r_type = ELF64_R_TYPE (irela->r_info);
   10985  1.1  christos 		  r_indx = ELF64_R_SYM (irela->r_info);
   10986  1.1  christos 
   10987  1.1  christos 		  if (r_type >= R_PPC64_max)
   10988  1.1  christos 		    {
   10989  1.1  christos 		      bfd_set_error (bfd_error_bad_value);
   10990  1.1  christos 		      goto error_ret_free_internal;
   10991  1.1  christos 		    }
   10992  1.1  christos 
   10993  1.1  christos 		  /* Only look for stubs on branch instructions.  */
   10994  1.1  christos 		  if (r_type != R_PPC64_REL24
   10995  1.1  christos 		      && r_type != R_PPC64_REL14
   10996  1.1  christos 		      && r_type != R_PPC64_REL14_BRTAKEN
   10997  1.1  christos 		      && r_type != R_PPC64_REL14_BRNTAKEN)
   10998  1.1  christos 		    continue;
   10999  1.1  christos 
   11000  1.1  christos 		  /* Now determine the call target, its name, value,
   11001  1.1  christos 		     section.  */
   11002  1.1  christos 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   11003  1.1  christos 				  r_indx, input_bfd))
   11004  1.1  christos 		    goto error_ret_free_internal;
   11005  1.1  christos 		  hash = (struct ppc_link_hash_entry *) h;
   11006  1.1  christos 
   11007  1.1  christos 		  ok_dest = FALSE;
   11008  1.1  christos 		  fdh = NULL;
   11009  1.1  christos 		  sym_value = 0;
   11010  1.1  christos 		  if (hash == NULL)
   11011  1.1  christos 		    {
   11012  1.1  christos 		      sym_value = sym->st_value;
   11013  1.1  christos 		      ok_dest = TRUE;
   11014  1.1  christos 		    }
   11015  1.1  christos 		  else if (hash->elf.root.type == bfd_link_hash_defined
   11016  1.1  christos 			   || hash->elf.root.type == bfd_link_hash_defweak)
   11017  1.1  christos 		    {
   11018  1.1  christos 		      sym_value = hash->elf.root.u.def.value;
   11019  1.1  christos 		      if (sym_sec->output_section != NULL)
   11020  1.1  christos 			ok_dest = TRUE;
   11021  1.1  christos 		    }
   11022  1.1  christos 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   11023  1.1  christos 			   || hash->elf.root.type == bfd_link_hash_undefined)
   11024  1.1  christos 		    {
   11025  1.1  christos 		      /* Recognise an old ABI func code entry sym, and
   11026  1.1  christos 			 use the func descriptor sym instead if it is
   11027  1.1  christos 			 defined.  */
   11028  1.1  christos 		      if (hash->elf.root.root.string[0] == '.'
   11029  1.1  christos 			  && (fdh = lookup_fdh (hash, htab)) != NULL)
   11030  1.1  christos 			{
   11031  1.1  christos 			  if (fdh->elf.root.type == bfd_link_hash_defined
   11032  1.1  christos 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   11033  1.1  christos 			    {
   11034  1.1  christos 			      sym_sec = fdh->elf.root.u.def.section;
   11035  1.1  christos 			      sym_value = fdh->elf.root.u.def.value;
   11036  1.1  christos 			      if (sym_sec->output_section != NULL)
   11037  1.1  christos 				ok_dest = TRUE;
   11038  1.1  christos 			    }
   11039  1.1  christos 			  else
   11040  1.1  christos 			    fdh = NULL;
   11041  1.1  christos 			}
   11042  1.1  christos 		    }
   11043  1.1  christos 		  else
   11044  1.1  christos 		    {
   11045  1.1  christos 		      bfd_set_error (bfd_error_bad_value);
   11046  1.1  christos 		      goto error_ret_free_internal;
   11047  1.1  christos 		    }
   11048  1.1  christos 
   11049  1.1  christos 		  destination = 0;
   11050  1.1  christos 		  if (ok_dest)
   11051  1.1  christos 		    {
   11052  1.1  christos 		      sym_value += irela->r_addend;
   11053  1.1  christos 		      destination = (sym_value
   11054  1.1  christos 				     + sym_sec->output_offset
   11055  1.1  christos 				     + sym_sec->output_section->vma);
   11056  1.1  christos 		    }
   11057  1.1  christos 
   11058  1.1  christos 		  code_sec = sym_sec;
   11059  1.1  christos 		  code_value = sym_value;
   11060  1.1  christos 		  opd = get_opd_info (sym_sec);
   11061  1.1  christos 		  if (opd != NULL)
   11062  1.1  christos 		    {
   11063  1.1  christos 		      bfd_vma dest;
   11064  1.1  christos 
   11065  1.1  christos 		      if (hash == NULL && opd->adjust != NULL)
   11066  1.1  christos 			{
   11067  1.1  christos 			  long adjust = opd->adjust[sym_value / 8];
   11068  1.1  christos 			  if (adjust == -1)
   11069  1.1  christos 			    continue;
   11070  1.1  christos 			  code_value += adjust;
   11071  1.1  christos 			  sym_value += adjust;
   11072  1.1  christos 			}
   11073  1.1  christos 		      dest = opd_entry_value (sym_sec, sym_value,
   11074  1.1  christos 					      &code_sec, &code_value);
   11075  1.1  christos 		      if (dest != (bfd_vma) -1)
   11076  1.1  christos 			{
   11077  1.1  christos 			  destination = dest;
   11078  1.1  christos 			  if (fdh != NULL)
   11079  1.1  christos 			    {
   11080  1.1  christos 			      /* Fixup old ABI sym to point at code
   11081  1.1  christos 				 entry.  */
   11082  1.1  christos 			      hash->elf.root.type = bfd_link_hash_defweak;
   11083  1.1  christos 			      hash->elf.root.u.def.section = code_sec;
   11084  1.1  christos 			      hash->elf.root.u.def.value = code_value;
   11085  1.1  christos 			    }
   11086  1.1  christos 			}
   11087  1.1  christos 		    }
   11088  1.1  christos 
   11089  1.1  christos 		  /* Determine what (if any) linker stub is needed.  */
   11090  1.1  christos 		  plt_ent = NULL;
   11091  1.1  christos 		  stub_type = ppc_type_of_stub (section, irela, &hash,
   11092  1.1  christos 						&plt_ent, destination);
   11093  1.1  christos 
   11094  1.1  christos 		  if (stub_type != ppc_stub_plt_call)
   11095  1.1  christos 		    {
   11096  1.1  christos 		      /* Check whether we need a TOC adjusting stub.
   11097  1.1  christos 			 Since the linker pastes together pieces from
   11098  1.1  christos 			 different object files when creating the
   11099  1.1  christos 			 _init and _fini functions, it may be that a
   11100  1.1  christos 			 call to what looks like a local sym is in
   11101  1.1  christos 			 fact a call needing a TOC adjustment.  */
   11102  1.1  christos 		      if (code_sec != NULL
   11103  1.1  christos 			  && code_sec->output_section != NULL
   11104  1.1  christos 			  && (htab->stub_group[code_sec->id].toc_off
   11105  1.1  christos 			      != htab->stub_group[section->id].toc_off)
   11106  1.1  christos 			  && (code_sec->has_toc_reloc
   11107  1.1  christos 			      || code_sec->makes_toc_func_call))
   11108  1.1  christos 			stub_type = ppc_stub_long_branch_r2off;
   11109  1.1  christos 		    }
   11110  1.1  christos 
   11111  1.1  christos 		  if (stub_type == ppc_stub_none)
   11112  1.1  christos 		    continue;
   11113  1.1  christos 
   11114  1.1  christos 		  /* __tls_get_addr calls might be eliminated.  */
   11115  1.1  christos 		  if (stub_type != ppc_stub_plt_call
   11116  1.1  christos 		      && hash != NULL
   11117  1.1  christos 		      && (hash == htab->tls_get_addr
   11118  1.1  christos 			  || hash == htab->tls_get_addr_fd)
   11119  1.1  christos 		      && section->has_tls_reloc
   11120  1.1  christos 		      && irela != internal_relocs)
   11121  1.1  christos 		    {
   11122  1.1  christos 		      /* Get tls info.  */
   11123  1.1  christos 		      unsigned char *tls_mask;
   11124  1.1  christos 
   11125  1.1  christos 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   11126  1.1  christos 					 irela - 1, input_bfd))
   11127  1.1  christos 			goto error_ret_free_internal;
   11128  1.1  christos 		      if (*tls_mask != 0)
   11129  1.1  christos 			continue;
   11130  1.1  christos 		    }
   11131  1.1  christos 
   11132  1.1  christos 		  /* Support for grouping stub sections.  */
   11133  1.1  christos 		  id_sec = htab->stub_group[section->id].link_sec;
   11134  1.1  christos 
   11135  1.1  christos 		  /* Get the name of this stub.  */
   11136  1.1  christos 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   11137  1.1  christos 		  if (!stub_name)
   11138  1.1  christos 		    goto error_ret_free_internal;
   11139  1.1  christos 
   11140  1.1  christos 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   11141  1.1  christos 						     stub_name, FALSE, FALSE);
   11142  1.1  christos 		  if (stub_entry != NULL)
   11143  1.1  christos 		    {
   11144  1.1  christos 		      /* The proper stub has already been created.  */
   11145  1.1  christos 		      free (stub_name);
   11146  1.1  christos 		      continue;
   11147  1.1  christos 		    }
   11148  1.1  christos 
   11149  1.1  christos 		  stub_entry = ppc_add_stub (stub_name, section, htab);
   11150  1.1  christos 		  if (stub_entry == NULL)
   11151  1.1  christos 		    {
   11152  1.1  christos 		      free (stub_name);
   11153  1.1  christos 		    error_ret_free_internal:
   11154  1.1  christos 		      if (elf_section_data (section)->relocs == NULL)
   11155  1.1  christos 			free (internal_relocs);
   11156  1.1  christos 		    error_ret_free_local:
   11157  1.1  christos 		      if (local_syms != NULL
   11158  1.1  christos 			  && (symtab_hdr->contents
   11159  1.1  christos 			      != (unsigned char *) local_syms))
   11160  1.1  christos 			free (local_syms);
   11161  1.1  christos 		      return FALSE;
   11162  1.1  christos 		    }
   11163  1.1  christos 
   11164  1.1  christos 		  stub_entry->stub_type = stub_type;
   11165  1.1  christos 		  if (stub_type != ppc_stub_plt_call)
   11166  1.1  christos 		    {
   11167  1.1  christos 		      stub_entry->target_value = code_value;
   11168  1.1  christos 		      stub_entry->target_section = code_sec;
   11169  1.1  christos 		    }
   11170  1.1  christos 		  else
   11171  1.1  christos 		    {
   11172  1.1  christos 		      stub_entry->target_value = sym_value;
   11173  1.1  christos 		      stub_entry->target_section = sym_sec;
   11174  1.1  christos 		    }
   11175  1.1  christos 		  stub_entry->h = hash;
   11176  1.1  christos 		  stub_entry->plt_ent = plt_ent;
   11177  1.1  christos 		  stub_entry->addend = irela->r_addend;
   11178  1.1  christos 
   11179  1.1  christos 		  if (stub_entry->h != NULL)
   11180  1.1  christos 		    htab->stub_globals += 1;
   11181  1.1  christos 		}
   11182  1.1  christos 
   11183  1.1  christos 	      /* We're done with the internal relocs, free them.  */
   11184  1.1  christos 	      if (elf_section_data (section)->relocs != internal_relocs)
   11185  1.1  christos 		free (internal_relocs);
   11186  1.1  christos 	    }
   11187  1.1  christos 
   11188  1.1  christos 	  if (local_syms != NULL
   11189  1.1  christos 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   11190  1.1  christos 	    {
   11191  1.1  christos 	      if (!info->keep_memory)
   11192  1.1  christos 		free (local_syms);
   11193  1.1  christos 	      else
   11194  1.1  christos 		symtab_hdr->contents = (unsigned char *) local_syms;
   11195  1.1  christos 	    }
   11196  1.1  christos 	}
   11197  1.1  christos 
   11198  1.1  christos       /* We may have added some stubs.  Find out the new size of the
   11199  1.1  christos 	 stub sections.  */
   11200  1.1  christos       for (stub_sec = htab->stub_bfd->sections;
   11201  1.1  christos 	   stub_sec != NULL;
   11202  1.1  christos 	   stub_sec = stub_sec->next)
   11203  1.1  christos 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11204  1.1  christos 	  {
   11205  1.1  christos 	    stub_sec->rawsize = stub_sec->size;
   11206  1.1  christos 	    stub_sec->size = 0;
   11207  1.1  christos 	    stub_sec->reloc_count = 0;
   11208  1.1  christos 	    stub_sec->flags &= ~SEC_RELOC;
   11209  1.1  christos 	  }
   11210  1.1  christos 
   11211  1.1  christos       htab->brlt->size = 0;
   11212  1.1  christos       htab->brlt->reloc_count = 0;
   11213  1.1  christos       htab->brlt->flags &= ~SEC_RELOC;
   11214  1.1  christos       if (htab->relbrlt != NULL)
   11215  1.1  christos 	htab->relbrlt->size = 0;
   11216  1.1  christos 
   11217  1.1  christos       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   11218  1.1  christos 
   11219  1.1  christos       if (info->emitrelocations
   11220  1.1  christos 	  && htab->glink != NULL && htab->glink->size != 0)
   11221  1.1  christos 	{
   11222  1.1  christos 	  htab->glink->reloc_count = 1;
   11223  1.1  christos 	  htab->glink->flags |= SEC_RELOC;
   11224  1.1  christos 	}
   11225  1.1  christos 
   11226  1.1  christos       for (stub_sec = htab->stub_bfd->sections;
   11227  1.1  christos 	   stub_sec != NULL;
   11228  1.1  christos 	   stub_sec = stub_sec->next)
   11229  1.1  christos 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11230  1.1  christos 	    && stub_sec->rawsize != stub_sec->size)
   11231  1.1  christos 	  break;
   11232  1.1  christos 
   11233  1.1  christos       /* Exit from this loop when no stubs have been added, and no stubs
   11234  1.1  christos 	 have changed size.  */
   11235  1.1  christos       if (stub_sec == NULL)
   11236  1.1  christos 	break;
   11237  1.1  christos 
   11238  1.1  christos       /* Ask the linker to do its stuff.  */
   11239  1.1  christos       (*htab->layout_sections_again) ();
   11240  1.1  christos     }
   11241  1.1  christos 
   11242  1.1  christos   /* It would be nice to strip htab->brlt from the output if the
   11243  1.1  christos      section is empty, but it's too late.  If we strip sections here,
   11244  1.1  christos      the dynamic symbol table is corrupted since the section symbol
   11245  1.1  christos      for the stripped section isn't written.  */
   11246  1.1  christos 
   11247  1.1  christos   return TRUE;
   11248  1.1  christos }
   11249  1.1  christos 
   11250  1.1  christos /* Called after we have determined section placement.  If sections
   11251  1.1  christos    move, we'll be called again.  Provide a value for TOCstart.  */
   11252  1.1  christos 
   11253  1.1  christos bfd_vma
   11254  1.1  christos ppc64_elf_toc (bfd *obfd)
   11255  1.1  christos {
   11256  1.1  christos   asection *s;
   11257  1.1  christos   bfd_vma TOCstart;
   11258  1.1  christos 
   11259  1.1  christos   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   11260  1.1  christos      order.  The TOC starts where the first of these sections starts.  */
   11261  1.1  christos   s = bfd_get_section_by_name (obfd, ".got");
   11262  1.1  christos   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11263  1.1  christos     s = bfd_get_section_by_name (obfd, ".toc");
   11264  1.1  christos   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11265  1.1  christos     s = bfd_get_section_by_name (obfd, ".tocbss");
   11266  1.1  christos   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11267  1.1  christos     s = bfd_get_section_by_name (obfd, ".plt");
   11268  1.1  christos   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11269  1.1  christos     {
   11270  1.1  christos       /* This may happen for
   11271  1.1  christos 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   11272  1.1  christos 	 .toc directive
   11273  1.1  christos 	 o  bad linker script
   11274  1.1  christos 	 o --gc-sections and empty TOC sections
   11275  1.1  christos 
   11276  1.1  christos 	 FIXME: Warn user?  */
   11277  1.1  christos 
   11278  1.1  christos       /* Look for a likely section.  We probably won't even be
   11279  1.1  christos 	 using TOCstart.  */
   11280  1.1  christos       for (s = obfd->sections; s != NULL; s = s->next)
   11281  1.1  christos 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   11282  1.1  christos 			 | SEC_EXCLUDE))
   11283  1.1  christos 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   11284  1.1  christos 	  break;
   11285  1.1  christos       if (s == NULL)
   11286  1.1  christos 	for (s = obfd->sections; s != NULL; s = s->next)
   11287  1.1  christos 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   11288  1.1  christos 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   11289  1.1  christos 	    break;
   11290  1.1  christos       if (s == NULL)
   11291  1.1  christos 	for (s = obfd->sections; s != NULL; s = s->next)
   11292  1.1  christos 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   11293  1.1  christos 	      == SEC_ALLOC)
   11294  1.1  christos 	    break;
   11295  1.1  christos       if (s == NULL)
   11296  1.1  christos 	for (s = obfd->sections; s != NULL; s = s->next)
   11297  1.1  christos 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   11298  1.1  christos 	    break;
   11299  1.1  christos     }
   11300  1.1  christos 
   11301  1.1  christos   TOCstart = 0;
   11302  1.1  christos   if (s != NULL)
   11303  1.1  christos     TOCstart = s->output_section->vma + s->output_offset;
   11304  1.1  christos 
   11305  1.1  christos   return TOCstart;
   11306  1.1  christos }
   11307  1.1  christos 
   11308  1.1  christos /* Build all the stubs associated with the current output file.
   11309  1.1  christos    The stubs are kept in a hash table attached to the main linker
   11310  1.1  christos    hash table.  This function is called via gldelf64ppc_finish.  */
   11311  1.1  christos 
   11312  1.1  christos bfd_boolean
   11313  1.1  christos ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
   11314  1.1  christos 		       struct bfd_link_info *info,
   11315  1.1  christos 		       char **stats)
   11316  1.1  christos {
   11317  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11318  1.1  christos   asection *stub_sec;
   11319  1.1  christos   bfd_byte *p;
   11320  1.1  christos   int stub_sec_count = 0;
   11321  1.1  christos 
   11322  1.1  christos   if (htab == NULL)
   11323  1.1  christos     return FALSE;
   11324  1.1  christos 
   11325  1.1  christos   htab->emit_stub_syms = emit_stub_syms;
   11326  1.1  christos 
   11327  1.1  christos   /* Allocate memory to hold the linker stubs.  */
   11328  1.1  christos   for (stub_sec = htab->stub_bfd->sections;
   11329  1.1  christos        stub_sec != NULL;
   11330  1.1  christos        stub_sec = stub_sec->next)
   11331  1.1  christos     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11332  1.1  christos 	&& stub_sec->size != 0)
   11333  1.1  christos       {
   11334  1.1  christos 	stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
   11335  1.1  christos 	if (stub_sec->contents == NULL)
   11336  1.1  christos 	  return FALSE;
   11337  1.1  christos 	/* We want to check that built size is the same as calculated
   11338  1.1  christos 	   size.  rawsize is a convenient location to use.  */
   11339  1.1  christos 	stub_sec->rawsize = stub_sec->size;
   11340  1.1  christos 	stub_sec->size = 0;
   11341  1.1  christos       }
   11342  1.1  christos 
   11343  1.1  christos   if (htab->glink != NULL && htab->glink->size != 0)
   11344  1.1  christos     {
   11345  1.1  christos       unsigned int indx;
   11346  1.1  christos       bfd_vma plt0;
   11347  1.1  christos 
   11348  1.1  christos       /* Build the .glink plt call stub.  */
   11349  1.1  christos       if (htab->emit_stub_syms)
   11350  1.1  christos 	{
   11351  1.1  christos 	  struct elf_link_hash_entry *h;
   11352  1.1  christos 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   11353  1.1  christos 				    TRUE, FALSE, FALSE);
   11354  1.1  christos 	  if (h == NULL)
   11355  1.1  christos 	    return FALSE;
   11356  1.1  christos 	  if (h->root.type == bfd_link_hash_new)
   11357  1.1  christos 	    {
   11358  1.1  christos 	      h->root.type = bfd_link_hash_defined;
   11359  1.1  christos 	      h->root.u.def.section = htab->glink;
   11360  1.1  christos 	      h->root.u.def.value = 8;
   11361  1.1  christos 	      h->ref_regular = 1;
   11362  1.1  christos 	      h->def_regular = 1;
   11363  1.1  christos 	      h->ref_regular_nonweak = 1;
   11364  1.1  christos 	      h->forced_local = 1;
   11365  1.1  christos 	      h->non_elf = 0;
   11366  1.1  christos 	    }
   11367  1.1  christos 	}
   11368  1.1  christos       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
   11369  1.1  christos       if (info->emitrelocations)
   11370  1.1  christos 	{
   11371  1.1  christos 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   11372  1.1  christos 	  if (r == NULL)
   11373  1.1  christos 	    return FALSE;
   11374  1.1  christos 	  r->r_offset = (htab->glink->output_offset
   11375  1.1  christos 			 + htab->glink->output_section->vma);
   11376  1.1  christos 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   11377  1.1  christos 	  r->r_addend = plt0;
   11378  1.1  christos 	}
   11379  1.1  christos       p = htab->glink->contents;
   11380  1.1  christos       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   11381  1.1  christos       bfd_put_64 (htab->glink->owner, plt0, p);
   11382  1.1  christos       p += 8;
   11383  1.1  christos       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   11384  1.1  christos       p += 4;
   11385  1.1  christos       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   11386  1.1  christos       p += 4;
   11387  1.1  christos       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   11388  1.1  christos       p += 4;
   11389  1.1  christos       bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
   11390  1.1  christos       p += 4;
   11391  1.1  christos       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   11392  1.1  christos       p += 4;
   11393  1.1  christos       bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
   11394  1.1  christos       p += 4;
   11395  1.1  christos       bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
   11396  1.1  christos       p += 4;
   11397  1.1  christos       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
   11398  1.1  christos       p += 4;
   11399  1.1  christos       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
   11400  1.1  christos       p += 4;
   11401  1.1  christos       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
   11402  1.1  christos       p += 4;
   11403  1.1  christos       bfd_put_32 (htab->glink->owner, BCTR, p);
   11404  1.1  christos       p += 4;
   11405  1.1  christos       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
   11406  1.1  christos 	{
   11407  1.1  christos 	  bfd_put_32 (htab->glink->owner, NOP, p);
   11408  1.1  christos 	  p += 4;
   11409  1.1  christos 	}
   11410  1.1  christos 
   11411  1.1  christos       /* Build the .glink lazy link call stubs.  */
   11412  1.1  christos       indx = 0;
   11413  1.1  christos       while (p < htab->glink->contents + htab->glink->size)
   11414  1.1  christos 	{
   11415  1.1  christos 	  if (indx < 0x8000)
   11416  1.1  christos 	    {
   11417  1.1  christos 	      bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   11418  1.1  christos 	      p += 4;
   11419  1.1  christos 	    }
   11420  1.1  christos 	  else
   11421  1.1  christos 	    {
   11422  1.1  christos 	      bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   11423  1.1  christos 	      p += 4;
   11424  1.1  christos 	      bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
   11425  1.1  christos 	      p += 4;
   11426  1.1  christos 	    }
   11427  1.1  christos 	  bfd_put_32 (htab->glink->owner,
   11428  1.1  christos 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   11429  1.1  christos 	  indx++;
   11430  1.1  christos 	  p += 4;
   11431  1.1  christos 	}
   11432  1.1  christos       htab->glink->rawsize = p - htab->glink->contents;
   11433  1.1  christos     }
   11434  1.1  christos 
   11435  1.1  christos   if (htab->brlt->size != 0)
   11436  1.1  christos     {
   11437  1.1  christos       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   11438  1.1  christos 					 htab->brlt->size);
   11439  1.1  christos       if (htab->brlt->contents == NULL)
   11440  1.1  christos 	return FALSE;
   11441  1.1  christos     }
   11442  1.1  christos   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   11443  1.1  christos     {
   11444  1.1  christos       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   11445  1.1  christos 					    htab->relbrlt->size);
   11446  1.1  christos       if (htab->relbrlt->contents == NULL)
   11447  1.1  christos 	return FALSE;
   11448  1.1  christos     }
   11449  1.1  christos 
   11450  1.1  christos   /* Build the stubs as directed by the stub hash table.  */
   11451  1.1  christos   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   11452  1.1  christos 
   11453  1.1  christos   if (htab->relbrlt != NULL)
   11454  1.1  christos     htab->relbrlt->reloc_count = 0;
   11455  1.1  christos 
   11456  1.1  christos   for (stub_sec = htab->stub_bfd->sections;
   11457  1.1  christos        stub_sec != NULL;
   11458  1.1  christos        stub_sec = stub_sec->next)
   11459  1.1  christos     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11460  1.1  christos       {
   11461  1.1  christos 	stub_sec_count += 1;
   11462  1.1  christos 	if (stub_sec->rawsize != stub_sec->size)
   11463  1.1  christos 	  break;
   11464  1.1  christos       }
   11465  1.1  christos 
   11466  1.1  christos   if (stub_sec != NULL
   11467  1.1  christos       || htab->glink->rawsize != htab->glink->size)
   11468  1.1  christos     {
   11469  1.1  christos       htab->stub_error = TRUE;
   11470  1.1  christos       (*_bfd_error_handler) (_("stubs don't match calculated size"));
   11471  1.1  christos     }
   11472  1.1  christos 
   11473  1.1  christos   if (htab->stub_error)
   11474  1.1  christos     return FALSE;
   11475  1.1  christos 
   11476  1.1  christos   if (stats != NULL)
   11477  1.1  christos     {
   11478  1.1  christos       *stats = bfd_malloc (500);
   11479  1.1  christos       if (*stats == NULL)
   11480  1.1  christos 	return FALSE;
   11481  1.1  christos 
   11482  1.1  christos       sprintf (*stats, _("linker stubs in %u group%s\n"
   11483  1.1  christos 			 "  branch       %lu\n"
   11484  1.1  christos 			 "  toc adjust   %lu\n"
   11485  1.1  christos 			 "  long branch  %lu\n"
   11486  1.1  christos 			 "  long toc adj %lu\n"
   11487  1.1  christos 			 "  plt call     %lu"),
   11488  1.1  christos 	       stub_sec_count,
   11489  1.1  christos 	       stub_sec_count == 1 ? "" : "s",
   11490  1.1  christos 	       htab->stub_count[ppc_stub_long_branch - 1],
   11491  1.1  christos 	       htab->stub_count[ppc_stub_long_branch_r2off - 1],
   11492  1.1  christos 	       htab->stub_count[ppc_stub_plt_branch - 1],
   11493  1.1  christos 	       htab->stub_count[ppc_stub_plt_branch_r2off - 1],
   11494  1.1  christos 	       htab->stub_count[ppc_stub_plt_call - 1]);
   11495  1.1  christos     }
   11496  1.1  christos   return TRUE;
   11497  1.1  christos }
   11498  1.1  christos 
   11499  1.1  christos /* This function undoes the changes made by add_symbol_adjust.  */
   11500  1.1  christos 
   11501  1.1  christos static bfd_boolean
   11502  1.1  christos undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   11503  1.1  christos {
   11504  1.1  christos   struct ppc_link_hash_entry *eh;
   11505  1.1  christos 
   11506  1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   11507  1.1  christos     return TRUE;
   11508  1.1  christos 
   11509  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   11510  1.1  christos     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   11511  1.1  christos 
   11512  1.1  christos   eh = (struct ppc_link_hash_entry *) h;
   11513  1.1  christos   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
   11514  1.1  christos     return TRUE;
   11515  1.1  christos 
   11516  1.1  christos   eh->elf.root.type = bfd_link_hash_undefined;
   11517  1.1  christos   return TRUE;
   11518  1.1  christos }
   11519  1.1  christos 
   11520  1.1  christos void
   11521  1.1  christos ppc64_elf_restore_symbols (struct bfd_link_info *info)
   11522  1.1  christos {
   11523  1.1  christos   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11524  1.1  christos 
   11525  1.1  christos   if (htab != NULL)
   11526  1.1  christos     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
   11527  1.1  christos }
   11528  1.1  christos 
   11529  1.1  christos /* What to do when ld finds relocations against symbols defined in
   11530  1.1  christos    discarded sections.  */
   11531  1.1  christos 
   11532  1.1  christos static unsigned int
   11533  1.1  christos ppc64_elf_action_discarded (asection *sec)
   11534  1.1  christos {
   11535  1.1  christos   if (strcmp (".opd", sec->name) == 0)
   11536  1.1  christos     return 0;
   11537  1.1  christos 
   11538  1.1  christos   if (strcmp (".toc", sec->name) == 0)
   11539  1.1  christos     return 0;
   11540  1.1  christos 
   11541  1.1  christos   if (strcmp (".toc1", sec->name) == 0)
   11542  1.1  christos     return 0;
   11543  1.1  christos 
   11544  1.1  christos   return _bfd_elf_default_action_discarded (sec);
   11545  1.1  christos }
   11546  1.1  christos 
   11547  1.1  christos /* REL points to a low-part reloc on a largetoc instruction sequence.
   11548  1.1  christos    Find the matching high-part reloc instruction and verify that it
   11549  1.1  christos    is addis REG,x,imm.  If so, set *REG to x and return a pointer to
   11550  1.1  christos    the high-part reloc.  */
   11551  1.1  christos 
   11552  1.1  christos static const Elf_Internal_Rela *
   11553  1.1  christos ha_reloc_match (const Elf_Internal_Rela *relocs,
   11554  1.1  christos 		const Elf_Internal_Rela *rel,
   11555  1.1  christos 		unsigned int *reg,
   11556  1.1  christos 		bfd_boolean match_addend,
   11557  1.1  christos 		const bfd *input_bfd,
   11558  1.1  christos 		const bfd_byte *contents)
   11559  1.1  christos {
   11560  1.1  christos   enum elf_ppc64_reloc_type r_type, r_type_ha;
   11561  1.1  christos   bfd_vma r_info_ha, r_addend;
   11562  1.1  christos 
   11563  1.1  christos   r_type = ELF64_R_TYPE (rel->r_info);
   11564  1.1  christos   switch (r_type)
   11565  1.1  christos     {
   11566  1.1  christos     case R_PPC64_GOT_TLSLD16_LO:
   11567  1.1  christos     case R_PPC64_GOT_TLSGD16_LO:
   11568  1.1  christos     case R_PPC64_GOT_TPREL16_LO_DS:
   11569  1.1  christos     case R_PPC64_GOT_DTPREL16_LO_DS:
   11570  1.1  christos     case R_PPC64_GOT16_LO:
   11571  1.1  christos     case R_PPC64_TOC16_LO:
   11572  1.1  christos       r_type_ha = r_type + 2;
   11573  1.1  christos       break;
   11574  1.1  christos     case R_PPC64_GOT16_LO_DS:
   11575  1.1  christos       r_type_ha = R_PPC64_GOT16_HA;
   11576  1.1  christos       break;
   11577  1.1  christos     case R_PPC64_TOC16_LO_DS:
   11578  1.1  christos       r_type_ha = R_PPC64_TOC16_HA;
   11579  1.1  christos       break;
   11580  1.1  christos     default:
   11581  1.1  christos       abort ();
   11582  1.1  christos     }
   11583  1.1  christos   r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
   11584  1.1  christos   r_addend = rel->r_addend;
   11585  1.1  christos 
   11586  1.1  christos   while (--rel >= relocs)
   11587  1.1  christos     if (rel->r_info == r_info_ha
   11588  1.1  christos 	&& (!match_addend
   11589  1.1  christos 	    || rel->r_addend == r_addend))
   11590  1.1  christos       {
   11591  1.1  christos 	const bfd_byte *p = contents + (rel->r_offset & ~3);
   11592  1.1  christos 	unsigned int insn = bfd_get_32 (input_bfd, p);
   11593  1.1  christos 	if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
   11594  1.1  christos 	    && (insn & (0x1f << 21)) == (*reg << 21))
   11595  1.1  christos 	  {
   11596  1.1  christos 	    *reg = (insn >> 16) & 0x1f;
   11597  1.1  christos 	    return rel;
   11598  1.1  christos 	  }
   11599  1.1  christos 	break;
   11600  1.1  christos       }
   11601  1.1  christos   return NULL;
   11602  1.1  christos }
   11603  1.1  christos 
   11604  1.1  christos /* The RELOCATE_SECTION function is called by the ELF backend linker
   11605  1.1  christos    to handle the relocations for a section.
   11606  1.1  christos 
   11607  1.1  christos    The relocs are always passed as Rela structures; if the section
   11608  1.1  christos    actually uses Rel structures, the r_addend field will always be
   11609  1.1  christos    zero.
   11610  1.1  christos 
   11611  1.1  christos    This function is responsible for adjust the section contents as
   11612  1.1  christos    necessary, and (if using Rela relocs and generating a
   11613  1.1  christos    relocatable output file) adjusting the reloc addend as
   11614  1.1  christos    necessary.
   11615  1.1  christos 
   11616  1.1  christos    This function does not have to worry about setting the reloc
   11617  1.1  christos    address or the reloc symbol index.
   11618  1.1  christos 
   11619  1.1  christos    LOCAL_SYMS is a pointer to the swapped in local symbols.
   11620  1.1  christos 
   11621  1.1  christos    LOCAL_SECTIONS is an array giving the section in the input file
   11622  1.1  christos    corresponding to the st_shndx field of each local symbol.
   11623  1.1  christos 
   11624  1.1  christos    The global hash table entry for the global symbols can be found
   11625  1.1  christos    via elf_sym_hashes (input_bfd).
   11626  1.1  christos 
   11627  1.1  christos    When generating relocatable output, this function must handle
   11628  1.1  christos    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   11629  1.1  christos    going to be the section symbol corresponding to the output
   11630  1.1  christos    section, which means that the addend must be adjusted
   11631  1.1  christos    accordingly.  */
   11632  1.1  christos 
   11633  1.1  christos static bfd_boolean
   11634  1.1  christos ppc64_elf_relocate_section (bfd *output_bfd,
   11635  1.1  christos 			    struct bfd_link_info *info,
   11636  1.1  christos 			    bfd *input_bfd,
   11637  1.1  christos 			    asection *input_section,
   11638  1.1  christos 			    bfd_byte *contents,
   11639  1.1  christos 			    Elf_Internal_Rela *relocs,
   11640  1.1  christos 			    Elf_Internal_Sym *local_syms,
   11641  1.1  christos 			    asection **local_sections)
   11642  1.1  christos {
   11643  1.1  christos   struct ppc_link_hash_table *htab;
   11644  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   11645  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   11646  1.1  christos   Elf_Internal_Rela *rel;
   11647  1.1  christos   Elf_Internal_Rela *relend;
   11648  1.1  christos   Elf_Internal_Rela outrel;
   11649  1.1  christos   bfd_byte *loc;
   11650  1.1  christos   struct got_entry **local_got_ents;
   11651  1.1  christos   unsigned char *ha_opt;
   11652  1.1  christos   bfd_vma TOCstart;
   11653  1.1  christos   bfd_boolean no_ha_opt;
   11654  1.1  christos   bfd_boolean ret = TRUE;
   11655  1.1  christos   bfd_boolean is_opd;
   11656  1.1  christos   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
   11657  1.1  christos   bfd_boolean is_power4 = FALSE;
   11658  1.1  christos   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
   11659  1.1  christos 
   11660  1.1  christos   /* Initialize howto table if needed.  */
   11661  1.1  christos   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   11662  1.1  christos     ppc_howto_init ();
   11663  1.1  christos 
   11664  1.1  christos   htab = ppc_hash_table (info);
   11665  1.1  christos   if (htab == NULL)
   11666  1.1  christos     return FALSE;
   11667  1.1  christos 
   11668  1.1  christos   /* Don't relocate stub sections.  */
   11669  1.1  christos   if (input_section->owner == htab->stub_bfd)
   11670  1.1  christos     return TRUE;
   11671  1.1  christos 
   11672  1.1  christos   BFD_ASSERT (is_ppc64_elf (input_bfd));
   11673  1.1  christos 
   11674  1.1  christos   local_got_ents = elf_local_got_ents (input_bfd);
   11675  1.1  christos   TOCstart = elf_gp (output_bfd);
   11676  1.1  christos   symtab_hdr = &elf_symtab_hdr (input_bfd);
   11677  1.1  christos   sym_hashes = elf_sym_hashes (input_bfd);
   11678  1.1  christos   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   11679  1.1  christos   ha_opt = NULL;
   11680  1.1  christos   no_ha_opt = FALSE;
   11681  1.1  christos 
   11682  1.1  christos   rel = relocs;
   11683  1.1  christos   relend = relocs + input_section->reloc_count;
   11684  1.1  christos   for (; rel < relend; rel++)
   11685  1.1  christos     {
   11686  1.1  christos       enum elf_ppc64_reloc_type r_type;
   11687  1.1  christos       bfd_vma addend, orig_addend;
   11688  1.1  christos       bfd_reloc_status_type r;
   11689  1.1  christos       Elf_Internal_Sym *sym;
   11690  1.1  christos       asection *sec;
   11691  1.1  christos       struct elf_link_hash_entry *h_elf;
   11692  1.1  christos       struct ppc_link_hash_entry *h;
   11693  1.1  christos       struct ppc_link_hash_entry *fdh;
   11694  1.1  christos       const char *sym_name;
   11695  1.1  christos       unsigned long r_symndx, toc_symndx;
   11696  1.1  christos       bfd_vma toc_addend;
   11697  1.1  christos       unsigned char tls_mask, tls_gd, tls_type;
   11698  1.1  christos       unsigned char sym_type;
   11699  1.1  christos       bfd_vma relocation;
   11700  1.1  christos       bfd_boolean unresolved_reloc;
   11701  1.1  christos       bfd_boolean warned;
   11702  1.1  christos       unsigned int insn;
   11703  1.1  christos       unsigned int mask;
   11704  1.1  christos       struct ppc_stub_hash_entry *stub_entry;
   11705  1.1  christos       bfd_vma max_br_offset;
   11706  1.1  christos       bfd_vma from;
   11707  1.1  christos 
   11708  1.1  christos       r_type = ELF64_R_TYPE (rel->r_info);
   11709  1.1  christos       r_symndx = ELF64_R_SYM (rel->r_info);
   11710  1.1  christos 
   11711  1.1  christos       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   11712  1.1  christos 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   11713  1.1  christos 	 proper TOC base to use.  */
   11714  1.1  christos       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   11715  1.1  christos 	  && rel != relocs
   11716  1.1  christos 	  && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
   11717  1.1  christos 	  && is_opd)
   11718  1.1  christos 	r_symndx = ELF64_R_SYM (rel[-1].r_info);
   11719  1.1  christos 
   11720  1.1  christos       sym = NULL;
   11721  1.1  christos       sec = NULL;
   11722  1.1  christos       h_elf = NULL;
   11723  1.1  christos       sym_name = NULL;
   11724  1.1  christos       unresolved_reloc = FALSE;
   11725  1.1  christos       warned = FALSE;
   11726  1.1  christos       orig_addend = rel->r_addend;
   11727  1.1  christos 
   11728  1.1  christos       if (r_symndx < symtab_hdr->sh_info)
   11729  1.1  christos 	{
   11730  1.1  christos 	  /* It's a local symbol.  */
   11731  1.1  christos 	  struct _opd_sec_data *opd;
   11732  1.1  christos 
   11733  1.1  christos 	  sym = local_syms + r_symndx;
   11734  1.1  christos 	  sec = local_sections[r_symndx];
   11735  1.1  christos 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   11736  1.1  christos 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   11737  1.1  christos 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   11738  1.1  christos 	  opd = get_opd_info (sec);
   11739  1.1  christos 	  if (opd != NULL && opd->adjust != NULL)
   11740  1.1  christos 	    {
   11741  1.1  christos 	      long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
   11742  1.1  christos 	      if (adjust == -1)
   11743  1.1  christos 		relocation = 0;
   11744  1.1  christos 	      else
   11745  1.1  christos 		{
   11746  1.1  christos 		  /* If this is a relocation against the opd section sym
   11747  1.1  christos 		     and we have edited .opd, adjust the reloc addend so
   11748  1.1  christos 		     that ld -r and ld --emit-relocs output is correct.
   11749  1.1  christos 		     If it is a reloc against some other .opd symbol,
   11750  1.1  christos 		     then the symbol value will be adjusted later.  */
   11751  1.1  christos 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   11752  1.1  christos 		    rel->r_addend += adjust;
   11753  1.1  christos 		  else
   11754  1.1  christos 		    relocation += adjust;
   11755  1.1  christos 		}
   11756  1.1  christos 	    }
   11757  1.1  christos 	}
   11758  1.1  christos       else
   11759  1.1  christos 	{
   11760  1.1  christos 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   11761  1.1  christos 				   r_symndx, symtab_hdr, sym_hashes,
   11762  1.1  christos 				   h_elf, sec, relocation,
   11763  1.1  christos 				   unresolved_reloc, warned);
   11764  1.1  christos 	  sym_name = h_elf->root.root.string;
   11765  1.1  christos 	  sym_type = h_elf->type;
   11766  1.1  christos 	}
   11767  1.1  christos       h = (struct ppc_link_hash_entry *) h_elf;
   11768  1.1  christos 
   11769  1.1  christos       if (sec != NULL && elf_discarded_section (sec))
   11770  1.1  christos 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   11771  1.1  christos 					 rel, relend,
   11772  1.1  christos 					 ppc64_elf_howto_table[r_type],
   11773  1.1  christos 					 contents);
   11774  1.1  christos 
   11775  1.1  christos       if (info->relocatable)
   11776  1.1  christos 	continue;
   11777  1.1  christos 
   11778  1.1  christos       /* TLS optimizations.  Replace instruction sequences and relocs
   11779  1.1  christos 	 based on information we collected in tls_optimize.  We edit
   11780  1.1  christos 	 RELOCS so that --emit-relocs will output something sensible
   11781  1.1  christos 	 for the final instruction stream.  */
   11782  1.1  christos       tls_mask = 0;
   11783  1.1  christos       tls_gd = 0;
   11784  1.1  christos       toc_symndx = 0;
   11785  1.1  christos       if (h != NULL)
   11786  1.1  christos 	tls_mask = h->tls_mask;
   11787  1.1  christos       else if (local_got_ents != NULL)
   11788  1.1  christos 	{
   11789  1.1  christos 	  struct plt_entry **local_plt = (struct plt_entry **)
   11790  1.1  christos 	    (local_got_ents + symtab_hdr->sh_info);
   11791  1.1  christos 	  unsigned char *lgot_masks = (unsigned char *)
   11792  1.1  christos 	    (local_plt + symtab_hdr->sh_info);
   11793  1.1  christos 	  tls_mask = lgot_masks[r_symndx];
   11794  1.1  christos 	}
   11795  1.1  christos       if (tls_mask == 0
   11796  1.1  christos 	  && (r_type == R_PPC64_TLS
   11797  1.1  christos 	      || r_type == R_PPC64_TLSGD
   11798  1.1  christos 	      || r_type == R_PPC64_TLSLD))
   11799  1.1  christos 	{
   11800  1.1  christos 	  /* Check for toc tls entries.  */
   11801  1.1  christos 	  unsigned char *toc_tls;
   11802  1.1  christos 
   11803  1.1  christos 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   11804  1.1  christos 			     &local_syms, rel, input_bfd))
   11805  1.1  christos 	    return FALSE;
   11806  1.1  christos 
   11807  1.1  christos 	  if (toc_tls)
   11808  1.1  christos 	    tls_mask = *toc_tls;
   11809  1.1  christos 	}
   11810  1.1  christos 
   11811  1.1  christos       /* Check that tls relocs are used with tls syms, and non-tls
   11812  1.1  christos 	 relocs are used with non-tls syms.  */
   11813  1.1  christos       if (r_symndx != STN_UNDEF
   11814  1.1  christos 	  && r_type != R_PPC64_NONE
   11815  1.1  christos 	  && (h == NULL
   11816  1.1  christos 	      || h->elf.root.type == bfd_link_hash_defined
   11817  1.1  christos 	      || h->elf.root.type == bfd_link_hash_defweak)
   11818  1.1  christos 	  && (IS_PPC64_TLS_RELOC (r_type)
   11819  1.1  christos 	      != (sym_type == STT_TLS
   11820  1.1  christos 		  || (sym_type == STT_SECTION
   11821  1.1  christos 		      && (sec->flags & SEC_THREAD_LOCAL) != 0))))
   11822  1.1  christos 	{
   11823  1.1  christos 	  if (tls_mask != 0
   11824  1.1  christos 	      && (r_type == R_PPC64_TLS
   11825  1.1  christos 		  || r_type == R_PPC64_TLSGD
   11826  1.1  christos 		  || r_type == R_PPC64_TLSLD))
   11827  1.1  christos 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   11828  1.1  christos 	    ;
   11829  1.1  christos 	  else
   11830  1.1  christos 	    (*_bfd_error_handler)
   11831  1.1  christos 	      (!IS_PPC64_TLS_RELOC (r_type)
   11832  1.1  christos 	       ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
   11833  1.1  christos 	       : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
   11834  1.1  christos 	       input_bfd,
   11835  1.1  christos 	       input_section,
   11836  1.1  christos 	       (long) rel->r_offset,
   11837  1.1  christos 	       ppc64_elf_howto_table[r_type]->name,
   11838  1.1  christos 	       sym_name);
   11839  1.1  christos 	}
   11840  1.1  christos 
   11841  1.1  christos       /* Ensure reloc mapping code below stays sane.  */
   11842  1.1  christos       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   11843  1.1  christos 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   11844  1.1  christos 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   11845  1.1  christos 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   11846  1.1  christos 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   11847  1.1  christos 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   11848  1.1  christos 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   11849  1.1  christos 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   11850  1.1  christos 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   11851  1.1  christos 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   11852  1.1  christos 	abort ();
   11853  1.1  christos 
   11854  1.1  christos       switch (r_type)
   11855  1.1  christos 	{
   11856  1.1  christos 	default:
   11857  1.1  christos 	  break;
   11858  1.1  christos 
   11859  1.1  christos 	case R_PPC64_LO_DS_OPT:
   11860  1.1  christos 	  insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   11861  1.1  christos 	  if ((insn & (0x3f << 26)) != 58u << 26)
   11862  1.1  christos 	    abort ();
   11863  1.1  christos 	  insn += (14u << 26) - (58u << 26);
   11864  1.1  christos 	  bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   11865  1.1  christos 	  r_type = R_PPC64_TOC16_LO;
   11866  1.1  christos 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11867  1.1  christos 	  break;
   11868  1.1  christos 
   11869  1.1  christos 	case R_PPC64_TOC16:
   11870  1.1  christos 	case R_PPC64_TOC16_LO:
   11871  1.1  christos 	case R_PPC64_TOC16_DS:
   11872  1.1  christos 	case R_PPC64_TOC16_LO_DS:
   11873  1.1  christos 	  {
   11874  1.1  christos 	    /* Check for toc tls entries.  */
   11875  1.1  christos 	    unsigned char *toc_tls;
   11876  1.1  christos 	    int retval;
   11877  1.1  christos 
   11878  1.1  christos 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   11879  1.1  christos 				   &local_syms, rel, input_bfd);
   11880  1.1  christos 	    if (retval == 0)
   11881  1.1  christos 	      return FALSE;
   11882  1.1  christos 
   11883  1.1  christos 	    if (toc_tls)
   11884  1.1  christos 	      {
   11885  1.1  christos 		tls_mask = *toc_tls;
   11886  1.1  christos 		if (r_type == R_PPC64_TOC16_DS
   11887  1.1  christos 		    || r_type == R_PPC64_TOC16_LO_DS)
   11888  1.1  christos 		  {
   11889  1.1  christos 		    if (tls_mask != 0
   11890  1.1  christos 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   11891  1.1  christos 		      goto toctprel;
   11892  1.1  christos 		  }
   11893  1.1  christos 		else
   11894  1.1  christos 		  {
   11895  1.1  christos 		    /* If we found a GD reloc pair, then we might be
   11896  1.1  christos 		       doing a GD->IE transition.  */
   11897  1.1  christos 		    if (retval == 2)
   11898  1.1  christos 		      {
   11899  1.1  christos 			tls_gd = TLS_TPRELGD;
   11900  1.1  christos 			if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   11901  1.1  christos 			  goto tls_ldgd_opt;
   11902  1.1  christos 		      }
   11903  1.1  christos 		    else if (retval == 3)
   11904  1.1  christos 		      {
   11905  1.1  christos 			if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   11906  1.1  christos 			  goto tls_ldgd_opt;
   11907  1.1  christos 		      }
   11908  1.1  christos 		  }
   11909  1.1  christos 	      }
   11910  1.1  christos 	  }
   11911  1.1  christos 	  break;
   11912  1.1  christos 
   11913  1.1  christos 	case R_PPC64_GOT_TPREL16_HI:
   11914  1.1  christos 	case R_PPC64_GOT_TPREL16_HA:
   11915  1.1  christos 	  if (tls_mask != 0
   11916  1.1  christos 	      && (tls_mask & TLS_TPREL) == 0)
   11917  1.1  christos 	    {
   11918  1.1  christos 	      rel->r_offset -= d_offset;
   11919  1.1  christos 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   11920  1.1  christos 	      r_type = R_PPC64_NONE;
   11921  1.1  christos 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11922  1.1  christos 	    }
   11923  1.1  christos 	  break;
   11924  1.1  christos 
   11925  1.1  christos 	case R_PPC64_GOT_TPREL16_DS:
   11926  1.1  christos 	case R_PPC64_GOT_TPREL16_LO_DS:
   11927  1.1  christos 	  if (tls_mask != 0
   11928  1.1  christos 	      && (tls_mask & TLS_TPREL) == 0)
   11929  1.1  christos 	    {
   11930  1.1  christos 	    toctprel:
   11931  1.1  christos 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   11932  1.1  christos 	      insn &= 31 << 21;
   11933  1.1  christos 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   11934  1.1  christos 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   11935  1.1  christos 	      r_type = R_PPC64_TPREL16_HA;
   11936  1.1  christos 	      if (toc_symndx != 0)
   11937  1.1  christos 		{
   11938  1.1  christos 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   11939  1.1  christos 		  rel->r_addend = toc_addend;
   11940  1.1  christos 		  /* We changed the symbol.  Start over in order to
   11941  1.1  christos 		     get h, sym, sec etc. right.  */
   11942  1.1  christos 		  rel--;
   11943  1.1  christos 		  continue;
   11944  1.1  christos 		}
   11945  1.1  christos 	      else
   11946  1.1  christos 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11947  1.1  christos 	    }
   11948  1.1  christos 	  break;
   11949  1.1  christos 
   11950  1.1  christos 	case R_PPC64_TLS:
   11951  1.1  christos 	  if (tls_mask != 0
   11952  1.1  christos 	      && (tls_mask & TLS_TPREL) == 0)
   11953  1.1  christos 	    {
   11954  1.1  christos 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
   11955  1.1  christos 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   11956  1.1  christos 	      if (insn == 0)
   11957  1.1  christos 		abort ();
   11958  1.1  christos 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   11959  1.1  christos 	      /* Was PPC64_TLS which sits on insn boundary, now
   11960  1.1  christos 		 PPC64_TPREL16_LO which is at low-order half-word.  */
   11961  1.1  christos 	      rel->r_offset += d_offset;
   11962  1.1  christos 	      r_type = R_PPC64_TPREL16_LO;
   11963  1.1  christos 	      if (toc_symndx != 0)
   11964  1.1  christos 		{
   11965  1.1  christos 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   11966  1.1  christos 		  rel->r_addend = toc_addend;
   11967  1.1  christos 		  /* We changed the symbol.  Start over in order to
   11968  1.1  christos 		     get h, sym, sec etc. right.  */
   11969  1.1  christos 		  rel--;
   11970  1.1  christos 		  continue;
   11971  1.1  christos 		}
   11972  1.1  christos 	      else
   11973  1.1  christos 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11974  1.1  christos 	    }
   11975  1.1  christos 	  break;
   11976  1.1  christos 
   11977  1.1  christos 	case R_PPC64_GOT_TLSGD16_HI:
   11978  1.1  christos 	case R_PPC64_GOT_TLSGD16_HA:
   11979  1.1  christos 	  tls_gd = TLS_TPRELGD;
   11980  1.1  christos 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   11981  1.1  christos 	    goto tls_gdld_hi;
   11982  1.1  christos 	  break;
   11983  1.1  christos 
   11984  1.1  christos 	case R_PPC64_GOT_TLSLD16_HI:
   11985  1.1  christos 	case R_PPC64_GOT_TLSLD16_HA:
   11986  1.1  christos 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   11987  1.1  christos 	    {
   11988  1.1  christos 	    tls_gdld_hi:
   11989  1.1  christos 	      if ((tls_mask & tls_gd) != 0)
   11990  1.1  christos 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   11991  1.1  christos 			  + R_PPC64_GOT_TPREL16_DS);
   11992  1.1  christos 	      else
   11993  1.1  christos 		{
   11994  1.1  christos 		  rel->r_offset -= d_offset;
   11995  1.1  christos 		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   11996  1.1  christos 		  r_type = R_PPC64_NONE;
   11997  1.1  christos 		}
   11998  1.1  christos 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11999  1.1  christos 	    }
   12000  1.1  christos 	  break;
   12001  1.1  christos 
   12002  1.1  christos 	case R_PPC64_GOT_TLSGD16:
   12003  1.1  christos 	case R_PPC64_GOT_TLSGD16_LO:
   12004  1.1  christos 	  tls_gd = TLS_TPRELGD;
   12005  1.1  christos 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12006  1.1  christos 	    goto tls_ldgd_opt;
   12007  1.1  christos 	  break;
   12008  1.1  christos 
   12009  1.1  christos 	case R_PPC64_GOT_TLSLD16:
   12010  1.1  christos 	case R_PPC64_GOT_TLSLD16_LO:
   12011  1.1  christos 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12012  1.1  christos 	    {
   12013  1.1  christos 	      unsigned int insn1, insn2, insn3;
   12014  1.1  christos 	      bfd_vma offset;
   12015  1.1  christos 
   12016  1.1  christos 	    tls_ldgd_opt:
   12017  1.1  christos 	      offset = (bfd_vma) -1;
   12018  1.1  christos 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   12019  1.1  christos 		 __tls_get_addr calls, we must trust that the call
   12020  1.1  christos 		 stays with its arg setup insns, ie. that the next
   12021  1.1  christos 		 reloc is the __tls_get_addr call associated with
   12022  1.1  christos 		 the current reloc.  Edit both insns.  */
   12023  1.1  christos 	      if (input_section->has_tls_get_addr_call
   12024  1.1  christos 		  && rel + 1 < relend
   12025  1.1  christos 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   12026  1.1  christos 					      htab->tls_get_addr,
   12027  1.1  christos 					      htab->tls_get_addr_fd))
   12028  1.1  christos 		offset = rel[1].r_offset;
   12029  1.1  christos 	      if ((tls_mask & tls_gd) != 0)
   12030  1.1  christos 		{
   12031  1.1  christos 		  /* IE */
   12032  1.1  christos 		  insn1 = bfd_get_32 (output_bfd,
   12033  1.1  christos 				      contents + rel->r_offset - d_offset);
   12034  1.1  christos 		  insn1 &= (1 << 26) - (1 << 2);
   12035  1.1  christos 		  insn1 |= 58 << 26;	/* ld */
   12036  1.1  christos 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12037  1.1  christos 		  if (offset != (bfd_vma) -1)
   12038  1.1  christos 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12039  1.1  christos 		  if ((tls_mask & TLS_EXPLICIT) == 0)
   12040  1.1  christos 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   12041  1.1  christos 			      + R_PPC64_GOT_TPREL16_DS);
   12042  1.1  christos 		  else
   12043  1.1  christos 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   12044  1.1  christos 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12045  1.1  christos 		}
   12046  1.1  christos 	      else
   12047  1.1  christos 		{
   12048  1.1  christos 		  /* LE */
   12049  1.1  christos 		  insn1 = 0x3c6d0000;	/* addis 3,13,0 */
   12050  1.1  christos 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12051  1.1  christos 		  if (tls_gd == 0)
   12052  1.1  christos 		    {
   12053  1.1  christos 		      /* Was an LD reloc.  */
   12054  1.1  christos 		      if (toc_symndx)
   12055  1.1  christos 			sec = local_sections[toc_symndx];
   12056  1.1  christos 		      for (r_symndx = 0;
   12057  1.1  christos 			   r_symndx < symtab_hdr->sh_info;
   12058  1.1  christos 			   r_symndx++)
   12059  1.1  christos 			if (local_sections[r_symndx] == sec)
   12060  1.1  christos 			  break;
   12061  1.1  christos 		      if (r_symndx >= symtab_hdr->sh_info)
   12062  1.1  christos 			r_symndx = STN_UNDEF;
   12063  1.1  christos 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12064  1.1  christos 		      if (r_symndx != STN_UNDEF)
   12065  1.1  christos 			rel->r_addend -= (local_syms[r_symndx].st_value
   12066  1.1  christos 					  + sec->output_offset
   12067  1.1  christos 					  + sec->output_section->vma);
   12068  1.1  christos 		    }
   12069  1.1  christos 		  else if (toc_symndx != 0)
   12070  1.1  christos 		    {
   12071  1.1  christos 		      r_symndx = toc_symndx;
   12072  1.1  christos 		      rel->r_addend = toc_addend;
   12073  1.1  christos 		    }
   12074  1.1  christos 		  r_type = R_PPC64_TPREL16_HA;
   12075  1.1  christos 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12076  1.1  christos 		  if (offset != (bfd_vma) -1)
   12077  1.1  christos 		    {
   12078  1.1  christos 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   12079  1.1  christos 						    R_PPC64_TPREL16_LO);
   12080  1.1  christos 		      rel[1].r_offset = offset + d_offset;
   12081  1.1  christos 		      rel[1].r_addend = rel->r_addend;
   12082  1.1  christos 		    }
   12083  1.1  christos 		}
   12084  1.1  christos 	      bfd_put_32 (output_bfd, insn1,
   12085  1.1  christos 			  contents + rel->r_offset - d_offset);
   12086  1.1  christos 	      if (offset != (bfd_vma) -1)
   12087  1.1  christos 		{
   12088  1.1  christos 		  insn3 = bfd_get_32 (output_bfd,
   12089  1.1  christos 				      contents + offset + 4);
   12090  1.1  christos 		  if (insn3 == NOP
   12091  1.1  christos 		      || insn3 == CROR_151515 || insn3 == CROR_313131)
   12092  1.1  christos 		    {
   12093  1.1  christos 		      rel[1].r_offset += 4;
   12094  1.1  christos 		      bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12095  1.1  christos 		      insn2 = NOP;
   12096  1.1  christos 		    }
   12097  1.1  christos 		  bfd_put_32 (output_bfd, insn2, contents + offset);
   12098  1.1  christos 		}
   12099  1.1  christos 	      if ((tls_mask & tls_gd) == 0
   12100  1.1  christos 		  && (tls_gd == 0 || toc_symndx != 0))
   12101  1.1  christos 		{
   12102  1.1  christos 		  /* We changed the symbol.  Start over in order
   12103  1.1  christos 		     to get h, sym, sec etc. right.  */
   12104  1.1  christos 		  rel--;
   12105  1.1  christos 		  continue;
   12106  1.1  christos 		}
   12107  1.1  christos 	    }
   12108  1.1  christos 	  break;
   12109  1.1  christos 
   12110  1.1  christos 	case R_PPC64_TLSGD:
   12111  1.1  christos 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12112  1.1  christos 	    {
   12113  1.1  christos 	      unsigned int insn2, insn3;
   12114  1.1  christos 	      bfd_vma offset = rel->r_offset;
   12115  1.1  christos 
   12116  1.1  christos 	      if ((tls_mask & TLS_TPRELGD) != 0)
   12117  1.1  christos 		{
   12118  1.1  christos 		  /* IE */
   12119  1.1  christos 		  r_type = R_PPC64_NONE;
   12120  1.1  christos 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12121  1.1  christos 		}
   12122  1.1  christos 	      else
   12123  1.1  christos 		{
   12124  1.1  christos 		  /* LE */
   12125  1.1  christos 		  if (toc_symndx != 0)
   12126  1.1  christos 		    {
   12127  1.1  christos 		      r_symndx = toc_symndx;
   12128  1.1  christos 		      rel->r_addend = toc_addend;
   12129  1.1  christos 		    }
   12130  1.1  christos 		  r_type = R_PPC64_TPREL16_LO;
   12131  1.1  christos 		  rel->r_offset = offset + d_offset;
   12132  1.1  christos 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12133  1.1  christos 		}
   12134  1.1  christos 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12135  1.1  christos 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12136  1.1  christos 	      BFD_ASSERT (offset == rel[1].r_offset);
   12137  1.1  christos 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12138  1.1  christos 	      insn3 = bfd_get_32 (output_bfd,
   12139  1.1  christos 				  contents + offset + 4);
   12140  1.1  christos 	      if (insn3 == NOP
   12141  1.1  christos 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12142  1.1  christos 		{
   12143  1.1  christos 		  rel->r_offset += 4;
   12144  1.1  christos 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12145  1.1  christos 		  insn2 = NOP;
   12146  1.1  christos 		}
   12147  1.1  christos 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12148  1.1  christos 	      if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
   12149  1.1  christos 		{
   12150  1.1  christos 		  rel--;
   12151  1.1  christos 		  continue;
   12152  1.1  christos 		}
   12153  1.1  christos 	    }
   12154  1.1  christos 	  break;
   12155  1.1  christos 
   12156  1.1  christos 	case R_PPC64_TLSLD:
   12157  1.1  christos 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12158  1.1  christos 	    {
   12159  1.1  christos 	      unsigned int insn2, insn3;
   12160  1.1  christos 	      bfd_vma offset = rel->r_offset;
   12161  1.1  christos 
   12162  1.1  christos 	      if (toc_symndx)
   12163  1.1  christos 		sec = local_sections[toc_symndx];
   12164  1.1  christos 	      for (r_symndx = 0;
   12165  1.1  christos 		   r_symndx < symtab_hdr->sh_info;
   12166  1.1  christos 		   r_symndx++)
   12167  1.1  christos 		if (local_sections[r_symndx] == sec)
   12168  1.1  christos 		  break;
   12169  1.1  christos 	      if (r_symndx >= symtab_hdr->sh_info)
   12170  1.1  christos 		r_symndx = STN_UNDEF;
   12171  1.1  christos 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12172  1.1  christos 	      if (r_symndx != STN_UNDEF)
   12173  1.1  christos 		rel->r_addend -= (local_syms[r_symndx].st_value
   12174  1.1  christos 				  + sec->output_offset
   12175  1.1  christos 				  + sec->output_section->vma);
   12176  1.1  christos 
   12177  1.1  christos 	      r_type = R_PPC64_TPREL16_LO;
   12178  1.1  christos 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12179  1.1  christos 	      rel->r_offset = offset + d_offset;
   12180  1.1  christos 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12181  1.1  christos 	      BFD_ASSERT (offset == rel[1].r_offset);
   12182  1.1  christos 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12183  1.1  christos 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   12184  1.1  christos 	      insn3 = bfd_get_32 (output_bfd,
   12185  1.1  christos 				  contents + offset + 4);
   12186  1.1  christos 	      if (insn3 == NOP
   12187  1.1  christos 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12188  1.1  christos 		{
   12189  1.1  christos 		  rel->r_offset += 4;
   12190  1.1  christos 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12191  1.1  christos 		  insn2 = NOP;
   12192  1.1  christos 		}
   12193  1.1  christos 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12194  1.1  christos 	      rel--;
   12195  1.1  christos 	      continue;
   12196  1.1  christos 	    }
   12197  1.1  christos 	  break;
   12198  1.1  christos 
   12199  1.1  christos 	case R_PPC64_DTPMOD64:
   12200  1.1  christos 	  if (rel + 1 < relend
   12201  1.1  christos 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   12202  1.1  christos 	      && rel[1].r_offset == rel->r_offset + 8)
   12203  1.1  christos 	    {
   12204  1.1  christos 	      if ((tls_mask & TLS_GD) == 0)
   12205  1.1  christos 		{
   12206  1.1  christos 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   12207  1.1  christos 		  if ((tls_mask & TLS_TPRELGD) != 0)
   12208  1.1  christos 		    r_type = R_PPC64_TPREL64;
   12209  1.1  christos 		  else
   12210  1.1  christos 		    {
   12211  1.1  christos 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12212  1.1  christos 		      r_type = R_PPC64_NONE;
   12213  1.1  christos 		    }
   12214  1.1  christos 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12215  1.1  christos 		}
   12216  1.1  christos 	    }
   12217  1.1  christos 	  else
   12218  1.1  christos 	    {
   12219  1.1  christos 	      if ((tls_mask & TLS_LD) == 0)
   12220  1.1  christos 		{
   12221  1.1  christos 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12222  1.1  christos 		  r_type = R_PPC64_NONE;
   12223  1.1  christos 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12224  1.1  christos 		}
   12225  1.1  christos 	    }
   12226  1.1  christos 	  break;
   12227  1.1  christos 
   12228  1.1  christos 	case R_PPC64_TPREL64:
   12229  1.1  christos 	  if ((tls_mask & TLS_TPREL) == 0)
   12230  1.1  christos 	    {
   12231  1.1  christos 	      r_type = R_PPC64_NONE;
   12232  1.1  christos 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12233  1.1  christos 	    }
   12234  1.1  christos 	  break;
   12235  1.1  christos 	}
   12236  1.1  christos 
   12237  1.1  christos       /* Handle other relocations that tweak non-addend part of insn.  */
   12238  1.1  christos       insn = 0;
   12239  1.1  christos       max_br_offset = 1 << 25;
   12240  1.1  christos       addend = rel->r_addend;
   12241  1.1  christos       switch (r_type)
   12242  1.1  christos 	{
   12243  1.1  christos 	default:
   12244  1.1  christos 	  break;
   12245  1.1  christos 
   12246  1.1  christos 	  /* Branch taken prediction relocations.  */
   12247  1.1  christos 	case R_PPC64_ADDR14_BRTAKEN:
   12248  1.1  christos 	case R_PPC64_REL14_BRTAKEN:
   12249  1.1  christos 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   12250  1.1  christos 	  /* Fall thru.  */
   12251  1.1  christos 
   12252  1.1  christos 	  /* Branch not taken prediction relocations.  */
   12253  1.1  christos 	case R_PPC64_ADDR14_BRNTAKEN:
   12254  1.1  christos 	case R_PPC64_REL14_BRNTAKEN:
   12255  1.1  christos 	  insn |= bfd_get_32 (output_bfd,
   12256  1.1  christos 			      contents + rel->r_offset) & ~(0x01 << 21);
   12257  1.1  christos 	  /* Fall thru.  */
   12258  1.1  christos 
   12259  1.1  christos 	case R_PPC64_REL14:
   12260  1.1  christos 	  max_br_offset = 1 << 15;
   12261  1.1  christos 	  /* Fall thru.  */
   12262  1.1  christos 
   12263  1.1  christos 	case R_PPC64_REL24:
   12264  1.1  christos 	  /* Calls to functions with a different TOC, such as calls to
   12265  1.1  christos 	     shared objects, need to alter the TOC pointer.  This is
   12266  1.1  christos 	     done using a linkage stub.  A REL24 branching to these
   12267  1.1  christos 	     linkage stubs needs to be followed by a nop, as the nop
   12268  1.1  christos 	     will be replaced with an instruction to restore the TOC
   12269  1.1  christos 	     base pointer.  */
   12270  1.1  christos 	  fdh = h;
   12271  1.1  christos 	  if (h != NULL
   12272  1.1  christos 	      && h->oh != NULL
   12273  1.1  christos 	      && h->oh->is_func_descriptor)
   12274  1.1  christos 	    fdh = ppc_follow_link (h->oh);
   12275  1.1  christos 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
   12276  1.1  christos 	  if (stub_entry != NULL
   12277  1.1  christos 	      && (stub_entry->stub_type == ppc_stub_plt_call
   12278  1.1  christos 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   12279  1.1  christos 		  || stub_entry->stub_type == ppc_stub_long_branch_r2off))
   12280  1.1  christos 	    {
   12281  1.1  christos 	      bfd_boolean can_plt_call = FALSE;
   12282  1.1  christos 
   12283  1.1  christos 	      if (rel->r_offset + 8 <= input_section->size)
   12284  1.1  christos 		{
   12285  1.1  christos 		  unsigned long nop;
   12286  1.1  christos 		  nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   12287  1.1  christos 		  if (nop == NOP
   12288  1.1  christos 		      || nop == CROR_151515 || nop == CROR_313131)
   12289  1.1  christos 		    {
   12290  1.1  christos 		      if (h != NULL
   12291  1.1  christos 			  && (h == htab->tls_get_addr_fd
   12292  1.1  christos 			      || h == htab->tls_get_addr)
   12293  1.1  christos 			  && !htab->no_tls_get_addr_opt)
   12294  1.1  christos 			{
   12295  1.1  christos 			  /* Special stub used, leave nop alone.  */
   12296  1.1  christos 			}
   12297  1.1  christos 		      else
   12298  1.1  christos 			bfd_put_32 (input_bfd, LD_R2_40R1,
   12299  1.1  christos 				    contents + rel->r_offset + 4);
   12300  1.1  christos 		      can_plt_call = TRUE;
   12301  1.1  christos 		    }
   12302  1.1  christos 		}
   12303  1.1  christos 
   12304  1.1  christos 	      if (!can_plt_call)
   12305  1.1  christos 		{
   12306  1.1  christos 		  if (stub_entry->stub_type == ppc_stub_plt_call)
   12307  1.1  christos 		    {
   12308  1.1  christos 		      /* If this is a plain branch rather than a branch
   12309  1.1  christos 			 and link, don't require a nop.  However, don't
   12310  1.1  christos 			 allow tail calls in a shared library as they
   12311  1.1  christos 			 will result in r2 being corrupted.  */
   12312  1.1  christos 		      unsigned long br;
   12313  1.1  christos 		      br = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12314  1.1  christos 		      if (info->executable && (br & 1) == 0)
   12315  1.1  christos 			can_plt_call = TRUE;
   12316  1.1  christos 		      else
   12317  1.1  christos 			stub_entry = NULL;
   12318  1.1  christos 		    }
   12319  1.1  christos 		  else if (h != NULL
   12320  1.1  christos 			   && strcmp (h->elf.root.root.string,
   12321  1.1  christos 				      ".__libc_start_main") == 0)
   12322  1.1  christos 		    {
   12323  1.1  christos 		      /* Allow crt1 branch to go via a toc adjusting stub.  */
   12324  1.1  christos 		      can_plt_call = TRUE;
   12325  1.1  christos 		    }
   12326  1.1  christos 		  else
   12327  1.1  christos 		    {
   12328  1.1  christos 		      if (strcmp (input_section->output_section->name,
   12329  1.1  christos 				  ".init") == 0
   12330  1.1  christos 			  || strcmp (input_section->output_section->name,
   12331  1.1  christos 				     ".fini") == 0)
   12332  1.1  christos 			(*_bfd_error_handler)
   12333  1.1  christos 			  (_("%B(%A+0x%lx): automatic multiple TOCs "
   12334  1.1  christos 			     "not supported using your crt files; "
   12335  1.1  christos 			     "recompile with -mminimal-toc or upgrade gcc"),
   12336  1.1  christos 			   input_bfd,
   12337  1.1  christos 			   input_section,
   12338  1.1  christos 			   (long) rel->r_offset);
   12339  1.1  christos 		      else
   12340  1.1  christos 			(*_bfd_error_handler)
   12341  1.1  christos 			  (_("%B(%A+0x%lx): sibling call optimization to `%s' "
   12342  1.1  christos 			     "does not allow automatic multiple TOCs; "
   12343  1.1  christos 			     "recompile with -mminimal-toc or "
   12344  1.1  christos 			     "-fno-optimize-sibling-calls, "
   12345  1.1  christos 			     "or make `%s' extern"),
   12346  1.1  christos 			   input_bfd,
   12347  1.1  christos 			   input_section,
   12348  1.1  christos 			   (long) rel->r_offset,
   12349  1.1  christos 			   sym_name,
   12350  1.1  christos 			   sym_name);
   12351  1.1  christos 		      bfd_set_error (bfd_error_bad_value);
   12352  1.1  christos 		      ret = FALSE;
   12353  1.1  christos 		    }
   12354  1.1  christos 		}
   12355  1.1  christos 
   12356  1.1  christos 	      if (can_plt_call
   12357  1.1  christos 		  && stub_entry->stub_type == ppc_stub_plt_call)
   12358  1.1  christos 		unresolved_reloc = FALSE;
   12359  1.1  christos 	    }
   12360  1.1  christos 
   12361  1.1  christos 	  if ((stub_entry == NULL
   12362  1.1  christos 	       || stub_entry->stub_type == ppc_stub_long_branch
   12363  1.1  christos 	       || stub_entry->stub_type == ppc_stub_plt_branch)
   12364  1.1  christos 	      && get_opd_info (sec) != NULL)
   12365  1.1  christos 	    {
   12366  1.1  christos 	      /* The branch destination is the value of the opd entry. */
   12367  1.1  christos 	      bfd_vma off = (relocation + addend
   12368  1.1  christos 			     - sec->output_section->vma
   12369  1.1  christos 			     - sec->output_offset);
   12370  1.1  christos 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
   12371  1.1  christos 	      if (dest != (bfd_vma) -1)
   12372  1.1  christos 		{
   12373  1.1  christos 		  relocation = dest;
   12374  1.1  christos 		  addend = 0;
   12375  1.1  christos 		}
   12376  1.1  christos 	    }
   12377  1.1  christos 
   12378  1.1  christos 	  /* If the branch is out of reach we ought to have a long
   12379  1.1  christos 	     branch stub.  */
   12380  1.1  christos 	  from = (rel->r_offset
   12381  1.1  christos 		  + input_section->output_offset
   12382  1.1  christos 		  + input_section->output_section->vma);
   12383  1.1  christos 
   12384  1.1  christos 	  if (stub_entry != NULL
   12385  1.1  christos 	      && (stub_entry->stub_type == ppc_stub_long_branch
   12386  1.1  christos 		  || stub_entry->stub_type == ppc_stub_plt_branch)
   12387  1.1  christos 	      && (r_type == R_PPC64_ADDR14_BRTAKEN
   12388  1.1  christos 		  || r_type == R_PPC64_ADDR14_BRNTAKEN
   12389  1.1  christos 		  || (relocation + addend - from + max_br_offset
   12390  1.1  christos 		      < 2 * max_br_offset)))
   12391  1.1  christos 	    /* Don't use the stub if this branch is in range.  */
   12392  1.1  christos 	    stub_entry = NULL;
   12393  1.1  christos 
   12394  1.1  christos 	  if (stub_entry != NULL)
   12395  1.1  christos 	    {
   12396  1.1  christos 	      /* Munge up the value and addend so that we call the stub
   12397  1.1  christos 		 rather than the procedure directly.  */
   12398  1.1  christos 	      relocation = (stub_entry->stub_offset
   12399  1.1  christos 			    + stub_entry->stub_sec->output_offset
   12400  1.1  christos 			    + stub_entry->stub_sec->output_section->vma);
   12401  1.1  christos 	      addend = 0;
   12402  1.1  christos 	    }
   12403  1.1  christos 
   12404  1.1  christos 	  if (insn != 0)
   12405  1.1  christos 	    {
   12406  1.1  christos 	      if (is_power4)
   12407  1.1  christos 		{
   12408  1.1  christos 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   12409  1.1  christos 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   12410  1.1  christos 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   12411  1.1  christos 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   12412  1.1  christos 		    insn |= 0x02 << 21;
   12413  1.1  christos 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   12414  1.1  christos 		    insn |= 0x08 << 21;
   12415  1.1  christos 		  else
   12416  1.1  christos 		    break;
   12417  1.1  christos 		}
   12418  1.1  christos 	      else
   12419  1.1  christos 		{
   12420  1.1  christos 		  /* Invert 'y' bit if not the default.  */
   12421  1.1  christos 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   12422  1.1  christos 		    insn ^= 0x01 << 21;
   12423  1.1  christos 		}
   12424  1.1  christos 
   12425  1.1  christos 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   12426  1.1  christos 	    }
   12427  1.1  christos 
   12428  1.1  christos 	  /* NOP out calls to undefined weak functions.
   12429  1.1  christos 	     We can thus call a weak function without first
   12430  1.1  christos 	     checking whether the function is defined.  */
   12431  1.1  christos 	  else if (h != NULL
   12432  1.1  christos 		   && h->elf.root.type == bfd_link_hash_undefweak
   12433  1.1  christos 		   && h->elf.dynindx == -1
   12434  1.1  christos 		   && r_type == R_PPC64_REL24
   12435  1.1  christos 		   && relocation == 0
   12436  1.1  christos 		   && addend == 0)
   12437  1.1  christos 	    {
   12438  1.1  christos 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   12439  1.1  christos 	      continue;
   12440  1.1  christos 	    }
   12441  1.1  christos 	  break;
   12442  1.1  christos 	}
   12443  1.1  christos 
   12444  1.1  christos       /* Set `addend'.  */
   12445  1.1  christos       tls_type = 0;
   12446  1.1  christos       switch (r_type)
   12447  1.1  christos 	{
   12448  1.1  christos 	default:
   12449  1.1  christos 	  (*_bfd_error_handler)
   12450  1.1  christos 	    (_("%B: unknown relocation type %d for symbol %s"),
   12451  1.1  christos 	     input_bfd, (int) r_type, sym_name);
   12452  1.1  christos 
   12453  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   12454  1.1  christos 	  ret = FALSE;
   12455  1.1  christos 	  continue;
   12456  1.1  christos 
   12457  1.1  christos 	case R_PPC64_NONE:
   12458  1.1  christos 	case R_PPC64_TLS:
   12459  1.1  christos 	case R_PPC64_TLSGD:
   12460  1.1  christos 	case R_PPC64_TLSLD:
   12461  1.1  christos 	case R_PPC64_GNU_VTINHERIT:
   12462  1.1  christos 	case R_PPC64_GNU_VTENTRY:
   12463  1.1  christos 	  continue;
   12464  1.1  christos 
   12465  1.1  christos 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   12466  1.1  christos 	     address in the GOT as relocation value instead of the
   12467  1.1  christos 	     symbol's value itself.  Also, create a GOT entry for the
   12468  1.1  christos 	     symbol and put the symbol value there.  */
   12469  1.1  christos 	case R_PPC64_GOT_TLSGD16:
   12470  1.1  christos 	case R_PPC64_GOT_TLSGD16_LO:
   12471  1.1  christos 	case R_PPC64_GOT_TLSGD16_HI:
   12472  1.1  christos 	case R_PPC64_GOT_TLSGD16_HA:
   12473  1.1  christos 	  tls_type = TLS_TLS | TLS_GD;
   12474  1.1  christos 	  goto dogot;
   12475  1.1  christos 
   12476  1.1  christos 	case R_PPC64_GOT_TLSLD16:
   12477  1.1  christos 	case R_PPC64_GOT_TLSLD16_LO:
   12478  1.1  christos 	case R_PPC64_GOT_TLSLD16_HI:
   12479  1.1  christos 	case R_PPC64_GOT_TLSLD16_HA:
   12480  1.1  christos 	  tls_type = TLS_TLS | TLS_LD;
   12481  1.1  christos 	  goto dogot;
   12482  1.1  christos 
   12483  1.1  christos 	case R_PPC64_GOT_TPREL16_DS:
   12484  1.1  christos 	case R_PPC64_GOT_TPREL16_LO_DS:
   12485  1.1  christos 	case R_PPC64_GOT_TPREL16_HI:
   12486  1.1  christos 	case R_PPC64_GOT_TPREL16_HA:
   12487  1.1  christos 	  tls_type = TLS_TLS | TLS_TPREL;
   12488  1.1  christos 	  goto dogot;
   12489  1.1  christos 
   12490  1.1  christos 	case R_PPC64_GOT_DTPREL16_DS:
   12491  1.1  christos 	case R_PPC64_GOT_DTPREL16_LO_DS:
   12492  1.1  christos 	case R_PPC64_GOT_DTPREL16_HI:
   12493  1.1  christos 	case R_PPC64_GOT_DTPREL16_HA:
   12494  1.1  christos 	  tls_type = TLS_TLS | TLS_DTPREL;
   12495  1.1  christos 	  goto dogot;
   12496  1.1  christos 
   12497  1.1  christos 	case R_PPC64_GOT16:
   12498  1.1  christos 	case R_PPC64_GOT16_LO:
   12499  1.1  christos 	case R_PPC64_GOT16_HI:
   12500  1.1  christos 	case R_PPC64_GOT16_HA:
   12501  1.1  christos 	case R_PPC64_GOT16_DS:
   12502  1.1  christos 	case R_PPC64_GOT16_LO_DS:
   12503  1.1  christos 	dogot:
   12504  1.1  christos 	  {
   12505  1.1  christos 	    /* Relocation is to the entry for this symbol in the global
   12506  1.1  christos 	       offset table.  */
   12507  1.1  christos 	    asection *got;
   12508  1.1  christos 	    bfd_vma *offp;
   12509  1.1  christos 	    bfd_vma off;
   12510  1.1  christos 	    unsigned long indx = 0;
   12511  1.1  christos 	    struct got_entry *ent;
   12512  1.1  christos 
   12513  1.1  christos 	    if (tls_type == (TLS_TLS | TLS_LD)
   12514  1.1  christos 		&& (h == NULL
   12515  1.1  christos 		    || !h->elf.def_dynamic))
   12516  1.1  christos 	      ent = ppc64_tlsld_got (input_bfd);
   12517  1.1  christos 	    else
   12518  1.1  christos 	      {
   12519  1.1  christos 
   12520  1.1  christos 		if (h != NULL)
   12521  1.1  christos 		  {
   12522  1.1  christos 		    bfd_boolean dyn = htab->elf.dynamic_sections_created;
   12523  1.1  christos 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
   12524  1.1  christos 							  &h->elf)
   12525  1.1  christos 			|| (info->shared
   12526  1.1  christos 			    && SYMBOL_CALLS_LOCAL (info, &h->elf)))
   12527  1.1  christos 		      /* This is actually a static link, or it is a
   12528  1.1  christos 			 -Bsymbolic link and the symbol is defined
   12529  1.1  christos 			 locally, or the symbol was forced to be local
   12530  1.1  christos 			 because of a version file.  */
   12531  1.1  christos 		      ;
   12532  1.1  christos 		    else
   12533  1.1  christos 		      {
   12534  1.1  christos 			indx = h->elf.dynindx;
   12535  1.1  christos 			unresolved_reloc = FALSE;
   12536  1.1  christos 		      }
   12537  1.1  christos 		    ent = h->elf.got.glist;
   12538  1.1  christos 		  }
   12539  1.1  christos 		else
   12540  1.1  christos 		  {
   12541  1.1  christos 		    if (local_got_ents == NULL)
   12542  1.1  christos 		      abort ();
   12543  1.1  christos 		    ent = local_got_ents[r_symndx];
   12544  1.1  christos 		  }
   12545  1.1  christos 
   12546  1.1  christos 		for (; ent != NULL; ent = ent->next)
   12547  1.1  christos 		  if (ent->addend == orig_addend
   12548  1.1  christos 		      && ent->owner == input_bfd
   12549  1.1  christos 		      && ent->tls_type == tls_type)
   12550  1.1  christos 		    break;
   12551  1.1  christos 	      }
   12552  1.1  christos 
   12553  1.1  christos 	    if (ent == NULL)
   12554  1.1  christos 	      abort ();
   12555  1.1  christos 	    if (ent->is_indirect)
   12556  1.1  christos 	      ent = ent->got.ent;
   12557  1.1  christos 	    offp = &ent->got.offset;
   12558  1.1  christos 	    got = ppc64_elf_tdata (ent->owner)->got;
   12559  1.1  christos 	    if (got == NULL)
   12560  1.1  christos 	      abort ();
   12561  1.1  christos 
   12562  1.1  christos 	    /* The offset must always be a multiple of 8.  We use the
   12563  1.1  christos 	       least significant bit to record whether we have already
   12564  1.1  christos 	       processed this entry.  */
   12565  1.1  christos 	    off = *offp;
   12566  1.1  christos 	    if ((off & 1) != 0)
   12567  1.1  christos 	      off &= ~1;
   12568  1.1  christos 	    else
   12569  1.1  christos 	      {
   12570  1.1  christos 		/* Generate relocs for the dynamic linker, except in
   12571  1.1  christos 		   the case of TLSLD where we'll use one entry per
   12572  1.1  christos 		   module.  */
   12573  1.1  christos 		asection *relgot;
   12574  1.1  christos 		bfd_boolean ifunc;
   12575  1.1  christos 
   12576  1.1  christos 		*offp = off | 1;
   12577  1.1  christos 		relgot = NULL;
   12578  1.1  christos 		ifunc = (h != NULL
   12579  1.1  christos 			 ? h->elf.type == STT_GNU_IFUNC
   12580  1.1  christos 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   12581  1.1  christos 		if ((info->shared || indx != 0)
   12582  1.1  christos 		    && (h == NULL
   12583  1.1  christos 			|| (tls_type == (TLS_TLS | TLS_LD)
   12584  1.1  christos 			    && !h->elf.def_dynamic)
   12585  1.1  christos 			|| ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   12586  1.1  christos 			|| h->elf.root.type != bfd_link_hash_undefweak))
   12587  1.1  christos 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   12588  1.1  christos 		else if (ifunc)
   12589  1.1  christos 		  relgot = htab->reliplt;
   12590  1.1  christos 		if (relgot != NULL)
   12591  1.1  christos 		  {
   12592  1.1  christos 		    outrel.r_offset = (got->output_section->vma
   12593  1.1  christos 				       + got->output_offset
   12594  1.1  christos 				       + off);
   12595  1.1  christos 		    outrel.r_addend = addend;
   12596  1.1  christos 		    if (tls_type & (TLS_LD | TLS_GD))
   12597  1.1  christos 		      {
   12598  1.1  christos 			outrel.r_addend = 0;
   12599  1.1  christos 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   12600  1.1  christos 			if (tls_type == (TLS_TLS | TLS_GD))
   12601  1.1  christos 			  {
   12602  1.1  christos 			    loc = relgot->contents;
   12603  1.1  christos 			    loc += (relgot->reloc_count++
   12604  1.1  christos 				    * sizeof (Elf64_External_Rela));
   12605  1.1  christos 			    bfd_elf64_swap_reloca_out (output_bfd,
   12606  1.1  christos 						       &outrel, loc);
   12607  1.1  christos 			    outrel.r_offset += 8;
   12608  1.1  christos 			    outrel.r_addend = addend;
   12609  1.1  christos 			    outrel.r_info
   12610  1.1  christos 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   12611  1.1  christos 			  }
   12612  1.1  christos 		      }
   12613  1.1  christos 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   12614  1.1  christos 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   12615  1.1  christos 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   12616  1.1  christos 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   12617  1.1  christos 		    else if (indx != 0)
   12618  1.1  christos 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   12619  1.1  christos 		    else
   12620  1.1  christos 		      {
   12621  1.1  christos 			if (ifunc)
   12622  1.1  christos 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   12623  1.1  christos 			else
   12624  1.1  christos 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   12625  1.1  christos 
   12626  1.1  christos 			/* Write the .got section contents for the sake
   12627  1.1  christos 			   of prelink.  */
   12628  1.1  christos 			loc = got->contents + off;
   12629  1.1  christos 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   12630  1.1  christos 				    loc);
   12631  1.1  christos 		      }
   12632  1.1  christos 
   12633  1.1  christos 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   12634  1.1  christos 		      {
   12635  1.1  christos 			outrel.r_addend += relocation;
   12636  1.1  christos 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   12637  1.1  christos 			  outrel.r_addend -= htab->elf.tls_sec->vma;
   12638  1.1  christos 		      }
   12639  1.1  christos 		    loc = relgot->contents;
   12640  1.1  christos 		    loc += (relgot->reloc_count++
   12641  1.1  christos 			    * sizeof (Elf64_External_Rela));
   12642  1.1  christos 		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   12643  1.1  christos 		  }
   12644  1.1  christos 
   12645  1.1  christos 		/* Init the .got section contents here if we're not
   12646  1.1  christos 		   emitting a reloc.  */
   12647  1.1  christos 		else
   12648  1.1  christos 		  {
   12649  1.1  christos 		    relocation += addend;
   12650  1.1  christos 		    if (tls_type == (TLS_TLS | TLS_LD))
   12651  1.1  christos 		      relocation = 1;
   12652  1.1  christos 		    else if (tls_type != 0)
   12653  1.1  christos 		      {
   12654  1.1  christos 			relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   12655  1.1  christos 			if (tls_type == (TLS_TLS | TLS_TPREL))
   12656  1.1  christos 			  relocation += DTP_OFFSET - TP_OFFSET;
   12657  1.1  christos 
   12658  1.1  christos 			if (tls_type == (TLS_TLS | TLS_GD))
   12659  1.1  christos 			  {
   12660  1.1  christos 			    bfd_put_64 (output_bfd, relocation,
   12661  1.1  christos 					got->contents + off + 8);
   12662  1.1  christos 			    relocation = 1;
   12663  1.1  christos 			  }
   12664  1.1  christos 		      }
   12665  1.1  christos 
   12666  1.1  christos 		    bfd_put_64 (output_bfd, relocation,
   12667  1.1  christos 				got->contents + off);
   12668  1.1  christos 		  }
   12669  1.1  christos 	      }
   12670  1.1  christos 
   12671  1.1  christos 	    if (off >= (bfd_vma) -2)
   12672  1.1  christos 	      abort ();
   12673  1.1  christos 
   12674  1.1  christos 	    relocation = got->output_section->vma + got->output_offset + off;
   12675  1.1  christos 	    addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
   12676  1.1  christos 	  }
   12677  1.1  christos 	  break;
   12678  1.1  christos 
   12679  1.1  christos 	case R_PPC64_PLT16_HA:
   12680  1.1  christos 	case R_PPC64_PLT16_HI:
   12681  1.1  christos 	case R_PPC64_PLT16_LO:
   12682  1.1  christos 	case R_PPC64_PLT32:
   12683  1.1  christos 	case R_PPC64_PLT64:
   12684  1.1  christos 	  /* Relocation is to the entry for this symbol in the
   12685  1.1  christos 	     procedure linkage table.  */
   12686  1.1  christos 
   12687  1.1  christos 	  /* Resolve a PLT reloc against a local symbol directly,
   12688  1.1  christos 	     without using the procedure linkage table.  */
   12689  1.1  christos 	  if (h == NULL)
   12690  1.1  christos 	    break;
   12691  1.1  christos 
   12692  1.1  christos 	  /* It's possible that we didn't make a PLT entry for this
   12693  1.1  christos 	     symbol.  This happens when statically linking PIC code,
   12694  1.1  christos 	     or when using -Bsymbolic.  Go find a match if there is a
   12695  1.1  christos 	     PLT entry.  */
   12696  1.1  christos 	  if (htab->plt != NULL)
   12697  1.1  christos 	    {
   12698  1.1  christos 	      struct plt_entry *ent;
   12699  1.1  christos 	      for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
   12700  1.1  christos 		if (ent->addend == orig_addend
   12701  1.1  christos 		    && ent->plt.offset != (bfd_vma) -1)
   12702  1.1  christos 		  {
   12703  1.1  christos 		    relocation = (htab->plt->output_section->vma
   12704  1.1  christos 				  + htab->plt->output_offset
   12705  1.1  christos 				  + ent->plt.offset);
   12706  1.1  christos 		    unresolved_reloc = FALSE;
   12707  1.1  christos 		  }
   12708  1.1  christos 	    }
   12709  1.1  christos 	  break;
   12710  1.1  christos 
   12711  1.1  christos 	case R_PPC64_TOC:
   12712  1.1  christos 	  /* Relocation value is TOC base.  */
   12713  1.1  christos 	  relocation = TOCstart;
   12714  1.1  christos 	  if (r_symndx == STN_UNDEF)
   12715  1.1  christos 	    relocation += htab->stub_group[input_section->id].toc_off;
   12716  1.1  christos 	  else if (unresolved_reloc)
   12717  1.1  christos 	    ;
   12718  1.1  christos 	  else if (sec != NULL && sec->id <= htab->top_id)
   12719  1.1  christos 	    relocation += htab->stub_group[sec->id].toc_off;
   12720  1.1  christos 	  else
   12721  1.1  christos 	    unresolved_reloc = TRUE;
   12722  1.1  christos 	  goto dodyn;
   12723  1.1  christos 
   12724  1.1  christos 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   12725  1.1  christos 	     which is the address of the start of the TOC plus 0x8000.
   12726  1.1  christos 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   12727  1.1  christos 	     in this order.  */
   12728  1.1  christos 	case R_PPC64_TOC16:
   12729  1.1  christos 	case R_PPC64_TOC16_LO:
   12730  1.1  christos 	case R_PPC64_TOC16_HI:
   12731  1.1  christos 	case R_PPC64_TOC16_DS:
   12732  1.1  christos 	case R_PPC64_TOC16_LO_DS:
   12733  1.1  christos 	case R_PPC64_TOC16_HA:
   12734  1.1  christos 	  addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
   12735  1.1  christos 	  break;
   12736  1.1  christos 
   12737  1.1  christos 	  /* Relocate against the beginning of the section.  */
   12738  1.1  christos 	case R_PPC64_SECTOFF:
   12739  1.1  christos 	case R_PPC64_SECTOFF_LO:
   12740  1.1  christos 	case R_PPC64_SECTOFF_HI:
   12741  1.1  christos 	case R_PPC64_SECTOFF_DS:
   12742  1.1  christos 	case R_PPC64_SECTOFF_LO_DS:
   12743  1.1  christos 	case R_PPC64_SECTOFF_HA:
   12744  1.1  christos 	  if (sec != NULL)
   12745  1.1  christos 	    addend -= sec->output_section->vma;
   12746  1.1  christos 	  break;
   12747  1.1  christos 
   12748  1.1  christos 	case R_PPC64_REL16:
   12749  1.1  christos 	case R_PPC64_REL16_LO:
   12750  1.1  christos 	case R_PPC64_REL16_HI:
   12751  1.1  christos 	case R_PPC64_REL16_HA:
   12752  1.1  christos 	  break;
   12753  1.1  christos 
   12754  1.1  christos 	case R_PPC64_REL14:
   12755  1.1  christos 	case R_PPC64_REL14_BRNTAKEN:
   12756  1.1  christos 	case R_PPC64_REL14_BRTAKEN:
   12757  1.1  christos 	case R_PPC64_REL24:
   12758  1.1  christos 	  break;
   12759  1.1  christos 
   12760  1.1  christos 	case R_PPC64_TPREL16:
   12761  1.1  christos 	case R_PPC64_TPREL16_LO:
   12762  1.1  christos 	case R_PPC64_TPREL16_HI:
   12763  1.1  christos 	case R_PPC64_TPREL16_HA:
   12764  1.1  christos 	case R_PPC64_TPREL16_DS:
   12765  1.1  christos 	case R_PPC64_TPREL16_LO_DS:
   12766  1.1  christos 	case R_PPC64_TPREL16_HIGHER:
   12767  1.1  christos 	case R_PPC64_TPREL16_HIGHERA:
   12768  1.1  christos 	case R_PPC64_TPREL16_HIGHEST:
   12769  1.1  christos 	case R_PPC64_TPREL16_HIGHESTA:
   12770  1.1  christos 	  if (h != NULL
   12771  1.1  christos 	      && h->elf.root.type == bfd_link_hash_undefweak
   12772  1.1  christos 	      && h->elf.dynindx == -1)
   12773  1.1  christos 	    {
   12774  1.1  christos 	      /* Make this relocation against an undefined weak symbol
   12775  1.1  christos 		 resolve to zero.  This is really just a tweak, since
   12776  1.1  christos 		 code using weak externs ought to check that they are
   12777  1.1  christos 		 defined before using them.  */
   12778  1.1  christos 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   12779  1.1  christos 
   12780  1.1  christos 	      insn = bfd_get_32 (output_bfd, p);
   12781  1.1  christos 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   12782  1.1  christos 	      if (insn != 0)
   12783  1.1  christos 		bfd_put_32 (output_bfd, insn, p);
   12784  1.1  christos 	      break;
   12785  1.1  christos 	    }
   12786  1.1  christos 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   12787  1.1  christos 	  if (info->shared)
   12788  1.1  christos 	    /* The TPREL16 relocs shouldn't really be used in shared
   12789  1.1  christos 	       libs as they will result in DT_TEXTREL being set, but
   12790  1.1  christos 	       support them anyway.  */
   12791  1.1  christos 	    goto dodyn;
   12792  1.1  christos 	  break;
   12793  1.1  christos 
   12794  1.1  christos 	case R_PPC64_DTPREL16:
   12795  1.1  christos 	case R_PPC64_DTPREL16_LO:
   12796  1.1  christos 	case R_PPC64_DTPREL16_HI:
   12797  1.1  christos 	case R_PPC64_DTPREL16_HA:
   12798  1.1  christos 	case R_PPC64_DTPREL16_DS:
   12799  1.1  christos 	case R_PPC64_DTPREL16_LO_DS:
   12800  1.1  christos 	case R_PPC64_DTPREL16_HIGHER:
   12801  1.1  christos 	case R_PPC64_DTPREL16_HIGHERA:
   12802  1.1  christos 	case R_PPC64_DTPREL16_HIGHEST:
   12803  1.1  christos 	case R_PPC64_DTPREL16_HIGHESTA:
   12804  1.1  christos 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   12805  1.1  christos 	  break;
   12806  1.1  christos 
   12807  1.1  christos 	case R_PPC64_DTPMOD64:
   12808  1.1  christos 	  relocation = 1;
   12809  1.1  christos 	  addend = 0;
   12810  1.1  christos 	  goto dodyn;
   12811  1.1  christos 
   12812  1.1  christos 	case R_PPC64_TPREL64:
   12813  1.1  christos 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   12814  1.1  christos 	  goto dodyn;
   12815  1.1  christos 
   12816  1.1  christos 	case R_PPC64_DTPREL64:
   12817  1.1  christos 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   12818  1.1  christos 	  /* Fall thru */
   12819  1.1  christos 
   12820  1.1  christos 	  /* Relocations that may need to be propagated if this is a
   12821  1.1  christos 	     dynamic object.  */
   12822  1.1  christos 	case R_PPC64_REL30:
   12823  1.1  christos 	case R_PPC64_REL32:
   12824  1.1  christos 	case R_PPC64_REL64:
   12825  1.1  christos 	case R_PPC64_ADDR14:
   12826  1.1  christos 	case R_PPC64_ADDR14_BRNTAKEN:
   12827  1.1  christos 	case R_PPC64_ADDR14_BRTAKEN:
   12828  1.1  christos 	case R_PPC64_ADDR16:
   12829  1.1  christos 	case R_PPC64_ADDR16_DS:
   12830  1.1  christos 	case R_PPC64_ADDR16_HA:
   12831  1.1  christos 	case R_PPC64_ADDR16_HI:
   12832  1.1  christos 	case R_PPC64_ADDR16_HIGHER:
   12833  1.1  christos 	case R_PPC64_ADDR16_HIGHERA:
   12834  1.1  christos 	case R_PPC64_ADDR16_HIGHEST:
   12835  1.1  christos 	case R_PPC64_ADDR16_HIGHESTA:
   12836  1.1  christos 	case R_PPC64_ADDR16_LO:
   12837  1.1  christos 	case R_PPC64_ADDR16_LO_DS:
   12838  1.1  christos 	case R_PPC64_ADDR24:
   12839  1.1  christos 	case R_PPC64_ADDR32:
   12840  1.1  christos 	case R_PPC64_ADDR64:
   12841  1.1  christos 	case R_PPC64_UADDR16:
   12842  1.1  christos 	case R_PPC64_UADDR32:
   12843  1.1  christos 	case R_PPC64_UADDR64:
   12844  1.1  christos 	dodyn:
   12845  1.1  christos 	  if ((input_section->flags & SEC_ALLOC) == 0)
   12846  1.1  christos 	    break;
   12847  1.1  christos 
   12848  1.1  christos 	  if (NO_OPD_RELOCS && is_opd)
   12849  1.1  christos 	    break;
   12850  1.1  christos 
   12851  1.1  christos 	  if ((info->shared
   12852  1.1  christos 	       && (h == NULL
   12853  1.1  christos 		   || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   12854  1.1  christos 		   || h->elf.root.type != bfd_link_hash_undefweak)
   12855  1.1  christos 	       && (must_be_dyn_reloc (info, r_type)
   12856  1.1  christos 		   || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
   12857  1.1  christos 	      || (ELIMINATE_COPY_RELOCS
   12858  1.1  christos 		  && !info->shared
   12859  1.1  christos 		  && h != NULL
   12860  1.1  christos 		  && h->elf.dynindx != -1
   12861  1.1  christos 		  && !h->elf.non_got_ref
   12862  1.1  christos 		  && !h->elf.def_regular)
   12863  1.1  christos 	      || (!info->shared
   12864  1.1  christos 		  && (h != NULL
   12865  1.1  christos 		      ? h->elf.type == STT_GNU_IFUNC
   12866  1.1  christos 		      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
   12867  1.1  christos 	    {
   12868  1.1  christos 	      bfd_boolean skip, relocate;
   12869  1.1  christos 	      asection *sreloc;
   12870  1.1  christos 	      bfd_vma out_off;
   12871  1.1  christos 
   12872  1.1  christos 	      /* When generating a dynamic object, these relocations
   12873  1.1  christos 		 are copied into the output file to be resolved at run
   12874  1.1  christos 		 time.  */
   12875  1.1  christos 
   12876  1.1  christos 	      skip = FALSE;
   12877  1.1  christos 	      relocate = FALSE;
   12878  1.1  christos 
   12879  1.1  christos 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   12880  1.1  christos 						 input_section, rel->r_offset);
   12881  1.1  christos 	      if (out_off == (bfd_vma) -1)
   12882  1.1  christos 		skip = TRUE;
   12883  1.1  christos 	      else if (out_off == (bfd_vma) -2)
   12884  1.1  christos 		skip = TRUE, relocate = TRUE;
   12885  1.1  christos 	      out_off += (input_section->output_section->vma
   12886  1.1  christos 			  + input_section->output_offset);
   12887  1.1  christos 	      outrel.r_offset = out_off;
   12888  1.1  christos 	      outrel.r_addend = rel->r_addend;
   12889  1.1  christos 
   12890  1.1  christos 	      /* Optimize unaligned reloc use.  */
   12891  1.1  christos 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   12892  1.1  christos 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   12893  1.1  christos 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   12894  1.1  christos 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   12895  1.1  christos 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   12896  1.1  christos 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   12897  1.1  christos 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   12898  1.1  christos 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   12899  1.1  christos 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   12900  1.1  christos 
   12901  1.1  christos 	      if (skip)
   12902  1.1  christos 		memset (&outrel, 0, sizeof outrel);
   12903  1.1  christos 	      else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
   12904  1.1  christos 		       && !is_opd
   12905  1.1  christos 		       && r_type != R_PPC64_TOC)
   12906  1.1  christos 		outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
   12907  1.1  christos 	      else
   12908  1.1  christos 		{
   12909  1.1  christos 		  /* This symbol is local, or marked to become local,
   12910  1.1  christos 		     or this is an opd section reloc which must point
   12911  1.1  christos 		     at a local function.  */
   12912  1.1  christos 		  outrel.r_addend += relocation;
   12913  1.1  christos 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   12914  1.1  christos 		    {
   12915  1.1  christos 		      if (is_opd && h != NULL)
   12916  1.1  christos 			{
   12917  1.1  christos 			  /* Lie about opd entries.  This case occurs
   12918  1.1  christos 			     when building shared libraries and we
   12919  1.1  christos 			     reference a function in another shared
   12920  1.1  christos 			     lib.  The same thing happens for a weak
   12921  1.1  christos 			     definition in an application that's
   12922  1.1  christos 			     overridden by a strong definition in a
   12923  1.1  christos 			     shared lib.  (I believe this is a generic
   12924  1.1  christos 			     bug in binutils handling of weak syms.)
   12925  1.1  christos 			     In these cases we won't use the opd
   12926  1.1  christos 			     entry in this lib.  */
   12927  1.1  christos 			  unresolved_reloc = FALSE;
   12928  1.1  christos 			}
   12929  1.1  christos 		      if (!is_opd
   12930  1.1  christos 			  && r_type == R_PPC64_ADDR64
   12931  1.1  christos 			  && (h != NULL
   12932  1.1  christos 			      ? h->elf.type == STT_GNU_IFUNC
   12933  1.1  christos 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   12934  1.1  christos 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   12935  1.1  christos 		      else
   12936  1.1  christos 			{
   12937  1.1  christos 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   12938  1.1  christos 
   12939  1.1  christos 			  /* We need to relocate .opd contents for ld.so.
   12940  1.1  christos 			     Prelink also wants simple and consistent rules
   12941  1.1  christos 			     for relocs.  This make all RELATIVE relocs have
   12942  1.1  christos 			     *r_offset equal to r_addend.  */
   12943  1.1  christos 			  relocate = TRUE;
   12944  1.1  christos 			}
   12945  1.1  christos 		    }
   12946  1.1  christos 		  else
   12947  1.1  christos 		    {
   12948  1.1  christos 		      long indx = 0;
   12949  1.1  christos 
   12950  1.1  christos 		      if (h != NULL
   12951  1.1  christos 			  ? h->elf.type == STT_GNU_IFUNC
   12952  1.1  christos 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   12953  1.1  christos 			{
   12954  1.1  christos 			  (*_bfd_error_handler)
   12955  1.1  christos 			    (_("%B(%A+0x%lx): relocation %s for indirect "
   12956  1.1  christos 			       "function %s unsupported"),
   12957  1.1  christos 			     input_bfd,
   12958  1.1  christos 			     input_section,
   12959  1.1  christos 			     (long) rel->r_offset,
   12960  1.1  christos 			     ppc64_elf_howto_table[r_type]->name,
   12961  1.1  christos 			     sym_name);
   12962  1.1  christos 			  ret = FALSE;
   12963  1.1  christos 			}
   12964  1.1  christos 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   12965  1.1  christos 			;
   12966  1.1  christos 		      else if (sec == NULL || sec->owner == NULL)
   12967  1.1  christos 			{
   12968  1.1  christos 			  bfd_set_error (bfd_error_bad_value);
   12969  1.1  christos 			  return FALSE;
   12970  1.1  christos 			}
   12971  1.1  christos 		      else
   12972  1.1  christos 			{
   12973  1.1  christos 			  asection *osec;
   12974  1.1  christos 
   12975  1.1  christos 			  osec = sec->output_section;
   12976  1.1  christos 			  indx = elf_section_data (osec)->dynindx;
   12977  1.1  christos 
   12978  1.1  christos 			  if (indx == 0)
   12979  1.1  christos 			    {
   12980  1.1  christos 			      if ((osec->flags & SEC_READONLY) == 0
   12981  1.1  christos 				  && htab->elf.data_index_section != NULL)
   12982  1.1  christos 				osec = htab->elf.data_index_section;
   12983  1.1  christos 			      else
   12984  1.1  christos 				osec = htab->elf.text_index_section;
   12985  1.1  christos 			      indx = elf_section_data (osec)->dynindx;
   12986  1.1  christos 			    }
   12987  1.1  christos 			  BFD_ASSERT (indx != 0);
   12988  1.1  christos 
   12989  1.1  christos 			  /* We are turning this relocation into one
   12990  1.1  christos 			     against a section symbol, so subtract out
   12991  1.1  christos 			     the output section's address but not the
   12992  1.1  christos 			     offset of the input section in the output
   12993  1.1  christos 			     section.  */
   12994  1.1  christos 			  outrel.r_addend -= osec->vma;
   12995  1.1  christos 			}
   12996  1.1  christos 
   12997  1.1  christos 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   12998  1.1  christos 		    }
   12999  1.1  christos 		}
   13000  1.1  christos 
   13001  1.1  christos 	      sreloc = elf_section_data (input_section)->sreloc;
   13002  1.1  christos 	      if (!htab->elf.dynamic_sections_created)
   13003  1.1  christos 		sreloc = htab->reliplt;
   13004  1.1  christos 	      if (sreloc == NULL)
   13005  1.1  christos 		abort ();
   13006  1.1  christos 
   13007  1.1  christos 	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   13008  1.1  christos 		  >= sreloc->size)
   13009  1.1  christos 		abort ();
   13010  1.1  christos 	      loc = sreloc->contents;
   13011  1.1  christos 	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   13012  1.1  christos 	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   13013  1.1  christos 
   13014  1.1  christos 	      /* If this reloc is against an external symbol, it will
   13015  1.1  christos 		 be computed at runtime, so there's no need to do
   13016  1.1  christos 		 anything now.  However, for the sake of prelink ensure
   13017  1.1  christos 		 that the section contents are a known value.  */
   13018  1.1  christos 	      if (! relocate)
   13019  1.1  christos 		{
   13020  1.1  christos 		  unresolved_reloc = FALSE;
   13021  1.1  christos 		  /* The value chosen here is quite arbitrary as ld.so
   13022  1.1  christos 		     ignores section contents except for the special
   13023  1.1  christos 		     case of .opd where the contents might be accessed
   13024  1.1  christos 		     before relocation.  Choose zero, as that won't
   13025  1.1  christos 		     cause reloc overflow.  */
   13026  1.1  christos 		  relocation = 0;
   13027  1.1  christos 		  addend = 0;
   13028  1.1  christos 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   13029  1.1  christos 		     to improve backward compatibility with older
   13030  1.1  christos 		     versions of ld.  */
   13031  1.1  christos 		  if (r_type == R_PPC64_ADDR64)
   13032  1.1  christos 		    addend = outrel.r_addend;
   13033  1.1  christos 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   13034  1.1  christos 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   13035  1.1  christos 		    addend = (input_section->output_section->vma
   13036  1.1  christos 			      + input_section->output_offset
   13037  1.1  christos 			      + rel->r_offset);
   13038  1.1  christos 		}
   13039  1.1  christos 	    }
   13040  1.1  christos 	  break;
   13041  1.1  christos 
   13042  1.1  christos 	case R_PPC64_COPY:
   13043  1.1  christos 	case R_PPC64_GLOB_DAT:
   13044  1.1  christos 	case R_PPC64_JMP_SLOT:
   13045  1.1  christos 	case R_PPC64_JMP_IREL:
   13046  1.1  christos 	case R_PPC64_RELATIVE:
   13047  1.1  christos 	  /* We shouldn't ever see these dynamic relocs in relocatable
   13048  1.1  christos 	     files.  */
   13049  1.1  christos 	  /* Fall through.  */
   13050  1.1  christos 
   13051  1.1  christos 	case R_PPC64_PLTGOT16:
   13052  1.1  christos 	case R_PPC64_PLTGOT16_DS:
   13053  1.1  christos 	case R_PPC64_PLTGOT16_HA:
   13054  1.1  christos 	case R_PPC64_PLTGOT16_HI:
   13055  1.1  christos 	case R_PPC64_PLTGOT16_LO:
   13056  1.1  christos 	case R_PPC64_PLTGOT16_LO_DS:
   13057  1.1  christos 	case R_PPC64_PLTREL32:
   13058  1.1  christos 	case R_PPC64_PLTREL64:
   13059  1.1  christos 	  /* These ones haven't been implemented yet.  */
   13060  1.1  christos 
   13061  1.1  christos 	  (*_bfd_error_handler)
   13062  1.1  christos 	    (_("%B: relocation %s is not supported for symbol %s."),
   13063  1.1  christos 	     input_bfd,
   13064  1.1  christos 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   13065  1.1  christos 
   13066  1.1  christos 	  bfd_set_error (bfd_error_invalid_operation);
   13067  1.1  christos 	  ret = FALSE;
   13068  1.1  christos 	  continue;
   13069  1.1  christos 	}
   13070  1.1  christos 
   13071  1.1  christos       /* Multi-instruction sequences that access the TOC can be
   13072  1.1  christos 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   13073  1.1  christos 	 to             nop;           addi rb,r2,x;  */
   13074  1.1  christos       switch (r_type)
   13075  1.1  christos 	{
   13076  1.1  christos 	default:
   13077  1.1  christos 	  break;
   13078  1.1  christos 
   13079  1.1  christos 	case R_PPC64_GOT_TLSLD16_HI:
   13080  1.1  christos 	case R_PPC64_GOT_TLSGD16_HI:
   13081  1.1  christos 	case R_PPC64_GOT_TPREL16_HI:
   13082  1.1  christos 	case R_PPC64_GOT_DTPREL16_HI:
   13083  1.1  christos 	case R_PPC64_GOT16_HI:
   13084  1.1  christos 	case R_PPC64_TOC16_HI:
   13085  1.1  christos 	  /* These relocs would only be useful if building up an
   13086  1.1  christos 	     offset to later add to r2, perhaps in an indexed
   13087  1.1  christos 	     addressing mode instruction.  Don't try to optimize.
   13088  1.1  christos 	     Unfortunately, the possibility of someone building up an
   13089  1.1  christos 	     offset like this or even with the HA relocs, means that
   13090  1.1  christos 	     we need to check the high insn when optimizing the low
   13091  1.1  christos 	     insn.  */
   13092  1.1  christos 	  break;
   13093  1.1  christos 
   13094  1.1  christos 	case R_PPC64_GOT_TLSLD16_HA:
   13095  1.1  christos 	case R_PPC64_GOT_TLSGD16_HA:
   13096  1.1  christos 	case R_PPC64_GOT_TPREL16_HA:
   13097  1.1  christos 	case R_PPC64_GOT_DTPREL16_HA:
   13098  1.1  christos 	case R_PPC64_GOT16_HA:
   13099  1.1  christos 	case R_PPC64_TOC16_HA:
   13100  1.1  christos 	  /* nop is done later.  */
   13101  1.1  christos 	  break;
   13102  1.1  christos 
   13103  1.1  christos 	case R_PPC64_GOT_TLSLD16_LO:
   13104  1.1  christos 	case R_PPC64_GOT_TLSGD16_LO:
   13105  1.1  christos 	case R_PPC64_GOT_TPREL16_LO_DS:
   13106  1.1  christos 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13107  1.1  christos 	case R_PPC64_GOT16_LO:
   13108  1.1  christos 	case R_PPC64_GOT16_LO_DS:
   13109  1.1  christos 	case R_PPC64_TOC16_LO:
   13110  1.1  christos 	case R_PPC64_TOC16_LO_DS:
   13111  1.1  christos 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
   13112  1.1  christos 	    {
   13113  1.1  christos 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   13114  1.1  christos 	      insn = bfd_get_32 (input_bfd, p);
   13115  1.1  christos 	      if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
   13116  1.1  christos 		  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
   13117  1.1  christos 		  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
   13118  1.1  christos 		  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
   13119  1.1  christos 		  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
   13120  1.1  christos 		  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
   13121  1.1  christos 		  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
   13122  1.1  christos 		  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
   13123  1.1  christos 		  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
   13124  1.1  christos 		  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
   13125  1.1  christos 		  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
   13126  1.1  christos 		  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
   13127  1.1  christos 		  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
   13128  1.1  christos 		  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
   13129  1.1  christos 		  || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
   13130  1.1  christos 		      && (insn & 3) != 1)
   13131  1.1  christos 		  || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
   13132  1.1  christos 		      && ((insn & 3) == 0 || (insn & 3) == 3)))
   13133  1.1  christos 		{
   13134  1.1  christos 		  unsigned int reg = (insn >> 16) & 0x1f;
   13135  1.1  christos 		  const Elf_Internal_Rela *ha;
   13136  1.1  christos 		  bfd_boolean match_addend;
   13137  1.1  christos 
   13138  1.1  christos 		  match_addend = (sym != NULL
   13139  1.1  christos 				  && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
   13140  1.1  christos 		  ha = ha_reloc_match (relocs, rel, &reg, match_addend,
   13141  1.1  christos 				       input_bfd, contents);
   13142  1.1  christos 		  if (ha != NULL)
   13143  1.1  christos 		    {
   13144  1.1  christos 		      insn &= ~(0x1f << 16);
   13145  1.1  christos 		      insn |= reg << 16;
   13146  1.1  christos 		      bfd_put_32 (input_bfd, insn, p);
   13147  1.1  christos 		      if (ha_opt == NULL)
   13148  1.1  christos 			{
   13149  1.1  christos 			  ha_opt = bfd_zmalloc (input_section->reloc_count);
   13150  1.1  christos 			  if (ha_opt == NULL)
   13151  1.1  christos 			    return FALSE;
   13152  1.1  christos 			}
   13153  1.1  christos 		      ha_opt[ha - relocs] = 1;
   13154  1.1  christos 		    }
   13155  1.1  christos 		  else
   13156  1.1  christos 		    /* If we don't find a matching high part insn,
   13157  1.1  christos 		       something is fishy.  Refuse to nop any high
   13158  1.1  christos 		       part insn in this section.  */
   13159  1.1  christos 		    no_ha_opt = TRUE;
   13160  1.1  christos 		}
   13161  1.1  christos 	    }
   13162  1.1  christos 	  break;
   13163  1.1  christos 	}
   13164  1.1  christos 
   13165  1.1  christos       /* Do any further special processing.  */
   13166  1.1  christos       switch (r_type)
   13167  1.1  christos 	{
   13168  1.1  christos 	default:
   13169  1.1  christos 	  break;
   13170  1.1  christos 
   13171  1.1  christos 	case R_PPC64_ADDR16_HA:
   13172  1.1  christos 	case R_PPC64_REL16_HA:
   13173  1.1  christos 	case R_PPC64_ADDR16_HIGHERA:
   13174  1.1  christos 	case R_PPC64_ADDR16_HIGHESTA:
   13175  1.1  christos 	case R_PPC64_TOC16_HA:
   13176  1.1  christos 	case R_PPC64_SECTOFF_HA:
   13177  1.1  christos 	case R_PPC64_TPREL16_HA:
   13178  1.1  christos 	case R_PPC64_DTPREL16_HA:
   13179  1.1  christos 	case R_PPC64_TPREL16_HIGHER:
   13180  1.1  christos 	case R_PPC64_TPREL16_HIGHERA:
   13181  1.1  christos 	case R_PPC64_TPREL16_HIGHEST:
   13182  1.1  christos 	case R_PPC64_TPREL16_HIGHESTA:
   13183  1.1  christos 	case R_PPC64_DTPREL16_HIGHER:
   13184  1.1  christos 	case R_PPC64_DTPREL16_HIGHERA:
   13185  1.1  christos 	case R_PPC64_DTPREL16_HIGHEST:
   13186  1.1  christos 	case R_PPC64_DTPREL16_HIGHESTA:
   13187  1.1  christos 	  /* It's just possible that this symbol is a weak symbol
   13188  1.1  christos 	     that's not actually defined anywhere. In that case,
   13189  1.1  christos 	     'sec' would be NULL, and we should leave the symbol
   13190  1.1  christos 	     alone (it will be set to zero elsewhere in the link).  */
   13191  1.1  christos 	  if (sec == NULL)
   13192  1.1  christos 	    break;
   13193  1.1  christos 	  /* Fall thru */
   13194  1.1  christos 
   13195  1.1  christos 	case R_PPC64_GOT16_HA:
   13196  1.1  christos 	case R_PPC64_PLTGOT16_HA:
   13197  1.1  christos 	case R_PPC64_PLT16_HA:
   13198  1.1  christos 	case R_PPC64_GOT_TLSGD16_HA:
   13199  1.1  christos 	case R_PPC64_GOT_TLSLD16_HA:
   13200  1.1  christos 	case R_PPC64_GOT_TPREL16_HA:
   13201  1.1  christos 	case R_PPC64_GOT_DTPREL16_HA:
   13202  1.1  christos 	  /* Add 0x10000 if sign bit in 0:15 is set.
   13203  1.1  christos 	     Bits 0:15 are not used.  */
   13204  1.1  christos 	  addend += 0x8000;
   13205  1.1  christos 	  break;
   13206  1.1  christos 
   13207  1.1  christos 	case R_PPC64_ADDR16_DS:
   13208  1.1  christos 	case R_PPC64_ADDR16_LO_DS:
   13209  1.1  christos 	case R_PPC64_GOT16_DS:
   13210  1.1  christos 	case R_PPC64_GOT16_LO_DS:
   13211  1.1  christos 	case R_PPC64_PLT16_LO_DS:
   13212  1.1  christos 	case R_PPC64_SECTOFF_DS:
   13213  1.1  christos 	case R_PPC64_SECTOFF_LO_DS:
   13214  1.1  christos 	case R_PPC64_TOC16_DS:
   13215  1.1  christos 	case R_PPC64_TOC16_LO_DS:
   13216  1.1  christos 	case R_PPC64_PLTGOT16_DS:
   13217  1.1  christos 	case R_PPC64_PLTGOT16_LO_DS:
   13218  1.1  christos 	case R_PPC64_GOT_TPREL16_DS:
   13219  1.1  christos 	case R_PPC64_GOT_TPREL16_LO_DS:
   13220  1.1  christos 	case R_PPC64_GOT_DTPREL16_DS:
   13221  1.1  christos 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13222  1.1  christos 	case R_PPC64_TPREL16_DS:
   13223  1.1  christos 	case R_PPC64_TPREL16_LO_DS:
   13224  1.1  christos 	case R_PPC64_DTPREL16_DS:
   13225  1.1  christos 	case R_PPC64_DTPREL16_LO_DS:
   13226  1.1  christos 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   13227  1.1  christos 	  mask = 3;
   13228  1.1  christos 	  /* If this reloc is against an lq insn, then the value must be
   13229  1.1  christos 	     a multiple of 16.  This is somewhat of a hack, but the
   13230  1.1  christos 	     "correct" way to do this by defining _DQ forms of all the
   13231  1.1  christos 	     _DS relocs bloats all reloc switches in this file.  It
   13232  1.1  christos 	     doesn't seem to make much sense to use any of these relocs
   13233  1.1  christos 	     in data, so testing the insn should be safe.  */
   13234  1.1  christos 	  if ((insn & (0x3f << 26)) == (56u << 26))
   13235  1.1  christos 	    mask = 15;
   13236  1.1  christos 	  if (((relocation + addend) & mask) != 0)
   13237  1.1  christos 	    {
   13238  1.1  christos 	      (*_bfd_error_handler)
   13239  1.1  christos 		(_("%B(%A+0x%lx): error: %s not a multiple of %u"),
   13240  1.1  christos 		 input_bfd, input_section, (long) rel->r_offset,
   13241  1.1  christos 		 ppc64_elf_howto_table[r_type]->name,
   13242  1.1  christos 		 mask + 1);
   13243  1.1  christos 	      bfd_set_error (bfd_error_bad_value);
   13244  1.1  christos 	      ret = FALSE;
   13245  1.1  christos 	      continue;
   13246  1.1  christos 	    }
   13247  1.1  christos 	  break;
   13248  1.1  christos 	}
   13249  1.1  christos 
   13250  1.1  christos       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   13251  1.1  christos 	 because such sections are not SEC_ALLOC and thus ld.so will
   13252  1.1  christos 	 not process them.  */
   13253  1.1  christos       if (unresolved_reloc
   13254  1.1  christos 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   13255  1.1  christos 	       && h->elf.def_dynamic))
   13256  1.1  christos 	{
   13257  1.1  christos 	  (*_bfd_error_handler)
   13258  1.1  christos 	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
   13259  1.1  christos 	     input_bfd,
   13260  1.1  christos 	     input_section,
   13261  1.1  christos 	     (long) rel->r_offset,
   13262  1.1  christos 	     ppc64_elf_howto_table[(int) r_type]->name,
   13263  1.1  christos 	     h->elf.root.root.string);
   13264  1.1  christos 	  ret = FALSE;
   13265  1.1  christos 	}
   13266  1.1  christos 
   13267  1.1  christos       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
   13268  1.1  christos 				    input_bfd,
   13269  1.1  christos 				    input_section,
   13270  1.1  christos 				    contents,
   13271  1.1  christos 				    rel->r_offset,
   13272  1.1  christos 				    relocation,
   13273  1.1  christos 				    addend);
   13274  1.1  christos 
   13275  1.1  christos       if (r != bfd_reloc_ok)
   13276  1.1  christos 	{
   13277  1.1  christos 	  if (sym_name == NULL)
   13278  1.1  christos 	    sym_name = "(null)";
   13279  1.1  christos 	  if (r == bfd_reloc_overflow)
   13280  1.1  christos 	    {
   13281  1.1  christos 	      if (warned)
   13282  1.1  christos 		continue;
   13283  1.1  christos 	      if (h != NULL
   13284  1.1  christos 		  && h->elf.root.type == bfd_link_hash_undefweak
   13285  1.1  christos 		  && ppc64_elf_howto_table[r_type]->pc_relative)
   13286  1.1  christos 		{
   13287  1.1  christos 		  /* Assume this is a call protected by other code that
   13288  1.1  christos 		     detects the symbol is undefined.  If this is the case,
   13289  1.1  christos 		     we can safely ignore the overflow.  If not, the
   13290  1.1  christos 		     program is hosed anyway, and a little warning isn't
   13291  1.1  christos 		     going to help.  */
   13292  1.1  christos 
   13293  1.1  christos 		  continue;
   13294  1.1  christos 		}
   13295  1.1  christos 
   13296  1.1  christos 	      if (!((*info->callbacks->reloc_overflow)
   13297  1.1  christos 		    (info, (h ? &h->elf.root : NULL), sym_name,
   13298  1.1  christos 		     ppc64_elf_howto_table[r_type]->name,
   13299  1.1  christos 		     orig_addend, input_bfd, input_section, rel->r_offset)))
   13300  1.1  christos 		return FALSE;
   13301  1.1  christos 	    }
   13302  1.1  christos 	  else
   13303  1.1  christos 	    {
   13304  1.1  christos 	      (*_bfd_error_handler)
   13305  1.1  christos 		(_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
   13306  1.1  christos 		 input_bfd,
   13307  1.1  christos 		 input_section,
   13308  1.1  christos 		 (long) rel->r_offset,
   13309  1.1  christos 		 ppc64_elf_howto_table[r_type]->name,
   13310  1.1  christos 		 sym_name,
   13311  1.1  christos 		 (int) r);
   13312  1.1  christos 	      ret = FALSE;
   13313  1.1  christos 	    }
   13314  1.1  christos 	}
   13315  1.1  christos     }
   13316  1.1  christos 
   13317  1.1  christos   if (ha_opt != NULL)
   13318  1.1  christos     {
   13319  1.1  christos       if (!no_ha_opt)
   13320  1.1  christos 	{
   13321  1.1  christos 	  unsigned char *opt = ha_opt;
   13322  1.1  christos 	  rel = relocs;
   13323  1.1  christos 	  relend = relocs + input_section->reloc_count;
   13324  1.1  christos 	  for (; rel < relend; opt++, rel++)
   13325  1.1  christos 	    if (*opt != 0)
   13326  1.1  christos 	      {
   13327  1.1  christos 		bfd_byte *p = contents + (rel->r_offset & ~3);
   13328  1.1  christos 		bfd_put_32 (input_bfd, NOP, p);
   13329  1.1  christos 	      }
   13330  1.1  christos 	}
   13331  1.1  christos       free (ha_opt);
   13332  1.1  christos     }
   13333  1.1  christos 
   13334  1.1  christos   /* If we're emitting relocations, then shortly after this function
   13335  1.1  christos      returns, reloc offsets and addends for this section will be
   13336  1.1  christos      adjusted.  Worse, reloc symbol indices will be for the output
   13337  1.1  christos      file rather than the input.  Save a copy of the relocs for
   13338  1.1  christos      opd_entry_value.  */
   13339  1.1  christos   if (is_opd && (info->emitrelocations || info->relocatable))
   13340  1.1  christos     {
   13341  1.1  christos       bfd_size_type amt;
   13342  1.1  christos       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   13343  1.1  christos       rel = bfd_alloc (input_bfd, amt);
   13344  1.1  christos       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
   13345  1.1  christos       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
   13346  1.1  christos       if (rel == NULL)
   13347  1.1  christos 	return FALSE;
   13348  1.1  christos       memcpy (rel, relocs, amt);
   13349  1.1  christos     }
   13350  1.1  christos   return ret;
   13351  1.1  christos }
   13352  1.1  christos 
   13353  1.1  christos /* Adjust the value of any local symbols in opd sections.  */
   13354  1.1  christos 
   13355  1.1  christos static int
   13356  1.1  christos ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   13357  1.1  christos 			      const char *name ATTRIBUTE_UNUSED,
   13358  1.1  christos 			      Elf_Internal_Sym *elfsym,
   13359  1.1  christos 			      asection *input_sec,
   13360  1.1  christos 			      struct elf_link_hash_entry *h)
   13361  1.1  christos {
   13362  1.1  christos   struct _opd_sec_data *opd;
   13363  1.1  christos   long adjust;
   13364  1.1  christos   bfd_vma value;
   13365  1.1  christos 
   13366  1.1  christos   if (h != NULL)
   13367  1.1  christos     return 1;
   13368  1.1  christos 
   13369  1.1  christos   opd = get_opd_info (input_sec);
   13370  1.1  christos   if (opd == NULL || opd->adjust == NULL)
   13371  1.1  christos     return 1;
   13372  1.1  christos 
   13373  1.1  christos   value = elfsym->st_value - input_sec->output_offset;
   13374  1.1  christos   if (!info->relocatable)
   13375  1.1  christos     value -= input_sec->output_section->vma;
   13376  1.1  christos 
   13377  1.1  christos   adjust = opd->adjust[value / 8];
   13378  1.1  christos   if (adjust == -1)
   13379  1.1  christos     return 2;
   13380  1.1  christos 
   13381  1.1  christos   elfsym->st_value += adjust;
   13382  1.1  christos   return 1;
   13383  1.1  christos }
   13384  1.1  christos 
   13385  1.1  christos /* Finish up dynamic symbol handling.  We set the contents of various
   13386  1.1  christos    dynamic sections here.  */
   13387  1.1  christos 
   13388  1.1  christos static bfd_boolean
   13389  1.1  christos ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   13390  1.1  christos 				 struct bfd_link_info *info,
   13391  1.1  christos 				 struct elf_link_hash_entry *h,
   13392  1.1  christos 				 Elf_Internal_Sym *sym)
   13393  1.1  christos {
   13394  1.1  christos   struct ppc_link_hash_table *htab;
   13395  1.1  christos   struct plt_entry *ent;
   13396  1.1  christos   Elf_Internal_Rela rela;
   13397  1.1  christos   bfd_byte *loc;
   13398  1.1  christos 
   13399  1.1  christos   htab = ppc_hash_table (info);
   13400  1.1  christos   if (htab == NULL)
   13401  1.1  christos     return FALSE;
   13402  1.1  christos 
   13403  1.1  christos   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   13404  1.1  christos     if (ent->plt.offset != (bfd_vma) -1)
   13405  1.1  christos       {
   13406  1.1  christos 	/* This symbol has an entry in the procedure linkage
   13407  1.1  christos 	   table.  Set it up.  */
   13408  1.1  christos 	if (!htab->elf.dynamic_sections_created
   13409  1.1  christos 	    || h->dynindx == -1)
   13410  1.1  christos 	  {
   13411  1.1  christos 	    BFD_ASSERT (h->type == STT_GNU_IFUNC
   13412  1.1  christos 			&& h->def_regular
   13413  1.1  christos 			&& (h->root.type == bfd_link_hash_defined
   13414  1.1  christos 			    || h->root.type == bfd_link_hash_defweak));
   13415  1.1  christos 	    rela.r_offset = (htab->iplt->output_section->vma
   13416  1.1  christos 			     + htab->iplt->output_offset
   13417  1.1  christos 			     + ent->plt.offset);
   13418  1.1  christos 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   13419  1.1  christos 	    rela.r_addend = (h->root.u.def.value
   13420  1.1  christos 			     + h->root.u.def.section->output_offset
   13421  1.1  christos 			     + h->root.u.def.section->output_section->vma
   13422  1.1  christos 			     + ent->addend);
   13423  1.1  christos 	    loc = (htab->reliplt->contents
   13424  1.1  christos 		   + (htab->reliplt->reloc_count++
   13425  1.1  christos 		      * sizeof (Elf64_External_Rela)));
   13426  1.1  christos 	  }
   13427  1.1  christos 	else
   13428  1.1  christos 	  {
   13429  1.1  christos 	    rela.r_offset = (htab->plt->output_section->vma
   13430  1.1  christos 			     + htab->plt->output_offset
   13431  1.1  christos 			     + ent->plt.offset);
   13432  1.1  christos 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   13433  1.1  christos 	    rela.r_addend = ent->addend;
   13434  1.1  christos 	    loc = (htab->relplt->contents
   13435  1.1  christos 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
   13436  1.1  christos 		      / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
   13437  1.1  christos 	  }
   13438  1.1  christos 	bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   13439  1.1  christos       }
   13440  1.1  christos 
   13441  1.1  christos   if (h->needs_copy)
   13442  1.1  christos     {
   13443  1.1  christos       /* This symbol needs a copy reloc.  Set it up.  */
   13444  1.1  christos 
   13445  1.1  christos       if (h->dynindx == -1
   13446  1.1  christos 	  || (h->root.type != bfd_link_hash_defined
   13447  1.1  christos 	      && h->root.type != bfd_link_hash_defweak)
   13448  1.1  christos 	  || htab->relbss == NULL)
   13449  1.1  christos 	abort ();
   13450  1.1  christos 
   13451  1.1  christos       rela.r_offset = (h->root.u.def.value
   13452  1.1  christos 		       + h->root.u.def.section->output_section->vma
   13453  1.1  christos 		       + h->root.u.def.section->output_offset);
   13454  1.1  christos       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   13455  1.1  christos       rela.r_addend = 0;
   13456  1.1  christos       loc = htab->relbss->contents;
   13457  1.1  christos       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
   13458  1.1  christos       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   13459  1.1  christos     }
   13460  1.1  christos 
   13461  1.1  christos   /* Mark some specially defined symbols as absolute.  */
   13462  1.1  christos   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
   13463  1.1  christos     sym->st_shndx = SHN_ABS;
   13464  1.1  christos 
   13465  1.1  christos   return TRUE;
   13466  1.1  christos }
   13467  1.1  christos 
   13468  1.1  christos /* Used to decide how to sort relocs in an optimal manner for the
   13469  1.1  christos    dynamic linker, before writing them out.  */
   13470  1.1  christos 
   13471  1.1  christos static enum elf_reloc_type_class
   13472  1.1  christos ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
   13473  1.1  christos {
   13474  1.1  christos   enum elf_ppc64_reloc_type r_type;
   13475  1.1  christos 
   13476  1.1  christos   r_type = ELF64_R_TYPE (rela->r_info);
   13477  1.1  christos   switch (r_type)
   13478  1.1  christos     {
   13479  1.1  christos     case R_PPC64_RELATIVE:
   13480  1.1  christos       return reloc_class_relative;
   13481  1.1  christos     case R_PPC64_JMP_SLOT:
   13482  1.1  christos       return reloc_class_plt;
   13483  1.1  christos     case R_PPC64_COPY:
   13484  1.1  christos       return reloc_class_copy;
   13485  1.1  christos     default:
   13486  1.1  christos       return reloc_class_normal;
   13487  1.1  christos     }
   13488  1.1  christos }
   13489  1.1  christos 
   13490  1.1  christos /* Finish up the dynamic sections.  */
   13491  1.1  christos 
   13492  1.1  christos static bfd_boolean
   13493  1.1  christos ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   13494  1.1  christos 				   struct bfd_link_info *info)
   13495  1.1  christos {
   13496  1.1  christos   struct ppc_link_hash_table *htab;
   13497  1.1  christos   bfd *dynobj;
   13498  1.1  christos   asection *sdyn;
   13499  1.1  christos 
   13500  1.1  christos   htab = ppc_hash_table (info);
   13501  1.1  christos   if (htab == NULL)
   13502  1.1  christos     return FALSE;
   13503  1.1  christos 
   13504  1.1  christos   dynobj = htab->elf.dynobj;
   13505  1.1  christos   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
   13506  1.1  christos 
   13507  1.1  christos   if (htab->elf.dynamic_sections_created)
   13508  1.1  christos     {
   13509  1.1  christos       Elf64_External_Dyn *dyncon, *dynconend;
   13510  1.1  christos 
   13511  1.1  christos       if (sdyn == NULL || htab->got == NULL)
   13512  1.1  christos 	abort ();
   13513  1.1  christos 
   13514  1.1  christos       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   13515  1.1  christos       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   13516  1.1  christos       for (; dyncon < dynconend; dyncon++)
   13517  1.1  christos 	{
   13518  1.1  christos 	  Elf_Internal_Dyn dyn;
   13519  1.1  christos 	  asection *s;
   13520  1.1  christos 
   13521  1.1  christos 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   13522  1.1  christos 
   13523  1.1  christos 	  switch (dyn.d_tag)
   13524  1.1  christos 	    {
   13525  1.1  christos 	    default:
   13526  1.1  christos 	      continue;
   13527  1.1  christos 
   13528  1.1  christos 	    case DT_PPC64_GLINK:
   13529  1.1  christos 	      s = htab->glink;
   13530  1.1  christos 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   13531  1.1  christos 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   13532  1.1  christos 		 of glink rather than the first entry point, which is
   13533  1.1  christos 		 what ld.so needs, and now have a bigger stub to
   13534  1.1  christos 		 support automatic multiple TOCs.  */
   13535  1.1  christos 	      dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
   13536  1.1  christos 	      break;
   13537  1.1  christos 
   13538  1.1  christos 	    case DT_PPC64_OPD:
   13539  1.1  christos 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   13540  1.1  christos 	      if (s == NULL)
   13541  1.1  christos 		continue;
   13542  1.1  christos 	      dyn.d_un.d_ptr = s->vma;
   13543  1.1  christos 	      break;
   13544  1.1  christos 
   13545  1.1  christos 	    case DT_PPC64_OPDSZ:
   13546  1.1  christos 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   13547  1.1  christos 	      if (s == NULL)
   13548  1.1  christos 		continue;
   13549  1.1  christos 	      dyn.d_un.d_val = s->size;
   13550  1.1  christos 	      break;
   13551  1.1  christos 
   13552  1.1  christos 	    case DT_PLTGOT:
   13553  1.1  christos 	      s = htab->plt;
   13554  1.1  christos 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   13555  1.1  christos 	      break;
   13556  1.1  christos 
   13557  1.1  christos 	    case DT_JMPREL:
   13558  1.1  christos 	      s = htab->relplt;
   13559  1.1  christos 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   13560  1.1  christos 	      break;
   13561  1.1  christos 
   13562  1.1  christos 	    case DT_PLTRELSZ:
   13563  1.1  christos 	      dyn.d_un.d_val = htab->relplt->size;
   13564  1.1  christos 	      break;
   13565  1.1  christos 
   13566  1.1  christos 	    case DT_RELASZ:
   13567  1.1  christos 	      /* Don't count procedure linkage table relocs in the
   13568  1.1  christos 		 overall reloc count.  */
   13569  1.1  christos 	      s = htab->relplt;
   13570  1.1  christos 	      if (s == NULL)
   13571  1.1  christos 		continue;
   13572  1.1  christos 	      dyn.d_un.d_val -= s->size;
   13573  1.1  christos 	      break;
   13574  1.1  christos 
   13575  1.1  christos 	    case DT_RELA:
   13576  1.1  christos 	      /* We may not be using the standard ELF linker script.
   13577  1.1  christos 		 If .rela.plt is the first .rela section, we adjust
   13578  1.1  christos 		 DT_RELA to not include it.  */
   13579  1.1  christos 	      s = htab->relplt;
   13580  1.1  christos 	      if (s == NULL)
   13581  1.1  christos 		continue;
   13582  1.1  christos 	      if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
   13583  1.1  christos 		continue;
   13584  1.1  christos 	      dyn.d_un.d_ptr += s->size;
   13585  1.1  christos 	      break;
   13586  1.1  christos 	    }
   13587  1.1  christos 
   13588  1.1  christos 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   13589  1.1  christos 	}
   13590  1.1  christos     }
   13591  1.1  christos 
   13592  1.1  christos   if (htab->got != NULL && htab->got->size != 0)
   13593  1.1  christos     {
   13594  1.1  christos       /* Fill in the first entry in the global offset table.
   13595  1.1  christos 	 We use it to hold the link-time TOCbase.  */
   13596  1.1  christos       bfd_put_64 (output_bfd,
   13597  1.1  christos 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   13598  1.1  christos 		  htab->got->contents);
   13599  1.1  christos 
   13600  1.1  christos       /* Set .got entry size.  */
   13601  1.1  christos       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
   13602  1.1  christos     }
   13603  1.1  christos 
   13604  1.1  christos   if (htab->plt != NULL && htab->plt->size != 0)
   13605  1.1  christos     {
   13606  1.1  christos       /* Set .plt entry size.  */
   13607  1.1  christos       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
   13608  1.1  christos 	= PLT_ENTRY_SIZE;
   13609  1.1  christos     }
   13610  1.1  christos 
   13611  1.1  christos   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   13612  1.1  christos      brlt ourselves if emitrelocations.  */
   13613  1.1  christos   if (htab->brlt != NULL
   13614  1.1  christos       && htab->brlt->reloc_count != 0
   13615  1.1  christos       && !_bfd_elf_link_output_relocs (output_bfd,
   13616  1.1  christos 				       htab->brlt,
   13617  1.1  christos 				       elf_section_data (htab->brlt)->rela.hdr,
   13618  1.1  christos 				       elf_section_data (htab->brlt)->relocs,
   13619  1.1  christos 				       NULL))
   13620  1.1  christos     return FALSE;
   13621  1.1  christos 
   13622  1.1  christos   if (htab->glink != NULL
   13623  1.1  christos       && htab->glink->reloc_count != 0
   13624  1.1  christos       && !_bfd_elf_link_output_relocs (output_bfd,
   13625  1.1  christos 				       htab->glink,
   13626  1.1  christos 				       elf_section_data (htab->glink)->rela.hdr,
   13627  1.1  christos 				       elf_section_data (htab->glink)->relocs,
   13628  1.1  christos 				       NULL))
   13629  1.1  christos     return FALSE;
   13630  1.1  christos 
   13631  1.1  christos   /* We need to handle writing out multiple GOT sections ourselves,
   13632  1.1  christos      since we didn't add them to DYNOBJ.  We know dynobj is the first
   13633  1.1  christos      bfd.  */
   13634  1.1  christos   while ((dynobj = dynobj->link_next) != NULL)
   13635  1.1  christos     {
   13636  1.1  christos       asection *s;
   13637  1.1  christos 
   13638  1.1  christos       if (!is_ppc64_elf (dynobj))
   13639  1.1  christos 	continue;
   13640  1.1  christos 
   13641  1.1  christos       s = ppc64_elf_tdata (dynobj)->got;
   13642  1.1  christos       if (s != NULL
   13643  1.1  christos 	  && s->size != 0
   13644  1.1  christos 	  && s->output_section != bfd_abs_section_ptr
   13645  1.1  christos 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   13646  1.1  christos 					s->contents, s->output_offset,
   13647  1.1  christos 					s->size))
   13648  1.1  christos 	return FALSE;
   13649  1.1  christos       s = ppc64_elf_tdata (dynobj)->relgot;
   13650  1.1  christos       if (s != NULL
   13651  1.1  christos 	  && s->size != 0
   13652  1.1  christos 	  && s->output_section != bfd_abs_section_ptr
   13653  1.1  christos 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   13654  1.1  christos 					s->contents, s->output_offset,
   13655  1.1  christos 					s->size))
   13656  1.1  christos 	return FALSE;
   13657  1.1  christos     }
   13658  1.1  christos 
   13659                  return TRUE;
   13660                }
   13661                
   13662                #include "elf64-target.h"
   13663