Home | History | Annotate | Line # | Download | only in bfd
      1 /* PowerPC-specific support for 32-bit ELF
      2    Copyright (C) 1994-2026 Free Software Foundation, Inc.
      3    Written by Ian Lance Taylor, Cygnus Support.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the
     19    Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
     20    Boston, MA 02110-1301, USA.  */
     21 
     22 /* The assembler should generate a full set of section symbols even
     23    when they appear unused.  The linux kernel build tool recordmcount
     24    needs them.  */
     25 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
     26 
     27 #include "sysdep.h"
     28 #include <stdarg.h>
     29 #include "bfd.h"
     30 #include "bfdlink.h"
     31 #include "libbfd.h"
     32 #include "elf-bfd.h"
     33 #include "elf/ppc.h"
     34 #include "elf32-ppc.h"
     35 #include "elf-vxworks.h"
     36 #include "dwarf2.h"
     37 #include "opcode/ppc.h"
     38 
     39 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
     40 #define OCTETS_PER_BYTE(ABFD, SEC) 1
     41 
     42 typedef enum split16_format_type
     43 {
     44   split16a_type = 0,
     45   split16d_type
     46 }
     47 split16_format_type;
     48 
     49 /* RELA relocations are used here.  */
     50 
     51 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
     52   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     53 static bfd_reloc_status_type ppc_elf_unhandled_reloc
     54   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     55 
     56 #ifndef ARRAY_SIZE
     57 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
     58 #endif
     59 
     60 /* Branch prediction bit for branch taken relocs.  */
     61 #define BRANCH_PREDICT_BIT 0x200000
     62 /* Mask to set RA in memory instructions.  */
     63 #define RA_REGISTER_MASK 0x001f0000
     64 /* Value to shift register by to insert RA.  */
     65 #define RA_REGISTER_SHIFT 16
     66 
     67 /* The name of the dynamic interpreter.  This is put in the .interp
     68    section.  */
     69 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
     70 
     71 /* For old-style PLT.  */
     72 /* The number of single-slot PLT entries (the rest use two slots).  */
     73 #define PLT_NUM_SINGLE_ENTRIES 8192
     74 
     75 /* For new-style .glink and .plt.  */
     76 #define GLINK_PLTRESOLVE 16*4
     77 #define GLINK_ENTRY_SIZE(htab, h)					\
     78   ((4*4									\
     79     + (h != NULL							\
     80        && h == htab->tls_get_addr					\
     81        && !htab->params->no_tls_get_addr_opt ? 8*4 : 0)			\
     82     + (1u << htab->params->plt_stub_align) - 1)				\
     83    & -(1u << htab->params->plt_stub_align))
     84 
     85 /* VxWorks uses its own plt layout, filled in by the static linker.  */
     86 
     87 /* The standard VxWorks PLT entry.  */
     88 #define VXWORKS_PLT_ENTRY_SIZE 32
     89 static const bfd_vma ppc_elf_vxworks_plt_entry
     90     [VXWORKS_PLT_ENTRY_SIZE / 4] =
     91   {
     92     0x3d800000, /* lis	   r12,0		 */
     93     0x818c0000, /* lwz	   r12,0(r12)		 */
     94     0x7d8903a6, /* mtctr   r12			 */
     95     0x4e800420, /* bctr				 */
     96     0x39600000, /* li	   r11,0		 */
     97     0x48000000, /* b	   14 <.PLT0resolve+0x4> */
     98     0x60000000, /* nop				 */
     99     0x60000000, /* nop				 */
    100   };
    101 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
    102     [VXWORKS_PLT_ENTRY_SIZE / 4] =
    103   {
    104     0x3d9e0000, /* addis r12,r30,0 */
    105     0x818c0000, /* lwz	 r12,0(r12) */
    106     0x7d8903a6, /* mtctr r12 */
    107     0x4e800420, /* bctr */
    108     0x39600000, /* li	 r11,0 */
    109     0x48000000, /* b	 14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
    110     0x60000000, /* nop */
    111     0x60000000, /* nop */
    112   };
    113 
    114 /* The initial VxWorks PLT entry.  */
    115 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
    116 static const bfd_vma ppc_elf_vxworks_plt0_entry
    117     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
    118   {
    119     0x3d800000, /* lis	   r12,0	*/
    120     0x398c0000, /* addi	   r12,r12,0	*/
    121     0x800c0008, /* lwz	   r0,8(r12)	*/
    122     0x7c0903a6, /* mtctr   r0		*/
    123     0x818c0004, /* lwz	   r12,4(r12)	*/
    124     0x4e800420, /* bctr			*/
    125     0x60000000, /* nop			*/
    126     0x60000000, /* nop			*/
    127   };
    128 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
    129     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
    130   {
    131     0x819e0008, /* lwz	 r12,8(r30) */
    132     0x7d8903a6, /* mtctr r12	    */
    133     0x819e0004, /* lwz	 r12,4(r30) */
    134     0x4e800420, /* bctr		    */
    135     0x60000000, /* nop		    */
    136     0x60000000, /* nop		    */
    137     0x60000000, /* nop		    */
    138     0x60000000, /* nop		    */
    139   };
    140 
    141 /* For executables, we have some additional relocations in
    142    .rela.plt.unloaded, for the kernel loader.  */
    143 
    144 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
    145 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
    146 /* The number of relocations in the PLTResolve slot. */
    147 #define VXWORKS_PLTRESOLVE_RELOCS 2
    148 /* The number of relocations in the PLTResolve slot when creating
    149    a shared library. */
    150 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
    151 
    152 /* Some instructions.  */
    153 #define ADDIS_11_11	0x3d6b0000
    154 #define ADDIS_11_30	0x3d7e0000
    155 #define ADDIS_12_12	0x3d8c0000
    156 #define ADDI_11_11	0x396b0000
    157 #define ADD_0_11_11	0x7c0b5a14
    158 #define ADD_3_12_2	0x7c6c1214
    159 #define ADD_11_0_11	0x7d605a14
    160 #define B		0x48000000
    161 #define BA		0x48000002
    162 #define BCL_20_31	0x429f0005
    163 #define BCTR		0x4e800420
    164 #define BEQLR		0x4d820020
    165 #define CMPWI_11_0	0x2c0b0000
    166 #define LIS_11		0x3d600000
    167 #define LIS_12		0x3d800000
    168 #define LWZU_0_12	0x840c0000
    169 #define LWZ_0_12	0x800c0000
    170 #define LWZ_11_3	0x81630000
    171 #define LWZ_11_11	0x816b0000
    172 #define LWZ_11_30	0x817e0000
    173 #define LWZ_12_3	0x81830000
    174 #define LWZ_12_12	0x818c0000
    175 #define MR_0_3		0x7c601b78
    176 #define MR_3_0		0x7c030378
    177 #define MFLR_0		0x7c0802a6
    178 #define MFLR_12		0x7d8802a6
    179 #define MTCTR_0		0x7c0903a6
    180 #define MTCTR_11	0x7d6903a6
    181 #define MTLR_0		0x7c0803a6
    182 #define NOP		0x60000000
    183 #define SUB_11_11_12	0x7d6c5850
    184 
    185 /* Offset of tp and dtp pointers from start of TLS block.  */
    186 #define TP_OFFSET	0x7000
    187 #define DTP_OFFSET	0x8000
    188 
    189 /* The value of a defined global symbol.  */
    190 #define SYM_VAL(SYM) \
    191   ((SYM)->root.u.def.section->output_section->vma	\
    192    + (SYM)->root.u.def.section->output_offset		\
    193    + (SYM)->root.u.def.value)
    194 
    195 /* Relocation HOWTO's.  */
    197 /* Like other ELF RELA targets that don't apply multiple
    198    field-altering relocations to the same localation, src_mask is
    199    always zero and pcrel_offset is the same as pc_relative.
    200    PowerPC can always use a zero bitpos, even when the field is not at
    201    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
    202    and bitpos=2 which matches the ABI description, or as we do here,
    203    rightshift=0, bitsize=26 and bitpos=0.  */
    204 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
    205 	    complain, special_func)				\
    206   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,	\
    207 	 complain_overflow_ ## complain, special_func,		\
    208 	 #type, false, 0, mask, pc_relative)
    209 
    210 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
    211 
    212 static reloc_howto_type ppc_elf_howto_raw[] = {
    213   /* This reloc does nothing.  */
    214   HOW (R_PPC_NONE, 0, 0, 0, 0, false, dont,
    215        bfd_elf_generic_reloc),
    216 
    217   /* A standard 32 bit relocation.  */
    218   HOW (R_PPC_ADDR32, 4, 32, 0xffffffff, 0, false, dont,
    219        bfd_elf_generic_reloc),
    220 
    221   /* An absolute 26 bit branch; the lower two bits must be zero.
    222      FIXME: we don't check that, we just clear them.  */
    223   HOW (R_PPC_ADDR24, 4, 26, 0x3fffffc, 0, false, signed,
    224        bfd_elf_generic_reloc),
    225 
    226   /* A standard 16 bit relocation.  */
    227   HOW (R_PPC_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
    228        bfd_elf_generic_reloc),
    229 
    230   /* A 16 bit relocation without overflow.  */
    231   HOW (R_PPC_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
    232        bfd_elf_generic_reloc),
    233 
    234   /* The high order 16 bits of an address.  */
    235   HOW (R_PPC_ADDR16_HI, 2, 16, 0xffff, 16, false, dont,
    236        bfd_elf_generic_reloc),
    237 
    238   /* The high order 16 bits of an address, plus 1 if the contents of
    239      the low 16 bits, treated as a signed number, is negative.  */
    240   HOW (R_PPC_ADDR16_HA, 2, 16, 0xffff, 16, false, dont,
    241        ppc_elf_addr16_ha_reloc),
    242 
    243   /* An absolute 16 bit branch; the lower two bits must be zero.
    244      FIXME: we don't check that, we just clear them.  */
    245   HOW (R_PPC_ADDR14, 4, 16, 0xfffc, 0, false, signed,
    246        bfd_elf_generic_reloc),
    247 
    248   /* An absolute 16 bit branch, for which bit 10 should be set to
    249      indicate that the branch is expected to be taken.	The lower two
    250      bits must be zero.  */
    251   HOW (R_PPC_ADDR14_BRTAKEN, 4, 16, 0xfffc, 0, false, signed,
    252        bfd_elf_generic_reloc),
    253 
    254   /* An absolute 16 bit branch, for which bit 10 should be set to
    255      indicate that the branch is not expected to be taken.  The lower
    256      two bits must be zero.  */
    257   HOW (R_PPC_ADDR14_BRNTAKEN, 4, 16, 0xfffc, 0, false, signed,
    258        bfd_elf_generic_reloc),
    259 
    260   /* A relative 26 bit branch; the lower two bits must be zero.  */
    261   HOW (R_PPC_REL24, 4, 26, 0x3fffffc, 0, true, signed,
    262        bfd_elf_generic_reloc),
    263 
    264   /* A relative 16 bit branch; the lower two bits must be zero.  */
    265   HOW (R_PPC_REL14, 4, 16, 0xfffc, 0, true, signed,
    266        bfd_elf_generic_reloc),
    267 
    268   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    269      the branch is expected to be taken.  The lower two bits must be
    270      zero.  */
    271   HOW (R_PPC_REL14_BRTAKEN, 4, 16, 0xfffc, 0, true, signed,
    272        bfd_elf_generic_reloc),
    273 
    274   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    275      the branch is not expected to be taken.  The lower two bits must
    276      be zero.  */
    277   HOW (R_PPC_REL14_BRNTAKEN, 4, 16, 0xfffc, 0, true, signed,
    278        bfd_elf_generic_reloc),
    279 
    280   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
    281      symbol.  */
    282   HOW (R_PPC_GOT16, 2, 16, 0xffff, 0, false, signed,
    283        ppc_elf_unhandled_reloc),
    284 
    285   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
    286      the symbol.  */
    287   HOW (R_PPC_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
    288        ppc_elf_unhandled_reloc),
    289 
    290   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
    291      the symbol.  */
    292   HOW (R_PPC_GOT16_HI, 2, 16, 0xffff, 16, false, dont,
    293        ppc_elf_unhandled_reloc),
    294 
    295   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
    296      the symbol.  */
    297   HOW (R_PPC_GOT16_HA, 2, 16, 0xffff, 16, false, dont,
    298        ppc_elf_unhandled_reloc),
    299 
    300   /* Like R_PPC_REL24, but referring to the procedure linkage table
    301      entry for the symbol.  */
    302   HOW (R_PPC_PLTREL24, 4, 26, 0x3fffffc, 0, true, signed,
    303        ppc_elf_unhandled_reloc),
    304 
    305   /* This is used only by the dynamic linker.  The symbol should exist
    306      both in the object being run and in some shared library.  The
    307      dynamic linker copies the data addressed by the symbol from the
    308      shared library into the object, because the object being
    309      run has to have the data at some particular address.  */
    310   HOW (R_PPC_COPY, 4, 32, 0, 0, false, dont,
    311        ppc_elf_unhandled_reloc),
    312 
    313   /* Like R_PPC_ADDR32, but used when setting global offset table
    314      entries.  */
    315   HOW (R_PPC_GLOB_DAT, 4, 32, 0xffffffff, 0, false, dont,
    316        ppc_elf_unhandled_reloc),
    317 
    318   /* Marks a procedure linkage table entry for a symbol.  */
    319   HOW (R_PPC_JMP_SLOT, 4, 32, 0, 0, false, dont,
    320        ppc_elf_unhandled_reloc),
    321 
    322   /* Used only by the dynamic linker.  When the object is run, this
    323      longword is set to the load address of the object, plus the
    324      addend.  */
    325   HOW (R_PPC_RELATIVE, 4, 32, 0xffffffff, 0, false, dont,
    326        bfd_elf_generic_reloc),
    327 
    328   /* Like R_PPC_REL24, but uses the value of the symbol within the
    329      object rather than the final value.  Normally used for
    330      _GLOBAL_OFFSET_TABLE_.  */
    331   HOW (R_PPC_LOCAL24PC, 4, 26, 0x3fffffc, 0, true, signed,
    332        bfd_elf_generic_reloc),
    333 
    334   /* Like R_PPC_ADDR32, but may be unaligned.  */
    335   HOW (R_PPC_UADDR32, 4, 32, 0xffffffff, 0, false, dont,
    336        bfd_elf_generic_reloc),
    337 
    338   /* Like R_PPC_ADDR16, but may be unaligned.  */
    339   HOW (R_PPC_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
    340        bfd_elf_generic_reloc),
    341 
    342   /* 32-bit PC relative */
    343   HOW (R_PPC_REL32, 4, 32, 0xffffffff, 0, true, dont,
    344        bfd_elf_generic_reloc),
    345 
    346   /* 32-bit relocation to the symbol's procedure linkage table.
    347      FIXME: not supported.  */
    348   HOW (R_PPC_PLT32, 4, 32, 0, 0, false, dont,
    349        ppc_elf_unhandled_reloc),
    350 
    351   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    352      FIXME: not supported.  */
    353   HOW (R_PPC_PLTREL32, 4, 32, 0, 0, true, dont,
    354        ppc_elf_unhandled_reloc),
    355 
    356   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
    357      the symbol.  */
    358   HOW (R_PPC_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
    359        ppc_elf_unhandled_reloc),
    360 
    361   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
    362      the symbol.  */
    363   HOW (R_PPC_PLT16_HI, 2, 16, 0xffff, 16, false, dont,
    364        ppc_elf_unhandled_reloc),
    365 
    366   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
    367      the symbol.  */
    368   HOW (R_PPC_PLT16_HA, 2, 16, 0xffff, 16, false, dont,
    369        ppc_elf_unhandled_reloc),
    370 
    371   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
    372      small data items.  */
    373   HOW (R_PPC_SDAREL16, 2, 16, 0xffff, 0, false, signed,
    374        ppc_elf_unhandled_reloc),
    375 
    376   /* 16-bit section relative relocation.  */
    377   HOW (R_PPC_SECTOFF, 2, 16, 0xffff, 0, false, signed,
    378        ppc_elf_unhandled_reloc),
    379 
    380   /* 16-bit lower half section relative relocation.  */
    381   HOW (R_PPC_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
    382        ppc_elf_unhandled_reloc),
    383 
    384   /* 16-bit upper half section relative relocation.  */
    385   HOW (R_PPC_SECTOFF_HI, 2, 16, 0xffff, 16, false, dont,
    386        ppc_elf_unhandled_reloc),
    387 
    388   /* 16-bit upper half adjusted section relative relocation.  */
    389   HOW (R_PPC_SECTOFF_HA, 2, 16, 0xffff, 16, false, dont,
    390        ppc_elf_unhandled_reloc),
    391 
    392   /* Marker relocs for TLS.  */
    393   HOW (R_PPC_TLS, 4, 32, 0, 0, false, dont,
    394        bfd_elf_generic_reloc),
    395 
    396   HOW (R_PPC_TLSGD, 4, 32, 0, 0, false, dont,
    397        bfd_elf_generic_reloc),
    398 
    399   HOW (R_PPC_TLSLD, 4, 32, 0, 0, false, dont,
    400        bfd_elf_generic_reloc),
    401 
    402   /* Marker relocs on inline plt call instructions.  */
    403   HOW (R_PPC_PLTSEQ, 4, 32, 0, 0, false, dont,
    404        bfd_elf_generic_reloc),
    405 
    406   HOW (R_PPC_PLTCALL, 4, 32, 0, 0, false, dont,
    407        bfd_elf_generic_reloc),
    408 
    409   /* Computes the load module index of the load module that contains the
    410      definition of its TLS sym.  */
    411   HOW (R_PPC_DTPMOD32, 4, 32, 0xffffffff, 0, false, dont,
    412        ppc_elf_unhandled_reloc),
    413 
    414   /* Computes a dtv-relative displacement, the difference between the value
    415      of sym+add and the base address of the thread-local storage block that
    416      contains the definition of sym, minus 0x8000.  */
    417   HOW (R_PPC_DTPREL32, 4, 32, 0xffffffff, 0, false, dont,
    418        ppc_elf_unhandled_reloc),
    419 
    420   /* A 16 bit dtprel reloc.  */
    421   HOW (R_PPC_DTPREL16, 2, 16, 0xffff, 0, false, signed,
    422        ppc_elf_unhandled_reloc),
    423 
    424   /* Like DTPREL16, but no overflow.  */
    425   HOW (R_PPC_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
    426        ppc_elf_unhandled_reloc),
    427 
    428   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
    429   HOW (R_PPC_DTPREL16_HI, 2, 16, 0xffff, 16, false, dont,
    430        ppc_elf_unhandled_reloc),
    431 
    432   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
    433   HOW (R_PPC_DTPREL16_HA, 2, 16, 0xffff, 16, false, dont,
    434        ppc_elf_unhandled_reloc),
    435 
    436   /* Computes a tp-relative displacement, the difference between the value of
    437      sym+add and the value of the thread pointer (r13).  */
    438   HOW (R_PPC_TPREL32, 4, 32, 0xffffffff, 0, false, dont,
    439        ppc_elf_unhandled_reloc),
    440 
    441   /* A 16 bit tprel reloc.  */
    442   HOW (R_PPC_TPREL16, 2, 16, 0xffff, 0, false, signed,
    443        ppc_elf_unhandled_reloc),
    444 
    445   /* Like TPREL16, but no overflow.  */
    446   HOW (R_PPC_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
    447        ppc_elf_unhandled_reloc),
    448 
    449   /* Like TPREL16_LO, but next higher group of 16 bits.  */
    450   HOW (R_PPC_TPREL16_HI, 2, 16, 0xffff, 16, false, dont,
    451        ppc_elf_unhandled_reloc),
    452 
    453   /* Like TPREL16_HI, but adjust for low 16 bits.  */
    454   HOW (R_PPC_TPREL16_HA, 2, 16, 0xffff, 16, false, dont,
    455        ppc_elf_unhandled_reloc),
    456 
    457   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    458      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
    459      to the first entry.  */
    460   HOW (R_PPC_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
    461        ppc_elf_unhandled_reloc),
    462 
    463   /* Like GOT_TLSGD16, but no overflow.  */
    464   HOW (R_PPC_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
    465        ppc_elf_unhandled_reloc),
    466 
    467   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
    468   HOW (R_PPC_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, dont,
    469        ppc_elf_unhandled_reloc),
    470 
    471   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
    472   HOW (R_PPC_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, dont,
    473        ppc_elf_unhandled_reloc),
    474 
    475   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    476      with values (sym+add)@dtpmod and zero, and computes the offset to the
    477      first entry.  */
    478   HOW (R_PPC_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
    479        ppc_elf_unhandled_reloc),
    480 
    481   /* Like GOT_TLSLD16, but no overflow.  */
    482   HOW (R_PPC_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
    483        ppc_elf_unhandled_reloc),
    484 
    485   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
    486   HOW (R_PPC_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, dont,
    487        ppc_elf_unhandled_reloc),
    488 
    489   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
    490   HOW (R_PPC_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, dont,
    491        ppc_elf_unhandled_reloc),
    492 
    493   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
    494      the offset to the entry.  */
    495   HOW (R_PPC_GOT_DTPREL16, 2, 16, 0xffff, 0, false, signed,
    496        ppc_elf_unhandled_reloc),
    497 
    498   /* Like GOT_DTPREL16, but no overflow.  */
    499   HOW (R_PPC_GOT_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
    500        ppc_elf_unhandled_reloc),
    501 
    502   /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
    503   HOW (R_PPC_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, dont,
    504        ppc_elf_unhandled_reloc),
    505 
    506   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
    507   HOW (R_PPC_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, dont,
    508        ppc_elf_unhandled_reloc),
    509 
    510   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
    511      offset to the entry.  */
    512   HOW (R_PPC_GOT_TPREL16, 2, 16, 0xffff, 0, false, signed,
    513        ppc_elf_unhandled_reloc),
    514 
    515   /* Like GOT_TPREL16, but no overflow.  */
    516   HOW (R_PPC_GOT_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
    517        ppc_elf_unhandled_reloc),
    518 
    519   /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
    520   HOW (R_PPC_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, dont,
    521        ppc_elf_unhandled_reloc),
    522 
    523   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
    524   HOW (R_PPC_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, dont,
    525        ppc_elf_unhandled_reloc),
    526 
    527   /* The remaining relocs are from the Embedded ELF ABI, and are not
    528      in the SVR4 ELF ABI.  */
    529 
    530   /* 32 bit value resulting from the addend minus the symbol.  */
    531   HOW (R_PPC_EMB_NADDR32, 4, 32, 0xffffffff, 0, false, dont,
    532        ppc_elf_unhandled_reloc),
    533 
    534   /* 16 bit value resulting from the addend minus the symbol.  */
    535   HOW (R_PPC_EMB_NADDR16, 2, 16, 0xffff, 0, false, signed,
    536        ppc_elf_unhandled_reloc),
    537 
    538   /* 16 bit value resulting from the addend minus the symbol.  */
    539   HOW (R_PPC_EMB_NADDR16_LO, 2, 16, 0xffff, 0, false, dont,
    540        ppc_elf_unhandled_reloc),
    541 
    542   /* The high order 16 bits of the addend minus the symbol.  */
    543   HOW (R_PPC_EMB_NADDR16_HI, 2, 16, 0xffff, 16, false, dont,
    544        ppc_elf_unhandled_reloc),
    545 
    546   /* The high order 16 bits of the result of the addend minus the address,
    547      plus 1 if the contents of the low 16 bits, treated as a signed number,
    548      is negative.  */
    549   HOW (R_PPC_EMB_NADDR16_HA, 2, 16, 0xffff, 16, false, dont,
    550        ppc_elf_unhandled_reloc),
    551 
    552   /* 16 bit value resulting from allocating a 4 byte word to hold an
    553      address in the .sdata section, and returning the offset from
    554      _SDA_BASE_ for that relocation.  */
    555   HOW (R_PPC_EMB_SDAI16, 2, 16, 0xffff, 0, false, signed,
    556        ppc_elf_unhandled_reloc),
    557 
    558   /* 16 bit value resulting from allocating a 4 byte word to hold an
    559      address in the .sdata2 section, and returning the offset from
    560      _SDA2_BASE_ for that relocation.  */
    561   HOW (R_PPC_EMB_SDA2I16, 2, 16, 0xffff, 0, false, signed,
    562        ppc_elf_unhandled_reloc),
    563 
    564   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
    565      small data items.	 */
    566   HOW (R_PPC_EMB_SDA2REL, 2, 16, 0xffff, 0, false, signed,
    567        ppc_elf_unhandled_reloc),
    568 
    569   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
    570      signed offset from the appropriate base, and filling in the register
    571      field with the appropriate register (0, 2, or 13).  */
    572   HOW (R_PPC_EMB_SDA21, 4, 16, 0xffff, 0, false, signed,
    573        ppc_elf_unhandled_reloc),
    574 
    575   /* Relocation not handled: R_PPC_EMB_MRKREF */
    576   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
    577   /* Relocation not handled: R_PPC_EMB_RELST_LO */
    578   /* Relocation not handled: R_PPC_EMB_RELST_HI */
    579   /* Relocation not handled: R_PPC_EMB_RELST_HA */
    580   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
    581 
    582   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
    583      in the 16 bit signed offset from the appropriate base, and filling in the
    584      register field with the appropriate register (0, 2, or 13).  */
    585   HOW (R_PPC_EMB_RELSDA, 2, 16, 0xffff, 0, false, signed,
    586        ppc_elf_unhandled_reloc),
    587 
    588   /* A relative 8 bit branch.  */
    589   HOW (R_PPC_VLE_REL8, 2, 8, 0xff, 1, true, signed,
    590        bfd_elf_generic_reloc),
    591 
    592   /* A relative 15 bit branch.  */
    593   HOW (R_PPC_VLE_REL15, 4, 16, 0xfffe, 0, true, signed,
    594        bfd_elf_generic_reloc),
    595 
    596   /* A relative 24 bit branch.  */
    597   HOW (R_PPC_VLE_REL24, 4, 25, 0x1fffffe, 0, true, signed,
    598        bfd_elf_generic_reloc),
    599 
    600   /* The 16 LSBS in split16a format.  */
    601   HOW (R_PPC_VLE_LO16A, 4, 16, 0x1f07ff, 0, false, dont,
    602        ppc_elf_unhandled_reloc),
    603 
    604   /* The 16 LSBS in split16d format.  */
    605   HOW (R_PPC_VLE_LO16D, 4, 16, 0x3e007ff, 0, false, dont,
    606        ppc_elf_unhandled_reloc),
    607 
    608   /* Bits 16-31 split16a format.  */
    609   HOW (R_PPC_VLE_HI16A, 4, 16, 0x1f07ff, 16, false, dont,
    610        ppc_elf_unhandled_reloc),
    611 
    612   /* Bits 16-31 split16d format.  */
    613   HOW (R_PPC_VLE_HI16D, 4, 16, 0x3e007ff, 16, false, dont,
    614        ppc_elf_unhandled_reloc),
    615 
    616   /* Bits 16-31 (High Adjusted) in split16a format.  */
    617   HOW (R_PPC_VLE_HA16A, 4, 16, 0x1f07ff, 16, false, dont,
    618        ppc_elf_unhandled_reloc),
    619 
    620   /* Bits 16-31 (High Adjusted) in split16d format.  */
    621   HOW (R_PPC_VLE_HA16D, 4, 16, 0x3e007ff, 16, false, dont,
    622        ppc_elf_unhandled_reloc),
    623 
    624   /* This reloc is like R_PPC_EMB_SDA21 but only applies to e_add16i
    625      instructions.  If the register base is 0 then the linker changes
    626      the e_add16i to an e_li instruction.  */
    627   HOW (R_PPC_VLE_SDA21, 4, 16, 0xffff, 0, false, signed,
    628        ppc_elf_unhandled_reloc),
    629 
    630   /* Like R_PPC_VLE_SDA21 but ignore overflow.  */
    631   HOW (R_PPC_VLE_SDA21_LO, 4, 16, 0xffff, 0, false, dont,
    632        ppc_elf_unhandled_reloc),
    633 
    634   /* The 16 LSBS relative to _SDA_BASE_ in split16a format.  */
    635   HOW (R_PPC_VLE_SDAREL_LO16A, 4, 16, 0x1f07ff, 0, false, dont,
    636        ppc_elf_unhandled_reloc),
    637 
    638   /* The 16 LSBS relative to _SDA_BASE_ in split16d format.  */
    639   HOW (R_PPC_VLE_SDAREL_LO16D, 4, 16, 0x3e007ff, 0, false, dont,
    640        ppc_elf_unhandled_reloc),
    641 
    642   /* Bits 16-31 relative to _SDA_BASE_ in split16a format.  */
    643   HOW (R_PPC_VLE_SDAREL_HI16A, 4, 16, 0x1f07ff, 16, false, dont,
    644        ppc_elf_unhandled_reloc),
    645 
    646   /* Bits 16-31 relative to _SDA_BASE_ in split16d format.  */
    647   HOW (R_PPC_VLE_SDAREL_HI16D, 4, 16, 0x3e007ff, 16, false, dont,
    648        ppc_elf_unhandled_reloc),
    649 
    650   /* Bits 16-31 (HA) relative to _SDA_BASE split16a format.  */
    651   HOW (R_PPC_VLE_SDAREL_HA16A, 4, 16, 0x1f07ff, 16, false, dont,
    652        ppc_elf_unhandled_reloc),
    653 
    654   /* Bits 16-31 (HA) relative to _SDA_BASE split16d format.  */
    655   HOW (R_PPC_VLE_SDAREL_HA16D, 4, 16, 0x3e007ff, 16, false, dont,
    656        ppc_elf_unhandled_reloc),
    657 
    658   /* e_li split20 format.  */
    659   HOW (R_PPC_VLE_ADDR20, 4, 20, 0x1f7fff, 0, false, dont,
    660        ppc_elf_unhandled_reloc),
    661 
    662   HOW (R_PPC_IRELATIVE, 4, 32, 0xffffffff, 0, false, dont,
    663        ppc_elf_unhandled_reloc),
    664 
    665   /* A 16 bit relative relocation.  */
    666   HOW (R_PPC_REL16, 2, 16, 0xffff, 0, true, signed,
    667        bfd_elf_generic_reloc),
    668 
    669   /* A 16 bit relative relocation without overflow.  */
    670   HOW (R_PPC_REL16_LO, 2, 16, 0xffff, 0, true, dont,
    671        bfd_elf_generic_reloc),
    672 
    673   /* The high order 16 bits of a relative address.  */
    674   HOW (R_PPC_REL16_HI, 2, 16, 0xffff, 16, true, dont,
    675        bfd_elf_generic_reloc),
    676 
    677   /* The high order 16 bits of a relative address, plus 1 if the contents of
    678      the low 16 bits, treated as a signed number, is negative.  */
    679   HOW (R_PPC_REL16_HA, 2, 16, 0xffff, 16, true, dont,
    680        ppc_elf_addr16_ha_reloc),
    681 
    682   /* Like R_PPC_REL16_HA but for split field in addpcis.  */
    683   HOW (R_PPC_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
    684        ppc_elf_addr16_ha_reloc),
    685 
    686   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
    687   HOW (R_PPC_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
    688        ppc_elf_addr16_ha_reloc),
    689 
    690   /* GNU extension to record C++ vtable hierarchy.  */
    691   HOW (R_PPC_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
    692        NULL),
    693 
    694   /* GNU extension to record C++ vtable member usage.  */
    695   HOW (R_PPC_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
    696        NULL),
    697 
    698   /* Phony reloc to handle AIX style TOC entries.  */
    699   HOW (R_PPC_TOC16, 2, 16, 0xffff, 0, false, signed,
    700        ppc_elf_unhandled_reloc),
    701 };
    702 
    703 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
    705 
    706 static void
    707 ppc_elf_howto_init (void)
    708 {
    709   unsigned int i, type;
    710 
    711   for (i = 0; i < ARRAY_SIZE (ppc_elf_howto_raw); i++)
    712     {
    713       type = ppc_elf_howto_raw[i].type;
    714       if (type >= ARRAY_SIZE (ppc_elf_howto_table))
    715 	abort ();
    716       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
    717     }
    718 }
    719 
    720 static reloc_howto_type *
    721 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    722 			   bfd_reloc_code_real_type code)
    723 {
    724   enum elf_ppc_reloc_type r;
    725 
    726   /* Initialize howto table if not already done.  */
    727   if (!ppc_elf_howto_table[R_PPC_ADDR32])
    728     ppc_elf_howto_init ();
    729 
    730   switch (code)
    731     {
    732     default:
    733       return NULL;
    734 
    735     case BFD_RELOC_NONE:		r = R_PPC_NONE;			break;
    736     case BFD_RELOC_32:			r = R_PPC_ADDR32;		break;
    737     case BFD_RELOC_PPC_BA26:		r = R_PPC_ADDR24;		break;
    738     case BFD_RELOC_PPC64_ADDR16_DS:
    739     case BFD_RELOC_16:			r = R_PPC_ADDR16;		break;
    740     case BFD_RELOC_PPC64_ADDR16_LO_DS:
    741     case BFD_RELOC_LO16:		r = R_PPC_ADDR16_LO;		break;
    742     case BFD_RELOC_HI16:		r = R_PPC_ADDR16_HI;		break;
    743     case BFD_RELOC_HI16_S:		r = R_PPC_ADDR16_HA;		break;
    744     case BFD_RELOC_PPC_BA16:		r = R_PPC_ADDR14;		break;
    745     case BFD_RELOC_PPC_BA16_BRTAKEN:	r = R_PPC_ADDR14_BRTAKEN;	break;
    746     case BFD_RELOC_PPC_BA16_BRNTAKEN:	r = R_PPC_ADDR14_BRNTAKEN;	break;
    747     case BFD_RELOC_PPC_B26:		r = R_PPC_REL24;		break;
    748     case BFD_RELOC_PPC_B16:		r = R_PPC_REL14;		break;
    749     case BFD_RELOC_PPC_B16_BRTAKEN:	r = R_PPC_REL14_BRTAKEN;	break;
    750     case BFD_RELOC_PPC_B16_BRNTAKEN:	r = R_PPC_REL14_BRNTAKEN;	break;
    751     case BFD_RELOC_PPC64_GOT16_DS:
    752     case BFD_RELOC_16_GOTOFF:		r = R_PPC_GOT16;		break;
    753     case BFD_RELOC_PPC64_GOT16_LO_DS:
    754     case BFD_RELOC_LO16_GOTOFF:		r = R_PPC_GOT16_LO;		break;
    755     case BFD_RELOC_HI16_GOTOFF:		r = R_PPC_GOT16_HI;		break;
    756     case BFD_RELOC_HI16_S_GOTOFF:	r = R_PPC_GOT16_HA;		break;
    757     case BFD_RELOC_24_PLT_PCREL:	r = R_PPC_PLTREL24;		break;
    758     case BFD_RELOC_COPY:		r = R_PPC_COPY;			break;
    759     case BFD_RELOC_GLOB_DAT:		r = R_PPC_GLOB_DAT;		break;
    760     case BFD_RELOC_PPC_LOCAL24PC:	r = R_PPC_LOCAL24PC;		break;
    761     case BFD_RELOC_32_PCREL:		r = R_PPC_REL32;		break;
    762     case BFD_RELOC_32_PLTOFF:		r = R_PPC_PLT32;		break;
    763     case BFD_RELOC_32_PLT_PCREL:	r = R_PPC_PLTREL32;		break;
    764     case BFD_RELOC_PPC64_PLT16_LO_DS:
    765     case BFD_RELOC_LO16_PLTOFF:		r = R_PPC_PLT16_LO;		break;
    766     case BFD_RELOC_HI16_PLTOFF:		r = R_PPC_PLT16_HI;		break;
    767     case BFD_RELOC_HI16_S_PLTOFF:	r = R_PPC_PLT16_HA;		break;
    768     case BFD_RELOC_GPREL16:		r = R_PPC_SDAREL16;		break;
    769     case BFD_RELOC_PPC64_SECTOFF_DS:
    770     case BFD_RELOC_16_BASEREL:		r = R_PPC_SECTOFF;		break;
    771     case BFD_RELOC_PPC64_SECTOFF_LO_DS:
    772     case BFD_RELOC_LO16_BASEREL:	r = R_PPC_SECTOFF_LO;		break;
    773     case BFD_RELOC_HI16_BASEREL:	r = R_PPC_SECTOFF_HI;		break;
    774     case BFD_RELOC_HI16_S_BASEREL:	r = R_PPC_SECTOFF_HA;		break;
    775     case BFD_RELOC_CTOR:		r = R_PPC_ADDR32;		break;
    776     case BFD_RELOC_PPC64_TOC16_DS:
    777     case BFD_RELOC_PPC_TOC16:		r = R_PPC_TOC16;		break;
    778     case BFD_RELOC_PPC_TLS:		r = R_PPC_TLS;			break;
    779     case BFD_RELOC_PPC_TLSGD:		r = R_PPC_TLSGD;		break;
    780     case BFD_RELOC_PPC_TLSLD:		r = R_PPC_TLSLD;		break;
    781     case BFD_RELOC_PPC_DTPMOD:		r = R_PPC_DTPMOD32;		break;
    782     case BFD_RELOC_PPC64_TPREL16_DS:
    783     case BFD_RELOC_PPC_TPREL16:		r = R_PPC_TPREL16;		break;
    784     case BFD_RELOC_PPC64_TPREL16_LO_DS:
    785     case BFD_RELOC_PPC_TPREL16_LO:	r = R_PPC_TPREL16_LO;		break;
    786     case BFD_RELOC_PPC_TPREL16_HI:	r = R_PPC_TPREL16_HI;		break;
    787     case BFD_RELOC_PPC_TPREL16_HA:	r = R_PPC_TPREL16_HA;		break;
    788     case BFD_RELOC_PPC_TPREL:		r = R_PPC_TPREL32;		break;
    789     case BFD_RELOC_PPC64_DTPREL16_DS:
    790     case BFD_RELOC_PPC_DTPREL16:	r = R_PPC_DTPREL16;		break;
    791     case BFD_RELOC_PPC64_DTPREL16_LO_DS:
    792     case BFD_RELOC_PPC_DTPREL16_LO:	r = R_PPC_DTPREL16_LO;		break;
    793     case BFD_RELOC_PPC_DTPREL16_HI:	r = R_PPC_DTPREL16_HI;		break;
    794     case BFD_RELOC_PPC_DTPREL16_HA:	r = R_PPC_DTPREL16_HA;		break;
    795     case BFD_RELOC_PPC_DTPREL:		r = R_PPC_DTPREL32;		break;
    796     case BFD_RELOC_PPC_GOT_TLSGD16:	r = R_PPC_GOT_TLSGD16;		break;
    797     case BFD_RELOC_PPC_GOT_TLSGD16_LO:	r = R_PPC_GOT_TLSGD16_LO;	break;
    798     case BFD_RELOC_PPC_GOT_TLSGD16_HI:	r = R_PPC_GOT_TLSGD16_HI;	break;
    799     case BFD_RELOC_PPC_GOT_TLSGD16_HA:	r = R_PPC_GOT_TLSGD16_HA;	break;
    800     case BFD_RELOC_PPC_GOT_TLSLD16:	r = R_PPC_GOT_TLSLD16;		break;
    801     case BFD_RELOC_PPC_GOT_TLSLD16_LO:	r = R_PPC_GOT_TLSLD16_LO;	break;
    802     case BFD_RELOC_PPC_GOT_TLSLD16_HI:	r = R_PPC_GOT_TLSLD16_HI;	break;
    803     case BFD_RELOC_PPC_GOT_TLSLD16_HA:	r = R_PPC_GOT_TLSLD16_HA;	break;
    804     case BFD_RELOC_PPC_GOT_TPREL16:	r = R_PPC_GOT_TPREL16;		break;
    805     case BFD_RELOC_PPC_GOT_TPREL16_LO:	r = R_PPC_GOT_TPREL16_LO;	break;
    806     case BFD_RELOC_PPC_GOT_TPREL16_HI:	r = R_PPC_GOT_TPREL16_HI;	break;
    807     case BFD_RELOC_PPC_GOT_TPREL16_HA:	r = R_PPC_GOT_TPREL16_HA;	break;
    808     case BFD_RELOC_PPC_GOT_DTPREL16:	r = R_PPC_GOT_DTPREL16;		break;
    809     case BFD_RELOC_PPC_GOT_DTPREL16_LO:	r = R_PPC_GOT_DTPREL16_LO;	break;
    810     case BFD_RELOC_PPC_GOT_DTPREL16_HI:	r = R_PPC_GOT_DTPREL16_HI;	break;
    811     case BFD_RELOC_PPC_GOT_DTPREL16_HA:	r = R_PPC_GOT_DTPREL16_HA;	break;
    812     case BFD_RELOC_PPC_EMB_NADDR32:	r = R_PPC_EMB_NADDR32;		break;
    813     case BFD_RELOC_PPC_EMB_NADDR16:	r = R_PPC_EMB_NADDR16;		break;
    814     case BFD_RELOC_PPC_EMB_NADDR16_LO:	r = R_PPC_EMB_NADDR16_LO;	break;
    815     case BFD_RELOC_PPC_EMB_NADDR16_HI:	r = R_PPC_EMB_NADDR16_HI;	break;
    816     case BFD_RELOC_PPC_EMB_NADDR16_HA:	r = R_PPC_EMB_NADDR16_HA;	break;
    817     case BFD_RELOC_PPC_EMB_SDAI16:	r = R_PPC_EMB_SDAI16;		break;
    818     case BFD_RELOC_PPC_EMB_SDA2I16:	r = R_PPC_EMB_SDA2I16;		break;
    819     case BFD_RELOC_PPC_EMB_SDA2REL:	r = R_PPC_EMB_SDA2REL;		break;
    820     case BFD_RELOC_PPC_EMB_SDA21:	r = R_PPC_EMB_SDA21;		break;
    821     case BFD_RELOC_PPC_EMB_MRKREF:	r = R_PPC_EMB_MRKREF;		break;
    822     case BFD_RELOC_PPC_EMB_RELSEC16:	r = R_PPC_EMB_RELSEC16;		break;
    823     case BFD_RELOC_PPC_EMB_RELST_LO:	r = R_PPC_EMB_RELST_LO;		break;
    824     case BFD_RELOC_PPC_EMB_RELST_HI:	r = R_PPC_EMB_RELST_HI;		break;
    825     case BFD_RELOC_PPC_EMB_RELST_HA:	r = R_PPC_EMB_RELST_HA;		break;
    826     case BFD_RELOC_PPC_EMB_BIT_FLD:	r = R_PPC_EMB_BIT_FLD;		break;
    827     case BFD_RELOC_PPC_EMB_RELSDA:	r = R_PPC_EMB_RELSDA;		break;
    828     case BFD_RELOC_PPC_VLE_REL8:	r = R_PPC_VLE_REL8;		break;
    829     case BFD_RELOC_PPC_VLE_REL15:	r = R_PPC_VLE_REL15;		break;
    830     case BFD_RELOC_PPC_VLE_REL24:	r = R_PPC_VLE_REL24;		break;
    831     case BFD_RELOC_PPC_VLE_LO16A:	r = R_PPC_VLE_LO16A;		break;
    832     case BFD_RELOC_PPC_VLE_LO16D:	r = R_PPC_VLE_LO16D;		break;
    833     case BFD_RELOC_PPC_VLE_HI16A:	r = R_PPC_VLE_HI16A;		break;
    834     case BFD_RELOC_PPC_VLE_HI16D:	r = R_PPC_VLE_HI16D;		break;
    835     case BFD_RELOC_PPC_VLE_HA16A:	r = R_PPC_VLE_HA16A;		break;
    836     case BFD_RELOC_PPC_VLE_HA16D:	r = R_PPC_VLE_HA16D;		break;
    837     case BFD_RELOC_PPC_VLE_SDA21:	r = R_PPC_VLE_SDA21;		break;
    838     case BFD_RELOC_PPC_VLE_SDA21_LO:	r = R_PPC_VLE_SDA21_LO;		break;
    839     case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
    840       r = R_PPC_VLE_SDAREL_LO16A;
    841       break;
    842     case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
    843       r = R_PPC_VLE_SDAREL_LO16D;
    844       break;
    845     case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
    846       r = R_PPC_VLE_SDAREL_HI16A;
    847       break;
    848     case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
    849       r = R_PPC_VLE_SDAREL_HI16D;
    850       break;
    851     case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
    852       r = R_PPC_VLE_SDAREL_HA16A;
    853       break;
    854     case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
    855       r = R_PPC_VLE_SDAREL_HA16D;
    856       break;
    857     case BFD_RELOC_16_PCREL:		r = R_PPC_REL16;		break;
    858     case BFD_RELOC_LO16_PCREL:		r = R_PPC_REL16_LO;		break;
    859     case BFD_RELOC_HI16_PCREL:		r = R_PPC_REL16_HI;		break;
    860     case BFD_RELOC_HI16_S_PCREL:	r = R_PPC_REL16_HA;		break;
    861     case BFD_RELOC_PPC_16DX_HA:		r = R_PPC_16DX_HA;		break;
    862     case BFD_RELOC_PPC_REL16DX_HA:	r = R_PPC_REL16DX_HA;		break;
    863     case BFD_RELOC_VTABLE_INHERIT:	r = R_PPC_GNU_VTINHERIT;	break;
    864     case BFD_RELOC_VTABLE_ENTRY:	r = R_PPC_GNU_VTENTRY;		break;
    865     }
    866 
    867   return ppc_elf_howto_table[r];
    868 };
    869 
    870 static reloc_howto_type *
    871 ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    872 			   const char *r_name)
    873 {
    874   unsigned int i;
    875 
    876   for (i = 0; i < ARRAY_SIZE (ppc_elf_howto_raw); i++)
    877     if (ppc_elf_howto_raw[i].name != NULL
    878 	&& strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
    879       return &ppc_elf_howto_raw[i];
    880 
    881   return NULL;
    882 }
    883 
    884 /* Set the howto pointer for a PowerPC ELF reloc.  */
    885 
    886 static bool
    887 ppc_elf_info_to_howto (bfd *abfd,
    888 		       arelent *cache_ptr,
    889 		       Elf_Internal_Rela *dst)
    890 {
    891   unsigned int r_type;
    892 
    893   /* Initialize howto table if not already done.  */
    894   if (!ppc_elf_howto_table[R_PPC_ADDR32])
    895     ppc_elf_howto_init ();
    896 
    897   r_type = ELF32_R_TYPE (dst->r_info);
    898   if (r_type >= ARRAY_SIZE (ppc_elf_howto_table)
    899       || ppc_elf_howto_table[r_type] == NULL)
    900     {
    901       /* xgettext:c-format */
    902       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    903 			  abfd, r_type);
    904       bfd_set_error (bfd_error_bad_value);
    905       return false;
    906     }
    907 
    908   cache_ptr->howto = ppc_elf_howto_table[r_type];
    909   return true;
    910 }
    911 
    912 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs.  */
    913 
    914 static bfd_reloc_status_type
    915 ppc_elf_addr16_ha_reloc (bfd *abfd,
    916 			 arelent *reloc_entry,
    917 			 asymbol *symbol,
    918 			 void *data,
    919 			 asection *input_section,
    920 			 bfd *output_bfd,
    921 			 char **error_message ATTRIBUTE_UNUSED)
    922 {
    923   enum elf_ppc_reloc_type r_type;
    924   long insn;
    925   bfd_size_type octets;
    926   bfd_vma value;
    927 
    928   if (output_bfd != NULL)
    929     {
    930       reloc_entry->address += input_section->output_offset;
    931       return bfd_reloc_ok;
    932     }
    933 
    934   reloc_entry->addend += 0x8000;
    935   r_type = reloc_entry->howto->type;
    936   if (r_type != R_PPC_REL16DX_HA)
    937     return bfd_reloc_continue;
    938 
    939   value = 0;
    940   if (!bfd_is_com_section (symbol->section))
    941     value = symbol->value;
    942   value += (reloc_entry->addend
    943 	    + symbol->section->output_offset
    944 	    + symbol->section->output_section->vma);
    945   value -= (reloc_entry->address
    946 	    + input_section->output_offset
    947 	    + input_section->output_section->vma);
    948   value >>= 16;
    949 
    950   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
    951   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
    952 				  input_section, octets))
    953     return bfd_reloc_outofrange;
    954 
    955   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
    956   insn &= ~0x1fffc1;
    957   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
    958   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
    959   return bfd_reloc_ok;
    960 }
    961 
    962 static bfd_reloc_status_type
    963 ppc_elf_unhandled_reloc (bfd *abfd,
    964 			 arelent *reloc_entry,
    965 			 asymbol *symbol,
    966 			 void *data,
    967 			 asection *input_section,
    968 			 bfd *output_bfd,
    969 			 char **error_message)
    970 {
    971   /* If this is a relocatable link (output_bfd test tells us), just
    972      call the generic function.  Any adjustment will be done at final
    973      link time.  */
    974   if (output_bfd != NULL)
    975     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
    976 				  input_section, output_bfd, error_message);
    977 
    978   if (error_message != NULL)
    979     *error_message = bfd_asprintf (_("generic linker can't handle %s"),
    980 				     reloc_entry->howto->name);
    981   return bfd_reloc_dangerous;
    982 }
    983 
    984 /* Sections created by the linker.  */
    986 
    987 typedef struct elf_linker_section
    988 {
    989   /* Pointer to the bfd section.  */
    990   asection *section;
    991   /* Section name.  */
    992   const char *name;
    993   /* Associated bss section name.  */
    994   const char *bss_name;
    995   /* Associated symbol name.  */
    996   const char *sym_name;
    997   /* Associated symbol.  */
    998   struct elf_link_hash_entry *sym;
    999 } elf_linker_section_t;
   1000 
   1001 /* Linked list of allocated pointer entries.  This hangs off of the
   1002    symbol lists, and provides allows us to return different pointers,
   1003    based on different addend's.  */
   1004 
   1005 typedef struct elf_linker_section_pointers
   1006 {
   1007   /* next allocated pointer for this symbol */
   1008   struct elf_linker_section_pointers *next;
   1009   /* offset of pointer from beginning of section */
   1010   bfd_vma offset;
   1011   /* addend used */
   1012   bfd_vma addend;
   1013   /* which linker section this is */
   1014   elf_linker_section_t *lsect;
   1015 } elf_linker_section_pointers_t;
   1016 
   1017 struct ppc_elf_obj_tdata
   1018 {
   1019   struct elf_obj_tdata elf;
   1020 
   1021   /* A mapping from local symbols to offsets into the various linker
   1022      sections added.  This is index by the symbol index.  */
   1023   elf_linker_section_pointers_t **linker_section_pointers;
   1024 
   1025   /* Flags used to auto-detect plt type.  */
   1026   unsigned int makes_plt_call : 1;
   1027   unsigned int has_rel16 : 1;
   1028 };
   1029 
   1030 #define ppc_elf_tdata(bfd) \
   1031   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
   1032 
   1033 #define elf_local_ptr_offsets(bfd) \
   1034   (ppc_elf_tdata (bfd)->linker_section_pointers)
   1035 
   1036 #define is_ppc_elf(bfd) \
   1037   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   1038    && elf_object_id (bfd) == PPC32_ELF_DATA)
   1039 
   1040 /* Override the generic function because we store some extras.  */
   1041 
   1042 static bool
   1043 ppc_elf_mkobject (bfd *abfd)
   1044 {
   1045   return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata));
   1046 }
   1047 
   1048 /* When defaulting arch/mach, decode apuinfo to find a better match.  */
   1049 
   1050 bool
   1051 _bfd_elf_ppc_set_arch (bfd *abfd)
   1052 {
   1053   unsigned long mach = 0;
   1054   asection *s;
   1055   unsigned char *contents;
   1056 
   1057   if (abfd->arch_info->bits_per_word == 32
   1058       && bfd_big_endian (abfd))
   1059     {
   1060 
   1061       for (s = abfd->sections; s != NULL; s = s->next)
   1062 	if ((elf_section_data (s)->this_hdr.sh_flags & SHF_PPC_VLE) != 0)
   1063 	  break;
   1064       if (s != NULL)
   1065 	mach = bfd_mach_ppc_vle;
   1066     }
   1067 
   1068   if (mach == 0)
   1069     {
   1070       s = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
   1071       if (s != NULL
   1072 	  && s->size >= 24
   1073 	  && (s->flags & SEC_HAS_CONTENTS) != 0
   1074 	  && bfd_malloc_and_get_section (abfd, s, &contents))
   1075 	{
   1076 	  unsigned int apuinfo_size = bfd_get_32 (abfd, contents + 4);
   1077 	  unsigned int i;
   1078 
   1079 	  for (i = 20; i < apuinfo_size + 20 && i + 4 <= s->size; i += 4)
   1080 	    {
   1081 	      unsigned int val = bfd_get_32 (abfd, contents + i);
   1082 	      switch (val >> 16)
   1083 		{
   1084 		case PPC_APUINFO_PMR:
   1085 		case PPC_APUINFO_RFMCI:
   1086 		  if (mach == 0)
   1087 		    mach = bfd_mach_ppc_titan;
   1088 		  break;
   1089 
   1090 		case PPC_APUINFO_ISEL:
   1091 		case PPC_APUINFO_CACHELCK:
   1092 		  if (mach == bfd_mach_ppc_titan)
   1093 		    mach = bfd_mach_ppc_e500mc;
   1094 		  break;
   1095 
   1096 		case PPC_APUINFO_SPE:
   1097 		case PPC_APUINFO_EFS:
   1098 		case PPC_APUINFO_BRLOCK:
   1099 		  if (mach != bfd_mach_ppc_vle)
   1100 		    mach = bfd_mach_ppc_e500;
   1101 		  break;
   1102 
   1103 		case PPC_APUINFO_VLE:
   1104 		  mach = bfd_mach_ppc_vle;
   1105 		  break;
   1106 
   1107 		default:
   1108 		  mach = -1ul;
   1109 		}
   1110 	    }
   1111 	  free (contents);
   1112 	}
   1113     }
   1114 
   1115   if (mach != 0 && mach != -1ul)
   1116     {
   1117       const bfd_arch_info_type *arch;
   1118 
   1119       for (arch = abfd->arch_info->next; arch; arch = arch->next)
   1120 	if (arch->mach == mach)
   1121 	  {
   1122 	    abfd->arch_info = arch;
   1123 	    break;
   1124 	  }
   1125     }
   1126   return true;
   1127 }
   1128 
   1129 /* Fix bad default arch selected for a 32 bit input bfd when the
   1130    default is 64 bit.  Also select arch based on apuinfo.  */
   1131 
   1132 static bool
   1133 ppc_elf_object_p (bfd *abfd)
   1134 {
   1135   if (!abfd->arch_info->the_default)
   1136     return true;
   1137 
   1138   if (abfd->arch_info->bits_per_word == 64)
   1139     {
   1140       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   1141 
   1142       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
   1143 	{
   1144 	  /* Relies on arch after 64 bit default being 32 bit default.  */
   1145 	  abfd->arch_info = abfd->arch_info->next;
   1146 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
   1147 	}
   1148     }
   1149   return _bfd_elf_ppc_set_arch (abfd);
   1150 }
   1151 
   1152 /* Function to set whether a module needs the -mrelocatable bit set.  */
   1153 
   1154 static bool
   1155 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
   1156 {
   1157   BFD_ASSERT (!elf_flags_init (abfd)
   1158 	      || elf_elfheader (abfd)->e_flags == flags);
   1159 
   1160   elf_elfheader (abfd)->e_flags = flags;
   1161   elf_flags_init (abfd) = true;
   1162   return true;
   1163 }
   1164 
   1165 /* Support for core dump NOTE sections.  */
   1166 
   1167 static bool
   1168 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   1169 {
   1170   int offset;
   1171   unsigned int size;
   1172 
   1173   switch (note->descsz)
   1174     {
   1175     default:
   1176       return false;
   1177 
   1178     case 268:		/* Linux/PPC.  */
   1179       /* pr_cursig */
   1180       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   1181 
   1182       /* pr_pid */
   1183       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
   1184 
   1185       /* pr_reg */
   1186       offset = 72;
   1187       size = 192;
   1188 
   1189       break;
   1190     }
   1191 
   1192   /* Make a ".reg/999" section.  */
   1193   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   1194 					  size, note->descpos + offset);
   1195 }
   1196 
   1197 static bool
   1198 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   1199 {
   1200   switch (note->descsz)
   1201     {
   1202     default:
   1203       return false;
   1204 
   1205     case 128:		/* Linux/PPC elf_prpsinfo.  */
   1206       elf_tdata (abfd)->core->pid
   1207 	= bfd_get_32 (abfd, note->descdata + 16);
   1208       elf_tdata (abfd)->core->program
   1209 	= _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
   1210       elf_tdata (abfd)->core->command
   1211 	= _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
   1212     }
   1213 
   1214   /* Note that for some reason, a spurious space is tacked
   1215      onto the end of the args in some (at least one anyway)
   1216      implementations, so strip it off if it exists.  */
   1217 
   1218   {
   1219     char *command = elf_tdata (abfd)->core->command;
   1220     int n = strlen (command);
   1221 
   1222     if (0 < n && command[n - 1] == ' ')
   1223       command[n - 1] = '\0';
   1224   }
   1225 
   1226   return true;
   1227 }
   1228 
   1229 static char *
   1230 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
   1231 {
   1232   switch (note_type)
   1233     {
   1234     default:
   1235       return NULL;
   1236 
   1237     case NT_PRPSINFO:
   1238       {
   1239 	char data[128] ATTRIBUTE_NONSTRING;
   1240 	va_list ap;
   1241 
   1242 	va_start (ap, note_type);
   1243 	memset (data, 0, sizeof (data));
   1244 	strncpy (data + 32, va_arg (ap, const char *), 16);
   1245 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1246 	DIAGNOSTIC_PUSH;
   1247 	/* GCC 8.0 and 8.1 warn about 80 equals destination size with
   1248 	   -Wstringop-truncation:
   1249 	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
   1250 	 */
   1251 	DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
   1252 #endif
   1253 	strncpy (data + 48, va_arg (ap, const char *), 80);
   1254 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1255 	DIAGNOSTIC_POP;
   1256 #endif
   1257 	va_end (ap);
   1258 	return elfcore_write_note (abfd, buf, bufsiz,
   1259 				   "CORE", note_type, data, sizeof (data));
   1260       }
   1261 
   1262     case NT_PRSTATUS:
   1263       {
   1264 	char data[268];
   1265 	va_list ap;
   1266 	long pid;
   1267 	int cursig;
   1268 	const void *greg;
   1269 
   1270 	va_start (ap, note_type);
   1271 	memset (data, 0, 72);
   1272 	pid = va_arg (ap, long);
   1273 	bfd_put_32 (abfd, pid, data + 24);
   1274 	cursig = va_arg (ap, int);
   1275 	bfd_put_16 (abfd, cursig, data + 12);
   1276 	greg = va_arg (ap, const void *);
   1277 	memcpy (data + 72, greg, 192);
   1278 	memset (data + 264, 0, 4);
   1279 	va_end (ap);
   1280 	return elfcore_write_note (abfd, buf, bufsiz,
   1281 				   "CORE", note_type, data, sizeof (data));
   1282       }
   1283     }
   1284 }
   1285 
   1286 static flagword
   1287 ppc_elf_lookup_section_flags (char *flag_name)
   1288 {
   1289 
   1290   if (!strcmp (flag_name, "SHF_PPC_VLE"))
   1291     return SHF_PPC_VLE;
   1292 
   1293   return 0;
   1294 }
   1295 
   1296 /* Return address for Ith PLT stub in section PLT, for relocation REL
   1297    or (bfd_vma) -1 if it should not be included.  */
   1298 
   1299 static bfd_vma
   1300 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
   1301 		     const asection *plt ATTRIBUTE_UNUSED,
   1302 		     const arelent *rel)
   1303 {
   1304   return rel->address;
   1305 }
   1306 
   1307 /* Handle a PowerPC specific section when reading an object file.  This
   1308    is called when bfd_section_from_shdr finds a section with an unknown
   1309    type.  */
   1310 
   1311 static bool
   1312 ppc_elf_section_from_shdr (bfd *abfd,
   1313 			   Elf_Internal_Shdr *hdr,
   1314 			   const char *name,
   1315 			   int shindex)
   1316 {
   1317   asection *newsect;
   1318   flagword flags;
   1319 
   1320   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   1321     return false;
   1322 
   1323   newsect = hdr->bfd_section;
   1324   flags = 0;
   1325   if (hdr->sh_flags & SHF_EXCLUDE)
   1326     flags |= SEC_EXCLUDE;
   1327 
   1328   if (hdr->sh_type == SHT_ORDERED)
   1329     flags |= SEC_SORT_ENTRIES;
   1330 
   1331   if (startswith (name, ".PPC.EMB"))
   1332     name += 8;
   1333   if (startswith (name, ".sbss")
   1334       || startswith (name, ".sdata"))
   1335     flags |= SEC_SMALL_DATA;
   1336 
   1337   return (flags == 0
   1338 	  || bfd_set_section_flags (newsect, newsect->flags | flags));
   1339 }
   1340 
   1341 /* Set up any other section flags and such that may be necessary.  */
   1342 
   1343 static bool
   1344 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
   1345 		       Elf_Internal_Shdr *shdr,
   1346 		       asection *asect)
   1347 {
   1348   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
   1349     shdr->sh_type = SHT_ORDERED;
   1350 
   1351   return true;
   1352 }
   1353 
   1354 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
   1355    need to bump up the number of section headers.  */
   1356 
   1357 static int
   1358 ppc_elf_additional_program_headers (bfd *abfd,
   1359 				    struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1360 {
   1361   asection *s;
   1362   int ret = 0;
   1363 
   1364   s = bfd_get_section_by_name (abfd, ".sbss2");
   1365   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
   1366     ++ret;
   1367 
   1368   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
   1369   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
   1370     ++ret;
   1371 
   1372   return ret;
   1373 }
   1374 
   1375 /* Modify the segment map for VLE executables.  */
   1376 
   1377 bool
   1378 ppc_elf_modify_segment_map (bfd *abfd,
   1379 			    struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1380 {
   1381   struct elf_segment_map *m;
   1382 
   1383   /* At this point in the link, output sections have already been sorted by
   1384      LMA and assigned to segments.  All that is left to do is to ensure
   1385      there is no mixing of VLE & non-VLE sections in a text segment.
   1386      If we find that case, we split the segment.
   1387      We maintain the original output section order.  */
   1388 
   1389   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   1390     {
   1391       struct elf_segment_map *n;
   1392       size_t amt;
   1393       unsigned int j, k;
   1394       unsigned int p_flags;
   1395 
   1396       if (m->p_type != PT_LOAD || m->count == 0)
   1397 	continue;
   1398 
   1399       for (p_flags = PF_R, j = 0; j != m->count; ++j)
   1400 	{
   1401 	  if ((m->sections[j]->flags & SEC_READONLY) == 0)
   1402 	    p_flags |= PF_W;
   1403 	  if ((m->sections[j]->flags & SEC_CODE) != 0)
   1404 	    {
   1405 	      p_flags |= PF_X;
   1406 	      if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
   1407 		p_flags |= PF_PPC_VLE;
   1408 	      break;
   1409 	    }
   1410 	}
   1411       if (j != m->count)
   1412 	while (++j != m->count)
   1413 	  {
   1414 	    unsigned int p_flags1 = PF_R;
   1415 
   1416 	    if ((m->sections[j]->flags & SEC_READONLY) == 0)
   1417 	      p_flags1 |= PF_W;
   1418 	    if ((m->sections[j]->flags & SEC_CODE) != 0)
   1419 	      {
   1420 		p_flags1 |= PF_X;
   1421 		if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
   1422 		  p_flags1 |= PF_PPC_VLE;
   1423 		if (((p_flags1 ^ p_flags) & PF_PPC_VLE) != 0)
   1424 		  break;
   1425 	      }
   1426 	    p_flags |= p_flags1;
   1427 	  }
   1428       /* If we're splitting a segment which originally contained rw
   1429 	 sections then those sections might now only be in one of the
   1430 	 two parts.  So always set p_flags if splitting, even if we
   1431 	 are being called for objcopy with p_flags_valid set.  */
   1432       if (j != m->count || !m->p_flags_valid)
   1433 	{
   1434 	  m->p_flags_valid = 1;
   1435 	  m->p_flags = p_flags;
   1436 	}
   1437       if (j == m->count)
   1438 	continue;
   1439 
   1440       /* Sections 0..j-1 stay in this (current) segment,
   1441 	 the remainder are put in a new segment.
   1442 	 The scan resumes with the new segment.  */
   1443 
   1444       amt = sizeof (struct elf_segment_map);
   1445       amt += (m->count - j - 1) * sizeof (asection *);
   1446       n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   1447       if (n == NULL)
   1448 	return false;
   1449 
   1450       n->p_type = PT_LOAD;
   1451       n->count = m->count - j;
   1452       for (k = 0; k < n->count; ++k)
   1453 	n->sections[k] = m->sections[j + k];
   1454       m->count = j;
   1455       m->p_size_valid = 0;
   1456       n->next = m->next;
   1457       m->next = n;
   1458     }
   1459 
   1460   return true;
   1461 }
   1462 
   1463 /* Add extra PPC sections -- Note, for now, make .sbss2 and
   1464    .PPC.EMB.sbss0 a normal section, and not a bss section so
   1465    that the linker doesn't crater when trying to make more than
   1466    2 sections.  */
   1467 
   1468 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
   1469 {
   1470   { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
   1471   { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
   1472   { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
   1473   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1474   { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
   1475   { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
   1476   { STRING_COMMA_LEN (APUINFO_SECTION_NAME), 0, SHT_NOTE, 0 },
   1477   { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
   1478   { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
   1479   { NULL, 0, 0, 0, 0 }
   1480 };
   1481 
   1482 /* This is what we want for new plt/got.  */
   1483 static const struct bfd_elf_special_section ppc_alt_plt =
   1484   { STRING_COMMA_LEN (".plt"),		   0, SHT_PROGBITS, SHF_ALLOC };
   1485 
   1486 static const struct bfd_elf_special_section *
   1487 ppc_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   1488 {
   1489   const struct bfd_elf_special_section *ssect;
   1490 
   1491   /* See if this is one of the special sections.  */
   1492   if (sec->name == NULL)
   1493     return NULL;
   1494 
   1495   ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
   1496 					sec->use_rela_p);
   1497   if (ssect != NULL)
   1498     {
   1499       if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
   1500 	ssect = &ppc_alt_plt;
   1501       return ssect;
   1502     }
   1503 
   1504   return _bfd_elf_get_sec_type_attr (abfd, sec);
   1505 }
   1506 
   1507 /* Very simple linked list structure for recording apuinfo values.  */
   1509 typedef struct apuinfo_list
   1510 {
   1511   struct apuinfo_list *next;
   1512   unsigned long value;
   1513 }
   1514 apuinfo_list;
   1515 
   1516 static apuinfo_list *head;
   1517 static bool apuinfo_set;
   1518 
   1519 static void
   1520 apuinfo_list_init (void)
   1521 {
   1522   head = NULL;
   1523   apuinfo_set = false;
   1524 }
   1525 
   1526 static void
   1527 apuinfo_list_add (unsigned long value)
   1528 {
   1529   apuinfo_list *entry = head;
   1530 
   1531   while (entry != NULL)
   1532     {
   1533       if (entry->value == value)
   1534 	return;
   1535       entry = entry->next;
   1536     }
   1537 
   1538   entry = bfd_malloc (sizeof (* entry));
   1539   if (entry == NULL)
   1540     return;
   1541 
   1542   entry->value = value;
   1543   entry->next  = head;
   1544   head = entry;
   1545 }
   1546 
   1547 static unsigned
   1548 apuinfo_list_length (void)
   1549 {
   1550   apuinfo_list *entry;
   1551   unsigned long count;
   1552 
   1553   for (entry = head, count = 0;
   1554        entry;
   1555        entry = entry->next)
   1556     ++ count;
   1557 
   1558   return count;
   1559 }
   1560 
   1561 static inline unsigned long
   1562 apuinfo_list_element (unsigned long number)
   1563 {
   1564   apuinfo_list * entry;
   1565 
   1566   for (entry = head;
   1567        entry && number --;
   1568        entry = entry->next)
   1569     ;
   1570 
   1571   return entry ? entry->value : 0;
   1572 }
   1573 
   1574 static void
   1575 apuinfo_list_finish (void)
   1576 {
   1577   apuinfo_list *entry;
   1578 
   1579   for (entry = head; entry;)
   1580     {
   1581       apuinfo_list *next = entry->next;
   1582       free (entry);
   1583       entry = next;
   1584     }
   1585 
   1586   head = NULL;
   1587 }
   1588 
   1589 /* Scan the input BFDs and create a linked list of
   1590    the APUinfo values that will need to be emitted.  */
   1591 
   1592 static void
   1593 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
   1594 {
   1595   bfd *ibfd;
   1596   asection *asec;
   1597   char *buffer = NULL;
   1598   bfd_size_type largest_input_size = 0;
   1599   unsigned i;
   1600   unsigned long length;
   1601   const char *error_message = NULL;
   1602 
   1603   if (link_info == NULL)
   1604     return;
   1605 
   1606   apuinfo_list_init ();
   1607 
   1608   /* Read in the input sections contents.  */
   1609   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link.next)
   1610     {
   1611       unsigned long datum;
   1612 
   1613       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
   1614       if (asec == NULL)
   1615 	continue;
   1616 
   1617       /* xgettext:c-format */
   1618       error_message = _("corrupt %s section in %pB");
   1619       length = asec->size;
   1620       if (length < 20)
   1621 	goto fail;
   1622 
   1623       apuinfo_set = true;
   1624       if (largest_input_size < asec->size)
   1625 	{
   1626 	  free (buffer);
   1627 	  largest_input_size = asec->size;
   1628 	  buffer = bfd_malloc (largest_input_size);
   1629 	  if (!buffer)
   1630 	    return;
   1631 	}
   1632 
   1633       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
   1634 	  || (bfd_read (buffer, length, ibfd) != length))
   1635 	{
   1636 	  /* xgettext:c-format */
   1637 	  error_message = _("unable to read in %s section from %pB");
   1638 	  goto fail;
   1639 	}
   1640 
   1641       /* Verify the contents of the header.  Note - we have to
   1642 	 extract the values this way in order to allow for a
   1643 	 host whose endian-ness is different from the target.  */
   1644       datum = bfd_get_32 (ibfd, buffer);
   1645       if (datum != sizeof APUINFO_LABEL)
   1646 	goto fail;
   1647 
   1648       datum = bfd_get_32 (ibfd, buffer + 8);
   1649       if (datum != 0x2)
   1650 	goto fail;
   1651 
   1652       if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
   1653 	goto fail;
   1654 
   1655       /* Get the number of bytes used for apuinfo entries.  */
   1656       datum = bfd_get_32 (ibfd, buffer + 4);
   1657       if (datum + 20 != length)
   1658 	goto fail;
   1659 
   1660       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
   1661       for (i = 0; i < datum; i += 4)
   1662 	apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
   1663     }
   1664 
   1665   error_message = NULL;
   1666 
   1667   if (apuinfo_set)
   1668     {
   1669       /* Compute the size of the output section.  */
   1670       unsigned num_entries = apuinfo_list_length ();
   1671 
   1672       /* Set the output section size, if it exists.  */
   1673       asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
   1674 
   1675       if (asec && !bfd_set_section_size (asec, 20 + num_entries * 4))
   1676 	{
   1677 	  ibfd = abfd;
   1678 	  /* xgettext:c-format */
   1679 	  error_message = _("warning: unable to set size of %s section in %pB");
   1680 	}
   1681     }
   1682 
   1683  fail:
   1684   free (buffer);
   1685 
   1686   if (error_message)
   1687     _bfd_error_handler (error_message, APUINFO_SECTION_NAME, ibfd);
   1688 }
   1689 
   1690 /* Prevent the output section from accumulating the input sections'
   1691    contents.  We have already stored this in our linked list structure.  */
   1692 
   1693 static bool
   1694 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
   1695 		       struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   1696 		       asection *asec,
   1697 		       bfd_byte *contents ATTRIBUTE_UNUSED)
   1698 {
   1699   return apuinfo_set && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
   1700 }
   1701 
   1702 /* Finally we can generate the output section.  */
   1703 
   1704 static void
   1705 ppc_final_write_processing (bfd *abfd)
   1706 {
   1707   bfd_byte *buffer;
   1708   asection *asec;
   1709   unsigned i;
   1710   unsigned num_entries;
   1711   bfd_size_type length;
   1712 
   1713   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
   1714   if (asec == NULL)
   1715     return;
   1716 
   1717   if (!apuinfo_set)
   1718     return;
   1719 
   1720   length = asec->size;
   1721   if (length < 20)
   1722     return;
   1723 
   1724   buffer = bfd_malloc (length);
   1725   if (buffer == NULL)
   1726     {
   1727       _bfd_error_handler
   1728 	(_("failed to allocate space for new APUinfo section"));
   1729       return;
   1730     }
   1731 
   1732   /* Create the apuinfo header.  */
   1733   num_entries = apuinfo_list_length ();
   1734   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
   1735   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
   1736   bfd_put_32 (abfd, 0x2, buffer + 8);
   1737   strcpy ((char *) buffer + 12, APUINFO_LABEL);
   1738 
   1739   length = 20;
   1740   for (i = 0; i < num_entries; i++)
   1741     {
   1742       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
   1743       length += 4;
   1744     }
   1745 
   1746   if (length != asec->size)
   1747     _bfd_error_handler (_("failed to compute new APUinfo section"));
   1748 
   1749   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
   1750     _bfd_error_handler (_("failed to install new APUinfo section"));
   1751 
   1752   free (buffer);
   1753 
   1754   apuinfo_list_finish ();
   1755 }
   1756 
   1757 static bool
   1758 ppc_elf_final_write_processing (bfd *abfd)
   1759 {
   1760   ppc_final_write_processing (abfd);
   1761   return _bfd_elf_final_write_processing (abfd);
   1762 }
   1763 
   1764 static bool
   1766 is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
   1767 {
   1768   bfd_byte buf[4 * 4];
   1769 
   1770   if (!bfd_get_section_contents (abfd, glink, buf, off, sizeof buf))
   1771     return false;
   1772 
   1773   return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
   1774 	  && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
   1775 	  && bfd_get_32 (abfd, buf + 8) == MTCTR_11
   1776 	  && bfd_get_32 (abfd, buf + 12) == BCTR);
   1777 }
   1778 
   1779 static bool
   1780 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   1781 {
   1782   bfd_vma vma = *(bfd_vma *) ptr;
   1783   return ((section->flags & SEC_ALLOC) != 0
   1784 	  && section->vma <= vma
   1785 	  && vma < section->vma + section->size);
   1786 }
   1787 
   1788 static long
   1789 ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
   1790 			      long dynsymcount, asymbol **dynsyms,
   1791 			      asymbol **ret)
   1792 {
   1793   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   1794   asection *plt, *relplt, *dynamic, *glink;
   1795   bfd_vma glink_vma = 0;
   1796   bfd_vma resolv_vma = 0;
   1797   bfd_vma stub_off;
   1798   asymbol *s;
   1799   arelent *p;
   1800   size_t count, i, stub_delta;
   1801   size_t size;
   1802   char *names;
   1803   bfd_byte buf[4];
   1804 
   1805   *ret = NULL;
   1806 
   1807   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   1808     return 0;
   1809 
   1810   if (dynsymcount <= 0)
   1811     return 0;
   1812 
   1813   relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   1814   if (relplt == NULL)
   1815     return 0;
   1816 
   1817   plt = bfd_get_section_by_name (abfd, ".plt");
   1818   if (plt == NULL)
   1819     return 0;
   1820 
   1821   /* Call common code to handle old-style executable PLTs.  */
   1822   if (elf_section_flags (plt) & SHF_EXECINSTR)
   1823     return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
   1824 					  dynsymcount, dynsyms, ret);
   1825 
   1826   /* If this object was prelinked, the prelinker stored the address
   1827      of .glink at got[1].  If it wasn't prelinked, got[1] will be zero.  */
   1828   dynamic = bfd_get_section_by_name (abfd, ".dynamic");
   1829   if (dynamic != NULL
   1830       && (dynamic->flags & SEC_HAS_CONTENTS) != 0)
   1831     {
   1832       bfd_byte *dynbuf, *extdyn, *extdynend;
   1833       size_t extdynsize;
   1834       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1835 
   1836       if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   1837 	return -1;
   1838 
   1839       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1840       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1841 
   1842       for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
   1843 	   (size_t) (extdynend - extdyn) >= extdynsize;
   1844 	   extdyn += extdynsize)
   1845 	{
   1846 	  Elf_Internal_Dyn dyn;
   1847 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1848 
   1849 	  if (dyn.d_tag == DT_NULL)
   1850 	    break;
   1851 
   1852 	  if (dyn.d_tag == DT_PPC_GOT)
   1853 	    {
   1854 	      unsigned int g_o_t = dyn.d_un.d_val;
   1855 	      asection *got = bfd_get_section_by_name (abfd, ".got");
   1856 	      if (got != NULL
   1857 		  && bfd_get_section_contents (abfd, got, buf,
   1858 					       g_o_t - got->vma + 4, 4))
   1859 		glink_vma = bfd_get_32 (abfd, buf);
   1860 	      break;
   1861 	    }
   1862 	}
   1863       free (dynbuf);
   1864     }
   1865 
   1866   /* Otherwise we read the first plt entry.  */
   1867   if (glink_vma == 0)
   1868     {
   1869       if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
   1870 	glink_vma = bfd_get_32 (abfd, buf);
   1871     }
   1872 
   1873   if (glink_vma == 0)
   1874     return 0;
   1875 
   1876   /* The .glink section usually does not survive the final
   1877      link; search for the section (usually .text) where the
   1878      glink stubs now reside.  */
   1879   glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
   1880   if (glink == NULL)
   1881     return 0;
   1882 
   1883   /* Determine glink PLT resolver by reading the relative branch
   1884      from the first glink stub.  */
   1885   if (bfd_get_section_contents (abfd, glink, buf,
   1886 				glink_vma - glink->vma, 4))
   1887     {
   1888       unsigned int insn = bfd_get_32 (abfd, buf);
   1889 
   1890       /* The first glink stub may either branch to the resolver ...  */
   1891       insn ^= B;
   1892       if ((insn & ~0x3fffffc) == 0)
   1893 	resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
   1894 
   1895       /* ... or fall through a bunch of NOPs.  */
   1896       else if ((insn ^ B ^ NOP) == 0)
   1897 	for (i = 4;
   1898 	     bfd_get_section_contents (abfd, glink, buf,
   1899 				       glink_vma - glink->vma + i, 4);
   1900 	     i += 4)
   1901 	  if (bfd_get_32 (abfd, buf) != NOP)
   1902 	    {
   1903 	      resolv_vma = glink_vma + i;
   1904 	      break;
   1905 	    }
   1906     }
   1907 
   1908   count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
   1909   /* If the stubs are those for -shared/-pie then we might have
   1910      multiple stubs for each plt entry.  If that is the case then
   1911      there is no way to associate stubs with their plt entries short
   1912      of figuring out the GOT pointer value used in the stub.
   1913      The offsets tested here need to cover all possible values of
   1914      GLINK_ENTRY_SIZE for other than __tls_get_addr_opt.  */
   1915   stub_off = glink_vma - glink->vma;
   1916   for (stub_delta = 16; stub_delta <= 32; stub_delta += 8)
   1917     if (is_nonpic_glink_stub (abfd, glink, stub_off - stub_delta))
   1918       break;
   1919   if (stub_delta > 32)
   1920     return 0;
   1921 
   1922   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   1923   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   1924     return -1;
   1925 
   1926   size = count * sizeof (asymbol);
   1927   p = relplt->relocation;
   1928   for (i = 0; i < count; i++, p++)
   1929     {
   1930       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   1931       if (p->addend != 0)
   1932 	size += sizeof ("+0x") - 1 + 8;
   1933     }
   1934 
   1935   size += sizeof (asymbol) + sizeof ("__glink");
   1936 
   1937   if (resolv_vma)
   1938     size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   1939 
   1940   s = *ret = bfd_malloc (size);
   1941   if (s == NULL)
   1942     return -1;
   1943 
   1944   stub_off = glink_vma - glink->vma;
   1945   names = (char *) (s + count + 1 + (resolv_vma != 0));
   1946   p = relplt->relocation + count - 1;
   1947   for (i = 0; i < count; i++)
   1948     {
   1949       size_t len;
   1950 
   1951       stub_off -= stub_delta;
   1952       if (strcmp ((*p->sym_ptr_ptr)->name, "__tls_get_addr_opt") == 0)
   1953 	stub_off -= 32;
   1954       *s = **p->sym_ptr_ptr;
   1955       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   1956 	 we are defining a symbol, ensure one of them is set.  */
   1957       if ((s->flags & BSF_LOCAL) == 0)
   1958 	s->flags |= BSF_GLOBAL;
   1959       s->flags |= BSF_SYNTHETIC;
   1960       s->section = glink;
   1961       s->value = stub_off;
   1962       s->name = names;
   1963       s->udata.p = NULL;
   1964       len = strlen ((*p->sym_ptr_ptr)->name);
   1965       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   1966       names += len;
   1967       if (p->addend != 0)
   1968 	{
   1969 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   1970 	  names += sizeof ("+0x") - 1;
   1971 	  bfd_sprintf_vma (abfd, names, p->addend);
   1972 	  names += strlen (names);
   1973 	}
   1974       memcpy (names, "@plt", sizeof ("@plt"));
   1975       names += sizeof ("@plt");
   1976       ++s;
   1977       --p;
   1978     }
   1979 
   1980   /* Add a symbol at the start of the glink branch table.  */
   1981   memset (s, 0, sizeof *s);
   1982   s->the_bfd = abfd;
   1983   s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   1984   s->section = glink;
   1985   s->value = glink_vma - glink->vma;
   1986   s->name = names;
   1987   memcpy (names, "__glink", sizeof ("__glink"));
   1988   names += sizeof ("__glink");
   1989   s++;
   1990   count++;
   1991 
   1992   if (resolv_vma)
   1993     {
   1994       /* Add a symbol for the glink PLT resolver.  */
   1995       memset (s, 0, sizeof *s);
   1996       s->the_bfd = abfd;
   1997       s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   1998       s->section = glink;
   1999       s->value = resolv_vma - glink->vma;
   2000       s->name = names;
   2001       memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   2002       names += sizeof ("__glink_PLTresolve");
   2003       s++;
   2004       count++;
   2005     }
   2006 
   2007   return count;
   2008 }
   2009 
   2010 /* The following functions are specific to the ELF linker, while
   2012    functions above are used generally.  They appear in this file more
   2013    or less in the order in which they are called.  eg.
   2014    ppc_elf_check_relocs is called early in the link process,
   2015    ppc_elf_finish_dynamic_sections is one of the last functions
   2016    called.  */
   2017 
   2018 /* Track PLT entries needed for a given symbol.  We might need more
   2019    than one glink entry per symbol when generating a pic binary.  */
   2020 struct plt_entry
   2021 {
   2022   struct plt_entry *next;
   2023 
   2024   /* -fPIC uses multiple GOT sections, one per file, called ".got2".
   2025      This field stores the offset into .got2 used to initialise the
   2026      GOT pointer reg.  It will always be at least 32768.  (Current
   2027      gcc always uses an offset of 32768, but ld -r will pack .got2
   2028      sections together resulting in larger offsets).  */
   2029   bfd_vma addend;
   2030 
   2031   /* The .got2 section.  */
   2032   asection *sec;
   2033 
   2034   /* PLT refcount or offset.  */
   2035   union
   2036     {
   2037       bfd_signed_vma refcount;
   2038       bfd_vma offset;
   2039     } plt;
   2040 
   2041   /* .glink stub offset.  */
   2042   bfd_vma glink_offset;
   2043 };
   2044 
   2045 /* Of those relocs that might be copied as dynamic relocs, this
   2046    function selects those that must be copied when linking a shared
   2047    library or PIE, even when the symbol is local.  */
   2048 
   2049 static int
   2050 must_be_dyn_reloc (struct bfd_link_info *info,
   2051 		   enum elf_ppc_reloc_type r_type)
   2052 {
   2053   switch (r_type)
   2054     {
   2055     default:
   2056       /* Only relative relocs can be resolved when the object load
   2057 	 address isn't fixed.  DTPREL32 is excluded because the
   2058 	 dynamic linker needs to differentiate global dynamic from
   2059 	 local dynamic __tls_index pairs when PPC_OPT_TLS is set.  */
   2060       return 1;
   2061 
   2062     case R_PPC_REL24:
   2063     case R_PPC_REL14:
   2064     case R_PPC_REL14_BRTAKEN:
   2065     case R_PPC_REL14_BRNTAKEN:
   2066     case R_PPC_REL32:
   2067       return 0;
   2068 
   2069     case R_PPC_TPREL32:
   2070     case R_PPC_TPREL16:
   2071     case R_PPC_TPREL16_LO:
   2072     case R_PPC_TPREL16_HI:
   2073     case R_PPC_TPREL16_HA:
   2074       /* These relocations are relative but in a shared library the
   2075 	 linker doesn't know the thread pointer base.  */
   2076       return bfd_link_dll (info);
   2077     }
   2078 }
   2079 
   2080 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   2081    copying dynamic variables from a shared lib into an app's dynbss
   2082    section, and instead use a dynamic relocation to point into the
   2083    shared lib.  */
   2084 #define ELIMINATE_COPY_RELOCS 1
   2085 
   2086 /* Used to track dynamic relocations for local symbols.  */
   2087 struct ppc_dyn_relocs
   2088 {
   2089   struct ppc_dyn_relocs *next;
   2090 
   2091   /* The input section of the reloc.  */
   2092   asection *sec;
   2093 
   2094   /* Total number of relocs copied for the input section.  */
   2095   unsigned int count : 31;
   2096 
   2097   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
   2098   unsigned int ifunc : 1;
   2099 };
   2100 
   2101 /* PPC ELF linker hash entry.  */
   2102 
   2103 struct ppc_elf_link_hash_entry
   2104 {
   2105   struct elf_link_hash_entry elf;
   2106 
   2107   /* If this symbol is used in the linker created sections, the processor
   2108      specific backend uses this field to map the field into the offset
   2109      from the beginning of the section.  */
   2110   elf_linker_section_pointers_t *linker_section_pointer;
   2111 
   2112   /* Contexts in which symbol is used in the GOT.
   2113      Bits are or'd into the mask as the corresponding relocs are
   2114      encountered during check_relocs, with TLS_TLS being set when any
   2115      of the other TLS bits are set.  tls_optimize clears bits when
   2116      optimizing to indicate the corresponding GOT entry type is not
   2117      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
   2118      set TLS_GDIE when a GD reloc turns into an IE one.
   2119      These flags are also kept for local symbols.  */
   2120 #define TLS_TLS		 1	/* Any TLS reloc.  */
   2121 #define TLS_GD		 2	/* GD reloc. */
   2122 #define TLS_LD		 4	/* LD reloc. */
   2123 #define TLS_TPREL	 8	/* TPREL reloc, => IE. */
   2124 #define TLS_DTPREL	16	/* DTPREL reloc, => LD. */
   2125 #define TLS_MARK	32	/* __tls_get_addr call marked. */
   2126 #define TLS_GDIE	64	/* GOT TPREL reloc resulting from GD->IE. */
   2127   unsigned char tls_mask;
   2128 
   2129   /* The above field is also used to mark function symbols.  In which
   2130      case TLS_TLS will be 0.  */
   2131 #define PLT_IFUNC	 2	/* STT_GNU_IFUNC.  */
   2132 #define PLT_KEEP	 4	/* inline plt call requires plt entry.  */
   2133 #define NON_GOT        256	/* local symbol plt, not stored.  */
   2134 
   2135   /* Nonzero if we have seen a small data relocation referring to this
   2136      symbol.  */
   2137   unsigned char has_sda_refs : 1;
   2138 
   2139   /* Flag use of given relocations.  */
   2140   unsigned char has_addr16_ha : 1;
   2141   unsigned char has_addr16_lo : 1;
   2142 };
   2143 
   2144 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
   2145 
   2146 /* PPC ELF linker hash table.  */
   2147 
   2148 struct ppc_elf_link_hash_table
   2149 {
   2150   struct elf_link_hash_table elf;
   2151 
   2152   /* Various options passed from the linker.  */
   2153   struct ppc_elf_params *params;
   2154 
   2155   /* Short-cuts to get to dynamic linker sections.  */
   2156   asection *glink;
   2157   asection *dynsbss;
   2158   asection *relsbss;
   2159   elf_linker_section_t sdata[2];
   2160   asection *sbss;
   2161   asection *glink_eh_frame;
   2162   asection *pltlocal;
   2163   asection *relpltlocal;
   2164 
   2165   /* The (unloaded but important) .rela.plt.unloaded on VxWorks.  */
   2166   asection *srelplt2;
   2167 
   2168   /* Shortcut to __tls_get_addr.  */
   2169   struct elf_link_hash_entry *tls_get_addr;
   2170 
   2171   /* The bfd that forced an old-style PLT.  */
   2172   bfd *old_bfd;
   2173 
   2174   /* TLS local dynamic got entry handling.  */
   2175   union {
   2176     bfd_signed_vma refcount;
   2177     bfd_vma offset;
   2178   } tlsld_got;
   2179 
   2180   /* Offset of branch table to PltResolve function in glink.  */
   2181   bfd_vma glink_pltresolve;
   2182 
   2183   /* Size of reserved GOT entries.  */
   2184   unsigned int got_header_size;
   2185   /* Non-zero if allocating the header left a gap.  */
   2186   unsigned int got_gap;
   2187 
   2188   /* The type of PLT we have chosen to use.  */
   2189   enum ppc_elf_plt_type plt_type;
   2190 
   2191   /* Whether there exist local gnu indirect function resolvers,
   2192      referenced by dynamic relocations.  */
   2193   unsigned int local_ifunc_resolver:1;
   2194   unsigned int maybe_local_ifunc_resolver:1;
   2195 
   2196   /* Set if tls optimization is enabled.  */
   2197   unsigned int do_tls_opt:1;
   2198 
   2199   /* Set if inline plt calls should be converted to direct calls.  */
   2200   unsigned int can_convert_all_inline_plt:1;
   2201 
   2202   /* The size of PLT entries.  */
   2203   int plt_entry_size;
   2204   /* The distance between adjacent PLT slots.  */
   2205   int plt_slot_size;
   2206   /* The size of the first PLT entry.  */
   2207   int plt_initial_entry_size;
   2208 };
   2209 
   2210 /* Rename some of the generic section flags to better document how they
   2211    are used for ppc32.  The flags are only valid for ppc32 elf objects.  */
   2212 
   2213 /* Nonzero if this section has TLS related relocations.  */
   2214 #define has_tls_reloc sec_flg0
   2215 
   2216 /* Nonzero if this section has a call to __tls_get_addr lacking marker
   2217    relocs.  */
   2218 #define nomark_tls_get_addr sec_flg1
   2219 
   2220   /* Flag set when PLTCALL relocs are detected.  */
   2221 #define has_pltcall sec_flg2
   2222 
   2223 /* Get the PPC ELF linker hash table from a link_info structure.  */
   2224 
   2225 #define ppc_elf_hash_table(p) \
   2226   ((is_elf_hash_table ((p)->hash)					\
   2227     && elf_hash_table_id (elf_hash_table (p)) == PPC32_ELF_DATA)	\
   2228    ? (struct ppc_elf_link_hash_table *) (p)->hash : NULL)
   2229 
   2230 /* Create an entry in a PPC ELF linker hash table.  */
   2231 
   2232 static struct bfd_hash_entry *
   2233 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   2234 			   struct bfd_hash_table *table,
   2235 			   const char *string)
   2236 {
   2237   /* Allocate the structure if it has not already been allocated by a
   2238      subclass.  */
   2239   if (entry == NULL)
   2240     {
   2241       entry = bfd_hash_allocate (table,
   2242 				 sizeof (struct ppc_elf_link_hash_entry));
   2243       if (entry == NULL)
   2244 	return entry;
   2245     }
   2246 
   2247   /* Call the allocation method of the superclass.  */
   2248   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   2249   if (entry != NULL)
   2250     {
   2251       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
   2252       ppc_elf_hash_entry (entry)->tls_mask = 0;
   2253       ppc_elf_hash_entry (entry)->has_sda_refs = 0;
   2254     }
   2255 
   2256   return entry;
   2257 }
   2258 
   2259 /* Create a PPC ELF linker hash table.  */
   2260 
   2261 static struct bfd_link_hash_table *
   2262 ppc_elf_link_hash_table_create (bfd *abfd)
   2263 {
   2264   struct ppc_elf_link_hash_table *ret;
   2265   static struct ppc_elf_params default_params
   2266     = { PLT_OLD, 0, 0, 1, 0, 0, 12, 0, 0, 0 };
   2267 
   2268   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
   2269   if (ret == NULL)
   2270     return NULL;
   2271 
   2272   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
   2273 				      ppc_elf_link_hash_newfunc,
   2274 				      sizeof (struct ppc_elf_link_hash_entry)))
   2275     {
   2276       free (ret);
   2277       return NULL;
   2278     }
   2279 
   2280   ret->elf.init_plt_refcount.refcount = 0;
   2281   ret->elf.init_plt_refcount.glist = NULL;
   2282   ret->elf.init_plt_offset.offset = 0;
   2283   ret->elf.init_plt_offset.glist = NULL;
   2284 
   2285   ret->params = &default_params;
   2286 
   2287   ret->sdata[0].name = ".sdata";
   2288   ret->sdata[0].sym_name = "_SDA_BASE_";
   2289   ret->sdata[0].bss_name = ".sbss";
   2290 
   2291   ret->sdata[1].name = ".sdata2";
   2292   ret->sdata[1].sym_name = "_SDA2_BASE_";
   2293   ret->sdata[1].bss_name = ".sbss2";
   2294 
   2295   ret->plt_entry_size = 12;
   2296   ret->plt_slot_size = 8;
   2297   ret->plt_initial_entry_size = 72;
   2298 
   2299   return &ret->elf.root;
   2300 }
   2301 
   2302 /* Hook linker params into hash table.  */
   2303 
   2304 void
   2305 ppc_elf_link_params (struct bfd_link_info *info, struct ppc_elf_params *params)
   2306 {
   2307   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   2308 
   2309   if (htab)
   2310     htab->params = params;
   2311   params->pagesize_p2 = bfd_log2 (params->pagesize);
   2312 }
   2313 
   2314 /* Create .got and the related sections.  */
   2315 
   2316 static bool
   2317 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
   2318 {
   2319   struct ppc_elf_link_hash_table *htab;
   2320 
   2321   if (!_bfd_elf_create_got_section (abfd, info))
   2322     return false;
   2323 
   2324   htab = ppc_elf_hash_table (info);
   2325   if (htab->elf.target_os != is_vxworks)
   2326     {
   2327       /* The powerpc .got has a blrl instruction in it.  Mark it
   2328 	 executable.  */
   2329       flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
   2330 			| SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2331       if (!bfd_set_section_flags (htab->elf.sgot, flags))
   2332 	return false;
   2333     }
   2334 
   2335   return true;
   2336 }
   2337 
   2338 /* Create a special linker section, used for R_PPC_EMB_SDAI16 and
   2339    R_PPC_EMB_SDA2I16 pointers.  These sections become part of .sdata
   2340    and .sdata2.  Create _SDA_BASE_ and _SDA2_BASE too.  */
   2341 
   2342 static bool
   2343 ppc_elf_create_linker_section (bfd *abfd,
   2344 			       struct bfd_link_info *info,
   2345 			       flagword flags,
   2346 			       elf_linker_section_t *lsect)
   2347 {
   2348   asection *s;
   2349 
   2350   flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   2351 	    | SEC_LINKER_CREATED);
   2352 
   2353   s = bfd_make_section_anyway_with_flags (abfd, lsect->name, flags);
   2354   if (s == NULL)
   2355     return false;
   2356   lsect->section = s;
   2357 
   2358   /* Define the sym on the first section of this name.  */
   2359   s = bfd_get_section_by_name (abfd, lsect->name);
   2360 
   2361   lsect->sym = _bfd_elf_define_linkage_sym (abfd, info, s, lsect->sym_name);
   2362   if (lsect->sym == NULL)
   2363     return false;
   2364   lsect->sym->root.u.def.value = 0x8000;
   2365   return true;
   2366 }
   2367 
   2368 static bool
   2369 ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
   2370 {
   2371   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   2372   asection *s;
   2373   flagword flags;
   2374   int p2align;
   2375 
   2376   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
   2377 	   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2378   s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
   2379   htab->glink = s;
   2380   p2align = htab->params->ppc476_workaround ? 6 : 4;
   2381   if (p2align < htab->params->plt_stub_align)
   2382     p2align = htab->params->plt_stub_align;
   2383   if (s == NULL
   2384       || !bfd_set_section_alignment (s, p2align))
   2385     return false;
   2386 
   2387   if (!info->no_ld_generated_unwind_info)
   2388     {
   2389       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   2390 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2391       s = bfd_make_section_anyway_with_flags (abfd, ".eh_frame", flags);
   2392       htab->glink_eh_frame = s;
   2393       if (s == NULL
   2394 	  || !bfd_set_section_alignment (s, 2))
   2395 	return false;
   2396     }
   2397 
   2398   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   2399   s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
   2400   htab->elf.iplt = s;
   2401   if (s == NULL
   2402       || !bfd_set_section_alignment (s, 4))
   2403     return false;
   2404 
   2405   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   2406 	   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2407   s = bfd_make_section_anyway_with_flags (abfd, ".rela.iplt", flags);
   2408   htab->elf.irelplt = s;
   2409   if (s == NULL
   2410       || ! bfd_set_section_alignment (s, 2))
   2411     return false;
   2412 
   2413   /* Local plt entries.  */
   2414   flags = (SEC_ALLOC | SEC_LOAD
   2415 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2416   htab->pltlocal = bfd_make_section_anyway_with_flags (abfd, ".branch_lt",
   2417 						       flags);
   2418   if (htab->pltlocal == NULL
   2419       || !bfd_set_section_alignment (htab->pltlocal, 2))
   2420     return false;
   2421 
   2422   if (bfd_link_pic (info))
   2423     {
   2424       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   2425 	       | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2426       htab->relpltlocal
   2427 	= bfd_make_section_anyway_with_flags (abfd, ".rela.branch_lt", flags);
   2428       if (htab->relpltlocal == NULL
   2429 	  || !bfd_set_section_alignment (htab->relpltlocal, 2))
   2430 	return false;
   2431     }
   2432 
   2433   if (!ppc_elf_create_linker_section (abfd, info, 0,
   2434 				      &htab->sdata[0]))
   2435     return false;
   2436 
   2437   if (!ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
   2438 				      &htab->sdata[1]))
   2439     return false;
   2440 
   2441   return true;
   2442 }
   2443 
   2444 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
   2445    to output sections (just like _bfd_elf_create_dynamic_sections has
   2446    to create .dynbss and .rela.bss).  */
   2447 
   2448 static bool
   2449 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   2450 {
   2451   struct ppc_elf_link_hash_table *htab;
   2452   asection *s;
   2453   flagword flags;
   2454 
   2455   htab = ppc_elf_hash_table (info);
   2456 
   2457   if (htab->elf.sgot == NULL
   2458       && !ppc_elf_create_got (abfd, info))
   2459     return false;
   2460 
   2461   if (!_bfd_elf_create_dynamic_sections (abfd, info))
   2462     return false;
   2463 
   2464   if (htab->glink == NULL
   2465       && !ppc_elf_create_glink (abfd, info))
   2466     return false;
   2467 
   2468   s = bfd_make_section_anyway_with_flags (abfd, ".dynsbss",
   2469 					  SEC_ALLOC | SEC_LINKER_CREATED);
   2470   htab->dynsbss = s;
   2471   if (s == NULL)
   2472     return false;
   2473 
   2474   if (! bfd_link_pic (info))
   2475     {
   2476       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   2477 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2478       s = bfd_make_section_anyway_with_flags (abfd, ".rela.sbss", flags);
   2479       htab->relsbss = s;
   2480       if (s == NULL
   2481 	  || !bfd_set_section_alignment (s, 2))
   2482 	return false;
   2483     }
   2484 
   2485   if (htab->elf.target_os == is_vxworks
   2486       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
   2487     return false;
   2488 
   2489   s = htab->elf.splt;
   2490   flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
   2491   if (htab->plt_type == PLT_VXWORKS)
   2492     /* The VxWorks PLT is a loaded section with contents.  */
   2493     flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
   2494   return bfd_set_section_flags (s, flags);
   2495 }
   2496 
   2497 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   2498 
   2499 static void
   2500 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
   2501 			      struct elf_link_hash_entry *dir,
   2502 			      struct elf_link_hash_entry *ind)
   2503 {
   2504   struct ppc_elf_link_hash_entry *edir, *eind;
   2505 
   2506   edir = (struct ppc_elf_link_hash_entry *) dir;
   2507   eind = (struct ppc_elf_link_hash_entry *) ind;
   2508 
   2509   edir->tls_mask |= eind->tls_mask;
   2510   edir->has_sda_refs |= eind->has_sda_refs;
   2511 
   2512   if (edir->elf.versioned != versioned_hidden)
   2513     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   2514   edir->elf.ref_regular |= eind->elf.ref_regular;
   2515   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   2516   edir->elf.non_got_ref |= eind->elf.non_got_ref;
   2517   edir->elf.needs_plt |= eind->elf.needs_plt;
   2518   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
   2519 
   2520   /* If we were called to copy over info for a weak sym, that's all.  */
   2521   if (eind->elf.root.type != bfd_link_hash_indirect)
   2522     return;
   2523 
   2524   if (ind->dyn_relocs != NULL)
   2525     {
   2526       if (dir->dyn_relocs != NULL)
   2527 	{
   2528 	  struct elf_dyn_relocs **pp;
   2529 	  struct elf_dyn_relocs *p;
   2530 
   2531 	  /* Add reloc counts against the indirect sym to the direct sym
   2532 	     list.  Merge any entries against the same section.  */
   2533 	  for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
   2534 	    {
   2535 	      struct elf_dyn_relocs *q;
   2536 
   2537 	      for (q = dir->dyn_relocs; q != NULL; q = q->next)
   2538 		if (q->sec == p->sec)
   2539 		  {
   2540 		    q->pc_count += p->pc_count;
   2541 		    q->count += p->count;
   2542 		    *pp = p->next;
   2543 		    break;
   2544 		  }
   2545 	      if (q == NULL)
   2546 		pp = &p->next;
   2547 	    }
   2548 	  *pp = dir->dyn_relocs;
   2549 	}
   2550 
   2551       dir->dyn_relocs = ind->dyn_relocs;
   2552       ind->dyn_relocs = NULL;
   2553     }
   2554 
   2555   /* Copy over the GOT refcount entries that we may have already seen to
   2556      the symbol which just became indirect.  */
   2557   edir->elf.got.refcount += eind->elf.got.refcount;
   2558   eind->elf.got.refcount = 0;
   2559 
   2560   /* And plt entries.  */
   2561   if (eind->elf.plt.plist != NULL)
   2562     {
   2563       if (edir->elf.plt.plist != NULL)
   2564 	{
   2565 	  struct plt_entry **entp;
   2566 	  struct plt_entry *ent;
   2567 
   2568 	  for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
   2569 	    {
   2570 	      struct plt_entry *dent;
   2571 
   2572 	      for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
   2573 		if (dent->sec == ent->sec && dent->addend == ent->addend)
   2574 		  {
   2575 		    dent->plt.refcount += ent->plt.refcount;
   2576 		    *entp = ent->next;
   2577 		    break;
   2578 		  }
   2579 	      if (dent == NULL)
   2580 		entp = &ent->next;
   2581 	    }
   2582 	  *entp = edir->elf.plt.plist;
   2583 	}
   2584 
   2585       edir->elf.plt.plist = eind->elf.plt.plist;
   2586       eind->elf.plt.plist = NULL;
   2587     }
   2588 
   2589   if (eind->elf.dynindx != -1)
   2590     {
   2591       if (edir->elf.dynindx != -1)
   2592 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   2593 				edir->elf.dynstr_index);
   2594       edir->elf.dynindx = eind->elf.dynindx;
   2595       edir->elf.dynstr_index = eind->elf.dynstr_index;
   2596       eind->elf.dynindx = -1;
   2597       eind->elf.dynstr_index = 0;
   2598     }
   2599 }
   2600 
   2601 /* Hook called by the linker routine which adds symbols from an object
   2602    file.  We use it to put .comm items in .sbss, and not .bss.  */
   2603 
   2604 static bool
   2605 ppc_elf_add_symbol_hook (bfd *abfd,
   2606 			 struct bfd_link_info *info,
   2607 			 Elf_Internal_Sym *sym,
   2608 			 const char **namep ATTRIBUTE_UNUSED,
   2609 			 flagword *flagsp ATTRIBUTE_UNUSED,
   2610 			 asection **secp,
   2611 			 bfd_vma *valp)
   2612 {
   2613   if (sym->st_shndx == SHN_COMMON
   2614       && !bfd_link_relocatable (info)
   2615       && is_ppc_elf (info->output_bfd)
   2616       && sym->st_size <= elf_gp_size (abfd))
   2617     {
   2618       /* Common symbols less than or equal to -G nn bytes are automatically
   2619 	 put into .sbss.  */
   2620       struct ppc_elf_link_hash_table *htab;
   2621 
   2622       htab = ppc_elf_hash_table (info);
   2623       if (htab->sbss == NULL)
   2624 	{
   2625 	  flagword flags = SEC_IS_COMMON | SEC_SMALL_DATA | SEC_LINKER_CREATED;
   2626 
   2627 	  if (!htab->elf.dynobj)
   2628 	    htab->elf.dynobj = abfd;
   2629 
   2630 	  htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
   2631 							   ".sbss",
   2632 							   flags);
   2633 	  if (htab->sbss == NULL)
   2634 	    return false;
   2635 	}
   2636 
   2637       *secp = htab->sbss;
   2638       *valp = sym->st_size;
   2639     }
   2640 
   2641   return true;
   2642 }
   2643 
   2644 /* Find a linker generated pointer with a given addend and type.  */
   2646 
   2647 static elf_linker_section_pointers_t *
   2648 elf_find_pointer_linker_section
   2649   (elf_linker_section_pointers_t *linker_pointers,
   2650    bfd_vma addend,
   2651    elf_linker_section_t *lsect)
   2652 {
   2653   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
   2654     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
   2655       return linker_pointers;
   2656 
   2657   return NULL;
   2658 }
   2659 
   2660 /* Allocate a pointer to live in a linker created section.  */
   2661 
   2662 static bool
   2663 elf_allocate_pointer_linker_section (bfd *abfd,
   2664 				     elf_linker_section_t *lsect,
   2665 				     struct elf_link_hash_entry *h,
   2666 				     const Elf_Internal_Rela *rel)
   2667 {
   2668   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
   2669   elf_linker_section_pointers_t *linker_section_ptr;
   2670   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
   2671   bfd_size_type amt;
   2672 
   2673   BFD_ASSERT (lsect != NULL);
   2674 
   2675   /* Is this a global symbol?  */
   2676   if (h != NULL)
   2677     {
   2678       struct ppc_elf_link_hash_entry *eh;
   2679 
   2680       /* Has this symbol already been allocated?  If so, our work is done.  */
   2681       eh = (struct ppc_elf_link_hash_entry *) h;
   2682       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
   2683 					   rel->r_addend,
   2684 					   lsect))
   2685 	return true;
   2686 
   2687       ptr_linker_section_ptr = &eh->linker_section_pointer;
   2688     }
   2689   else
   2690     {
   2691       BFD_ASSERT (is_ppc_elf (abfd));
   2692 
   2693       /* Allocation of a pointer to a local symbol.  */
   2694       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
   2695 
   2696       /* Allocate a table to hold the local symbols if first time.  */
   2697       if (!ptr)
   2698 	{
   2699 	  unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
   2700 
   2701 	  amt = num_symbols;
   2702 	  amt *= sizeof (elf_linker_section_pointers_t *);
   2703 	  ptr = bfd_zalloc (abfd, amt);
   2704 
   2705 	  if (!ptr)
   2706 	    return false;
   2707 
   2708 	  elf_local_ptr_offsets (abfd) = ptr;
   2709 	}
   2710 
   2711       /* Has this symbol already been allocated?  If so, our work is done.  */
   2712       if (elf_find_pointer_linker_section (ptr[r_symndx],
   2713 					   rel->r_addend,
   2714 					   lsect))
   2715 	return true;
   2716 
   2717       ptr_linker_section_ptr = &ptr[r_symndx];
   2718     }
   2719 
   2720   /* Allocate space for a pointer in the linker section, and allocate
   2721      a new pointer record from internal memory.  */
   2722   BFD_ASSERT (ptr_linker_section_ptr != NULL);
   2723   amt = sizeof (elf_linker_section_pointers_t);
   2724   linker_section_ptr = bfd_alloc (abfd, amt);
   2725 
   2726   if (!linker_section_ptr)
   2727     return false;
   2728 
   2729   linker_section_ptr->next = *ptr_linker_section_ptr;
   2730   linker_section_ptr->addend = rel->r_addend;
   2731   linker_section_ptr->lsect = lsect;
   2732   *ptr_linker_section_ptr = linker_section_ptr;
   2733 
   2734   if (!bfd_set_section_alignment (lsect->section, 2))
   2735     return false;
   2736   linker_section_ptr->offset = lsect->section->size;
   2737   lsect->section->size += 4;
   2738 
   2739 #ifdef DEBUG
   2740   fprintf (stderr,
   2741 	   "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
   2742 	   lsect->name, (long) linker_section_ptr->offset,
   2743 	   (long) lsect->section->size);
   2744 #endif
   2745 
   2746   return true;
   2747 }
   2748 
   2749 static struct plt_entry **
   2750 update_local_sym_info (bfd *abfd,
   2751 		       Elf_Internal_Shdr *symtab_hdr,
   2752 		       unsigned long r_symndx,
   2753 		       int tls_type)
   2754 {
   2755   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
   2756   struct plt_entry **local_plt;
   2757   unsigned char *local_got_tls_masks;
   2758 
   2759   if (local_got_refcounts == NULL)
   2760     {
   2761       bfd_size_type size = symtab_hdr->sh_info;
   2762 
   2763       size *= (sizeof (*local_got_refcounts)
   2764 	       + sizeof (*local_plt)
   2765 	       + sizeof (*local_got_tls_masks));
   2766       local_got_refcounts = bfd_zalloc (abfd, size);
   2767       if (local_got_refcounts == NULL)
   2768 	return NULL;
   2769       elf_local_got_refcounts (abfd) = local_got_refcounts;
   2770     }
   2771 
   2772   local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
   2773   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   2774   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
   2775   if ((tls_type & NON_GOT) == 0)
   2776     local_got_refcounts[r_symndx] += 1;
   2777   return local_plt + r_symndx;
   2778 }
   2779 
   2780 static bool
   2781 update_plt_info (bfd *abfd, struct plt_entry **plist,
   2782 		 asection *sec, bfd_vma addend)
   2783 {
   2784   struct plt_entry *ent;
   2785 
   2786   if (addend < 32768)
   2787     sec = NULL;
   2788   for (ent = *plist; ent != NULL; ent = ent->next)
   2789     if (ent->sec == sec && ent->addend == addend)
   2790       break;
   2791   if (ent == NULL)
   2792     {
   2793       size_t amt = sizeof (*ent);
   2794       ent = bfd_alloc (abfd, amt);
   2795       if (ent == NULL)
   2796 	return false;
   2797       ent->next = *plist;
   2798       ent->sec = sec;
   2799       ent->addend = addend;
   2800       ent->plt.refcount = 0;
   2801       *plist = ent;
   2802     }
   2803   ent->plt.refcount += 1;
   2804   return true;
   2805 }
   2806 
   2807 static struct plt_entry *
   2808 find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
   2809 {
   2810   struct plt_entry *ent;
   2811 
   2812   if (addend < 32768)
   2813     sec = NULL;
   2814   for (ent = *plist; ent != NULL; ent = ent->next)
   2815     if (ent->sec == sec && ent->addend == addend)
   2816       break;
   2817   return ent;
   2818 }
   2819 
   2820 static bool
   2821 is_branch_reloc (enum elf_ppc_reloc_type r_type)
   2822 {
   2823   return (r_type == R_PPC_PLTREL24
   2824 	  || r_type == R_PPC_LOCAL24PC
   2825 	  || r_type == R_PPC_REL24
   2826 	  || r_type == R_PPC_REL14
   2827 	  || r_type == R_PPC_REL14_BRTAKEN
   2828 	  || r_type == R_PPC_REL14_BRNTAKEN
   2829 	  || r_type == R_PPC_ADDR24
   2830 	  || r_type == R_PPC_ADDR14
   2831 	  || r_type == R_PPC_ADDR14_BRTAKEN
   2832 	  || r_type == R_PPC_ADDR14_BRNTAKEN
   2833 	  || r_type == R_PPC_VLE_REL24);
   2834 }
   2835 
   2836 /* Relocs on inline plt call sequence insns prior to the call.  */
   2837 
   2838 static bool
   2839 is_plt_seq_reloc (enum elf_ppc_reloc_type r_type)
   2840 {
   2841   return (r_type == R_PPC_PLT16_HA
   2842 	  || r_type == R_PPC_PLT16_HI
   2843 	  || r_type == R_PPC_PLT16_LO
   2844 	  || r_type == R_PPC_PLTSEQ);
   2845 }
   2846 
   2847 /* Like bfd_reloc_offset_in_range but without a howto.  Return true
   2848    iff a field of SIZE bytes at OFFSET is within SEC limits.  */
   2849 
   2850 static bool
   2851 offset_in_range (asection *sec, bfd_vma offset, size_t size)
   2852 {
   2853   return offset <= sec->size && size <= sec->size - offset;
   2854 }
   2855 
   2856 static void
   2857 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
   2858 {
   2859   _bfd_error_handler
   2860     /* xgettext:c-format */
   2861     (_("%pB: relocation %s cannot be used when making a shared object"),
   2862      abfd,
   2863      ppc_elf_howto_table[r_type]->name);
   2864   bfd_set_error (bfd_error_bad_value);
   2865 }
   2866 
   2867 /* Look through the relocs for a section during the first phase, and
   2868    allocate space in the global offset table or procedure linkage
   2869    table.  */
   2870 
   2871 static bool
   2872 ppc_elf_check_relocs (bfd *abfd,
   2873 		      struct bfd_link_info *info,
   2874 		      asection *sec,
   2875 		      const Elf_Internal_Rela *relocs)
   2876 {
   2877   struct ppc_elf_link_hash_table *htab;
   2878   Elf_Internal_Shdr *symtab_hdr;
   2879   struct elf_link_hash_entry **sym_hashes;
   2880   const Elf_Internal_Rela *rel;
   2881   const Elf_Internal_Rela *rel_end;
   2882   asection *got2, *sreloc;
   2883   struct elf_link_hash_entry *tga;
   2884 
   2885   if (bfd_link_relocatable (info))
   2886     return true;
   2887 
   2888 #ifdef DEBUG
   2889   _bfd_error_handler ("ppc_elf_check_relocs called for section %pA in %pB",
   2890 		      sec, abfd);
   2891 #endif
   2892 
   2893   BFD_ASSERT (is_ppc_elf (abfd));
   2894 
   2895   /* Initialize howto table if not already done.  */
   2896   if (!ppc_elf_howto_table[R_PPC_ADDR32])
   2897     ppc_elf_howto_init ();
   2898 
   2899   htab = ppc_elf_hash_table (info);
   2900   if (htab->glink == NULL)
   2901     {
   2902       if (htab->elf.dynobj == NULL)
   2903 	htab->elf.dynobj = abfd;
   2904       if (!ppc_elf_create_glink (htab->elf.dynobj, info))
   2905 	return false;
   2906     }
   2907   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   2908 			      false, false, true);
   2909   symtab_hdr = &elf_symtab_hdr (abfd);
   2910   sym_hashes = elf_sym_hashes (abfd);
   2911   got2 = bfd_get_section_by_name (abfd, ".got2");
   2912   sreloc = NULL;
   2913 
   2914   rel_end = relocs + sec->reloc_count;
   2915   for (rel = relocs; rel < rel_end; rel++)
   2916     {
   2917       unsigned long r_symndx;
   2918       enum elf_ppc_reloc_type r_type;
   2919       struct elf_link_hash_entry *h;
   2920       Elf_Internal_Sym *isym;
   2921       int tls_type;
   2922       struct plt_entry **ifunc;
   2923       struct plt_entry **pltent;
   2924       bfd_vma addend;
   2925 
   2926       r_symndx = ELF32_R_SYM (rel->r_info);
   2927       if (r_symndx < symtab_hdr->sh_info)
   2928 	{
   2929 	  h = NULL;
   2930 	  isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
   2931 	  if (isym == NULL)
   2932 	    return false;
   2933 	}
   2934       else
   2935 	{
   2936 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   2937 	  while (h->root.type == bfd_link_hash_indirect
   2938 		 || h->root.type == bfd_link_hash_warning)
   2939 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2940 	  isym = NULL;
   2941 	}
   2942 
   2943       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
   2944 	 This shows up in particular in an R_PPC_ADDR32 in the eabi
   2945 	 startup code.  */
   2946       if (h != NULL
   2947 	  && htab->elf.sgot == NULL
   2948 	  && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
   2949 	{
   2950 	  if (htab->elf.dynobj == NULL)
   2951 	    htab->elf.dynobj = abfd;
   2952 	  if (!ppc_elf_create_got (htab->elf.dynobj, info))
   2953 	    return false;
   2954 	  BFD_ASSERT (h == htab->elf.hgot);
   2955 	}
   2956 
   2957       tls_type = 0;
   2958       r_type = ELF32_R_TYPE (rel->r_info);
   2959       ifunc = NULL;
   2960       if (h != NULL)
   2961 	{
   2962 	  if (h->type == STT_GNU_IFUNC)
   2963 	    {
   2964 	      h->needs_plt = 1;
   2965 	      ifunc = &h->plt.plist;
   2966 	    }
   2967 	}
   2968       else if (htab->elf.target_os != is_vxworks)
   2969 	{
   2970 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   2971 	    {
   2972 	      /* Set PLT_IFUNC flag for this sym, no GOT entry yet.  */
   2973 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   2974 					     NON_GOT | PLT_IFUNC);
   2975 	      if (ifunc == NULL)
   2976 		return false;
   2977 
   2978 	      /* STT_GNU_IFUNC symbols must have a PLT entry;
   2979 		 In a non-pie executable even when there are
   2980 		 no plt calls.  */
   2981 	      if (!bfd_link_pic (info)
   2982 		  || is_branch_reloc (r_type)
   2983 		  || r_type == R_PPC_PLT16_LO
   2984 		  || r_type == R_PPC_PLT16_HI
   2985 		  || r_type == R_PPC_PLT16_HA)
   2986 		{
   2987 		  addend = 0;
   2988 		  if (r_type == R_PPC_PLTREL24)
   2989 		    ppc_elf_tdata (abfd)->makes_plt_call = 1;
   2990 		  if (bfd_link_pic (info)
   2991 		      && (r_type == R_PPC_PLTREL24
   2992 			  || r_type == R_PPC_PLT16_LO
   2993 			  || r_type == R_PPC_PLT16_HI
   2994 			  || r_type == R_PPC_PLT16_HA))
   2995 		    addend = rel->r_addend;
   2996 		  if (!update_plt_info (abfd, ifunc, got2, addend))
   2997 		    return false;
   2998 		}
   2999 	    }
   3000 	}
   3001 
   3002       if (htab->elf.target_os != is_vxworks
   3003 	  && is_branch_reloc (r_type)
   3004 	  && h != NULL
   3005 	  && h == tga)
   3006 	{
   3007 	  if (rel != relocs
   3008 	      && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
   3009 		  || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
   3010 	    /* We have a new-style __tls_get_addr call with a marker
   3011 	       reloc.  */
   3012 	    ;
   3013 	  else
   3014 	    /* Mark this section as having an old-style call.  */
   3015 	    sec->nomark_tls_get_addr = 1;
   3016 	}
   3017 
   3018       switch (r_type)
   3019 	{
   3020 	case R_PPC_TLSGD:
   3021 	case R_PPC_TLSLD:
   3022 	  /* These special tls relocs tie a call to __tls_get_addr with
   3023 	     its parameter symbol.  */
   3024 	  if (h != NULL)
   3025 	    ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
   3026 	  else
   3027 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   3028 					NON_GOT | TLS_TLS | TLS_MARK))
   3029 	      return false;
   3030 	  break;
   3031 
   3032 	case R_PPC_PLTSEQ:
   3033 	  break;
   3034 
   3035 	case R_PPC_GOT_TLSLD16:
   3036 	case R_PPC_GOT_TLSLD16_LO:
   3037 	case R_PPC_GOT_TLSLD16_HI:
   3038 	case R_PPC_GOT_TLSLD16_HA:
   3039 	  tls_type = TLS_TLS | TLS_LD;
   3040 	  goto dogottls;
   3041 
   3042 	case R_PPC_GOT_TLSGD16:
   3043 	case R_PPC_GOT_TLSGD16_LO:
   3044 	case R_PPC_GOT_TLSGD16_HI:
   3045 	case R_PPC_GOT_TLSGD16_HA:
   3046 	  tls_type = TLS_TLS | TLS_GD;
   3047 	  goto dogottls;
   3048 
   3049 	case R_PPC_GOT_TPREL16:
   3050 	case R_PPC_GOT_TPREL16_LO:
   3051 	case R_PPC_GOT_TPREL16_HI:
   3052 	case R_PPC_GOT_TPREL16_HA:
   3053 	  if (bfd_link_dll (info))
   3054 	    info->flags |= DF_STATIC_TLS;
   3055 	  tls_type = TLS_TLS | TLS_TPREL;
   3056 	  goto dogottls;
   3057 
   3058 	case R_PPC_GOT_DTPREL16:
   3059 	case R_PPC_GOT_DTPREL16_LO:
   3060 	case R_PPC_GOT_DTPREL16_HI:
   3061 	case R_PPC_GOT_DTPREL16_HA:
   3062 	  tls_type = TLS_TLS | TLS_DTPREL;
   3063 	dogottls:
   3064 	  sec->has_tls_reloc = 1;
   3065 	  /* Fall through.  */
   3066 
   3067 	  /* GOT16 relocations */
   3068 	case R_PPC_GOT16:
   3069 	case R_PPC_GOT16_LO:
   3070 	case R_PPC_GOT16_HI:
   3071 	case R_PPC_GOT16_HA:
   3072 	  /* This symbol requires a global offset table entry.  */
   3073 	  if (htab->elf.sgot == NULL)
   3074 	    {
   3075 	      if (htab->elf.dynobj == NULL)
   3076 		htab->elf.dynobj = abfd;
   3077 	      if (!ppc_elf_create_got (htab->elf.dynobj, info))
   3078 		return false;
   3079 	    }
   3080 	  if (h != NULL)
   3081 	    {
   3082 	      h->got.refcount += 1;
   3083 	      ppc_elf_hash_entry (h)->tls_mask |= tls_type;
   3084 	    }
   3085 	  else
   3086 	    /* This is a global offset table entry for a local symbol.  */
   3087 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
   3088 	      return false;
   3089 
   3090 	  /* We may also need a plt entry if the symbol turns out to be
   3091 	     an ifunc.  */
   3092 	  if (h != NULL && !bfd_link_pic (info))
   3093 	    {
   3094 	      if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
   3095 		return false;
   3096 	    }
   3097 	  break;
   3098 
   3099 	  /* Indirect .sdata relocation.  */
   3100 	case R_PPC_EMB_SDAI16:
   3101 	  htab->sdata[0].sym->ref_regular = 1;
   3102 	  if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[0],
   3103 						    h, rel))
   3104 	    return false;
   3105 	  if (h != NULL)
   3106 	    {
   3107 	      ppc_elf_hash_entry (h)->has_sda_refs = true;
   3108 	      h->non_got_ref = true;
   3109 	    }
   3110 	  break;
   3111 
   3112 	  /* Indirect .sdata2 relocation.  */
   3113 	case R_PPC_EMB_SDA2I16:
   3114 	  if (!bfd_link_executable (info))
   3115 	    {
   3116 	      bad_shared_reloc (abfd, r_type);
   3117 	      return false;
   3118 	    }
   3119 	  htab->sdata[1].sym->ref_regular = 1;
   3120 	  if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[1],
   3121 						    h, rel))
   3122 	    return false;
   3123 	  if (h != NULL)
   3124 	    {
   3125 	      ppc_elf_hash_entry (h)->has_sda_refs = true;
   3126 	      h->non_got_ref = true;
   3127 	    }
   3128 	  break;
   3129 
   3130 	case R_PPC_SDAREL16:
   3131 	  htab->sdata[0].sym->ref_regular = 1;
   3132 	  /* Fall through.  */
   3133 
   3134 	case R_PPC_VLE_SDAREL_LO16A:
   3135 	case R_PPC_VLE_SDAREL_LO16D:
   3136 	case R_PPC_VLE_SDAREL_HI16A:
   3137 	case R_PPC_VLE_SDAREL_HI16D:
   3138 	case R_PPC_VLE_SDAREL_HA16A:
   3139 	case R_PPC_VLE_SDAREL_HA16D:
   3140 	  if (h != NULL)
   3141 	    {
   3142 	      ppc_elf_hash_entry (h)->has_sda_refs = true;
   3143 	      h->non_got_ref = true;
   3144 	    }
   3145 	  break;
   3146 
   3147 	case R_PPC_VLE_REL8:
   3148 	case R_PPC_VLE_REL15:
   3149 	case R_PPC_VLE_REL24:
   3150 	case R_PPC_VLE_LO16A:
   3151 	case R_PPC_VLE_LO16D:
   3152 	case R_PPC_VLE_HI16A:
   3153 	case R_PPC_VLE_HI16D:
   3154 	case R_PPC_VLE_HA16A:
   3155 	case R_PPC_VLE_HA16D:
   3156 	case R_PPC_VLE_ADDR20:
   3157 	  break;
   3158 
   3159 	case R_PPC_EMB_SDA2REL:
   3160 	  if (!bfd_link_executable (info))
   3161 	    {
   3162 	      bad_shared_reloc (abfd, r_type);
   3163 	      return false;
   3164 	    }
   3165 	  htab->sdata[1].sym->ref_regular = 1;
   3166 	  if (h != NULL)
   3167 	    {
   3168 	      ppc_elf_hash_entry (h)->has_sda_refs = true;
   3169 	      h->non_got_ref = true;
   3170 	    }
   3171 	  break;
   3172 
   3173 	case R_PPC_VLE_SDA21_LO:
   3174 	case R_PPC_VLE_SDA21:
   3175 	case R_PPC_EMB_SDA21:
   3176 	case R_PPC_EMB_RELSDA:
   3177 	  if (h != NULL)
   3178 	    {
   3179 	      ppc_elf_hash_entry (h)->has_sda_refs = true;
   3180 	      h->non_got_ref = true;
   3181 	    }
   3182 	  break;
   3183 
   3184 	case R_PPC_EMB_NADDR32:
   3185 	case R_PPC_EMB_NADDR16:
   3186 	case R_PPC_EMB_NADDR16_LO:
   3187 	case R_PPC_EMB_NADDR16_HI:
   3188 	case R_PPC_EMB_NADDR16_HA:
   3189 	  if (h != NULL)
   3190 	    h->non_got_ref = true;
   3191 	  break;
   3192 
   3193 	case R_PPC_PLTREL24:
   3194 	  if (h == NULL)
   3195 	    break;
   3196 	  ppc_elf_tdata (abfd)->makes_plt_call = 1;
   3197 	  goto pltentry;
   3198 
   3199 	case R_PPC_PLTCALL:
   3200 	  sec->has_pltcall = 1;
   3201 	  /* Fall through.  */
   3202 
   3203 	case R_PPC_PLT32:
   3204 	case R_PPC_PLTREL32:
   3205 	case R_PPC_PLT16_LO:
   3206 	case R_PPC_PLT16_HI:
   3207 	case R_PPC_PLT16_HA:
   3208 	pltentry:
   3209 #ifdef DEBUG
   3210 	  fprintf (stderr, "Reloc requires a PLT entry\n");
   3211 #endif
   3212 	  /* This symbol requires a procedure linkage table entry.  */
   3213 	  if (h == NULL)
   3214 	    {
   3215 	      pltent = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   3216 					      NON_GOT | PLT_KEEP);
   3217 	      if (pltent == NULL)
   3218 		return false;
   3219 	    }
   3220 	  else
   3221 	    {
   3222 	      if (r_type != R_PPC_PLTREL24)
   3223 		ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
   3224 	      h->needs_plt = 1;
   3225 	      pltent = &h->plt.plist;
   3226 	    }
   3227 	  addend = 0;
   3228 	  if (bfd_link_pic (info)
   3229 	      && (r_type == R_PPC_PLTREL24
   3230 		  || r_type == R_PPC_PLT16_LO
   3231 		  || r_type == R_PPC_PLT16_HI
   3232 		  || r_type == R_PPC_PLT16_HA))
   3233 	    addend = rel->r_addend;
   3234 	  if (!update_plt_info (abfd, pltent, got2, addend))
   3235 	    return false;
   3236 	  break;
   3237 
   3238 	  /* The following relocations don't need to propagate the
   3239 	     relocation if linking a shared object since they are
   3240 	     section relative.  */
   3241 	case R_PPC_SECTOFF:
   3242 	case R_PPC_SECTOFF_LO:
   3243 	case R_PPC_SECTOFF_HI:
   3244 	case R_PPC_SECTOFF_HA:
   3245 	case R_PPC_DTPREL16:
   3246 	case R_PPC_DTPREL16_LO:
   3247 	case R_PPC_DTPREL16_HI:
   3248 	case R_PPC_DTPREL16_HA:
   3249 	case R_PPC_TOC16:
   3250 	  break;
   3251 
   3252 	case R_PPC_REL16:
   3253 	case R_PPC_REL16_LO:
   3254 	case R_PPC_REL16_HI:
   3255 	case R_PPC_REL16_HA:
   3256 	case R_PPC_REL16DX_HA:
   3257 	  ppc_elf_tdata (abfd)->has_rel16 = 1;
   3258 	  break;
   3259 
   3260 	  /* These are just markers.  */
   3261 	case R_PPC_TLS:
   3262 	case R_PPC_EMB_MRKREF:
   3263 	case R_PPC_NONE:
   3264 	case R_PPC_max:
   3265 	case R_PPC_RELAX:
   3266 	case R_PPC_RELAX_PLT:
   3267 	case R_PPC_RELAX_PLTREL24:
   3268 	case R_PPC_16DX_HA:
   3269 	  break;
   3270 
   3271 	  /* These should only appear in dynamic objects.  */
   3272 	case R_PPC_COPY:
   3273 	case R_PPC_GLOB_DAT:
   3274 	case R_PPC_JMP_SLOT:
   3275 	case R_PPC_RELATIVE:
   3276 	case R_PPC_IRELATIVE:
   3277 	  break;
   3278 
   3279 	  /* These aren't handled yet.  We'll report an error later.  */
   3280 	case R_PPC_ADDR30:
   3281 	case R_PPC_EMB_RELSEC16:
   3282 	case R_PPC_EMB_RELST_LO:
   3283 	case R_PPC_EMB_RELST_HI:
   3284 	case R_PPC_EMB_RELST_HA:
   3285 	case R_PPC_EMB_BIT_FLD:
   3286 	  break;
   3287 
   3288 	  /* This refers only to functions defined in the shared library.  */
   3289 	case R_PPC_LOCAL24PC:
   3290 	  if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
   3291 	    {
   3292 	      htab->plt_type = PLT_OLD;
   3293 	      htab->old_bfd = abfd;
   3294 	    }
   3295 	  if (h != NULL
   3296 	      && ifunc != NULL
   3297 	      && !update_plt_info (abfd, ifunc, NULL, 0))
   3298 	    return false;
   3299 	  break;
   3300 
   3301 	  /* This relocation describes the C++ object vtable hierarchy.
   3302 	     Reconstruct it for later use during GC.  */
   3303 	case R_PPC_GNU_VTINHERIT:
   3304 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   3305 	    return false;
   3306 	  break;
   3307 
   3308 	  /* This relocation describes which C++ vtable entries are actually
   3309 	     used.  Record for later use during GC.  */
   3310 	case R_PPC_GNU_VTENTRY:
   3311 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   3312 	    return false;
   3313 	  break;
   3314 
   3315 	case R_PPC_TPREL16_HI:
   3316 	case R_PPC_TPREL16_HA:
   3317 	  sec->has_tls_reloc = 1;
   3318 	  /* Fall through.  */
   3319 	  /* We shouldn't really be seeing TPREL32.  */
   3320 	case R_PPC_TPREL32:
   3321 	case R_PPC_TPREL16:
   3322 	case R_PPC_TPREL16_LO:
   3323 	  if (bfd_link_dll (info))
   3324 	    info->flags |= DF_STATIC_TLS;
   3325 	  goto dodyn;
   3326 
   3327 	  /* Nor these.  */
   3328 	case R_PPC_DTPMOD32:
   3329 	case R_PPC_DTPREL32:
   3330 	  goto dodyn;
   3331 
   3332 	case R_PPC_REL32:
   3333 	  if (h == NULL
   3334 	      && got2 != NULL
   3335 	      && (sec->flags & SEC_CODE) != 0
   3336 	      && bfd_link_pic (info)
   3337 	      && htab->plt_type == PLT_UNSET)
   3338 	    {
   3339 	      /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
   3340 		 the start of a function, which assembles to a REL32
   3341 		 reference to .got2.  If we detect one of these, then
   3342 		 force the old PLT layout because the linker cannot
   3343 		 reliably deduce the GOT pointer value needed for
   3344 		 PLT call stubs.  */
   3345 	      asection *s;
   3346 
   3347 	      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3348 	      if (s == got2)
   3349 		{
   3350 		  htab->plt_type = PLT_OLD;
   3351 		  htab->old_bfd = abfd;
   3352 		}
   3353 	    }
   3354 	  if (h == NULL || h == htab->elf.hgot)
   3355 	    break;
   3356 	  /* fall through */
   3357 
   3358 	case R_PPC_ADDR32:
   3359 	case R_PPC_ADDR16:
   3360 	case R_PPC_ADDR16_LO:
   3361 	case R_PPC_ADDR16_HI:
   3362 	case R_PPC_ADDR16_HA:
   3363 	case R_PPC_UADDR32:
   3364 	case R_PPC_UADDR16:
   3365 	  if (h != NULL && !bfd_link_pic (info))
   3366 	    {
   3367 	      /* We may need a plt entry if the symbol turns out to be
   3368 		 a function defined in a dynamic object.  */
   3369 	      if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
   3370 		return false;
   3371 
   3372 	      /* We may need a copy reloc too.  */
   3373 	      h->non_got_ref = 1;
   3374 	      h->pointer_equality_needed = 1;
   3375 	      if (r_type == R_PPC_ADDR16_HA)
   3376 		ppc_elf_hash_entry (h)->has_addr16_ha = 1;
   3377 	      if (r_type == R_PPC_ADDR16_LO)
   3378 		ppc_elf_hash_entry (h)->has_addr16_lo = 1;
   3379 	    }
   3380 	  goto dodyn;
   3381 
   3382 	case R_PPC_REL24:
   3383 	case R_PPC_REL14:
   3384 	case R_PPC_REL14_BRTAKEN:
   3385 	case R_PPC_REL14_BRNTAKEN:
   3386 	  if (h == NULL)
   3387 	    break;
   3388 	  if (h == htab->elf.hgot)
   3389 	    {
   3390 	      if (htab->plt_type == PLT_UNSET)
   3391 		{
   3392 		  htab->plt_type = PLT_OLD;
   3393 		  htab->old_bfd = abfd;
   3394 		}
   3395 	      break;
   3396 	    }
   3397 	  /* fall through */
   3398 
   3399 	case R_PPC_ADDR24:
   3400 	case R_PPC_ADDR14:
   3401 	case R_PPC_ADDR14_BRTAKEN:
   3402 	case R_PPC_ADDR14_BRNTAKEN:
   3403 	  if (h != NULL && !bfd_link_pic (info))
   3404 	    {
   3405 	      /* We may need a plt entry if the symbol turns out to be
   3406 		 a function defined in a dynamic object.  */
   3407 	      h->needs_plt = 1;
   3408 	      if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
   3409 		return false;
   3410 	      break;
   3411 	    }
   3412 
   3413 	dodyn:
   3414 	  /* Set up information for symbols that might need dynamic
   3415 	     relocations.  At this point in linking we have read all
   3416 	     the input files and resolved most symbols, but have not
   3417 	     yet decided whether symbols are dynamic or finalized
   3418 	     symbol flags.  In some cases we might be setting dynamic
   3419 	     reloc info for symbols that do not end up needing such.
   3420 	     That's OK, adjust_dynamic_symbol and allocate_dynrelocs
   3421 	     work together with this code.  */
   3422 	  if ((h != NULL
   3423 	       && !SYMBOL_REFERENCES_LOCAL (info, h))
   3424 	      || (bfd_link_pic (info)
   3425 		  && (h != NULL
   3426 		      ? !bfd_is_abs_symbol (&h->root)
   3427 		      : isym->st_shndx != SHN_ABS)
   3428 		  && must_be_dyn_reloc (info, r_type)))
   3429 	    {
   3430 #ifdef DEBUG
   3431 	      fprintf (stderr,
   3432 		       "ppc_elf_check_relocs needs to "
   3433 		       "create relocation for %s\n",
   3434 		       (h && h->root.root.string
   3435 			? h->root.root.string : "<unknown>"));
   3436 #endif
   3437 	      if (sreloc == NULL)
   3438 		{
   3439 		  if (htab->elf.dynobj == NULL)
   3440 		    htab->elf.dynobj = abfd;
   3441 
   3442 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   3443 		    (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ true);
   3444 
   3445 		  if (sreloc == NULL)
   3446 		    return false;
   3447 		}
   3448 
   3449 	      /* If this is a global symbol, we count the number of
   3450 		 relocations we need for this symbol.  */
   3451 	      if (h != NULL)
   3452 		{
   3453 		  struct elf_dyn_relocs *p;
   3454 		  struct elf_dyn_relocs **rel_head;
   3455 
   3456 		  rel_head = &h->dyn_relocs;
   3457 		  p = *rel_head;
   3458 		  if (p == NULL || p->sec != sec)
   3459 		    {
   3460 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   3461 		      if (p == NULL)
   3462 			return false;
   3463 		      p->next = *rel_head;
   3464 		      *rel_head = p;
   3465 		      p->sec = sec;
   3466 		      p->count = 0;
   3467 		      p->pc_count = 0;
   3468 		    }
   3469 		  p->count += 1;
   3470 		  if (!must_be_dyn_reloc (info, r_type))
   3471 		    p->pc_count += 1;
   3472 		}
   3473 	      else
   3474 		{
   3475 		  /* Track dynamic relocs needed for local syms too.
   3476 		     We really need local syms available to do this
   3477 		     easily.  Oh well.  */
   3478 		  struct ppc_dyn_relocs *p;
   3479 		  struct ppc_dyn_relocs **rel_head;
   3480 		  bool is_ifunc;
   3481 		  asection *s;
   3482 		  void *vpp;
   3483 
   3484 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3485 		  if (s == NULL)
   3486 		    s = sec;
   3487 
   3488 		  vpp = &elf_section_data (s)->local_dynrel;
   3489 		  rel_head = (struct ppc_dyn_relocs **) vpp;
   3490 		  is_ifunc = ifunc != NULL;
   3491 		  p = *rel_head;
   3492 		  if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
   3493 		    p = p->next;
   3494 		  if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
   3495 		    {
   3496 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   3497 		      if (p == NULL)
   3498 			return false;
   3499 		      p->next = *rel_head;
   3500 		      *rel_head = p;
   3501 		      p->sec = sec;
   3502 		      p->ifunc = is_ifunc;
   3503 		      p->count = 0;
   3504 		    }
   3505 		  p->count += 1;
   3506 		}
   3507 	    }
   3508 
   3509 	  break;
   3510 	}
   3511     }
   3512 
   3513   return true;
   3514 }
   3515 
   3516 /* Warn for conflicting Tag_GNU_Power_ABI_FP attributes between IBFD
   3518    and OBFD, and merge non-conflicting ones.  */
   3519 bool
   3520 _bfd_elf_ppc_merge_fp_attributes (bfd *ibfd, struct bfd_link_info *info)
   3521 {
   3522   bfd *obfd = info->output_bfd;
   3523   obj_attribute *in_attr, *in_attrs;
   3524   obj_attribute *out_attr, *out_attrs;
   3525   bool ret = true;
   3526   bool warn_only;
   3527 
   3528   /* We only warn about shared library mismatches, because common
   3529      libraries advertise support for a particular long double variant
   3530      but actually support more than one variant.  For example, glibc
   3531      typically supports 128-bit IBM long double in the shared library
   3532      but has a compatibility static archive for 64-bit long double.
   3533      The linker doesn't have the smarts to see that an app using
   3534      object files marked as 64-bit long double call the compatibility
   3535      layer objects and only from there call into the shared library.  */
   3536   warn_only = (ibfd->flags & DYNAMIC) != 0;
   3537 
   3538   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   3539   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   3540 
   3541   in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
   3542   out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
   3543 
   3544   if (in_attr->i != out_attr->i)
   3545     {
   3546       int in_fp = in_attr->i & 3;
   3547       int out_fp = out_attr->i & 3;
   3548       static bfd *last_fp, *last_ld;
   3549 
   3550       if (in_fp == 0)
   3551 	;
   3552       else if (out_fp == 0)
   3553 	{
   3554 	  if (!warn_only)
   3555 	    {
   3556 	      out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3557 	      out_attr->i ^= in_fp;
   3558 	      last_fp = ibfd;
   3559 	    }
   3560 	}
   3561       else if (out_fp != 2 && in_fp == 2)
   3562 	{
   3563 	  _bfd_error_handler
   3564 	    /* xgettext:c-format */
   3565 	    (_("%pB uses hard float, %pB uses soft float"),
   3566 	     last_fp, ibfd);
   3567 	  ret = warn_only;
   3568 	}
   3569       else if (out_fp == 2 && in_fp != 2)
   3570 	{
   3571 	  _bfd_error_handler
   3572 	    /* xgettext:c-format */
   3573 	    (_("%pB uses hard float, %pB uses soft float"),
   3574 	     ibfd, last_fp);
   3575 	  ret = warn_only;
   3576 	}
   3577       else if (out_fp == 1 && in_fp == 3)
   3578 	{
   3579 	  _bfd_error_handler
   3580 	    /* xgettext:c-format */
   3581 	    (_("%pB uses double-precision hard float, "
   3582 	       "%pB uses single-precision hard float"), last_fp, ibfd);
   3583 	  ret = warn_only;
   3584 	}
   3585       else if (out_fp == 3 && in_fp == 1)
   3586 	{
   3587 	  _bfd_error_handler
   3588 	    /* xgettext:c-format */
   3589 	    (_("%pB uses double-precision hard float, "
   3590 	       "%pB uses single-precision hard float"), ibfd, last_fp);
   3591 	  ret = warn_only;
   3592 	}
   3593 
   3594       in_fp = in_attr->i & 0xc;
   3595       out_fp = out_attr->i & 0xc;
   3596       if (in_fp == 0)
   3597 	;
   3598       else if (out_fp == 0)
   3599 	{
   3600 	  if (!warn_only)
   3601 	    {
   3602 	      out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3603 	      out_attr->i ^= in_fp;
   3604 	      last_ld = ibfd;
   3605 	    }
   3606 	}
   3607       else if (out_fp != 2 * 4 && in_fp == 2 * 4)
   3608 	{
   3609 	  _bfd_error_handler
   3610 	    /* xgettext:c-format */
   3611 	    (_("%pB uses 64-bit long double, "
   3612 	       "%pB uses 128-bit long double"), ibfd, last_ld);
   3613 	  ret = warn_only;
   3614 	}
   3615       else if (in_fp != 2 * 4 && out_fp == 2 * 4)
   3616 	{
   3617 	  _bfd_error_handler
   3618 	    /* xgettext:c-format */
   3619 	    (_("%pB uses 64-bit long double, "
   3620 	       "%pB uses 128-bit long double"), last_ld, ibfd);
   3621 	  ret = warn_only;
   3622 	}
   3623       else if (out_fp == 1 * 4 && in_fp == 3 * 4)
   3624 	{
   3625 	  _bfd_error_handler
   3626 	    /* xgettext:c-format */
   3627 	    (_("%pB uses IBM long double, "
   3628 	       "%pB uses IEEE long double"), last_ld, ibfd);
   3629 	  ret = warn_only;
   3630 	}
   3631       else if (out_fp == 3 * 4 && in_fp == 1 * 4)
   3632 	{
   3633 	  _bfd_error_handler
   3634 	    /* xgettext:c-format */
   3635 	    (_("%pB uses IBM long double, "
   3636 	       "%pB uses IEEE long double"), ibfd, last_ld);
   3637 	  ret = warn_only;
   3638 	}
   3639     }
   3640 
   3641   if (!ret)
   3642     {
   3643       out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3644       bfd_set_error (bfd_error_bad_value);
   3645     }
   3646   return ret;
   3647 }
   3648 
   3649 /* Merge object attributes from IBFD into OBFD.  Warn if
   3650    there are conflicting attributes.  */
   3651 static bool
   3652 ppc_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
   3653 {
   3654   bfd *obfd;
   3655   obj_attribute *in_attr, *in_attrs;
   3656   obj_attribute *out_attr, *out_attrs;
   3657   bool ret;
   3658 
   3659   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
   3660     return false;
   3661 
   3662   obfd = info->output_bfd;
   3663   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   3664   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   3665 
   3666   /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
   3667      merge non-conflicting ones.  */
   3668   in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
   3669   out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
   3670   ret = true;
   3671   if (in_attr->i != out_attr->i)
   3672     {
   3673       int in_vec = in_attr->i & 3;
   3674       int out_vec = out_attr->i & 3;
   3675       static bfd *last_vec;
   3676 
   3677       if (in_vec == 0)
   3678 	;
   3679       else if (out_vec == 0)
   3680 	{
   3681 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3682 	  out_attr->i = in_vec;
   3683 	  last_vec = ibfd;
   3684 	}
   3685       /* For now, allow generic to transition to AltiVec or SPE
   3686 	 without a warning.  If GCC marked files with their stack
   3687 	 alignment and used don't-care markings for files which are
   3688 	 not affected by the vector ABI, we could warn about this
   3689 	 case too.  */
   3690       else if (in_vec == 1)
   3691 	;
   3692       else if (out_vec == 1)
   3693 	{
   3694 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3695 	  out_attr->i = in_vec;
   3696 	  last_vec = ibfd;
   3697 	}
   3698       else if (out_vec < in_vec)
   3699 	{
   3700 	  _bfd_error_handler
   3701 	    /* xgettext:c-format */
   3702 	    (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
   3703 	     last_vec, ibfd);
   3704 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3705 	  ret = false;
   3706 	}
   3707       else if (out_vec > in_vec)
   3708 	{
   3709 	  _bfd_error_handler
   3710 	    /* xgettext:c-format */
   3711 	    (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
   3712 	     ibfd, last_vec);
   3713 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3714 	  ret = false;
   3715 	}
   3716     }
   3717 
   3718   /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
   3719      and merge non-conflicting ones.  */
   3720   in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
   3721   out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
   3722   if (in_attr->i != out_attr->i)
   3723     {
   3724       int in_struct = in_attr->i & 3;
   3725       int out_struct = out_attr->i & 3;
   3726       static bfd *last_struct;
   3727 
   3728       if (in_struct == 0 || in_struct == 3)
   3729        ;
   3730       else if (out_struct == 0)
   3731 	{
   3732 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3733 	  out_attr->i = in_struct;
   3734 	  last_struct = ibfd;
   3735 	}
   3736       else if (out_struct < in_struct)
   3737 	{
   3738 	  _bfd_error_handler
   3739 	    /* xgettext:c-format */
   3740 	    (_("%pB uses r3/r4 for small structure returns, "
   3741 	       "%pB uses memory"), last_struct, ibfd);
   3742 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3743 	  ret = false;
   3744 	}
   3745       else if (out_struct > in_struct)
   3746 	{
   3747 	  _bfd_error_handler
   3748 	    /* xgettext:c-format */
   3749 	    (_("%pB uses r3/r4 for small structure returns, "
   3750 	       "%pB uses memory"), ibfd, last_struct);
   3751 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3752 	  ret = false;
   3753 	}
   3754     }
   3755   if (!ret)
   3756     {
   3757       bfd_set_error (bfd_error_bad_value);
   3758       return false;
   3759     }
   3760 
   3761   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   3762   return _bfd_elf_merge_object_attributes (ibfd, info);
   3763 }
   3764 
   3765 /* Merge backend specific data from an object file to the output
   3766    object file when linking.  */
   3767 
   3768 static bool
   3769 ppc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   3770 {
   3771   bfd *obfd = info->output_bfd;
   3772   flagword old_flags;
   3773   flagword new_flags;
   3774   bool error;
   3775 
   3776   if (!is_ppc_elf (ibfd))
   3777     return true;
   3778 
   3779   /* Check if we have the same endianness.  */
   3780   if (! _bfd_generic_verify_endian_match (ibfd, info))
   3781     return false;
   3782 
   3783   if (!ppc_elf_merge_obj_attributes (ibfd, info))
   3784     return false;
   3785 
   3786   if ((ibfd->flags & DYNAMIC) != 0)
   3787     return true;
   3788 
   3789   new_flags = elf_elfheader (ibfd)->e_flags;
   3790   old_flags = elf_elfheader (obfd)->e_flags;
   3791   if (!elf_flags_init (obfd))
   3792     {
   3793       /* First call, no flags set.  */
   3794       elf_flags_init (obfd) = true;
   3795       elf_elfheader (obfd)->e_flags = new_flags;
   3796     }
   3797 
   3798   /* Compatible flags are ok.  */
   3799   else if (new_flags == old_flags)
   3800     ;
   3801 
   3802   /* Incompatible flags.  */
   3803   else
   3804     {
   3805       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
   3806 	 to be linked with either.  */
   3807       error = false;
   3808       if ((new_flags & EF_PPC_RELOCATABLE) != 0
   3809 	  && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
   3810 	{
   3811 	  error = true;
   3812 	  _bfd_error_handler
   3813 	    (_("%pB: compiled with -mrelocatable and linked with "
   3814 	       "modules compiled normally"), ibfd);
   3815 	}
   3816       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
   3817 	       && (old_flags & EF_PPC_RELOCATABLE) != 0)
   3818 	{
   3819 	  error = true;
   3820 	  _bfd_error_handler
   3821 	    (_("%pB: compiled normally and linked with "
   3822 	       "modules compiled with -mrelocatable"), ibfd);
   3823 	}
   3824 
   3825       /* The output is -mrelocatable-lib iff both the input files are.  */
   3826       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
   3827 	elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
   3828 
   3829       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
   3830 	 but each input file is either -mrelocatable or -mrelocatable-lib.  */
   3831       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
   3832 	  && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
   3833 	  && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
   3834 	elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
   3835 
   3836       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
   3837 	 any module uses it.  */
   3838       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
   3839 
   3840       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
   3841       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
   3842 
   3843       /* Warn about any other mismatches.  */
   3844       if (new_flags != old_flags)
   3845 	{
   3846 	  error = true;
   3847 	  _bfd_error_handler
   3848 	    /* xgettext:c-format */
   3849 	    (_("%pB: uses different e_flags (%#x) fields "
   3850 	       "than previous modules (%#x)"),
   3851 	     ibfd, new_flags, old_flags);
   3852 	}
   3853 
   3854       if (error)
   3855 	{
   3856 	  bfd_set_error (bfd_error_bad_value);
   3857 	  return false;
   3858 	}
   3859     }
   3860 
   3861   return true;
   3862 }
   3863 
   3864 static bfd_reloc_status_type
   3865 ppc_elf_vle_split16 (bfd *input_bfd,
   3866 		     asection *input_section,
   3867 		     unsigned long offset,
   3868 		     bfd_byte *loc,
   3869 		     bfd_vma value,
   3870 		     split16_format_type split16_format,
   3871 		     bool fixup)
   3872 {
   3873   unsigned int insn, opcode;
   3874 
   3875   if (!offset_in_range (input_section, offset, 4))
   3876     return bfd_reloc_outofrange;
   3877   insn = bfd_get_32 (input_bfd, loc);
   3878   opcode = insn & E_OPCODE_MASK;
   3879   if (opcode == E_OR2I_INSN
   3880       || opcode == E_AND2I_DOT_INSN
   3881       || opcode == E_OR2IS_INSN
   3882       || opcode == E_LIS_INSN
   3883       || opcode == E_AND2IS_DOT_INSN)
   3884     {
   3885       if (split16_format != split16a_type)
   3886 	{
   3887 	  if (fixup)
   3888 	    split16_format = split16a_type;
   3889 	  else
   3890 	    _bfd_error_handler
   3891 	      /* xgettext:c-format */
   3892 	      (_("%pB(%pA+0x%lx): expected 16A style relocation on 0x%08x insn"),
   3893 	       input_bfd, input_section, offset, opcode);
   3894 	}
   3895     }
   3896   else if (opcode == E_ADD2I_DOT_INSN
   3897 	   || opcode == E_ADD2IS_INSN
   3898 	   || opcode == E_CMP16I_INSN
   3899 	   || opcode == E_MULL2I_INSN
   3900 	   || opcode == E_CMPL16I_INSN
   3901 	   || opcode == E_CMPH16I_INSN
   3902 	   || opcode == E_CMPHL16I_INSN)
   3903     {
   3904       if (split16_format != split16d_type)
   3905 	{
   3906 	  if (fixup)
   3907 	    split16_format = split16d_type;
   3908 	  else
   3909 	    _bfd_error_handler
   3910 	      /* xgettext:c-format */
   3911 	      (_("%pB(%pA+0x%lx): expected 16D style relocation on 0x%08x insn"),
   3912 	       input_bfd, input_section, offset, opcode);
   3913 	}
   3914     }
   3915   if (split16_format == split16a_type)
   3916     {
   3917       insn &= ~((0xf800 << 5) | 0x7ff);
   3918       insn |= (value & 0xf800) << 5;
   3919       if ((insn & E_LI_MASK) == E_LI_INSN)
   3920 	{
   3921 	  /* Hack for e_li.  Extend sign.  */
   3922 	  insn &= ~(0xf0000 >> 5);
   3923 	  insn |= (-(value & 0x8000) & 0xf0000) >> 5;
   3924 	}
   3925     }
   3926   else
   3927     {
   3928       insn &= ~((0xf800 << 10) | 0x7ff);
   3929       insn |= (value & 0xf800) << 10;
   3930     }
   3931   insn |= value & 0x7ff;
   3932   bfd_put_32 (input_bfd, insn, loc);
   3933   return bfd_reloc_ok;
   3934 }
   3935 
   3936 static void
   3937 ppc_elf_vle_split20 (bfd *output_bfd, bfd_byte *loc, bfd_vma value)
   3938 {
   3939   unsigned int insn;
   3940 
   3941   insn = bfd_get_32 (output_bfd, loc);
   3942   /* We have an li20 field, bits 17..20, 11..15, 21..31.  */
   3943   /* Top 4 bits of value to 17..20.  */
   3944   insn |= (value & 0xf0000) >> 5;
   3945   /* Next 5 bits of the value to 11..15.  */
   3946   insn |= (value & 0xf800) << 5;
   3947   /* And the final 11 bits of the value to bits 21 to 31.  */
   3948   insn |= value & 0x7ff;
   3949   bfd_put_32 (output_bfd, insn, loc);
   3950 }
   3951 
   3952 
   3953 /* Choose which PLT scheme to use, and set .plt flags appropriately.
   3955    Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
   3956 int
   3957 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
   3958 			   struct bfd_link_info *info)
   3959 {
   3960   struct ppc_elf_link_hash_table *htab;
   3961   flagword flags;
   3962 
   3963   htab = ppc_elf_hash_table (info);
   3964 
   3965   if (htab->plt_type == PLT_UNSET)
   3966     {
   3967       struct elf_link_hash_entry *h;
   3968 
   3969       if (htab->params->plt_style == PLT_OLD)
   3970 	htab->plt_type = PLT_OLD;
   3971       else if (bfd_link_pic (info)
   3972 	       && htab->elf.dynamic_sections_created
   3973 	       && (h = elf_link_hash_lookup (&htab->elf, "_mcount",
   3974 					     false, false, true)) != NULL
   3975 	       && (h->type == STT_FUNC
   3976 		   || h->needs_plt)
   3977 	       && h->ref_regular
   3978 	       && !(SYMBOL_CALLS_LOCAL (info, h)
   3979 		    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
   3980 	{
   3981 	  /* Profiling of shared libs (and pies) is not supported with
   3982 	     secure plt, because ppc32 does profiling before a
   3983 	     function prologue and a secure plt pic call stubs needs
   3984 	     r30 to be set up.  */
   3985 	  htab->plt_type = PLT_OLD;
   3986 	}
   3987       else
   3988 	{
   3989 	  bfd *ibfd;
   3990 	  enum ppc_elf_plt_type plt_type = htab->params->plt_style;
   3991 
   3992 	  /* Look through the reloc flags left by ppc_elf_check_relocs.
   3993 	     Use the old style bss plt if a file makes plt calls
   3994 	     without using the new relocs, and if ld isn't given
   3995 	     --secure-plt and we never see REL16 relocs.  */
   3996 	  if (plt_type == PLT_UNSET)
   3997 	    plt_type = PLT_OLD;
   3998 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   3999 	    if (is_ppc_elf (ibfd))
   4000 	      {
   4001 		if (ppc_elf_tdata (ibfd)->has_rel16)
   4002 		  plt_type = PLT_NEW;
   4003 		else if (ppc_elf_tdata (ibfd)->makes_plt_call)
   4004 		  {
   4005 		    plt_type = PLT_OLD;
   4006 		    htab->old_bfd = ibfd;
   4007 		    break;
   4008 		  }
   4009 	      }
   4010 	  htab->plt_type = plt_type;
   4011 	}
   4012     }
   4013   if (htab->plt_type == PLT_OLD)
   4014     {
   4015       if (!info->user_warn_rwx_segments)
   4016 	info->no_warn_rwx_segments = 1;
   4017       if (htab->params->plt_style == PLT_NEW
   4018 	  || (htab->params->plt_style != PLT_OLD
   4019 	      && !info->no_warn_rwx_segments))
   4020 	{
   4021 	  if (htab->old_bfd != NULL)
   4022 	    _bfd_error_handler (_("bss-plt forced due to %pB"), htab->old_bfd);
   4023 	  else
   4024 	    _bfd_error_handler (_("bss-plt forced by profiling"));
   4025 	}
   4026     }
   4027 
   4028   BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
   4029 
   4030   if (htab->plt_type == PLT_NEW)
   4031     {
   4032       flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
   4033 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4034 
   4035       /* The new PLT is a loaded section.  */
   4036       if (htab->elf.splt != NULL
   4037 	  && !bfd_set_section_flags (htab->elf.splt, flags))
   4038 	return -1;
   4039 
   4040       /* The new GOT is not executable.  */
   4041       if (htab->elf.sgot != NULL
   4042 	  && !bfd_set_section_flags (htab->elf.sgot, flags))
   4043 	return -1;
   4044     }
   4045   else
   4046     {
   4047       /* Stop an unused .glink section from affecting .text alignment.  */
   4048       if (htab->glink != NULL
   4049 	  && !bfd_set_section_alignment (htab->glink, 0))
   4050 	return -1;
   4051     }
   4052   return htab->plt_type == PLT_NEW;
   4053 }
   4054 
   4055 /* Return the section that should be marked against GC for a given
   4057    relocation.  */
   4058 
   4059 static asection *
   4060 ppc_elf_gc_mark_hook (asection *sec,
   4061 		      struct bfd_link_info *info,
   4062 		      struct elf_reloc_cookie *cookie,
   4063 		      struct elf_link_hash_entry *h,
   4064 		      unsigned int symndx)
   4065 {
   4066   if (h != NULL)
   4067     switch (ELF32_R_TYPE (cookie->rel->r_info))
   4068       {
   4069       case R_PPC_GNU_VTINHERIT:
   4070       case R_PPC_GNU_VTENTRY:
   4071 	return NULL;
   4072       }
   4073 
   4074   return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
   4075 }
   4076 
   4077 static bool
   4078 get_sym_h (struct elf_link_hash_entry **hp,
   4079 	   Elf_Internal_Sym **symp,
   4080 	   asection **symsecp,
   4081 	   unsigned char **tls_maskp,
   4082 	   Elf_Internal_Sym **locsymsp,
   4083 	   unsigned long r_symndx,
   4084 	   bfd *ibfd)
   4085 {
   4086   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   4087 
   4088   if (r_symndx >= symtab_hdr->sh_info)
   4089     {
   4090       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   4091       struct elf_link_hash_entry *h;
   4092 
   4093       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4094       while (h->root.type == bfd_link_hash_indirect
   4095 	     || h->root.type == bfd_link_hash_warning)
   4096 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4097 
   4098       if (hp != NULL)
   4099 	*hp = h;
   4100 
   4101       if (symp != NULL)
   4102 	*symp = NULL;
   4103 
   4104       if (symsecp != NULL)
   4105 	{
   4106 	  asection *symsec = NULL;
   4107 	  if (h->root.type == bfd_link_hash_defined
   4108 	      || h->root.type == bfd_link_hash_defweak)
   4109 	    symsec = h->root.u.def.section;
   4110 	  *symsecp = symsec;
   4111 	}
   4112 
   4113       if (tls_maskp != NULL)
   4114 	*tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
   4115     }
   4116   else
   4117     {
   4118       Elf_Internal_Sym *sym;
   4119       Elf_Internal_Sym *locsyms = *locsymsp;
   4120 
   4121       if (locsyms == NULL)
   4122 	{
   4123 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   4124 	  if (locsyms == NULL)
   4125 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   4126 					    symtab_hdr->sh_info,
   4127 					    0, NULL, NULL, NULL);
   4128 	  if (locsyms == NULL)
   4129 	    return false;
   4130 	  *locsymsp = locsyms;
   4131 	}
   4132       sym = locsyms + r_symndx;
   4133 
   4134       if (hp != NULL)
   4135 	*hp = NULL;
   4136 
   4137       if (symp != NULL)
   4138 	*symp = sym;
   4139 
   4140       if (symsecp != NULL)
   4141 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   4142 
   4143       if (tls_maskp != NULL)
   4144 	{
   4145 	  bfd_signed_vma *local_got;
   4146 	  unsigned char *tls_mask;
   4147 
   4148 	  tls_mask = NULL;
   4149 	  local_got = elf_local_got_refcounts (ibfd);
   4150 	  if (local_got != NULL)
   4151 	    {
   4152 	      struct plt_entry **local_plt = (struct plt_entry **)
   4153 		(local_got + symtab_hdr->sh_info);
   4154 	      unsigned char *lgot_masks = (unsigned char *)
   4155 		(local_plt + symtab_hdr->sh_info);
   4156 	      tls_mask = &lgot_masks[r_symndx];
   4157 	    }
   4158 	  *tls_maskp = tls_mask;
   4159 	}
   4160     }
   4161   return true;
   4162 }
   4163 
   4164 /* Analyze inline PLT call relocations to see whether calls to locally
   4166    defined functions can be converted to direct calls.  */
   4167 
   4168 bool
   4169 ppc_elf_inline_plt (struct bfd_link_info *info)
   4170 {
   4171   struct ppc_elf_link_hash_table *htab;
   4172   bfd *ibfd;
   4173   asection *sec;
   4174   bfd_vma low_vma, high_vma, limit;
   4175 
   4176   htab = ppc_elf_hash_table (info);
   4177   if (htab == NULL)
   4178     return false;
   4179 
   4180   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
   4181      reduced somewhat to cater for possible stubs that might be added
   4182      between the call and its destination.  */
   4183   limit = 0x1e00000;
   4184   low_vma = -1;
   4185   high_vma = 0;
   4186   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
   4187     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
   4188       {
   4189 	if (low_vma > sec->vma)
   4190 	  low_vma = sec->vma;
   4191 	if (high_vma < sec->vma + sec->size)
   4192 	  high_vma = sec->vma + sec->size;
   4193       }
   4194 
   4195   /* If a "bl" can reach anywhere in local code sections, then we can
   4196      convert all inline PLT sequences to direct calls when the symbol
   4197      is local.  */
   4198   if (high_vma - low_vma < limit)
   4199     {
   4200       htab->can_convert_all_inline_plt = 1;
   4201       return true;
   4202     }
   4203 
   4204   /* Otherwise, go looking through relocs for cases where a direct
   4205      call won't reach.  Mark the symbol on any such reloc to disable
   4206      the optimization and keep the PLT entry as it seems likely that
   4207      this will be better than creating trampolines.  Note that this
   4208      will disable the optimization for all inline PLT calls to a
   4209      particular symbol, not just those that won't reach.  The
   4210      difficulty in doing a more precise optimization is that the
   4211      linker needs to make a decision depending on whether a
   4212      particular R_PPC_PLTCALL insn can be turned into a direct
   4213      call, for each of the R_PPC_PLTSEQ and R_PPC_PLT16* insns in
   4214      the sequence, and there is nothing that ties those relocs
   4215      together except their symbol.  */
   4216 
   4217   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   4218     {
   4219       Elf_Internal_Shdr *symtab_hdr;
   4220       Elf_Internal_Sym *local_syms;
   4221 
   4222       if (!is_ppc_elf (ibfd))
   4223 	continue;
   4224 
   4225       local_syms = NULL;
   4226       symtab_hdr = &elf_symtab_hdr (ibfd);
   4227 
   4228       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   4229 	if (sec->has_pltcall
   4230 	    && !bfd_is_abs_section (sec->output_section))
   4231 	  {
   4232 	    Elf_Internal_Rela *relstart, *rel, *relend;
   4233 
   4234 	    /* Read the relocations.  */
   4235 	    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   4236 						  info->keep_memory);
   4237 	    if (relstart == NULL)
   4238 	      return false;
   4239 
   4240 	    relend = relstart + sec->reloc_count;
   4241 	    for (rel = relstart; rel < relend; rel++)
   4242 	      {
   4243 		enum elf_ppc_reloc_type r_type;
   4244 		unsigned long r_symndx;
   4245 		asection *sym_sec;
   4246 		struct elf_link_hash_entry *h;
   4247 		Elf_Internal_Sym *sym;
   4248 		unsigned char *tls_maskp;
   4249 
   4250 		r_type = ELF32_R_TYPE (rel->r_info);
   4251 		if (r_type != R_PPC_PLTCALL)
   4252 		  continue;
   4253 
   4254 		r_symndx = ELF32_R_SYM (rel->r_info);
   4255 		if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
   4256 				r_symndx, ibfd))
   4257 		  {
   4258 		    if (elf_section_data (sec)->relocs != relstart)
   4259 		      free (relstart);
   4260 		    if (symtab_hdr->contents != (unsigned char *) local_syms)
   4261 		      free (local_syms);
   4262 		    return false;
   4263 		  }
   4264 
   4265 		if (sym_sec != NULL && sym_sec->output_section != NULL)
   4266 		  {
   4267 		    bfd_vma from, to;
   4268 		    if (h != NULL)
   4269 		      to = h->root.u.def.value;
   4270 		    else
   4271 		      to = sym->st_value;
   4272 		    to += (rel->r_addend
   4273 			   + sym_sec->output_offset
   4274 			   + sym_sec->output_section->vma);
   4275 		    from = (rel->r_offset
   4276 			    + sec->output_offset
   4277 			    + sec->output_section->vma);
   4278 		    if (to - from + limit < 2 * limit)
   4279 		      *tls_maskp &= ~PLT_KEEP;
   4280 		  }
   4281 	      }
   4282 	    if (elf_section_data (sec)->relocs != relstart)
   4283 	      free (relstart);
   4284 	  }
   4285 
   4286       if (local_syms != NULL
   4287 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   4288 	{
   4289 	  if (!info->keep_memory)
   4290 	    free (local_syms);
   4291 	  else
   4292 	    symtab_hdr->contents = (unsigned char *) local_syms;
   4293 	}
   4294     }
   4295 
   4296   return true;
   4297 }
   4298 
   4299 /* Set plt output section type, htab->tls_get_addr, and call the
   4300    generic ELF tls_setup function.  */
   4301 
   4302 asection *
   4303 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   4304 {
   4305   struct ppc_elf_link_hash_table *htab;
   4306 
   4307   htab = ppc_elf_hash_table (info);
   4308   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4309 					     false, false, true);
   4310   if (htab->plt_type != PLT_NEW)
   4311     htab->params->no_tls_get_addr_opt = true;
   4312 
   4313   if (!htab->params->no_tls_get_addr_opt)
   4314     {
   4315       struct elf_link_hash_entry *opt, *tga;
   4316       opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   4317 				  false, false, true);
   4318       if (opt != NULL
   4319 	  && (opt->root.type == bfd_link_hash_defined
   4320 	      || opt->root.type == bfd_link_hash_defweak))
   4321 	{
   4322 	  /* If glibc supports an optimized __tls_get_addr call stub,
   4323 	     signalled by the presence of __tls_get_addr_opt, and we'll
   4324 	     be calling __tls_get_addr via a plt call stub, then
   4325 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   4326 	  tga = htab->tls_get_addr;
   4327 	  if (htab->elf.dynamic_sections_created
   4328 	      && tga != NULL
   4329 	      && (tga->type == STT_FUNC
   4330 		  || tga->needs_plt)
   4331 	      && !(SYMBOL_CALLS_LOCAL (info, tga)
   4332 		   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga)))
   4333 	    {
   4334 	      struct plt_entry *ent;
   4335 	      for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
   4336 		if (ent->plt.refcount > 0)
   4337 		  break;
   4338 	      if (ent != NULL)
   4339 		{
   4340 		  tga->root.type = bfd_link_hash_indirect;
   4341 		  tga->root.u.i.link = &opt->root;
   4342 		  ppc_elf_copy_indirect_symbol (info, opt, tga);
   4343 		  opt->mark = 1;
   4344 		  if (opt->dynindx != -1)
   4345 		    {
   4346 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   4347 		      opt->dynindx = -1;
   4348 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4349 					      opt->dynstr_index);
   4350 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt))
   4351 			return NULL;
   4352 		    }
   4353 		  htab->tls_get_addr = opt;
   4354 		}
   4355 	    }
   4356 	}
   4357       else
   4358 	htab->params->no_tls_get_addr_opt = true;
   4359     }
   4360   if (htab->plt_type == PLT_NEW
   4361       && htab->elf.splt != NULL
   4362       && htab->elf.splt->output_section != NULL)
   4363     {
   4364       elf_section_type (htab->elf.splt->output_section) = SHT_PROGBITS;
   4365       elf_section_flags (htab->elf.splt->output_section) = SHF_ALLOC + SHF_WRITE;
   4366     }
   4367 
   4368   return bfd_elf_tls_setup (obfd, info);
   4369 }
   4370 
   4371 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   4372    HASH.  */
   4373 
   4374 static bool
   4375 branch_reloc_hash_match (const bfd *ibfd,
   4376 			 const Elf_Internal_Rela *rel,
   4377 			 const struct elf_link_hash_entry *hash)
   4378 {
   4379   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   4380   enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
   4381   unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
   4382 
   4383   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   4384     {
   4385       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   4386       struct elf_link_hash_entry *h;
   4387 
   4388       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4389       while (h->root.type == bfd_link_hash_indirect
   4390 	     || h->root.type == bfd_link_hash_warning)
   4391 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4392       if (h == hash)
   4393 	return true;
   4394     }
   4395   return false;
   4396 }
   4397 
   4398 /* Run through all the TLS relocs looking for optimization
   4399    opportunities.  */
   4400 
   4401 bool
   4402 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
   4403 		      struct bfd_link_info *info)
   4404 {
   4405   bfd *ibfd;
   4406   asection *sec;
   4407   struct ppc_elf_link_hash_table *htab;
   4408   int pass;
   4409 
   4410   if (!bfd_link_executable (info))
   4411     return true;
   4412 
   4413   htab = ppc_elf_hash_table (info);
   4414   if (htab == NULL)
   4415     return false;
   4416 
   4417   htab->do_tls_opt = 1;
   4418 
   4419   /* Make two passes through the relocs.  First time check that tls
   4420      relocs involved in setting up a tls_get_addr call are indeed
   4421      followed by such a call.  If they are not, don't do any tls
   4422      optimization.  On the second pass twiddle tls_mask flags to
   4423      notify relocate_section that optimization can be done, and
   4424      adjust got and plt refcounts.  */
   4425   for (pass = 0; pass < 2; ++pass)
   4426     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   4427       {
   4428 	Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   4429 	asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
   4430 
   4431 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   4432 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   4433 	    {
   4434 	      Elf_Internal_Rela *relstart, *rel, *relend;
   4435 	      int expecting_tls_get_addr = 0;
   4436 
   4437 	      /* Read the relocations.  */
   4438 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   4439 						    info->keep_memory);
   4440 	      if (relstart == NULL)
   4441 		return false;
   4442 
   4443 	      relend = relstart + sec->reloc_count;
   4444 	      for (rel = relstart; rel < relend; rel++)
   4445 		{
   4446 		  enum elf_ppc_reloc_type r_type;
   4447 		  unsigned long r_symndx;
   4448 		  struct elf_link_hash_entry *h = NULL;
   4449 		  unsigned char *tls_mask;
   4450 		  unsigned char tls_set, tls_clear;
   4451 		  bool is_local;
   4452 		  bfd_signed_vma *got_count;
   4453 
   4454 		  r_symndx = ELF32_R_SYM (rel->r_info);
   4455 		  if (r_symndx >= symtab_hdr->sh_info)
   4456 		    {
   4457 		      struct elf_link_hash_entry **sym_hashes;
   4458 
   4459 		      sym_hashes = elf_sym_hashes (ibfd);
   4460 		      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4461 		      while (h->root.type == bfd_link_hash_indirect
   4462 			     || h->root.type == bfd_link_hash_warning)
   4463 			h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4464 		    }
   4465 
   4466 		  is_local = SYMBOL_REFERENCES_LOCAL (info, h);
   4467 		  r_type = ELF32_R_TYPE (rel->r_info);
   4468 		  /* If this section has old-style __tls_get_addr calls
   4469 		     without marker relocs, then check that each
   4470 		     __tls_get_addr call reloc is preceded by a reloc
   4471 		     that conceivably belongs to the __tls_get_addr arg
   4472 		     setup insn.  If we don't find matching arg setup
   4473 		     relocs, don't do any tls optimization.  */
   4474 		  if (pass == 0
   4475 		      && sec->nomark_tls_get_addr
   4476 		      && h != NULL
   4477 		      && h == htab->tls_get_addr
   4478 		      && !expecting_tls_get_addr
   4479 		      && is_branch_reloc (r_type))
   4480 		    {
   4481 		      info->callbacks->minfo ("%H __tls_get_addr lost arg, "
   4482 					      "TLS optimization disabled\n",
   4483 					      ibfd, sec, rel->r_offset);
   4484 		      if (elf_section_data (sec)->relocs != relstart)
   4485 			free (relstart);
   4486 		      return true;
   4487 		    }
   4488 
   4489 		  expecting_tls_get_addr = 0;
   4490 		  switch (r_type)
   4491 		    {
   4492 		    case R_PPC_GOT_TLSLD16:
   4493 		    case R_PPC_GOT_TLSLD16_LO:
   4494 		      expecting_tls_get_addr = 1;
   4495 		      /* Fall through.  */
   4496 
   4497 		    case R_PPC_GOT_TLSLD16_HI:
   4498 		    case R_PPC_GOT_TLSLD16_HA:
   4499 		      /* These relocs should never be against a symbol
   4500 			 defined in a shared lib.  Leave them alone if
   4501 			 that turns out to be the case.  */
   4502 		      if (!is_local)
   4503 			continue;
   4504 
   4505 		      /* LD -> LE */
   4506 		      tls_set = 0;
   4507 		      tls_clear = TLS_LD;
   4508 		      break;
   4509 
   4510 		    case R_PPC_GOT_TLSGD16:
   4511 		    case R_PPC_GOT_TLSGD16_LO:
   4512 		      expecting_tls_get_addr = 1;
   4513 		      /* Fall through.  */
   4514 
   4515 		    case R_PPC_GOT_TLSGD16_HI:
   4516 		    case R_PPC_GOT_TLSGD16_HA:
   4517 		      if (is_local)
   4518 			/* GD -> LE */
   4519 			tls_set = 0;
   4520 		      else
   4521 			/* GD -> IE */
   4522 			tls_set = TLS_TLS | TLS_GDIE;
   4523 		      tls_clear = TLS_GD;
   4524 		      break;
   4525 
   4526 		    case R_PPC_GOT_TPREL16:
   4527 		    case R_PPC_GOT_TPREL16_LO:
   4528 		    case R_PPC_GOT_TPREL16_HI:
   4529 		    case R_PPC_GOT_TPREL16_HA:
   4530 		      if (is_local)
   4531 			{
   4532 			  /* IE -> LE */
   4533 			  tls_set = 0;
   4534 			  tls_clear = TLS_TPREL;
   4535 			  break;
   4536 			}
   4537 		      else
   4538 			continue;
   4539 
   4540 		    case R_PPC_TLSLD:
   4541 		      if (!is_local)
   4542 			continue;
   4543 		      /* Fall through.  */
   4544 		    case R_PPC_TLSGD:
   4545 		      if (rel + 1 < relend
   4546 			  && is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
   4547 			{
   4548 			  if (pass != 0
   4549 			      && ELF32_R_TYPE (rel[1].r_info) != R_PPC_PLTSEQ)
   4550 			    {
   4551 			      r_type = ELF32_R_TYPE (rel[1].r_info);
   4552 			      r_symndx = ELF32_R_SYM (rel[1].r_info);
   4553 			      if (r_symndx >= symtab_hdr->sh_info)
   4554 				{
   4555 				  struct elf_link_hash_entry **sym_hashes;
   4556 
   4557 				  sym_hashes = elf_sym_hashes (ibfd);
   4558 				  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4559 				  while (h->root.type == bfd_link_hash_indirect
   4560 					 || h->root.type == bfd_link_hash_warning)
   4561 				    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4562 				  if (h != NULL)
   4563 				    {
   4564 				      struct plt_entry *ent = NULL;
   4565 				      bfd_vma addend = 0;
   4566 
   4567 				      if (bfd_link_pic (info))
   4568 					addend = rel->r_addend;
   4569 				      ent = find_plt_ent (&h->plt.plist,
   4570 							  got2, addend);
   4571 				      if (ent != NULL
   4572 					  && ent->plt.refcount > 0)
   4573 					ent->plt.refcount -= 1;
   4574 				    }
   4575 				}
   4576 			    }
   4577 			  continue;
   4578 			}
   4579 		      expecting_tls_get_addr = 2;
   4580 		      tls_set = 0;
   4581 		      tls_clear = 0;
   4582 		      break;
   4583 
   4584 		    case R_PPC_TPREL16_HA:
   4585 		      if (pass == 0)
   4586 			{
   4587 			  unsigned char buf[4];
   4588 			  unsigned int insn;
   4589 			  bfd_vma off = rel->r_offset & ~3;
   4590 			  if (!bfd_get_section_contents (ibfd, sec, buf,
   4591 							 off, 4))
   4592 			    {
   4593 			      if (elf_section_data (sec)->relocs != relstart)
   4594 				free (relstart);
   4595 			      return false;
   4596 			    }
   4597 			  insn = bfd_get_32 (ibfd, buf);
   4598 			  /* addis rt,2,imm */
   4599 			  if ((insn & ((0x3fu << 26) | 0x1f << 16))
   4600 			      != ((15u << 26) | (2 << 16)))
   4601 			    {
   4602 			      /* xgettext:c-format */
   4603 			      info->callbacks->minfo
   4604 				(_("%H: warning: %s unexpected insn %#x.\n"),
   4605 				 ibfd, sec, off, "R_PPC_TPREL16_HA", insn);
   4606 			      htab->do_tls_opt = 0;
   4607 			    }
   4608 			}
   4609 		      continue;
   4610 
   4611 		    case R_PPC_TPREL16_HI:
   4612 		      htab->do_tls_opt = 0;
   4613 		      continue;
   4614 
   4615 		    default:
   4616 		      continue;
   4617 		    }
   4618 
   4619 		  if (pass == 0)
   4620 		    {
   4621 		      if (!expecting_tls_get_addr
   4622 			  || !sec->nomark_tls_get_addr)
   4623 			continue;
   4624 
   4625 		      if (rel + 1 < relend
   4626 			  && branch_reloc_hash_match (ibfd, rel + 1,
   4627 						      htab->tls_get_addr))
   4628 			continue;
   4629 
   4630 		      /* Uh oh, we didn't find the expected call.  We
   4631 			 could just mark this symbol to exclude it
   4632 			 from tls optimization but it's safer to skip
   4633 			 the entire optimization.  */
   4634 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   4635 						"TLS optimization disabled\n"),
   4636 					      ibfd, sec, rel->r_offset);
   4637 		      if (elf_section_data (sec)->relocs != relstart)
   4638 			free (relstart);
   4639 		      return true;
   4640 		    }
   4641 
   4642 		  if (h != NULL)
   4643 		    {
   4644 		      tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
   4645 		      got_count = &h->got.refcount;
   4646 		    }
   4647 		  else
   4648 		    {
   4649 		      bfd_signed_vma *lgot_refs;
   4650 		      struct plt_entry **local_plt;
   4651 		      unsigned char *lgot_masks;
   4652 
   4653 		      lgot_refs = elf_local_got_refcounts (ibfd);
   4654 		      if (lgot_refs == NULL)
   4655 			abort ();
   4656 		      local_plt = (struct plt_entry **)
   4657 			(lgot_refs + symtab_hdr->sh_info);
   4658 		      lgot_masks = (unsigned char *)
   4659 			(local_plt + symtab_hdr->sh_info);
   4660 		      tls_mask = &lgot_masks[r_symndx];
   4661 		      got_count = &lgot_refs[r_symndx];
   4662 		    }
   4663 
   4664 		  /* If we don't have old-style __tls_get_addr calls
   4665 		     without TLSGD/TLSLD marker relocs, and we haven't
   4666 		     found a new-style __tls_get_addr call with a
   4667 		     marker for this symbol, then we either have a
   4668 		     broken object file or an -mlongcall style
   4669 		     indirect call to __tls_get_addr without a marker.
   4670 		     Disable optimization in this case.  */
   4671 		  if ((tls_clear & (TLS_GD | TLS_LD)) != 0
   4672 		      && !sec->nomark_tls_get_addr
   4673 		      && ((*tls_mask & (TLS_TLS | TLS_MARK))
   4674 			  != (TLS_TLS | TLS_MARK)))
   4675 		    continue;
   4676 
   4677 		  if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
   4678 		    {
   4679 		      struct plt_entry *ent;
   4680 		      bfd_vma addend = 0;
   4681 
   4682 		      if (bfd_link_pic (info)
   4683 			  && (ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24
   4684 			      || ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTCALL))
   4685 			addend = rel[1].r_addend;
   4686 		      ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
   4687 					  got2, addend);
   4688 		      if (ent != NULL && ent->plt.refcount > 0)
   4689 			ent->plt.refcount -= 1;
   4690 		    }
   4691 		  if (tls_clear == 0)
   4692 		    continue;
   4693 
   4694 		  if (tls_set == 0)
   4695 		    {
   4696 		      /* We managed to get rid of a got entry.  */
   4697 		      if (*got_count > 0)
   4698 			*got_count -= 1;
   4699 		    }
   4700 
   4701 		  *tls_mask |= tls_set;
   4702 		  *tls_mask &= ~tls_clear;
   4703 		}
   4704 
   4705 	      if (elf_section_data (sec)->relocs != relstart)
   4706 		free (relstart);
   4707 	    }
   4708       }
   4709   return true;
   4710 }
   4711 
   4712 /* Return true if we have dynamic relocs against H or any of its weak
   4714    aliases, that apply to read-only sections.  Cannot be used after
   4715    size_dynamic_sections.  */
   4716 
   4717 static bool
   4718 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
   4719 {
   4720   struct ppc_elf_link_hash_entry *eh = ppc_elf_hash_entry (h);
   4721   do
   4722     {
   4723       if (_bfd_elf_readonly_dynrelocs (&eh->elf))
   4724 	return true;
   4725       eh = ppc_elf_hash_entry (eh->elf.u.alias);
   4726     } while (eh != NULL && &eh->elf != h);
   4727 
   4728   return false;
   4729 }
   4730 
   4731 /* Return whether H has pc-relative dynamic relocs.  */
   4732 
   4733 static bool
   4734 pc_dynrelocs (struct elf_link_hash_entry *h)
   4735 {
   4736   struct elf_dyn_relocs *p;
   4737 
   4738   for (p = h->dyn_relocs; p != NULL; p = p->next)
   4739     if (p->pc_count != 0)
   4740       return true;
   4741   return false;
   4742 }
   4743 
   4744 /* Adjust a symbol defined by a dynamic object and referenced by a
   4745    regular object.  The current definition is in some section of the
   4746    dynamic object, but we're not including those sections.  We have to
   4747    change the definition to something the rest of the link can
   4748    understand.  */
   4749 
   4750 static bool
   4751 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   4752 			       struct elf_link_hash_entry *h)
   4753 {
   4754   struct ppc_elf_link_hash_table *htab;
   4755   asection *s;
   4756 
   4757 #ifdef DEBUG
   4758   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
   4759 	   h->root.root.string);
   4760 #endif
   4761 
   4762   /* Make sure we know what is going on here.  */
   4763   htab = ppc_elf_hash_table (info);
   4764   BFD_ASSERT (htab->elf.dynobj != NULL
   4765 	      && (h->needs_plt
   4766 		  || h->type == STT_GNU_IFUNC
   4767 		  || h->is_weakalias
   4768 		  || (h->def_dynamic
   4769 		      && h->ref_regular
   4770 		      && !h->def_regular)));
   4771 
   4772   /* Deal with function syms.  */
   4773   if (h->type == STT_FUNC
   4774       || h->type == STT_GNU_IFUNC
   4775       || h->needs_plt)
   4776     {
   4777       bool local = (SYMBOL_CALLS_LOCAL (info, h)
   4778 			   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   4779       /* Discard dyn_relocs when non-pic if we've decided that a
   4780 	 function symbol is local.  */
   4781       if (!bfd_link_pic (info) && local)
   4782 	h->dyn_relocs = NULL;
   4783 
   4784       /* Clear procedure linkage table information for any symbol that
   4785 	 won't need a .plt entry.  */
   4786       struct plt_entry *ent;
   4787       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   4788 	if (ent->plt.refcount > 0)
   4789 	  break;
   4790       if (ent == NULL
   4791 	  || (h->type != STT_GNU_IFUNC
   4792 	      && local
   4793 	      && (htab->can_convert_all_inline_plt
   4794 		  || (ppc_elf_hash_entry (h)->tls_mask
   4795 		      & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
   4796 	{
   4797 	  /* A PLT entry is not required/allowed when:
   4798 
   4799 	     1. We are not using ld.so; because then the PLT entry
   4800 	     can't be set up, so we can't use one.  In this case,
   4801 	     ppc_elf_adjust_dynamic_symbol won't even be called.
   4802 
   4803 	     2. GC has rendered the entry unused.
   4804 
   4805 	     3. We know for certain that a call to this symbol
   4806 	     will go to this object, or will remain undefined.  */
   4807 	  h->plt.plist = NULL;
   4808 	  h->needs_plt = 0;
   4809 	  h->pointer_equality_needed = 0;
   4810 	}
   4811       else
   4812 	{
   4813 	  /* Taking a function's address in a read/write section
   4814 	     doesn't require us to define the function symbol in the
   4815 	     executable on a plt call stub.  A dynamic reloc can
   4816 	     be used instead, giving better runtime performance.
   4817 	     (Calls via that function pointer don't need to bounce
   4818 	     through the plt call stub.)  Similarly, use a dynamic
   4819 	     reloc for a weak reference when possible, allowing the
   4820 	     resolution of the symbol to be set at load time rather
   4821 	     than link time.  */
   4822 	  if ((h->pointer_equality_needed
   4823 	       || (h->non_got_ref
   4824 		   && !h->ref_regular_nonweak
   4825 		   && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
   4826 	      && htab->elf.target_os != is_vxworks
   4827 	      && !ppc_elf_hash_entry (h)->has_sda_refs
   4828 	      && !_bfd_elf_readonly_dynrelocs (h))
   4829 	    {
   4830 	      h->pointer_equality_needed = 0;
   4831 	      /* If we haven't seen a branch reloc and the symbol
   4832 		 isn't an ifunc then we don't need a plt entry.  */
   4833 	      if (!h->needs_plt && h->type != STT_GNU_IFUNC)
   4834 		h->plt.plist = NULL;
   4835 	    }
   4836 	  else if (!bfd_link_pic (info))
   4837 	    /* We are going to be defining the function symbol on the
   4838 	       plt stub, so no dyn_relocs needed when non-pic.  */
   4839 	    h->dyn_relocs = NULL;
   4840 	}
   4841       h->protected_def = 0;
   4842       /* Function symbols can't have copy relocs.  */
   4843       return true;
   4844     }
   4845   else
   4846     h->plt.plist = NULL;
   4847 
   4848   /* If this is a weak symbol, and there is a real definition, the
   4849      processor independent code will have arranged for us to see the
   4850      real definition first, and we can just use the same value.  */
   4851   if (h->is_weakalias)
   4852     {
   4853       struct elf_link_hash_entry *def = weakdef (h);
   4854       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   4855       h->root.u.def.section = def->root.u.def.section;
   4856       h->root.u.def.value = def->root.u.def.value;
   4857       if (def->root.u.def.section == htab->elf.sdynbss
   4858 	  || def->root.u.def.section == htab->elf.sdynrelro
   4859 	  || def->root.u.def.section == htab->dynsbss)
   4860 	h->dyn_relocs = NULL;
   4861       return true;
   4862     }
   4863 
   4864   /* This is a reference to a symbol defined by a dynamic object which
   4865      is not a function.  */
   4866 
   4867   /* If we are creating a shared library, we must presume that the
   4868      only references to the symbol are via the global offset table.
   4869      For such cases we need not do anything here; the relocations will
   4870      be handled correctly by relocate_section.  */
   4871   if (bfd_link_pic (info))
   4872     {
   4873       h->protected_def = 0;
   4874       return true;
   4875     }
   4876 
   4877   /* If there are no references to this symbol that do not use the
   4878      GOT, we don't need to generate a copy reloc.  */
   4879   if (!h->non_got_ref)
   4880     {
   4881       h->protected_def = 0;
   4882       return true;
   4883     }
   4884 
   4885   /* Protected variables do not work with .dynbss.  The copy in
   4886      .dynbss won't be used by the shared library with the protected
   4887      definition for the variable.  Editing to PIC, or text relocations
   4888      are preferable to an incorrect program.  */
   4889   if (h->protected_def)
   4890     {
   4891       if (ELIMINATE_COPY_RELOCS
   4892 	  && ppc_elf_hash_entry (h)->has_addr16_ha
   4893 	  && ppc_elf_hash_entry (h)->has_addr16_lo
   4894 	  && htab->params->pic_fixup == 0
   4895 	  && info->disable_target_specific_optimizations <= 1)
   4896 	htab->params->pic_fixup = 1;
   4897       return true;
   4898     }
   4899 
   4900   /* If -z nocopyreloc was given, we won't generate them either.  */
   4901   if (info->nocopyreloc)
   4902     return true;
   4903 
   4904    /* If we don't find any dynamic relocs in read-only sections, then
   4905       we'll be keeping the dynamic relocs and avoiding the copy reloc.
   4906       We can't do this if there are any small data relocations.  This
   4907       doesn't work on VxWorks, where we can not have dynamic
   4908       relocations (other than copy and jump slot relocations) in an
   4909       executable.  */
   4910   if (ELIMINATE_COPY_RELOCS
   4911       && !ppc_elf_hash_entry (h)->has_sda_refs
   4912       && htab->elf.target_os != is_vxworks
   4913       && !h->def_regular
   4914       && !alias_readonly_dynrelocs (h))
   4915     return true;
   4916 
   4917   /* We must allocate the symbol in our .dynbss section, which will
   4918      become part of the .bss section of the executable.  There will be
   4919      an entry for this symbol in the .dynsym section.  The dynamic
   4920      object will contain position independent code, so all references
   4921      from the dynamic object to this symbol will go through the global
   4922      offset table.  The dynamic linker will use the .dynsym entry to
   4923      determine the address it must put in the global offset table, so
   4924      both the dynamic object and the regular object will refer to the
   4925      same memory location for the variable.
   4926 
   4927      Of course, if the symbol is referenced using SDAREL relocs, we
   4928      must instead allocate it in .sbss.  */
   4929   if (ppc_elf_hash_entry (h)->has_sda_refs)
   4930     s = htab->dynsbss;
   4931   else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   4932     s = htab->elf.sdynrelro;
   4933   else
   4934     s = htab->elf.sdynbss;
   4935   BFD_ASSERT (s != NULL);
   4936 
   4937   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   4938     {
   4939       asection *srel;
   4940 
   4941       /* We must generate a R_PPC_COPY reloc to tell the dynamic
   4942 	 linker to copy the initial value out of the dynamic object
   4943 	 and into the runtime process image.  */
   4944       if (ppc_elf_hash_entry (h)->has_sda_refs)
   4945 	srel = htab->relsbss;
   4946       else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   4947 	srel = htab->elf.sreldynrelro;
   4948       else
   4949 	srel = htab->elf.srelbss;
   4950       BFD_ASSERT (srel != NULL);
   4951       srel->size += sizeof (Elf32_External_Rela);
   4952       h->needs_copy = 1;
   4953     }
   4954 
   4955   /* We no longer want dyn_relocs.  */
   4956   h->dyn_relocs = NULL;
   4957   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   4958 }
   4959 
   4960 /* Generate a symbol to mark plt call stubs.  For non-PIC code the sym is
   4962    xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
   4963    specifying the addend on the plt relocation.  For -fpic code, the sym
   4964    is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
   4965    xxxxxxxx.got2.plt_pic32.<callee>.  */
   4966 
   4967 static bool
   4968 add_stub_sym (struct plt_entry *ent,
   4969 	      struct elf_link_hash_entry *h,
   4970 	      struct bfd_link_info *info)
   4971 {
   4972   struct elf_link_hash_entry *sh;
   4973   size_t len1, len2, len3;
   4974   char *name;
   4975   const char *stub;
   4976   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   4977 
   4978   if (bfd_link_pic (info))
   4979     stub = ".plt_pic32.";
   4980   else
   4981     stub = ".plt_call32.";
   4982 
   4983   len1 = strlen (h->root.root.string);
   4984   len2 = strlen (stub);
   4985   len3 = 0;
   4986   if (ent->sec)
   4987     len3 = strlen (ent->sec->name);
   4988   name = bfd_alloc (info->output_bfd, len1 + len2 + len3 + 9);
   4989   if (name == NULL)
   4990     return false;
   4991   sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
   4992   if (ent->sec)
   4993     memcpy (name + 8, ent->sec->name, len3);
   4994   memcpy (name + 8 + len3, stub, len2);
   4995   memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
   4996   sh = elf_link_hash_lookup (&htab->elf, name, true, false, false);
   4997   if (sh == NULL)
   4998     return false;
   4999   if (sh->root.type == bfd_link_hash_new)
   5000     {
   5001       sh->root.type = bfd_link_hash_defined;
   5002       sh->root.u.def.section = htab->glink;
   5003       sh->root.u.def.value = ent->glink_offset;
   5004       sh->ref_regular = 1;
   5005       sh->def_regular = 1;
   5006       sh->ref_regular_nonweak = 1;
   5007       sh->forced_local = 1;
   5008       sh->non_elf = 0;
   5009       sh->root.linker_def = 1;
   5010     }
   5011   return true;
   5012 }
   5013 
   5014 /* Allocate NEED contiguous space in .got, and return the offset.
   5015    Handles allocation of the got header when crossing 32k.  */
   5016 
   5017 static bfd_vma
   5018 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
   5019 {
   5020   bfd_vma where;
   5021   unsigned int max_before_header;
   5022 
   5023   if (htab->plt_type == PLT_VXWORKS)
   5024     {
   5025       where = htab->elf.sgot->size;
   5026       htab->elf.sgot->size += need;
   5027     }
   5028   else
   5029     {
   5030       max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
   5031       if (need <= htab->got_gap)
   5032 	{
   5033 	  where = max_before_header - htab->got_gap;
   5034 	  htab->got_gap -= need;
   5035 	}
   5036       else
   5037 	{
   5038 	  if (htab->elf.sgot->size + need > max_before_header
   5039 	      && htab->elf.sgot->size <= max_before_header)
   5040 	    {
   5041 	      htab->got_gap = max_before_header - htab->elf.sgot->size;
   5042 	      htab->elf.sgot->size = max_before_header + htab->got_header_size;
   5043 	    }
   5044 	  where = htab->elf.sgot->size;
   5045 	  htab->elf.sgot->size += need;
   5046 	}
   5047     }
   5048   return where;
   5049 }
   5050 
   5051 /* Calculate size of GOT entries for symbol given its TLS_MASK.
   5052    TLS_LD is excluded because those go in a special GOT slot.  */
   5053 
   5054 static inline unsigned int
   5055 got_entries_needed (int tls_mask)
   5056 {
   5057   unsigned int need;
   5058   if ((tls_mask & TLS_TLS) == 0)
   5059     need = 4;
   5060   else
   5061     {
   5062       need = 0;
   5063       if ((tls_mask & TLS_GD) != 0)
   5064 	need += 8;
   5065       if ((tls_mask & (TLS_TPREL | TLS_GDIE)) != 0)
   5066 	need += 4;
   5067       if ((tls_mask & TLS_DTPREL) != 0)
   5068 	need += 4;
   5069     }
   5070   return need;
   5071 }
   5072 
   5073 /* If H is undefined, make it dynamic if that makes sense.  */
   5074 
   5075 static bool
   5076 ensure_undef_dynamic (struct bfd_link_info *info,
   5077 		      struct elf_link_hash_entry *h)
   5078 {
   5079   struct elf_link_hash_table *htab = elf_hash_table (info);
   5080 
   5081   if (htab->dynamic_sections_created
   5082       && ((info->dynamic_undefined_weak != 0
   5083 	   && h->root.type == bfd_link_hash_undefweak)
   5084 	  || h->root.type == bfd_link_hash_undefined)
   5085       && h->dynindx == -1
   5086       && !h->forced_local
   5087       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   5088     return bfd_elf_link_record_dynamic_symbol (info, h);
   5089   return true;
   5090 }
   5091 
   5092 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
   5093    usual htab->elf.splt section for a PLT entry.  */
   5094 
   5095 static inline
   5096 bool use_local_plt (struct bfd_link_info *info,
   5097 			   struct elf_link_hash_entry *h)
   5098 {
   5099   return (h == NULL
   5100 	  || h->dynindx == -1
   5101 	  || !elf_hash_table (info)->dynamic_sections_created);
   5102 }
   5103 
   5104 /* Allocate space in associated reloc sections for dynamic relocs.  */
   5105 
   5106 static bool
   5107 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   5108 {
   5109   struct bfd_link_info *info = inf;
   5110   struct ppc_elf_link_hash_entry *eh;
   5111   struct ppc_elf_link_hash_table *htab;
   5112   struct elf_dyn_relocs *p;
   5113 
   5114   if (h->root.type == bfd_link_hash_indirect)
   5115     return true;
   5116 
   5117   htab = ppc_elf_hash_table (info);
   5118   eh = (struct ppc_elf_link_hash_entry *) h;
   5119   if (eh->elf.got.refcount > 0
   5120       || (ELIMINATE_COPY_RELOCS
   5121 	  && !eh->elf.def_regular
   5122 	  && eh->elf.protected_def
   5123 	  && eh->has_addr16_ha
   5124 	  && eh->has_addr16_lo
   5125 	  && htab->params->pic_fixup > 0))
   5126     {
   5127       /* Make sure this symbol is output as a dynamic symbol.  */
   5128       if (!ensure_undef_dynamic (info, &eh->elf))
   5129 	return false;
   5130 
   5131       unsigned int need = got_entries_needed (eh->tls_mask);
   5132       unsigned int rel_need = need * sizeof (Elf32_External_Rela) / 4;
   5133       if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
   5134 	{
   5135 	  if (SYMBOL_REFERENCES_LOCAL (info, &eh->elf))
   5136 	    /* We'll just use htab->tlsld_got.offset.  This should
   5137 	       always be the case.  It's a little odd if we have
   5138 	       a local dynamic reloc against a non-local symbol.  */
   5139 	    htab->tlsld_got.refcount += 1;
   5140 	  else
   5141 	    {
   5142 	      need += 8;
   5143 	      rel_need += sizeof (Elf32_External_Rela);
   5144 	    }
   5145 	}
   5146       if (need == 0)
   5147 	eh->elf.got.offset = (bfd_vma) -1;
   5148       else
   5149 	{
   5150 	  eh->elf.got.offset = allocate_got (htab, need);
   5151 	  if (((bfd_link_pic (info)
   5152 		&& !((eh->tls_mask & TLS_TLS) != 0
   5153 		     && bfd_link_executable (info)
   5154 		     && SYMBOL_REFERENCES_LOCAL (info, &eh->elf))
   5155 		&& !bfd_is_abs_symbol (&h->root))
   5156 	       || (htab->elf.dynamic_sections_created
   5157 		   && eh->elf.dynindx != -1
   5158 		   && !SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
   5159 	      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &eh->elf))
   5160 	    {
   5161 	      asection *rsec;
   5162 
   5163 	      rsec = htab->elf.srelgot;
   5164 	      if (eh->elf.type == STT_GNU_IFUNC)
   5165 		rsec = htab->elf.irelplt;
   5166 	      rsec->size += rel_need;
   5167 	    }
   5168 	}
   5169     }
   5170   else
   5171     eh->elf.got.offset = (bfd_vma) -1;
   5172 
   5173   /* If no dynamic sections we can't have dynamic relocs, except for
   5174      IFUNCs which are handled even in static executables.  */
   5175   if (!htab->elf.dynamic_sections_created
   5176       && h->type != STT_GNU_IFUNC)
   5177     h->dyn_relocs = NULL;
   5178 
   5179   /* Discard relocs on undefined symbols that must be local.  */
   5180   else if (h->root.type == bfd_link_hash_undefined
   5181 	   && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   5182     h->dyn_relocs = NULL;
   5183 
   5184   /* Also discard relocs on undefined weak syms with non-default
   5185      visibility, or when dynamic_undefined_weak says so.  */
   5186   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   5187     h->dyn_relocs = NULL;
   5188 
   5189   if (h->dyn_relocs == NULL)
   5190     ;
   5191 
   5192   /* In the shared -Bsymbolic case, discard space allocated for
   5193      dynamic pc-relative relocs against symbols which turn out to be
   5194      defined in regular objects.  For the normal shared case, discard
   5195      space for relocs that have become local due to symbol visibility
   5196      changes.  */
   5197   else if (bfd_link_pic (info))
   5198     {
   5199       /* Relocs that use pc_count are those that appear on a call insn,
   5200 	 or certain REL relocs (see must_be_dyn_reloc) that can be
   5201 	 generated via assembly.  We want calls to protected symbols to
   5202 	 resolve directly to the function rather than going via the plt.
   5203 	 If people want function pointer comparisons to work as expected
   5204 	 then they should avoid writing weird assembly.  */
   5205       if (SYMBOL_CALLS_LOCAL (info, h))
   5206 	{
   5207 	  struct elf_dyn_relocs **pp;
   5208 
   5209 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
   5210 	    {
   5211 	      p->count -= p->pc_count;
   5212 	      p->pc_count = 0;
   5213 	      if (p->count == 0)
   5214 		*pp = p->next;
   5215 	      else
   5216 		pp = &p->next;
   5217 	    }
   5218 	}
   5219 
   5220       if (htab->elf.target_os == is_vxworks)
   5221 	{
   5222 	  struct elf_dyn_relocs **pp;
   5223 
   5224 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
   5225 	    {
   5226 	      if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
   5227 		*pp = p->next;
   5228 	      else
   5229 		pp = &p->next;
   5230 	    }
   5231 	}
   5232 
   5233       if (h->dyn_relocs != NULL)
   5234 	{
   5235 	  /* Make sure this symbol is output as a dynamic symbol.  */
   5236 	  if (!ensure_undef_dynamic (info, h))
   5237 	    return false;
   5238 	}
   5239     }
   5240   else if (ELIMINATE_COPY_RELOCS)
   5241     {
   5242       /* For the non-pic case, discard space for relocs against
   5243 	 symbols which turn out to need copy relocs or are not
   5244 	 dynamic.  */
   5245       if ((h->dynamic_adjusted
   5246 	   || (h->ref_regular
   5247 	       && h->root.type == bfd_link_hash_undefweak
   5248 	       && (info->dynamic_undefined_weak > 0
   5249 		   || !_bfd_elf_readonly_dynrelocs (h))))
   5250 	  && !h->def_regular
   5251 	  && !ELF_COMMON_DEF_P (h)
   5252 	  && !(h->protected_def
   5253 	       && eh->has_addr16_ha
   5254 	       && eh->has_addr16_lo
   5255 	       && htab->params->pic_fixup > 0))
   5256 	{
   5257 	  /* Make sure this symbol is output as a dynamic symbol.  */
   5258 	  if (!ensure_undef_dynamic (info, h))
   5259 	    return false;
   5260 
   5261 	  if (h->dynindx == -1)
   5262 	    h->dyn_relocs = NULL;
   5263 	}
   5264       else
   5265 	h->dyn_relocs = NULL;
   5266     }
   5267 
   5268   /* Allocate space.  */
   5269   for (p = h->dyn_relocs; p != NULL; p = p->next)
   5270     if (!discarded_section (p->sec))
   5271       {
   5272 	asection *sreloc = elf_section_data (p->sec)->sreloc;
   5273 	if (eh->elf.type == STT_GNU_IFUNC)
   5274 	  sreloc = htab->elf.irelplt;
   5275 	sreloc->size += p->count * sizeof (Elf32_External_Rela);
   5276       }
   5277 
   5278   /* Handle PLT relocs.  Done last, after dynindx has settled.
   5279      We might need a PLT entry when the symbol
   5280      a) is dynamic, or
   5281      b) is an ifunc, or
   5282      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
   5283      d) has plt16 relocs and we are linking statically.  */
   5284   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
   5285       || h->type == STT_GNU_IFUNC
   5286       || (h->needs_plt && h->dynamic_adjusted)
   5287       || (h->needs_plt
   5288 	  && h->def_regular
   5289 	  && !htab->elf.dynamic_sections_created
   5290 	  && !htab->can_convert_all_inline_plt
   5291 	  && (ppc_elf_hash_entry (h)->tls_mask
   5292 	      & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
   5293     {
   5294       struct plt_entry *ent;
   5295       bool doneone = false;
   5296       bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
   5297 
   5298       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   5299 	if (ent->plt.refcount > 0)
   5300 	  {
   5301 	    asection *s;
   5302 	    bool dyn;
   5303 
   5304 	    if (!ensure_undef_dynamic (info, h))
   5305 	      return false;
   5306 
   5307 	    dyn = !use_local_plt (info, h);
   5308 	    s = htab->elf.splt;
   5309 	    if (!dyn)
   5310 	      {
   5311 		if (h->type == STT_GNU_IFUNC)
   5312 		  s = htab->elf.iplt;
   5313 		else
   5314 		  s = htab->pltlocal;
   5315 	      }
   5316 
   5317 	    if (htab->plt_type == PLT_NEW || !dyn)
   5318 	      {
   5319 		if (!doneone)
   5320 		  {
   5321 		    plt_offset = s->size;
   5322 		    s->size += 4;
   5323 		  }
   5324 		ent->plt.offset = plt_offset;
   5325 
   5326 		if (s == htab->pltlocal)
   5327 		  ent->glink_offset = glink_offset;
   5328 		else
   5329 		  {
   5330 		    s = htab->glink;
   5331 		    if (!doneone || bfd_link_pic (info))
   5332 		      {
   5333 			glink_offset = s->size;
   5334 			s->size += GLINK_ENTRY_SIZE (htab, h);
   5335 		      }
   5336 		    if (!doneone
   5337 			&& !bfd_link_pic (info)
   5338 			&& h->def_dynamic
   5339 			&& !h->def_regular)
   5340 		      {
   5341 			h->root.u.def.section = s;
   5342 			h->root.u.def.value = glink_offset;
   5343 		      }
   5344 		    ent->glink_offset = glink_offset;
   5345 
   5346 		    if (htab->params->emit_stub_syms
   5347 			&& !add_stub_sym (ent, h, info))
   5348 		      return false;
   5349 		  }
   5350 	      }
   5351 	    else
   5352 	      {
   5353 		if (!doneone)
   5354 		  {
   5355 		    /* If this is the first .plt entry, make room
   5356 		       for the special first entry.  */
   5357 		    if (s->size == 0)
   5358 		      s->size += htab->plt_initial_entry_size;
   5359 
   5360 		    /* The PowerPC PLT is actually composed of two
   5361 		       parts, the first part is 2 words (for a load
   5362 		       and a jump), and then there is a remaining
   5363 		       word available at the end.  */
   5364 		    plt_offset = (htab->plt_initial_entry_size
   5365 				  + (htab->plt_slot_size
   5366 				     * ((s->size
   5367 					 - htab->plt_initial_entry_size)
   5368 					/ htab->plt_entry_size)));
   5369 
   5370 		    /* If this symbol is not defined in a regular
   5371 		       file, and we are not generating a shared
   5372 		       library, then set the symbol to this location
   5373 		       in the .plt.  This is to avoid text
   5374 		       relocations, and is required to make
   5375 		       function pointers compare as equal between
   5376 		       the normal executable and the shared library.  */
   5377 		    if (! bfd_link_pic (info)
   5378 			&& h->def_dynamic
   5379 			&& !h->def_regular)
   5380 		      {
   5381 			h->root.u.def.section = s;
   5382 			h->root.u.def.value = plt_offset;
   5383 		      }
   5384 
   5385 		    /* Make room for this entry.  */
   5386 		    s->size += htab->plt_entry_size;
   5387 		    /* After the 8192nd entry, room for two entries
   5388 		       is allocated.  */
   5389 		    if (htab->plt_type == PLT_OLD
   5390 			&& (s->size - htab->plt_initial_entry_size)
   5391 			/ htab->plt_entry_size
   5392 			> PLT_NUM_SINGLE_ENTRIES)
   5393 		      s->size += htab->plt_entry_size;
   5394 		  }
   5395 		ent->plt.offset = plt_offset;
   5396 	      }
   5397 
   5398 	    /* We also need to make an entry in the .rela.plt section.  */
   5399 	    if (!doneone)
   5400 	      {
   5401 		if (!dyn)
   5402 		  {
   5403 		    if (h->type == STT_GNU_IFUNC)
   5404 		      {
   5405 			s = htab->elf.irelplt;
   5406 			s->size += sizeof (Elf32_External_Rela);
   5407 		      }
   5408 		    else if (bfd_link_pic (info))
   5409 		      {
   5410 			s = htab->relpltlocal;
   5411 			s->size += sizeof (Elf32_External_Rela);
   5412 		      }
   5413 		  }
   5414 		else
   5415 		  {
   5416 		    htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
   5417 
   5418 		    if (htab->plt_type == PLT_VXWORKS)
   5419 		      {
   5420 			/* Allocate space for the unloaded relocations.  */
   5421 			if (!bfd_link_pic (info)
   5422 			    && htab->elf.dynamic_sections_created)
   5423 			  {
   5424 			    if (ent->plt.offset
   5425 				== (bfd_vma) htab->plt_initial_entry_size)
   5426 			      {
   5427 				htab->srelplt2->size
   5428 				  += (sizeof (Elf32_External_Rela)
   5429 				      * VXWORKS_PLTRESOLVE_RELOCS);
   5430 			      }
   5431 
   5432 			    htab->srelplt2->size
   5433 			      += (sizeof (Elf32_External_Rela)
   5434 				  * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
   5435 			  }
   5436 
   5437 			/* Every PLT entry has an associated GOT entry in
   5438 			   .got.plt.  */
   5439 			htab->elf.sgotplt->size += 4;
   5440 		      }
   5441 		  }
   5442 		doneone = true;
   5443 	      }
   5444 	  }
   5445 	else
   5446 	  ent->plt.offset = (bfd_vma) -1;
   5447 
   5448       if (!doneone)
   5449 	{
   5450 	  h->plt.plist = NULL;
   5451 	  h->needs_plt = 0;
   5452 	}
   5453     }
   5454   else
   5455     {
   5456       h->plt.plist = NULL;
   5457       h->needs_plt = 0;
   5458     }
   5459 
   5460   return true;
   5461 }
   5462 
   5463 static const unsigned char glink_eh_frame_cie[] =
   5464 {
   5465   0, 0, 0, 16,				/* length.  */
   5466   0, 0, 0, 0,				/* id.  */
   5467   1,					/* CIE version.  */
   5468   'z', 'R', 0,				/* Augmentation string.  */
   5469   4,					/* Code alignment.  */
   5470   0x7c,					/* Data alignment.  */
   5471   65,					/* RA reg.  */
   5472   1,					/* Augmentation size.  */
   5473   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   5474   DW_CFA_def_cfa, 1, 0			/* def_cfa: r1 offset 0.  */
   5475 };
   5476 
   5477 /* Set the sizes of the dynamic sections.  */
   5478 
   5479 static bool
   5480 ppc_elf_late_size_sections (bfd *output_bfd,
   5481 			    struct bfd_link_info *info)
   5482 {
   5483   struct ppc_elf_link_hash_table *htab;
   5484   asection *s;
   5485   bool relocs;
   5486   bfd *ibfd;
   5487 
   5488 #ifdef DEBUG
   5489   fprintf (stderr, "ppc_elf_late_size_sections called\n");
   5490 #endif
   5491 
   5492   htab = ppc_elf_hash_table (info);
   5493   if (htab->elf.dynobj == NULL)
   5494     return true;
   5495 
   5496   if (elf_hash_table (info)->dynamic_sections_created)
   5497     {
   5498       /* Set the contents of the .interp section to the interpreter.  */
   5499       if (bfd_link_executable (info) && !info->nointerp)
   5500 	{
   5501 	  s = elf_hash_table (info)->interp;
   5502 	  BFD_ASSERT (s != NULL);
   5503 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   5504 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   5505 	  s->alloced = 1;
   5506 	}
   5507     }
   5508 
   5509   if (htab->plt_type == PLT_OLD)
   5510     htab->got_header_size = 16;
   5511   else if (htab->plt_type == PLT_NEW)
   5512     htab->got_header_size = 12;
   5513 
   5514   /* Set up .got offsets for local syms, and space for local dynamic
   5515      relocs.  */
   5516   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   5517     {
   5518       bfd_signed_vma *local_got;
   5519       bfd_signed_vma *end_local_got;
   5520       struct plt_entry **local_plt;
   5521       struct plt_entry **end_local_plt;
   5522       char *lgot_masks;
   5523       bfd_size_type locsymcount;
   5524       Elf_Internal_Shdr *symtab_hdr;
   5525       Elf_Internal_Sym *local_syms;
   5526       Elf_Internal_Sym *isym;
   5527 
   5528       if (!is_ppc_elf (ibfd))
   5529 	continue;
   5530 
   5531       for (s = ibfd->sections; s != NULL; s = s->next)
   5532 	{
   5533 	  struct ppc_dyn_relocs *p;
   5534 
   5535 	  for (p = ((struct ppc_dyn_relocs *)
   5536 		    elf_section_data (s)->local_dynrel);
   5537 	       p != NULL;
   5538 	       p = p->next)
   5539 	    {
   5540 	      if (discarded_section (p->sec))
   5541 		{
   5542 		  /* Input section has been discarded, either because
   5543 		     it is a copy of a linkonce section or due to
   5544 		     linker script /DISCARD/, so we'll be discarding
   5545 		     the relocs too.  */
   5546 		}
   5547 	      else if (htab->elf.target_os == is_vxworks
   5548 		       && strcmp (p->sec->output_section->name,
   5549 				  ".tls_vars") == 0)
   5550 		{
   5551 		  /* Relocations in vxworks .tls_vars sections are
   5552 		     handled specially by the loader.  */
   5553 		}
   5554 	      else if (p->count != 0)
   5555 		{
   5556 		  asection *sreloc = elf_section_data (p->sec)->sreloc;
   5557 		  if (p->ifunc)
   5558 		    sreloc = htab->elf.irelplt;
   5559 		  sreloc->size += p->count * sizeof (Elf32_External_Rela);
   5560 		  if ((p->sec->output_section->flags
   5561 		       & (SEC_READONLY | SEC_ALLOC))
   5562 		      == (SEC_READONLY | SEC_ALLOC))
   5563 		    {
   5564 		      info->flags |= DF_TEXTREL;
   5565 		      info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
   5566 					      p->sec->owner, p->sec);
   5567 		    }
   5568 		}
   5569 	    }
   5570 	}
   5571 
   5572       local_got = elf_local_got_refcounts (ibfd);
   5573       if (!local_got)
   5574 	continue;
   5575 
   5576       symtab_hdr = &elf_symtab_hdr (ibfd);
   5577       locsymcount = symtab_hdr->sh_info;
   5578       end_local_got = local_got + locsymcount;
   5579       local_plt = (struct plt_entry **) end_local_got;
   5580       end_local_plt = local_plt + locsymcount;
   5581       lgot_masks = (char *) end_local_plt;
   5582       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
   5583       if (local_syms == NULL && locsymcount != 0)
   5584 	{
   5585 	  local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
   5586 					     0, NULL, NULL, NULL);
   5587 	  if (local_syms == NULL)
   5588 	    return false;
   5589 	}
   5590 
   5591       for (isym = local_syms;
   5592 	   local_got < end_local_got;
   5593 	   ++local_got, ++lgot_masks, ++isym)
   5594 	if (*local_got > 0)
   5595 	  {
   5596 	    unsigned int need;
   5597 	    if ((*lgot_masks & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
   5598 	      htab->tlsld_got.refcount += 1;
   5599 	    need = got_entries_needed (*lgot_masks);
   5600 	    if (need == 0)
   5601 	      *local_got = (bfd_vma) -1;
   5602 	    else
   5603 	      {
   5604 		*local_got = allocate_got (htab, need);
   5605 		if (bfd_link_pic (info)
   5606 		    && !((*lgot_masks & TLS_TLS) != 0
   5607 			 && bfd_link_executable (info))
   5608 		    && isym->st_shndx != SHN_ABS)
   5609 		  {
   5610 		    asection *srel;
   5611 
   5612 		    need *= sizeof (Elf32_External_Rela) / 4;
   5613 		    srel = htab->elf.srelgot;
   5614 		    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   5615 		      srel = htab->elf.irelplt;
   5616 		    srel->size += need;
   5617 		  }
   5618 	      }
   5619 	  }
   5620 	else
   5621 	  *local_got = (bfd_vma) -1;
   5622 
   5623       if (htab->elf.target_os == is_vxworks)
   5624 	continue;
   5625 
   5626       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   5627       lgot_masks = (char *) end_local_plt;
   5628       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
   5629 	{
   5630 	  struct plt_entry *ent;
   5631 	  bool doneone = false;
   5632 	  bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
   5633 
   5634 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   5635 	    if (ent->plt.refcount > 0)
   5636 	      {
   5637 		if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   5638 		  s = htab->elf.iplt;
   5639 		else if (htab->can_convert_all_inline_plt
   5640 			 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
   5641 		  {
   5642 		    ent->plt.offset = (bfd_vma) -1;
   5643 		    continue;
   5644 		  }
   5645 		else
   5646 		  s = htab->pltlocal;
   5647 
   5648 		if (!doneone)
   5649 		  {
   5650 		    plt_offset = s->size;
   5651 		    s->size += 4;
   5652 		  }
   5653 		ent->plt.offset = plt_offset;
   5654 
   5655 		if (s != htab->pltlocal && (!doneone || bfd_link_pic (info)))
   5656 		  {
   5657 		    s = htab->glink;
   5658 		    glink_offset = s->size;
   5659 		    s->size += GLINK_ENTRY_SIZE (htab, NULL);
   5660 		  }
   5661 		ent->glink_offset = glink_offset;
   5662 
   5663 		if (!doneone)
   5664 		  {
   5665 		    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   5666 		      {
   5667 			s = htab->elf.irelplt;
   5668 			s->size += sizeof (Elf32_External_Rela);
   5669 		      }
   5670 		    else if (bfd_link_pic (info))
   5671 		      {
   5672 			s = htab->relpltlocal;
   5673 			s->size += sizeof (Elf32_External_Rela);
   5674 		      }
   5675 		    doneone = true;
   5676 		  }
   5677 	      }
   5678 	    else
   5679 	      ent->plt.offset = (bfd_vma) -1;
   5680 	}
   5681 
   5682       if (local_syms != NULL
   5683 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   5684 	{
   5685 	  if (!info->keep_memory)
   5686 	    free (local_syms);
   5687 	  else
   5688 	    symtab_hdr->contents = (unsigned char *) local_syms;
   5689 	}
   5690     }
   5691 
   5692   /* Allocate space for global sym dynamic relocs.  */
   5693   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
   5694 
   5695   if (htab->tlsld_got.refcount > 0)
   5696     {
   5697       htab->tlsld_got.offset = allocate_got (htab, 8);
   5698       if (bfd_link_dll (info))
   5699 	htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
   5700     }
   5701   else
   5702     htab->tlsld_got.offset = (bfd_vma) -1;
   5703 
   5704   if (htab->elf.sgot != NULL && htab->plt_type != PLT_VXWORKS)
   5705     {
   5706       unsigned int g_o_t = 32768;
   5707 
   5708       /* If we haven't allocated the header, do so now.  When we get here,
   5709 	 for old plt/got the got size will be 0 to 32764 (not allocated),
   5710 	 or 32780 to 65536 (header allocated).  For new plt/got, the
   5711 	 corresponding ranges are 0 to 32768 and 32780 to 65536.  */
   5712       if (htab->elf.sgot->size <= 32768)
   5713 	{
   5714 	  g_o_t = htab->elf.sgot->size;
   5715 	  if (htab->plt_type == PLT_OLD)
   5716 	    g_o_t += 4;
   5717 	  htab->elf.sgot->size += htab->got_header_size;
   5718 	}
   5719 
   5720       htab->elf.hgot->root.u.def.value = g_o_t;
   5721     }
   5722   if (bfd_link_pic (info))
   5723     {
   5724       struct elf_link_hash_entry *sda = htab->sdata[0].sym;
   5725 
   5726       sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
   5727       sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
   5728     }
   5729   if (info->emitrelocations)
   5730     {
   5731       struct elf_link_hash_entry *sda = htab->sdata[0].sym;
   5732 
   5733       if (sda != NULL && sda->ref_regular)
   5734 	sda->root.u.def.section->flags |= SEC_KEEP;
   5735       sda = htab->sdata[1].sym;
   5736       if (sda != NULL && sda->ref_regular)
   5737 	sda->root.u.def.section->flags |= SEC_KEEP;
   5738     }
   5739 
   5740   if (htab->glink != NULL
   5741       && htab->glink->size != 0
   5742       && htab->elf.dynamic_sections_created)
   5743     {
   5744       htab->glink_pltresolve = htab->glink->size;
   5745       /* Space for the branch table.  */
   5746       htab->glink->size
   5747 	+= htab->elf.srelplt->size / (sizeof (Elf32_External_Rela) / 4) - 4;
   5748       /* Pad out to align the start of PLTresolve.  */
   5749       htab->glink->size += -htab->glink->size & (htab->params->ppc476_workaround
   5750 						 ? 63 : 15);
   5751       htab->glink->size += GLINK_PLTRESOLVE;
   5752 
   5753       if (htab->params->emit_stub_syms)
   5754 	{
   5755 	  struct elf_link_hash_entry *sh;
   5756 	  sh = elf_link_hash_lookup (&htab->elf, "__glink",
   5757 				     true, false, false);
   5758 	  if (sh == NULL)
   5759 	    return false;
   5760 	  if (sh->root.type == bfd_link_hash_new)
   5761 	    {
   5762 	      sh->root.type = bfd_link_hash_defined;
   5763 	      sh->root.u.def.section = htab->glink;
   5764 	      sh->root.u.def.value = htab->glink_pltresolve;
   5765 	      sh->ref_regular = 1;
   5766 	      sh->def_regular = 1;
   5767 	      sh->ref_regular_nonweak = 1;
   5768 	      sh->forced_local = 1;
   5769 	      sh->non_elf = 0;
   5770 	      sh->root.linker_def = 1;
   5771 	    }
   5772 	  sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   5773 				     true, false, false);
   5774 	  if (sh == NULL)
   5775 	    return false;
   5776 	  if (sh->root.type == bfd_link_hash_new)
   5777 	    {
   5778 	      sh->root.type = bfd_link_hash_defined;
   5779 	      sh->root.u.def.section = htab->glink;
   5780 	      sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
   5781 	      sh->ref_regular = 1;
   5782 	      sh->def_regular = 1;
   5783 	      sh->ref_regular_nonweak = 1;
   5784 	      sh->forced_local = 1;
   5785 	      sh->non_elf = 0;
   5786 	      sh->root.linker_def = 1;
   5787 	    }
   5788 	}
   5789     }
   5790 
   5791   if (htab->glink != NULL
   5792       && htab->glink->size != 0
   5793       && htab->glink_eh_frame != NULL
   5794       && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   5795       && _bfd_elf_eh_frame_present (info))
   5796     {
   5797       s = htab->glink_eh_frame;
   5798       s->size = sizeof (glink_eh_frame_cie) + 20;
   5799       if (bfd_link_pic (info))
   5800 	{
   5801 	  s->size += 4;
   5802 	  if (htab->glink->size - GLINK_PLTRESOLVE + 8 >= 256)
   5803 	    s->size += 4;
   5804 	}
   5805     }
   5806 
   5807   /* We've now determined the sizes of the various dynamic sections.
   5808      Allocate memory for them.  */
   5809   relocs = false;
   5810   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
   5811     {
   5812       bool strip_section = true;
   5813 
   5814       if ((s->flags & SEC_LINKER_CREATED) == 0)
   5815 	continue;
   5816 
   5817       if (s == htab->elf.splt
   5818 	  || s == htab->elf.sgot)
   5819 	{
   5820 	  /* We'd like to strip these sections if they aren't needed, but if
   5821 	     we've exported dynamic symbols from them we must leave them.
   5822 	     It's too late to tell BFD to get rid of the symbols.  */
   5823 	  if (htab->elf.hplt != NULL)
   5824 	    strip_section = false;
   5825 	  /* Strip this section if we don't need it; see the
   5826 	     comment below.  */
   5827 	}
   5828       else if (s == htab->elf.iplt
   5829 	       || s == htab->pltlocal
   5830 	       || s == htab->glink
   5831 	       || s == htab->glink_eh_frame
   5832 	       || s == htab->elf.sgotplt
   5833 	       || s == htab->sbss
   5834 	       || s == htab->elf.sdynbss
   5835 	       || s == htab->elf.sdynrelro
   5836 	       || s == htab->dynsbss)
   5837 	{
   5838 	  /* Strip these too.  */
   5839 	}
   5840       else if (s == htab->sdata[0].section
   5841 	       || s == htab->sdata[1].section)
   5842 	{
   5843 	  strip_section = (s->flags & SEC_KEEP) == 0;
   5844 	}
   5845       else if (startswith (bfd_section_name (s), ".rela"))
   5846 	{
   5847 	  if (s->size != 0)
   5848 	    {
   5849 	      /* Remember whether there are any relocation sections.  */
   5850 	      relocs = true;
   5851 
   5852 	      /* We use the reloc_count field as a counter if we need
   5853 		 to copy relocs into the output file.  */
   5854 	      s->reloc_count = 0;
   5855 	    }
   5856 	}
   5857       else
   5858 	{
   5859 	  /* It's not one of our sections, so don't allocate space.  */
   5860 	  continue;
   5861 	}
   5862 
   5863       if (s->size == 0 && strip_section)
   5864 	{
   5865 	  /* If we don't need this section, strip it from the
   5866 	     output file.  This is mostly to handle .rela.bss and
   5867 	     .rela.plt.  We must create both sections in
   5868 	     create_dynamic_sections, because they must be created
   5869 	     before the linker maps input sections to output
   5870 	     sections.  The linker does that before
   5871 	     adjust_dynamic_symbol is called, and it is that
   5872 	     function which decides whether anything needs to go
   5873 	     into these sections.  */
   5874 	  s->flags |= SEC_EXCLUDE;
   5875 	  continue;
   5876 	}
   5877 
   5878       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   5879 	continue;
   5880 
   5881       /* Allocate memory for the section contents.  */
   5882       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
   5883       if (s->contents == NULL)
   5884 	return false;
   5885       s->alloced = 1;
   5886     }
   5887 
   5888   if (htab->elf.dynamic_sections_created)
   5889     {
   5890       /* Add some entries to the .dynamic section.  We fill in the
   5891 	 values later, in ppc_elf_finish_dynamic_sections, but we
   5892 	 must add the entries now so that we get the correct size for
   5893 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   5894 	 dynamic linker and used by the debugger.  */
   5895 #define add_dynamic_entry(TAG, VAL) \
   5896   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   5897 
   5898       if (!_bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
   5899 						    relocs))
   5900 	return false;
   5901 
   5902       if (htab->plt_type == PLT_NEW
   5903 	  && htab->glink != NULL
   5904 	  && htab->glink->size != 0)
   5905 	{
   5906 	  if (!add_dynamic_entry (DT_PPC_GOT, 0))
   5907 	    return false;
   5908 	  if (!htab->params->no_tls_get_addr_opt
   5909 	      && htab->tls_get_addr != NULL
   5910 	      && htab->tls_get_addr->plt.plist != NULL
   5911 	      && !add_dynamic_entry (DT_PPC_OPT, PPC_OPT_TLS))
   5912 	    return false;
   5913 	}
   5914    }
   5915 #undef add_dynamic_entry
   5916 
   5917   if (htab->glink_eh_frame != NULL
   5918       && htab->glink_eh_frame->contents != NULL)
   5919     {
   5920       unsigned char *p = htab->glink_eh_frame->contents;
   5921       bfd_vma val;
   5922 
   5923       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   5924       /* CIE length (rewrite in case little-endian).  */
   5925       bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
   5926       p += sizeof (glink_eh_frame_cie);
   5927       /* FDE length.  */
   5928       val = htab->glink_eh_frame->size - 4 - sizeof (glink_eh_frame_cie);
   5929       bfd_put_32 (htab->elf.dynobj, val, p);
   5930       p += 4;
   5931       /* CIE pointer.  */
   5932       val = p - htab->glink_eh_frame->contents;
   5933       bfd_put_32 (htab->elf.dynobj, val, p);
   5934       p += 4;
   5935       /* Offset to .glink.  Set later.  */
   5936       p += 4;
   5937       /* .glink size.  */
   5938       bfd_put_32 (htab->elf.dynobj, htab->glink->size, p);
   5939       p += 4;
   5940       /* Augmentation.  */
   5941       p += 1;
   5942 
   5943       if (bfd_link_pic (info)
   5944 	  && htab->elf.dynamic_sections_created)
   5945 	{
   5946 	  bfd_vma adv = (htab->glink->size - GLINK_PLTRESOLVE + 8) >> 2;
   5947 	  if (adv < 64)
   5948 	    *p++ = DW_CFA_advance_loc + adv;
   5949 	  else if (adv < 256)
   5950 	    {
   5951 	      *p++ = DW_CFA_advance_loc1;
   5952 	      *p++ = adv;
   5953 	    }
   5954 	  else if (adv < 65536)
   5955 	    {
   5956 	      *p++ = DW_CFA_advance_loc2;
   5957 	      bfd_put_16 (htab->elf.dynobj, adv, p);
   5958 	      p += 2;
   5959 	    }
   5960 	  else
   5961 	    {
   5962 	      *p++ = DW_CFA_advance_loc4;
   5963 	      bfd_put_32 (htab->elf.dynobj, adv, p);
   5964 	      p += 4;
   5965 	    }
   5966 	  *p++ = DW_CFA_register;
   5967 	  *p++ = 65;
   5968 	  p++;
   5969 	  *p++ = DW_CFA_advance_loc + 4;
   5970 	  *p++ = DW_CFA_restore_extended;
   5971 	  *p++ = 65;
   5972 	}
   5973       BFD_ASSERT ((bfd_vma) ((p + 3 - htab->glink_eh_frame->contents) & -4)
   5974 		  == htab->glink_eh_frame->size);
   5975     }
   5976 
   5977   return true;
   5978 }
   5979 
   5980 /* Arrange to have _SDA_BASE_ or _SDA2_BASE_ stripped from the output
   5981    if it looks like nothing is using them.  */
   5982 
   5983 static void
   5984 maybe_strip_sdasym (bfd *output_bfd, elf_linker_section_t *lsect)
   5985 {
   5986   struct elf_link_hash_entry *sda = lsect->sym;
   5987 
   5988   if (sda != NULL && !sda->ref_regular && sda->dynindx == -1)
   5989     {
   5990       asection *s;
   5991 
   5992       s = bfd_get_section_by_name (output_bfd, lsect->name);
   5993       if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
   5994 	{
   5995 	  s = bfd_get_section_by_name (output_bfd, lsect->bss_name);
   5996 	  if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
   5997 	    {
   5998 	      sda->def_regular = 0;
   5999 	      /* This is somewhat magic.  See elf_link_output_extsym.  */
   6000 	      sda->ref_dynamic = 1;
   6001 	      sda->forced_local = 0;
   6002 	    }
   6003 	}
   6004     }
   6005 }
   6006 
   6007 void
   6008 ppc_elf_maybe_strip_sdata_syms (struct bfd_link_info *info)
   6009 {
   6010   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   6011 
   6012   if (htab != NULL)
   6013     {
   6014       maybe_strip_sdasym (info->output_bfd, &htab->sdata[0]);
   6015       maybe_strip_sdasym (info->output_bfd, &htab->sdata[1]);
   6016     }
   6017 }
   6018 
   6019 
   6020 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   6021 
   6022 static bool
   6023 ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
   6024 {
   6025   if (h->plt.plist != NULL
   6026       && !h->def_regular
   6027       && (!h->pointer_equality_needed
   6028 	  || !h->ref_regular_nonweak))
   6029     return false;
   6030 
   6031   return _bfd_elf_hash_symbol (h);
   6032 }
   6033 
   6034 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
   6036 
   6037 /* Relaxation trampolines.  r12 is available for clobbering (r11, is
   6038    used for some functions that are allowed to break the ABI).  */
   6039 static const int shared_stub_entry[] =
   6040   {
   6041     0x7c0802a6, /* mflr 0 */
   6042     0x429f0005, /* bcl 20, 31, .Lxxx */
   6043     0x7d8802a6, /* mflr 12 */
   6044     0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
   6045     0x398c0000, /* addi 12, 12, (xxx-.Lxxx)@l */
   6046     0x7c0803a6, /* mtlr 0 */
   6047     0x7d8903a6, /* mtctr 12 */
   6048     0x4e800420, /* bctr */
   6049   };
   6050 
   6051 static const int stub_entry[] =
   6052   {
   6053     0x3d800000, /* lis 12,xxx@ha */
   6054     0x398c0000, /* addi 12,12,xxx@l */
   6055     0x7d8903a6, /* mtctr 12 */
   6056     0x4e800420, /* bctr */
   6057   };
   6058 
   6059 struct ppc_elf_relax_info
   6060 {
   6061   unsigned int workaround_size;
   6062   unsigned int picfixup_size;
   6063 };
   6064 
   6065 /* This function implements long branch trampolines, and the ppc476
   6066    icache bug workaround.  Any section needing trampolines or patch
   6067    space for the workaround has its size extended so that we can
   6068    add trampolines at the end of the section.  */
   6069 
   6070 static bool
   6071 ppc_elf_relax_section (bfd *abfd,
   6072 		       asection *isec,
   6073 		       struct bfd_link_info *link_info,
   6074 		       bool *again)
   6075 {
   6076   struct one_branch_fixup
   6077   {
   6078     struct one_branch_fixup *next;
   6079     asection *tsec;
   6080     /* Final link, can use the symbol offset.  For a
   6081        relocatable link we use the symbol's index.  */
   6082     bfd_vma toff;
   6083     bfd_vma trampoff;
   6084   };
   6085 
   6086   Elf_Internal_Shdr *symtab_hdr;
   6087   bfd_byte *contents = NULL;
   6088   Elf_Internal_Sym *isymbuf = NULL;
   6089   Elf_Internal_Rela *internal_relocs = NULL;
   6090   Elf_Internal_Rela *irel, *irelend = NULL;
   6091   struct one_branch_fixup *branch_fixups = NULL;
   6092   struct ppc_elf_relax_info *relax_info = NULL;
   6093   unsigned changes = 0;
   6094   bool workaround_change;
   6095   struct ppc_elf_link_hash_table *htab;
   6096   bfd_size_type trampbase, trampoff, newsize, picfixup_size;
   6097   asection *got2;
   6098   bool maybe_pasted;
   6099 
   6100   *again = false;
   6101 
   6102   /* No need to do anything with non-alloc or non-code sections.  */
   6103   if ((isec->flags & SEC_ALLOC) == 0
   6104       || (isec->flags & SEC_CODE) == 0
   6105       || (isec->flags & SEC_HAS_CONTENTS) == 0
   6106       || (isec->flags & SEC_LINKER_CREATED) != 0
   6107       || isec->size < 4)
   6108     return true;
   6109 
   6110   /* We cannot represent the required PIC relocs in the output, so don't
   6111      do anything.  The linker doesn't support mixing -shared and -r
   6112      anyway.  */
   6113   if (bfd_link_relocatable (link_info) && bfd_link_pic (link_info))
   6114     return true;
   6115 
   6116   htab = ppc_elf_hash_table (link_info);
   6117   if (htab == NULL)
   6118     return true;
   6119 
   6120   isec->size = (isec->size + 3) & -4;
   6121   if (isec->rawsize == 0)
   6122     isec->rawsize = isec->size;
   6123   trampbase = isec->size;
   6124 
   6125   BFD_ASSERT (isec->sec_info_type == SEC_INFO_TYPE_NONE
   6126 	      || isec->sec_info_type == SEC_INFO_TYPE_TARGET);
   6127   isec->sec_info_type = SEC_INFO_TYPE_TARGET;
   6128 
   6129   if (htab->params->ppc476_workaround
   6130       || htab->params->pic_fixup > 0)
   6131     {
   6132       if (isec->sec_info == NULL)
   6133 	{
   6134 	  isec->sec_info
   6135 	    = bfd_zalloc (abfd, sizeof (struct ppc_elf_relax_info));
   6136 	  if (isec->sec_info == NULL)
   6137 	    return false;
   6138 	}
   6139       relax_info = isec->sec_info;
   6140       trampbase -= relax_info->workaround_size;
   6141     }
   6142 
   6143   maybe_pasted = (strcmp (isec->output_section->name, ".init") == 0
   6144 		  || strcmp (isec->output_section->name, ".fini") == 0);
   6145   /* Space for a branch around any trampolines.  */
   6146   trampoff = trampbase;
   6147   if (maybe_pasted && trampbase == isec->rawsize)
   6148     trampoff += 4;
   6149 
   6150   symtab_hdr = &elf_symtab_hdr (abfd);
   6151   picfixup_size = 0;
   6152   if (htab->params->branch_trampolines
   6153       || htab->params->pic_fixup > 0)
   6154     {
   6155       /* Get a copy of the native relocations.  */
   6156       if (isec->reloc_count != 0)
   6157 	{
   6158 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
   6159 						       link_info->keep_memory);
   6160 	  if (internal_relocs == NULL)
   6161 	    goto error_return;
   6162 	}
   6163 
   6164       got2 = bfd_get_section_by_name (abfd, ".got2");
   6165 
   6166       irelend = internal_relocs + isec->reloc_count;
   6167       for (irel = internal_relocs; irel < irelend; irel++)
   6168 	{
   6169 	  unsigned long r_type = ELF32_R_TYPE (irel->r_info);
   6170 	  bfd_vma toff, roff;
   6171 	  asection *tsec;
   6172 	  struct one_branch_fixup *f;
   6173 	  size_t insn_offset = 0;
   6174 	  bfd_vma max_branch_offset = 0, val, reladdr;
   6175 	  bfd_byte *hit_addr;
   6176 	  unsigned long t0;
   6177 	  struct elf_link_hash_entry *h;
   6178 	  Elf_Internal_Sym *isym;
   6179 	  struct plt_entry **plist;
   6180 	  unsigned char sym_type;
   6181 
   6182 	  switch (r_type)
   6183 	    {
   6184 	    case R_PPC_REL24:
   6185 	    case R_PPC_LOCAL24PC:
   6186 	    case R_PPC_PLTREL24:
   6187 	    case R_PPC_PLTCALL:
   6188 	      max_branch_offset = 1 << 25;
   6189 	      break;
   6190 
   6191 	    case R_PPC_REL14:
   6192 	    case R_PPC_REL14_BRTAKEN:
   6193 	    case R_PPC_REL14_BRNTAKEN:
   6194 	      max_branch_offset = 1 << 15;
   6195 	      break;
   6196 
   6197 	    case R_PPC_ADDR16_HA:
   6198 	      if (htab->params->pic_fixup > 0)
   6199 		break;
   6200 	      continue;
   6201 
   6202 	    default:
   6203 	      continue;
   6204 	    }
   6205 
   6206 	  /* Get the value of the symbol referred to by the reloc.  */
   6207 	  if (!get_sym_h (&h, &isym, &tsec, NULL, &isymbuf,
   6208 			  ELF32_R_SYM (irel->r_info), abfd))
   6209 	    goto error_return;
   6210 
   6211 	  if (isym != NULL)
   6212 	    {
   6213 	      if (tsec != NULL)
   6214 		;
   6215 	      else if (isym->st_shndx == SHN_ABS)
   6216 		tsec = bfd_abs_section_ptr;
   6217 	      else
   6218 		continue;
   6219 
   6220 	      toff = isym->st_value;
   6221 	      sym_type = ELF_ST_TYPE (isym->st_info);
   6222 	    }
   6223 	  else
   6224 	    {
   6225 	      if (tsec != NULL)
   6226 		toff = h->root.u.def.value;
   6227 	      else if (h->root.type == bfd_link_hash_undefined
   6228 		       || h->root.type == bfd_link_hash_undefweak)
   6229 		{
   6230 		  unsigned long indx;
   6231 
   6232 		  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   6233 		  tsec = bfd_und_section_ptr;
   6234 		  toff = bfd_link_relocatable (link_info) ? indx : 0;
   6235 		}
   6236 	      else
   6237 		continue;
   6238 
   6239 	      /* If this branch is to __tls_get_addr then we may later
   6240 		 optimise away the call.  We won't be needing a long-
   6241 		 branch stub in that case.  */
   6242 	      if (bfd_link_executable (link_info)
   6243 		  && h == htab->tls_get_addr
   6244 		  && irel != internal_relocs)
   6245 		{
   6246 		  unsigned long t_symndx = ELF32_R_SYM (irel[-1].r_info);
   6247 		  unsigned long t_rtype = ELF32_R_TYPE (irel[-1].r_info);
   6248 		  unsigned int tls_mask = 0;
   6249 
   6250 		  /* The previous reloc should be one of R_PPC_TLSGD or
   6251 		     R_PPC_TLSLD, or for older object files, a reloc
   6252 		     on the __tls_get_addr arg setup insn.  Get tls
   6253 		     mask bits from the symbol on that reloc.  */
   6254 		  if (t_symndx < symtab_hdr->sh_info)
   6255 		    {
   6256 		      bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
   6257 
   6258 		      if (local_got_offsets != NULL)
   6259 			{
   6260 			  struct plt_entry **local_plt = (struct plt_entry **)
   6261 			    (local_got_offsets + symtab_hdr->sh_info);
   6262 			  char *lgot_masks = (char *)
   6263 			    (local_plt + symtab_hdr->sh_info);
   6264 			  tls_mask = lgot_masks[t_symndx];
   6265 			}
   6266 		    }
   6267 		  else
   6268 		    {
   6269 		      struct elf_link_hash_entry *th
   6270 			= elf_sym_hashes (abfd)[t_symndx - symtab_hdr->sh_info];
   6271 
   6272 		      while (th->root.type == bfd_link_hash_indirect
   6273 			     || th->root.type == bfd_link_hash_warning)
   6274 			th = (struct elf_link_hash_entry *) th->root.u.i.link;
   6275 
   6276 		      tls_mask
   6277 			= ((struct ppc_elf_link_hash_entry *) th)->tls_mask;
   6278 		    }
   6279 
   6280 		  /* The mask bits tell us if the call will be
   6281 		     optimised away.  */
   6282 		  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   6283 		      && (t_rtype == R_PPC_TLSGD
   6284 			  || t_rtype == R_PPC_GOT_TLSGD16
   6285 			  || t_rtype == R_PPC_GOT_TLSGD16_LO))
   6286 		    continue;
   6287 		  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   6288 		      && (t_rtype == R_PPC_TLSLD
   6289 			  || t_rtype == R_PPC_GOT_TLSLD16
   6290 			  || t_rtype == R_PPC_GOT_TLSLD16_LO))
   6291 		    continue;
   6292 		}
   6293 
   6294 	      sym_type = h->type;
   6295 	    }
   6296 
   6297 	  if (r_type == R_PPC_ADDR16_HA)
   6298 	    {
   6299 	      if (h != NULL
   6300 		  && !h->def_regular
   6301 		  && h->protected_def
   6302 		  && ppc_elf_hash_entry (h)->has_addr16_ha
   6303 		  && ppc_elf_hash_entry (h)->has_addr16_lo)
   6304 		picfixup_size += 12;
   6305 	      continue;
   6306 	    }
   6307 
   6308 	  /* The condition here under which we call find_plt_ent must
   6309 	     match that in relocate_section.  If we call find_plt_ent here
   6310 	     but not in relocate_section, or vice versa, then the branch
   6311 	     destination used here may be incorrect.  */
   6312 	  plist = NULL;
   6313 	  if (h != NULL)
   6314 	    {
   6315 	      /* We know is_branch_reloc (r_type) is true.  */
   6316 	      if (h->type == STT_GNU_IFUNC
   6317 		  || r_type == R_PPC_PLTREL24)
   6318 		plist = &h->plt.plist;
   6319 	    }
   6320 	  else if (sym_type == STT_GNU_IFUNC
   6321 		   && elf_local_got_offsets (abfd) != NULL)
   6322 	    {
   6323 	      bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
   6324 	      struct plt_entry **local_plt = (struct plt_entry **)
   6325 		(local_got_offsets + symtab_hdr->sh_info);
   6326 	      plist = local_plt + ELF32_R_SYM (irel->r_info);
   6327 	    }
   6328 	  if (plist != NULL)
   6329 	    {
   6330 	      bfd_vma addend = 0;
   6331 	      struct plt_entry *ent;
   6332 
   6333 	      if (r_type == R_PPC_PLTREL24 && bfd_link_pic (link_info))
   6334 		addend = irel->r_addend;
   6335 	      ent = find_plt_ent (plist, got2, addend);
   6336 	      if (ent != NULL)
   6337 		{
   6338 		  if (htab->plt_type == PLT_NEW
   6339 		      || h == NULL
   6340 		      || !htab->elf.dynamic_sections_created
   6341 		      || h->dynindx == -1)
   6342 		    {
   6343 		      tsec = htab->glink;
   6344 		      toff = ent->glink_offset;
   6345 		    }
   6346 		  else
   6347 		    {
   6348 		      tsec = htab->elf.splt;
   6349 		      toff = ent->plt.offset;
   6350 		    }
   6351 		}
   6352 	    }
   6353 
   6354 	  /* If the branch and target are in the same section, you have
   6355 	     no hope of adding stubs.  We'll error out later should the
   6356 	     branch overflow.  */
   6357 	  if (tsec == isec)
   6358 	    continue;
   6359 
   6360 	  /* toff is used for the symbol index when the symbol is
   6361 	     undefined and we're doing a relocatable link, so we can't
   6362 	     support addends.  It would be possible to do so by
   6363 	     putting the addend in one_branch_fixup but addends on
   6364 	     branches are rare so it hardly seems worth supporting.  */
   6365 	  if (bfd_link_relocatable (link_info)
   6366 	      && tsec == bfd_und_section_ptr
   6367 	      && r_type != R_PPC_PLTREL24
   6368 	      && irel->r_addend != 0)
   6369 	    continue;
   6370 
   6371 	  /* There probably isn't any reason to handle symbols in
   6372 	     SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
   6373 	     attribute for a code section, and we are only looking at
   6374 	     branches.  However, implement it correctly here as a
   6375 	     reference for other target relax_section functions.  */
   6376 	  if (0 && tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
   6377 	    {
   6378 	      /* At this stage in linking, no SEC_MERGE symbol has been
   6379 		 adjusted, so all references to such symbols need to be
   6380 		 passed through _bfd_merged_section_offset.  (Later, in
   6381 		 relocate_section, all SEC_MERGE symbols *except* for
   6382 		 section symbols have been adjusted.)
   6383 
   6384 		 gas may reduce relocations against symbols in SEC_MERGE
   6385 		 sections to a relocation against the section symbol when
   6386 		 the original addend was zero.  When the reloc is against
   6387 		 a section symbol we should include the addend in the
   6388 		 offset passed to _bfd_merged_section_offset, since the
   6389 		 location of interest is the original symbol.  On the
   6390 		 other hand, an access to "sym+addend" where "sym" is not
   6391 		 a section symbol should not include the addend;  Such an
   6392 		 access is presumed to be an offset from "sym";  The
   6393 		 location of interest is just "sym".  */
   6394 	      if (sym_type == STT_SECTION
   6395 		  && r_type != R_PPC_PLTREL24)
   6396 		toff += irel->r_addend;
   6397 
   6398 	      toff
   6399 		= _bfd_merged_section_offset (abfd, &tsec, toff);
   6400 
   6401 	      if (sym_type != STT_SECTION
   6402 		  && r_type != R_PPC_PLTREL24)
   6403 		toff += irel->r_addend;
   6404 	    }
   6405 	  /* PLTREL24 addends are special.  */
   6406 	  else if (r_type != R_PPC_PLTREL24)
   6407 	    toff += irel->r_addend;
   6408 
   6409 	  /* Attempted -shared link of non-pic code loses.  */
   6410 	  if ((!bfd_link_relocatable (link_info)
   6411 	       && tsec == bfd_und_section_ptr)
   6412 	      || tsec->output_section == NULL
   6413 	      || (tsec->owner != NULL
   6414 		  && (tsec->owner->flags & BFD_PLUGIN) != 0))
   6415 	    continue;
   6416 
   6417 	  roff = irel->r_offset;
   6418 	  reladdr = isec->output_section->vma + isec->output_offset + roff;
   6419 
   6420 	  /* Avoid creating a lot of unnecessary fixups when
   6421 	     relocatable if the output section size is such that a
   6422 	     fixup can be created at final link.
   6423 	     The max_branch_offset adjustment allows for some number
   6424 	     of other fixups being needed at final link.  */
   6425 	  if (bfd_link_relocatable (link_info)
   6426 	      && (isec->output_section->rawsize - (isec->output_offset + roff)
   6427 		  < max_branch_offset - (max_branch_offset >> 4)))
   6428 	    continue;
   6429 
   6430 	  /* If the branch is in range, no need to do anything.  */
   6431 	  if (tsec != bfd_und_section_ptr
   6432 	      && (!bfd_link_relocatable (link_info)
   6433 		  /* A relocatable link may have sections moved during
   6434 		     final link, so do not presume they remain in range.  */
   6435 		  || tsec->output_section == isec->output_section))
   6436 	    {
   6437 	      bfd_vma symaddr;
   6438 
   6439 	      symaddr = tsec->output_section->vma + tsec->output_offset + toff;
   6440 	      if (symaddr - reladdr + max_branch_offset
   6441 		  < 2 * max_branch_offset)
   6442 		continue;
   6443 	    }
   6444 
   6445 	  /* Look for an existing fixup to this address.  */
   6446 	  for (f = branch_fixups; f ; f = f->next)
   6447 	    if (f->tsec == tsec && f->toff == toff)
   6448 	      break;
   6449 
   6450 	  if (f == NULL)
   6451 	    {
   6452 	      size_t size;
   6453 	      unsigned long stub_rtype;
   6454 
   6455 	      val = trampoff - roff;
   6456 	      if (val >= max_branch_offset)
   6457 		/* Oh dear, we can't reach a trampoline.  Don't try to add
   6458 		   one.  We'll report an error later.  */
   6459 		continue;
   6460 
   6461 	      if (bfd_link_pic (link_info))
   6462 		{
   6463 		  size = 4 * ARRAY_SIZE (shared_stub_entry);
   6464 		  insn_offset = 12;
   6465 		}
   6466 	      else
   6467 		{
   6468 		  size = 4 * ARRAY_SIZE (stub_entry);
   6469 		  insn_offset = 0;
   6470 		}
   6471 	      stub_rtype = R_PPC_RELAX;
   6472 	      if (tsec == htab->elf.splt
   6473 		  || tsec == htab->glink)
   6474 		{
   6475 		  stub_rtype = R_PPC_RELAX_PLT;
   6476 		  if (r_type == R_PPC_PLTREL24)
   6477 		    stub_rtype = R_PPC_RELAX_PLTREL24;
   6478 		}
   6479 
   6480 	      /* Hijack the old relocation.  Since we need two
   6481 		 relocations for this use a "composite" reloc.  */
   6482 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   6483 					   stub_rtype);
   6484 	      irel->r_offset = trampoff + insn_offset;
   6485 	      if (r_type == R_PPC_PLTREL24
   6486 		  && stub_rtype != R_PPC_RELAX_PLTREL24)
   6487 		irel->r_addend = 0;
   6488 
   6489 	      /* Record the fixup so we don't do it again this section.  */
   6490 	      f = bfd_malloc (sizeof (*f));
   6491 	      f->next = branch_fixups;
   6492 	      f->tsec = tsec;
   6493 	      f->toff = toff;
   6494 	      f->trampoff = trampoff;
   6495 	      branch_fixups = f;
   6496 
   6497 	      trampoff += size;
   6498 	      changes++;
   6499 	    }
   6500 	  else
   6501 	    {
   6502 	      val = f->trampoff - roff;
   6503 	      if (val >= max_branch_offset)
   6504 		continue;
   6505 
   6506 	      /* Nop out the reloc, since we're finalizing things here.  */
   6507 	      irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
   6508 	    }
   6509 
   6510 	  link_info->callbacks->minfo
   6511 	    (_("%pB: Adjusting branch at 0x%V towards \"%s\" in section %s\n"),
   6512 	     abfd, reladdr,
   6513 	     (h && h->root.root.string? h->root.root.string : "<unknown>"),
   6514 	     f->tsec->name);
   6515 
   6516 	  /* Get the section contents.  */
   6517 	  if (contents == NULL)
   6518 	    {
   6519 	      /* Get cached copy if it exists.  */
   6520 	      if (elf_section_data (isec)->this_hdr.contents != NULL)
   6521 		contents = elf_section_data (isec)->this_hdr.contents;
   6522 	      /* Go get them off disk.  */
   6523 	      else if (!bfd_malloc_and_get_section (abfd, isec, &contents))
   6524 		goto error_return;
   6525 	    }
   6526 
   6527 	  /* Fix up the existing branch to hit the trampoline.  */
   6528 	  hit_addr = contents + roff;
   6529 	  switch (r_type)
   6530 	    {
   6531 	    case R_PPC_REL24:
   6532 	    case R_PPC_LOCAL24PC:
   6533 	    case R_PPC_PLTREL24:
   6534 	      t0 = bfd_get_32 (abfd, hit_addr);
   6535 	      t0 &= ~0x3fffffc;
   6536 	      t0 |= val & 0x3fffffc;
   6537 	      bfd_put_32 (abfd, t0, hit_addr);
   6538 	      break;
   6539 
   6540 	    case R_PPC_REL14:
   6541 	    case R_PPC_REL14_BRTAKEN:
   6542 	    case R_PPC_REL14_BRNTAKEN:
   6543 	      t0 = bfd_get_32 (abfd, hit_addr);
   6544 	      t0 &= ~0xfffc;
   6545 	      t0 |= val & 0xfffc;
   6546 	      bfd_put_32 (abfd, t0, hit_addr);
   6547 	      break;
   6548 	    }
   6549 	}
   6550 
   6551       while (branch_fixups != NULL)
   6552 	{
   6553 	  struct one_branch_fixup *f = branch_fixups;
   6554 	  branch_fixups = branch_fixups->next;
   6555 	  free (f);
   6556 	}
   6557     }
   6558 
   6559   workaround_change = false;
   6560   newsize = trampoff;
   6561   if (htab->params->ppc476_workaround
   6562       && (!bfd_link_relocatable (link_info)
   6563 	  || isec->output_section->alignment_power >= htab->params->pagesize_p2))
   6564     {
   6565       bfd_vma addr, end_addr;
   6566       unsigned int crossings;
   6567       bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
   6568 
   6569       addr = isec->output_section->vma + isec->output_offset;
   6570       end_addr = addr + trampoff;
   6571       addr &= -pagesize;
   6572       crossings = ((end_addr & -pagesize) - addr) >> htab->params->pagesize_p2;
   6573       if (crossings != 0)
   6574 	{
   6575 	  /* Keep space aligned, to ensure the patch code itself does
   6576 	     not cross a page.  Don't decrease size calculated on a
   6577 	     previous pass as otherwise we might never settle on a layout.  */
   6578 	  newsize = 15 - ((end_addr - 1) & 15);
   6579 	  newsize += crossings * 16;
   6580 	  if (relax_info->workaround_size < newsize)
   6581 	    {
   6582 	      relax_info->workaround_size = newsize;
   6583 	      workaround_change = true;
   6584 	    }
   6585 	  /* Ensure relocate_section is called.  */
   6586 	  isec->flags |= SEC_RELOC;
   6587 	}
   6588       newsize = trampoff + relax_info->workaround_size;
   6589     }
   6590 
   6591   if (htab->params->pic_fixup > 0)
   6592     {
   6593       picfixup_size -= relax_info->picfixup_size;
   6594       if (picfixup_size != 0)
   6595 	relax_info->picfixup_size += picfixup_size;
   6596       newsize += relax_info->picfixup_size;
   6597     }
   6598 
   6599   if (changes != 0 || picfixup_size != 0 || workaround_change)
   6600     isec->size = newsize;
   6601 
   6602   if (isymbuf != NULL
   6603       && symtab_hdr->contents != (unsigned char *) isymbuf)
   6604     {
   6605       if (! link_info->keep_memory)
   6606 	free (isymbuf);
   6607       else
   6608 	{
   6609 	  /* Cache the symbols for elf_link_input_bfd.  */
   6610 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   6611 	}
   6612     }
   6613 
   6614   if (contents != NULL
   6615       && elf_section_data (isec)->this_hdr.contents != contents)
   6616     {
   6617       if (!changes && !link_info->keep_memory)
   6618 	free (contents);
   6619       else
   6620 	{
   6621 	  /* Cache the section contents for elf_link_input_bfd.  */
   6622 	  elf_section_data (isec)->this_hdr.contents = contents;
   6623 	}
   6624     }
   6625 
   6626   changes += picfixup_size;
   6627   if (changes != 0)
   6628     {
   6629       /* Append sufficient NOP relocs so we can write out relocation
   6630 	 information for the trampolines.  */
   6631       size_t old_size = isec->reloc_count * sizeof (*internal_relocs);
   6632       size_t extra_size = changes * sizeof (*internal_relocs);
   6633       internal_relocs = bfd_realloc (internal_relocs, old_size + extra_size);
   6634       elf_section_data (isec)->relocs = internal_relocs;
   6635       if (!internal_relocs)
   6636 	goto error_return;
   6637       memset ((char *) internal_relocs + old_size, 0, extra_size);
   6638       isec->reloc_count += changes;
   6639       Elf_Internal_Shdr *rel_hdr = _bfd_elf_single_rel_hdr (isec);
   6640       rel_hdr->sh_size += changes * rel_hdr->sh_entsize;
   6641     }
   6642   else if (elf_section_data (isec)->relocs != internal_relocs)
   6643     free (internal_relocs);
   6644 
   6645   *again = changes != 0 || workaround_change;
   6646   return true;
   6647 
   6648  error_return:
   6649   while (branch_fixups != NULL)
   6650     {
   6651       struct one_branch_fixup *f = branch_fixups;
   6652       branch_fixups = branch_fixups->next;
   6653       free (f);
   6654     }
   6655   if ((unsigned char *) isymbuf != symtab_hdr->contents)
   6656     free (isymbuf);
   6657   if (elf_section_data (isec)->this_hdr.contents != contents)
   6658     free (contents);
   6659   if (elf_section_data (isec)->relocs != internal_relocs)
   6660     free (internal_relocs);
   6661   return false;
   6662 }
   6663 
   6664 /* What to do when ld finds relocations against symbols defined in
   6666    discarded sections.  */
   6667 
   6668 static unsigned int
   6669 ppc_elf_action_discarded (asection *sec)
   6670 {
   6671   if (strcmp (".fixup", sec->name) == 0)
   6672     return 0;
   6673 
   6674   if (strcmp (".got2", sec->name) == 0)
   6675     return 0;
   6676 
   6677   return _bfd_elf_default_action_discarded (sec);
   6678 }
   6679 
   6680 /* Fill in the address for a pointer generated in a linker section.  */
   6682 
   6683 static bfd_vma
   6684 elf_finish_pointer_linker_section (bfd *input_bfd,
   6685 				   elf_linker_section_t *lsect,
   6686 				   struct elf_link_hash_entry *h,
   6687 				   bfd_vma relocation,
   6688 				   const Elf_Internal_Rela *rel)
   6689 {
   6690   elf_linker_section_pointers_t *linker_section_ptr;
   6691 
   6692   BFD_ASSERT (lsect != NULL);
   6693 
   6694   if (h != NULL)
   6695     {
   6696       /* Handle global symbol.  */
   6697       struct ppc_elf_link_hash_entry *eh;
   6698 
   6699       eh = (struct ppc_elf_link_hash_entry *) h;
   6700       BFD_ASSERT (eh->elf.def_regular);
   6701       linker_section_ptr = eh->linker_section_pointer;
   6702     }
   6703   else
   6704     {
   6705       /* Handle local symbol.  */
   6706       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
   6707 
   6708       BFD_ASSERT (is_ppc_elf (input_bfd));
   6709       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
   6710       linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
   6711     }
   6712 
   6713   linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
   6714 							rel->r_addend,
   6715 							lsect);
   6716   BFD_ASSERT (linker_section_ptr != NULL);
   6717 
   6718   /* Offset will always be a multiple of four, so use the bottom bit
   6719      as a "written" flag.  */
   6720   if ((linker_section_ptr->offset & 1) == 0)
   6721     {
   6722       bfd_put_32 (lsect->section->owner,
   6723 		  relocation + linker_section_ptr->addend,
   6724 		  lsect->section->contents + linker_section_ptr->offset);
   6725       linker_section_ptr->offset += 1;
   6726     }
   6727 
   6728   relocation = (lsect->section->output_section->vma
   6729 		+ lsect->section->output_offset
   6730 		+ linker_section_ptr->offset - 1
   6731 		- SYM_VAL (lsect->sym));
   6732 
   6733 #ifdef DEBUG
   6734   fprintf (stderr,
   6735 	   "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
   6736 	   lsect->name, (long) relocation, (long) relocation);
   6737 #endif
   6738 
   6739   return relocation;
   6740 }
   6741 
   6742 #define PPC_LO(v) ((v) & 0xffff)
   6743 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   6744 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   6745 
   6746 static void
   6747 write_glink_stub (struct elf_link_hash_entry *h, struct plt_entry *ent,
   6748 		  asection *plt_sec, unsigned char *p,
   6749 		  struct bfd_link_info *info)
   6750 {
   6751   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   6752   bfd *output_bfd = info->output_bfd;
   6753   bfd_vma plt;
   6754   unsigned char *end = p + GLINK_ENTRY_SIZE (htab, h);
   6755 
   6756   if (h != NULL
   6757       && h == htab->tls_get_addr
   6758       && !htab->params->no_tls_get_addr_opt)
   6759     {
   6760       bfd_put_32 (output_bfd, LWZ_11_3, p);
   6761       p += 4;
   6762       bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
   6763       p += 4;
   6764       bfd_put_32 (output_bfd, MR_0_3, p);
   6765       p += 4;
   6766       bfd_put_32 (output_bfd, CMPWI_11_0, p);
   6767       p += 4;
   6768       bfd_put_32 (output_bfd, ADD_3_12_2, p);
   6769       p += 4;
   6770       bfd_put_32 (output_bfd, BEQLR, p);
   6771       p += 4;
   6772       bfd_put_32 (output_bfd, MR_3_0, p);
   6773       p += 4;
   6774       bfd_put_32 (output_bfd, NOP, p);
   6775       p += 4;
   6776     }
   6777 
   6778   plt = ((ent->plt.offset & ~1)
   6779 	 + plt_sec->output_section->vma
   6780 	 + plt_sec->output_offset);
   6781 
   6782   if (bfd_link_pic (info))
   6783     {
   6784       bfd_vma got = 0;
   6785 
   6786       if (ent->addend >= 32768)
   6787 	got = (ent->addend
   6788 	       + ent->sec->output_section->vma
   6789 	       + ent->sec->output_offset);
   6790       else if (htab->elf.hgot != NULL)
   6791 	got = SYM_VAL (htab->elf.hgot);
   6792 
   6793       plt -= got;
   6794 
   6795       if (plt + 0x8000 < 0x10000)
   6796 	bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
   6797       else
   6798 	{
   6799 	  bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
   6800 	  p += 4;
   6801 	  bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
   6802 	}
   6803     }
   6804   else
   6805     {
   6806       bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
   6807       p += 4;
   6808       bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
   6809     }
   6810   p += 4;
   6811   bfd_put_32 (output_bfd, MTCTR_11, p);
   6812   p += 4;
   6813   bfd_put_32 (output_bfd, BCTR, p);
   6814   p += 4;
   6815   while (p < end)
   6816     {
   6817       bfd_put_32 (output_bfd, htab->params->ppc476_workaround ? BA : NOP, p);
   6818       p += 4;
   6819     }
   6820 }
   6821 
   6822 /* Return true if symbol is defined statically.  */
   6823 
   6824 static bool
   6825 is_static_defined (struct elf_link_hash_entry *h)
   6826 {
   6827   return ((h->root.type == bfd_link_hash_defined
   6828 	   || h->root.type == bfd_link_hash_defweak)
   6829 	  && h->root.u.def.section != NULL
   6830 	  && h->root.u.def.section->output_section != NULL);
   6831 }
   6832 
   6833 /* If INSN is an opcode that may be used with an @tls operand, return
   6834    the transformed insn for TLS optimisation, otherwise return 0.  If
   6835    REG is non-zero only match an insn with RB or RA equal to REG.  */
   6836 
   6837 unsigned int
   6838 bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
   6839 {
   6840   unsigned int rtra;
   6841 
   6842   if ((insn & (0x3fu << 26)) != 31 << 26)
   6843     return 0;
   6844 
   6845   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
   6846     rtra = insn & ((1 << 26) - (1 << 16));
   6847   else if (((insn >> 16) & 0x1f) == reg)
   6848     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
   6849   else
   6850     return 0;
   6851 
   6852   if ((insn & (0x3ff << 1)) == 266 << 1)
   6853     /* add -> addi.  */
   6854     insn = 14 << 26;
   6855   else if ((insn & (0x1f << 1)) == 23 << 1
   6856 	   && ((insn & (0x1f << 6)) < 14 << 6
   6857 	       || ((insn & (0x1f << 6)) >= 16 << 6
   6858 		   && (insn & (0x1f << 6)) < 24 << 6)))
   6859     /* load and store indexed -> dform.  */
   6860     insn = (32u | ((insn >> 6) & 0x1f)) << 26;
   6861   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
   6862     /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
   6863     insn = ((58u | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
   6864   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
   6865     /* lwax -> lwa.  */
   6866     insn = (58u << 26) | 2;
   6867   else
   6868     return 0;
   6869   insn |= rtra;
   6870   return insn;
   6871 }
   6872 
   6873 /* If INSN is an opcode that may be used with an @tprel operand, return
   6874    the transformed insn for an undefined weak symbol, ie. with the
   6875    thread pointer REG operand removed.  Otherwise return 0.  */
   6876 
   6877 unsigned int
   6878 _bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
   6879 {
   6880   if ((insn & (0x1f << 16)) == reg << 16
   6881       && ((insn & (0x3fu << 26)) == 14u << 26 /* addi */
   6882 	  || (insn & (0x3fu << 26)) == 15u << 26 /* addis */
   6883 	  || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
   6884 	  || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
   6885 	  || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
   6886 	  || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
   6887 	  || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
   6888 	  || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
   6889 	  || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
   6890 	  || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
   6891 	  || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
   6892 	  || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
   6893 	  || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
   6894 	  || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
   6895 	  || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
   6896 	  || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
   6897 	      && (insn & 3) != 1)
   6898 	  || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
   6899 	      && ((insn & 3) == 0 || (insn & 3) == 3))))
   6900     {
   6901       insn &= ~(0x1f << 16);
   6902     }
   6903   else if ((insn & (0x1f << 21)) == reg << 21
   6904 	   && ((insn & (0x3eu << 26)) == 24u << 26 /* ori, oris */
   6905 	       || (insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */
   6906 	       || (insn & (0x3eu << 26)) == 28u << 26 /* andi,andis */))
   6907     {
   6908       insn &= ~(0x1f << 21);
   6909       insn |= (insn & (0x1f << 16)) << 5;
   6910       if ((insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */)
   6911 	insn -= 2 >> 26;  /* convert to ori,oris */
   6912     }
   6913   else
   6914     insn = 0;
   6915   return insn;
   6916 }
   6917 
   6918 static bool
   6919 is_insn_ds_form (unsigned int insn)
   6920 {
   6921   return ((insn & (0x3fu << 26)) == 58u << 26 /* ld,ldu,lwa */
   6922 	  || (insn & (0x3fu << 26)) == 62u << 26 /* std,stdu,stq */
   6923 	  || (insn & (0x3fu << 26)) == 57u << 26 /* lfdp */
   6924 	  || (insn & (0x3fu << 26)) == 61u << 26 /* stfdp */);
   6925 }
   6926 
   6927 static bool
   6928 is_insn_dq_form (unsigned int insn)
   6929 {
   6930   return ((insn & (0x3fu << 26)) == 56u << 26 /* lq */
   6931 	  || ((insn & (0x3fu << 26)) == (61u << 26) /* lxv, stxv */
   6932 	      && (insn & 3) == 1));
   6933 }
   6934 
   6935 static bool
   6936 swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
   6937 {
   6938   if ((size_t) (loc - s->contents) >= s->size)
   6939     return false;
   6940   bfd_elf32_swap_reloca_out (obfd, rel, loc);
   6941   return true;
   6942 }
   6943 
   6944 static bool
   6945 count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
   6946 {
   6947   bfd_byte *loc = s->contents;
   6948   loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
   6949   return swap_reloc_out (obfd, rel, loc, s);
   6950 }
   6951 
   6952 /* The RELOCATE_SECTION function is called by the ELF backend linker
   6953    to handle the relocations for a section.
   6954 
   6955    The relocs are always passed as Rela structures; if the section
   6956    actually uses Rel structures, the r_addend field will always be
   6957    zero.
   6958 
   6959    This function is responsible for adjust the section contents as
   6960    necessary, and (if using Rela relocs and generating a
   6961    relocatable output file) adjusting the reloc addend as
   6962    necessary.
   6963 
   6964    This function does not have to worry about setting the reloc
   6965    address or the reloc symbol index.
   6966 
   6967    LOCAL_SYMS is a pointer to the swapped in local symbols.
   6968 
   6969    LOCAL_SECTIONS is an array giving the section in the input file
   6970    corresponding to the st_shndx field of each local symbol.
   6971 
   6972    The global hash table entry for the global symbols can be found
   6973    via elf_sym_hashes (input_bfd).
   6974 
   6975    When generating relocatable output, this function must handle
   6976    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   6977    going to be the section symbol corresponding to the output
   6978    section, which means that the addend must be adjusted
   6979    accordingly.  */
   6980 
   6981 static int
   6982 ppc_elf_relocate_section (bfd *output_bfd,
   6983 			  struct bfd_link_info *info,
   6984 			  bfd *input_bfd,
   6985 			  asection *input_section,
   6986 			  bfd_byte *contents,
   6987 			  Elf_Internal_Rela *relocs,
   6988 			  Elf_Internal_Sym *local_syms,
   6989 			  asection **local_sections)
   6990 {
   6991   Elf_Internal_Shdr *symtab_hdr;
   6992   struct elf_link_hash_entry **sym_hashes;
   6993   struct ppc_elf_link_hash_table *htab;
   6994   Elf_Internal_Rela *rel;
   6995   Elf_Internal_Rela *wrel;
   6996   Elf_Internal_Rela *relend;
   6997   Elf_Internal_Rela outrel;
   6998   asection *got2;
   6999   bfd_vma *local_got_offsets;
   7000   bool ret = true;
   7001   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
   7002   bool is_vxworks_tls;
   7003   unsigned int picfixup_size = 0;
   7004   struct ppc_elf_relax_info *relax_info = NULL;
   7005 
   7006 #ifdef DEBUG
   7007   _bfd_error_handler ("ppc_elf_relocate_section called for %pB section %pA, "
   7008 		      "%ld relocations%s",
   7009 		      input_bfd, input_section,
   7010 		      (long) input_section->reloc_count,
   7011 		      (bfd_link_relocatable (info)) ? " (relocatable)" : "");
   7012 #endif
   7013 
   7014   if (!is_ppc_elf (input_bfd))
   7015     {
   7016       bfd_set_error (bfd_error_wrong_format);
   7017       return false;
   7018     }
   7019 
   7020   got2 = bfd_get_section_by_name (input_bfd, ".got2");
   7021 
   7022   /* Initialize howto table if not already done.  */
   7023   if (!ppc_elf_howto_table[R_PPC_ADDR32])
   7024     ppc_elf_howto_init ();
   7025 
   7026   htab = ppc_elf_hash_table (info);
   7027   local_got_offsets = elf_local_got_offsets (input_bfd);
   7028   symtab_hdr = &elf_symtab_hdr (input_bfd);
   7029   sym_hashes = elf_sym_hashes (input_bfd);
   7030   /* We have to handle relocations in vxworks .tls_vars sections
   7031      specially, because the dynamic loader is 'weird'.  */
   7032   is_vxworks_tls = (htab->elf.target_os == is_vxworks && bfd_link_pic (info)
   7033 		    && !strcmp (input_section->output_section->name,
   7034 				".tls_vars"));
   7035   if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET)
   7036     relax_info = input_section->sec_info;
   7037   rel = wrel = relocs;
   7038   relend = relocs + input_section->reloc_count;
   7039   for (; rel < relend; wrel++, rel++)
   7040     {
   7041       enum elf_ppc_reloc_type r_type;
   7042       bfd_vma addend;
   7043       bfd_reloc_status_type r;
   7044       Elf_Internal_Sym *sym;
   7045       asection *sec;
   7046       struct elf_link_hash_entry *h;
   7047       const char *sym_name;
   7048       reloc_howto_type *howto;
   7049       unsigned long r_symndx;
   7050       bfd_vma relocation;
   7051       bfd_vma branch_bit, from;
   7052       bool unresolved_reloc, save_unresolved_reloc;
   7053       bool warned;
   7054       unsigned int tls_type, tls_mask, tls_gd;
   7055       struct plt_entry **ifunc, **plt_list;
   7056       struct reloc_howto_struct alt_howto;
   7057 
   7058     again:
   7059       r_type = ELF32_R_TYPE (rel->r_info);
   7060       sym = NULL;
   7061       sec = NULL;
   7062       h = NULL;
   7063       unresolved_reloc = false;
   7064       warned = false;
   7065       r_symndx = ELF32_R_SYM (rel->r_info);
   7066 
   7067       if (r_symndx < symtab_hdr->sh_info)
   7068 	{
   7069 	  sym = local_syms + r_symndx;
   7070 	  sec = local_sections[r_symndx];
   7071 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   7072 
   7073 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   7074 	}
   7075       else
   7076 	{
   7077 	  bool ignored;
   7078 
   7079 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   7080 				   r_symndx, symtab_hdr, sym_hashes,
   7081 				   h, sec, relocation,
   7082 				   unresolved_reloc, warned, ignored);
   7083 
   7084 	  sym_name = h->root.root.string;
   7085 	}
   7086 
   7087       if (sec != NULL && discarded_section (sec))
   7088 	{
   7089 	  /* For relocs against symbols from removed linkonce sections,
   7090 	     or sections discarded by a linker script, we just want the
   7091 	     section contents zeroed.  Avoid any special processing.  */
   7092 	  if (r_type < ARRAY_SIZE (ppc_elf_howto_table)
   7093 	      && ppc_elf_howto_table[r_type] != NULL)
   7094 	    _bfd_clear_contents (ppc_elf_howto_table[r_type],
   7095 				 input_bfd, input_section,
   7096 				 contents, rel->r_offset);
   7097 	  wrel->r_offset = rel->r_offset;
   7098 	  wrel->r_info = 0;
   7099 	  wrel->r_addend = 0;
   7100 
   7101 	  /* For ld -r, remove relocations in debug sections against
   7102 	     symbols defined in discarded sections.  Not done for
   7103 	     non-debug to preserve relocs in .eh_frame which the
   7104 	     eh_frame editing code expects to be present.  */
   7105 	  if (bfd_link_relocatable (info)
   7106 	      && (input_section->flags & SEC_DEBUGGING))
   7107 	    wrel--;
   7108 
   7109 	  continue;
   7110 	}
   7111 
   7112       if (bfd_link_relocatable (info))
   7113 	{
   7114 	  if (got2 != NULL
   7115 	      && r_type == R_PPC_PLTREL24
   7116 	      && rel->r_addend != 0)
   7117 	    {
   7118 	      /* R_PPC_PLTREL24 is rather special.  If non-zero, the
   7119 		 addend specifies the GOT pointer offset within .got2.  */
   7120 	      rel->r_addend += got2->output_offset;
   7121 	    }
   7122 	  if (r_type != R_PPC_RELAX_PLT
   7123 	      && r_type != R_PPC_RELAX_PLTREL24
   7124 	      && r_type != R_PPC_RELAX)
   7125 	    goto copy_reloc;
   7126 	}
   7127 
   7128       /* TLS optimizations.  Replace instruction sequences and relocs
   7129 	 based on information we collected in tls_optimize.  We edit
   7130 	 RELOCS so that --emit-relocs will output something sensible
   7131 	 for the final instruction stream.  */
   7132       tls_mask = 0;
   7133       tls_gd = 0;
   7134       if (h != NULL)
   7135 	tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
   7136       else if (local_got_offsets != NULL)
   7137 	{
   7138 	  struct plt_entry **local_plt;
   7139 	  char *lgot_masks;
   7140 	  local_plt
   7141 	    = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
   7142 	  lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
   7143 	  tls_mask = lgot_masks[r_symndx];
   7144 	}
   7145 
   7146       /* Ensure reloc mapping code below stays sane.  */
   7147       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
   7148 	  || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
   7149 	  || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
   7150 	  || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
   7151 	  || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
   7152 	  || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
   7153 	  || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
   7154 	  || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
   7155 	abort ();
   7156       switch (r_type)
   7157 	{
   7158 	default:
   7159 	  break;
   7160 
   7161 	case R_PPC_GOT_TPREL16:
   7162 	case R_PPC_GOT_TPREL16_LO:
   7163 	  if ((tls_mask & TLS_TLS) != 0
   7164 	      && (tls_mask & TLS_TPREL) == 0
   7165 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   7166 	    {
   7167 	      bfd_vma insn;
   7168 
   7169 	      insn = bfd_get_32 (input_bfd,
   7170 				 contents + rel->r_offset - d_offset);
   7171 	      insn &= 31 << 21;
   7172 	      insn |= 0x3c020000;	/* addis 0,2,0 */
   7173 	      bfd_put_32 (input_bfd, insn,
   7174 			  contents + rel->r_offset - d_offset);
   7175 	      r_type = R_PPC_TPREL16_HA;
   7176 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7177 	    }
   7178 	  break;
   7179 
   7180 	case R_PPC_TLS:
   7181 	  if ((tls_mask & TLS_TLS) != 0
   7182 	      && (tls_mask & TLS_TPREL) == 0
   7183 	      && offset_in_range (input_section, rel->r_offset, 4))
   7184 	    {
   7185 	      bfd_vma insn;
   7186 
   7187 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   7188 	      insn = bfd_elf_ppc_at_tls_transform (insn, 2);
   7189 	      if (insn == 0)
   7190 		abort ();
   7191 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   7192 	      r_type = R_PPC_TPREL16_LO;
   7193 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7194 
   7195 	      /* Was PPC_TLS which sits on insn boundary, now
   7196 		 PPC_TPREL16_LO which is at low-order half-word.  */
   7197 	      rel->r_offset += d_offset;
   7198 	    }
   7199 	  break;
   7200 
   7201 	case R_PPC_GOT_TLSGD16_HI:
   7202 	case R_PPC_GOT_TLSGD16_HA:
   7203 	  tls_gd = TLS_GDIE;
   7204 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   7205 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   7206 	    goto tls_gdld_hi;
   7207 	  break;
   7208 
   7209 	case R_PPC_GOT_TLSLD16_HI:
   7210 	case R_PPC_GOT_TLSLD16_HA:
   7211 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   7212 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   7213 	    {
   7214 	    tls_gdld_hi:
   7215 	      if ((tls_mask & tls_gd) != 0)
   7216 		r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
   7217 			  + R_PPC_GOT_TPREL16);
   7218 	      else
   7219 		{
   7220 		  rel->r_offset -= d_offset;
   7221 		  bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   7222 		  r_type = R_PPC_NONE;
   7223 		}
   7224 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7225 	    }
   7226 	  break;
   7227 
   7228 	case R_PPC_GOT_TLSGD16:
   7229 	case R_PPC_GOT_TLSGD16_LO:
   7230 	  tls_gd = TLS_GDIE;
   7231 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   7232 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   7233 	    goto tls_ldgd_opt;
   7234 	  break;
   7235 
   7236 	case R_PPC_GOT_TLSLD16:
   7237 	case R_PPC_GOT_TLSLD16_LO:
   7238 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   7239 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   7240 	    {
   7241 	      unsigned int insn1, insn2;
   7242 	      bfd_vma offset;
   7243 
   7244 	    tls_ldgd_opt:
   7245 	      offset = (bfd_vma) -1;
   7246 	      /* If not using the newer R_PPC_TLSGD/LD to mark
   7247 		 __tls_get_addr calls, we must trust that the call
   7248 		 stays with its arg setup insns, ie. that the next
   7249 		 reloc is the __tls_get_addr call associated with
   7250 		 the current reloc.  Edit both insns.  */
   7251 	      if (input_section->nomark_tls_get_addr
   7252 		  && rel + 1 < relend
   7253 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   7254 					      htab->tls_get_addr))
   7255 		offset = rel[1].r_offset;
   7256 	      /* We read the low GOT_TLS insn because we need to keep
   7257 		 the destination reg.  It may be something other than
   7258 		 the usual r3, and moved to r3 before the call by
   7259 		 intervening code.  */
   7260 	      insn1 = bfd_get_32 (input_bfd,
   7261 				  contents + rel->r_offset - d_offset);
   7262 	      if ((tls_mask & tls_gd) != 0)
   7263 		{
   7264 		  /* IE */
   7265 		  insn1 &= (0x1f << 21) | (0x1f << 16);
   7266 		  insn1 |= 32u << 26;	/* lwz */
   7267 		  if (offset != (bfd_vma) -1
   7268 		      && offset_in_range (input_section, offset, 4))
   7269 		    {
   7270 		      rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7271 		      insn2 = 0x7c631214;	/* add 3,3,2 */
   7272 		      bfd_put_32 (input_bfd, insn2, contents + offset);
   7273 		    }
   7274 		  r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
   7275 			    + R_PPC_GOT_TPREL16);
   7276 		  rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7277 		}
   7278 	      else
   7279 		{
   7280 		  /* LE */
   7281 		  insn1 &= 0x1f << 21;
   7282 		  insn1 |= 0x3c020000;	/* addis r,2,0 */
   7283 		  if (tls_gd == 0)
   7284 		    {
   7285 		      /* Was an LD reloc.  */
   7286 		      for (r_symndx = 0;
   7287 			   r_symndx < symtab_hdr->sh_info;
   7288 			   r_symndx++)
   7289 			if (local_sections[r_symndx] == sec)
   7290 			  break;
   7291 		      if (r_symndx >= symtab_hdr->sh_info)
   7292 			r_symndx = STN_UNDEF;
   7293 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   7294 		      if (r_symndx != STN_UNDEF)
   7295 			rel->r_addend -= (local_syms[r_symndx].st_value
   7296 					  + sec->output_offset
   7297 					  + sec->output_section->vma);
   7298 		    }
   7299 		  r_type = R_PPC_TPREL16_HA;
   7300 		  rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7301 		  if (offset != (bfd_vma) -1
   7302 		      && offset_in_range (input_section, offset, 4))
   7303 		    {
   7304 		      rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
   7305 		      rel[1].r_offset = offset + d_offset;
   7306 		      rel[1].r_addend = rel->r_addend;
   7307 		      insn2 = 0x38630000;	/* addi 3,3,0 */
   7308 		      bfd_put_32 (input_bfd, insn2, contents + offset);
   7309 		    }
   7310 		}
   7311 	      bfd_put_32 (input_bfd, insn1,
   7312 			  contents + rel->r_offset - d_offset);
   7313 	      if (tls_gd == 0)
   7314 		{
   7315 		  /* We changed the symbol on an LD reloc.  Start over
   7316 		     in order to get h, sym, sec etc. right.  */
   7317 		  goto again;
   7318 		}
   7319 	    }
   7320 	  break;
   7321 
   7322 	case R_PPC_TLSGD:
   7323 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   7324 	      && rel + 1 < relend
   7325 	      && offset_in_range (input_section, rel->r_offset, 4))
   7326 	    {
   7327 	      unsigned int insn2;
   7328 	      bfd_vma offset = rel->r_offset;
   7329 
   7330 	      if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
   7331 		{
   7332 		  bfd_put_32 (input_bfd, NOP, contents + offset);
   7333 		  rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7334 		  break;
   7335 		}
   7336 
   7337 	      if ((tls_mask & TLS_GDIE) != 0)
   7338 		{
   7339 		  /* IE */
   7340 		  r_type = R_PPC_NONE;
   7341 		  insn2 = 0x7c631214;	/* add 3,3,2 */
   7342 		}
   7343 	      else
   7344 		{
   7345 		  /* LE */
   7346 		  r_type = R_PPC_TPREL16_LO;
   7347 		  rel->r_offset += d_offset;
   7348 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   7349 		}
   7350 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7351 	      bfd_put_32 (input_bfd, insn2, contents + offset);
   7352 	      /* Zap the reloc on the _tls_get_addr call too.  */
   7353 	      BFD_ASSERT (offset == rel[1].r_offset);
   7354 	      rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7355 	    }
   7356 	  break;
   7357 
   7358 	case R_PPC_TLSLD:
   7359 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   7360 	      && rel + 1 < relend
   7361 	      && offset_in_range (input_section, rel->r_offset, 4))
   7362 	    {
   7363 	      unsigned int insn2;
   7364 
   7365 	      if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
   7366 		{
   7367 		  bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   7368 		  rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7369 		  break;
   7370 		}
   7371 
   7372 	      for (r_symndx = 0;
   7373 		   r_symndx < symtab_hdr->sh_info;
   7374 		   r_symndx++)
   7375 		if (local_sections[r_symndx] == sec)
   7376 		  break;
   7377 	      if (r_symndx >= symtab_hdr->sh_info)
   7378 		r_symndx = STN_UNDEF;
   7379 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   7380 	      if (r_symndx != STN_UNDEF)
   7381 		rel->r_addend -= (local_syms[r_symndx].st_value
   7382 				  + sec->output_offset
   7383 				  + sec->output_section->vma);
   7384 
   7385 	      rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
   7386 	      rel->r_offset += d_offset;
   7387 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   7388 	      bfd_put_32 (input_bfd, insn2,
   7389 			  contents + rel->r_offset - d_offset);
   7390 	      /* Zap the reloc on the _tls_get_addr call too.  */
   7391 	      BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
   7392 	      rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7393 	      goto again;
   7394 	    }
   7395 	  break;
   7396 	}
   7397 
   7398       /* Handle other relocations that tweak non-addend part of insn.  */
   7399       branch_bit = 0;
   7400       switch (r_type)
   7401 	{
   7402 	default:
   7403 	  break;
   7404 
   7405 	  /* Branch taken prediction relocations.  */
   7406 	case R_PPC_ADDR14_BRTAKEN:
   7407 	case R_PPC_REL14_BRTAKEN:
   7408 	  branch_bit = BRANCH_PREDICT_BIT;
   7409 	  /* Fall through.  */
   7410 
   7411 	  /* Branch not taken prediction relocations.  */
   7412 	case R_PPC_ADDR14_BRNTAKEN:
   7413 	case R_PPC_REL14_BRNTAKEN:
   7414 	  if (offset_in_range (input_section, rel->r_offset, 4))
   7415 	    {
   7416 	      unsigned int insn;
   7417 
   7418 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   7419 	      insn &= ~BRANCH_PREDICT_BIT;
   7420 	      insn |= branch_bit;
   7421 
   7422 	      from = (rel->r_offset
   7423 		      + input_section->output_offset
   7424 		      + input_section->output_section->vma);
   7425 
   7426 	      /* Invert 'y' bit if not the default.  */
   7427 	      if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
   7428 		insn ^= BRANCH_PREDICT_BIT;
   7429 
   7430 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   7431 	    }
   7432 	  break;
   7433 
   7434 	case R_PPC_PLT16_HA:
   7435 	  if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
   7436 	    {
   7437 	      unsigned int insn;
   7438 
   7439 	      insn = bfd_get_32 (input_bfd,
   7440 				 contents + rel->r_offset - d_offset);
   7441 	      if ((insn & (0x3fu << 26)) == 15u << 26
   7442 		  && (insn & (0x1f << 16)) != 0)
   7443 		{
   7444 		  if (!bfd_link_pic (info))
   7445 		    {
   7446 		      /* Convert addis to lis.  */
   7447 		      insn &= ~(0x1f << 16);
   7448 		      bfd_put_32 (input_bfd, insn,
   7449 				  contents + rel->r_offset - d_offset);
   7450 		    }
   7451 		}
   7452 	      else if (bfd_link_pic (info))
   7453 		info->callbacks->einfo
   7454 		  (_("%P: %H: error: %s with unexpected instruction %x\n"),
   7455 		   input_bfd, input_section, rel->r_offset,
   7456 		   "R_PPC_PLT16_HA", insn);
   7457 	    }
   7458 	  break;
   7459 	}
   7460 
   7461       if (ELIMINATE_COPY_RELOCS
   7462 	  && h != NULL
   7463 	  && !h->def_regular
   7464 	  && h->protected_def
   7465 	  && ppc_elf_hash_entry (h)->has_addr16_ha
   7466 	  && ppc_elf_hash_entry (h)->has_addr16_lo
   7467 	  && htab->params->pic_fixup > 0)
   7468 	{
   7469 	  /* Convert lis;addi or lis;load/store accessing a protected
   7470 	     variable defined in a shared library to PIC.  */
   7471 	  unsigned int insn;
   7472 
   7473 	  if (r_type == R_PPC_ADDR16_HA
   7474 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   7475 	    {
   7476 	      insn = bfd_get_32 (input_bfd,
   7477 				 contents + rel->r_offset - d_offset);
   7478 	      if ((insn & (0x3fu << 26)) == (15u << 26)
   7479 		  && (insn & (0x1f << 16)) == 0 /* lis */)
   7480 		{
   7481 		  bfd_byte *p;
   7482 		  bfd_vma off;
   7483 		  bfd_vma got_addr;
   7484 
   7485 		  p = (contents + input_section->size
   7486 		       - relax_info->workaround_size
   7487 		       - relax_info->picfixup_size
   7488 		       + picfixup_size);
   7489 		  off = (p - contents) - (rel->r_offset - d_offset);
   7490 		  if (off > 0x1fffffc || (off & 3) != 0)
   7491 		    info->callbacks->einfo
   7492 		      (_("%H: fixup branch overflow\n"),
   7493 		       input_bfd, input_section, rel->r_offset);
   7494 
   7495 		  bfd_put_32 (input_bfd, B | off,
   7496 			      contents + rel->r_offset - d_offset);
   7497 		  got_addr = (htab->elf.sgot->output_section->vma
   7498 			      + htab->elf.sgot->output_offset
   7499 			      + (h->got.offset & ~1));
   7500 		  wrel->r_offset = (p - contents) + d_offset;
   7501 		  wrel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_HA);
   7502 		  wrel->r_addend = got_addr;
   7503 		  insn &= ~0xffff;
   7504 		  insn |= ((unsigned int) (got_addr + 0x8000) >> 16) & 0xffff;
   7505 		  bfd_put_32 (input_bfd, insn, p);
   7506 
   7507 		  /* Convert lis to lwz, loading address from GOT.  */
   7508 		  insn &= ~0xffff;
   7509 		  insn ^= (32u ^ 15u) << 26;
   7510 		  insn |= (insn & (0x1f << 21)) >> 5;
   7511 		  insn |= got_addr & 0xffff;
   7512 		  bfd_put_32 (input_bfd, insn, p + 4);
   7513 
   7514 		  bfd_put_32 (input_bfd, B | ((-4 - off) & 0x3ffffff), p + 8);
   7515 		  picfixup_size += 12;
   7516 
   7517 		  /* Use one of the spare relocs, so --emit-relocs
   7518 		     output is reasonable.  */
   7519 		  memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
   7520 		  wrel++, rel++;
   7521 		  rel->r_offset = wrel[-1].r_offset + 4;
   7522 		  rel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_LO);
   7523 		  rel->r_addend = wrel[-1].r_addend;
   7524 
   7525 		  /* Continue on as if we had a got reloc, to output
   7526 		     dynamic reloc.  */
   7527 		  r_type = R_PPC_GOT16_LO;
   7528 		}
   7529 	      else
   7530 		_bfd_error_handler
   7531 		  /* xgettext:c-format */
   7532 		  (_("%pB(%pA+%#" PRIx64 "): error: "
   7533 		     "%s with unexpected instruction %#x"),
   7534 		   input_bfd, input_section, (uint64_t) rel->r_offset,
   7535 		   "R_PPC_ADDR16_HA", insn);
   7536 	    }
   7537 	  else if (r_type == R_PPC_ADDR16_LO
   7538 		   && offset_in_range (input_section,
   7539 				       rel->r_offset - d_offset, 4))
   7540 	    {
   7541 	      insn = bfd_get_32 (input_bfd,
   7542 				 contents + rel->r_offset - d_offset);
   7543 	      if ((insn & (0x3fu << 26)) == 14u << 26    /* addi */
   7544 		  || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
   7545 		  || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
   7546 		  || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
   7547 		  || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
   7548 		  || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
   7549 		  || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
   7550 		  || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
   7551 		  || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
   7552 		  || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
   7553 		  || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
   7554 		  || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
   7555 		  || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
   7556 		  || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
   7557 		  || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
   7558 		      && (insn & 3) != 1)
   7559 		  || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
   7560 		      && ((insn & 3) == 0 || (insn & 3) == 3)))
   7561 		{
   7562 		  /* Arrange to apply the reloc addend, if any.  */
   7563 		  relocation = 0;
   7564 		  unresolved_reloc = false;
   7565 		  rel->r_info = ELF32_R_INFO (0, r_type);
   7566 		}
   7567 	      else
   7568 		_bfd_error_handler
   7569 		  /* xgettext:c-format */
   7570 		  (_("%pB(%pA+%#" PRIx64 "): error: "
   7571 		     "%s with unexpected instruction %#x"),
   7572 		   input_bfd, input_section, (uint64_t) rel->r_offset,
   7573 		   "R_PPC_ADDR16_LO", insn);
   7574 	    }
   7575 	}
   7576 
   7577       ifunc = NULL;
   7578       if (htab->elf.target_os != is_vxworks)
   7579 	{
   7580 	  struct plt_entry *ent;
   7581 
   7582 	  if (h != NULL)
   7583 	    {
   7584 	      if (h->type == STT_GNU_IFUNC)
   7585 		ifunc = &h->plt.plist;
   7586 	    }
   7587 	  else if (local_got_offsets != NULL
   7588 		   && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   7589 	    {
   7590 	      struct plt_entry **local_plt;
   7591 
   7592 	      local_plt = (struct plt_entry **) (local_got_offsets
   7593 						 + symtab_hdr->sh_info);
   7594 	      ifunc = local_plt + r_symndx;
   7595 	    }
   7596 
   7597 	  ent = NULL;
   7598 	  if (ifunc != NULL
   7599 	      && (!bfd_link_pic (info)
   7600 		  || is_branch_reloc (r_type)
   7601 		  || r_type == R_PPC_PLT16_LO
   7602 		  || r_type == R_PPC_PLT16_HI
   7603 		  || r_type == R_PPC_PLT16_HA))
   7604 	    {
   7605 	      addend = 0;
   7606 	      if (bfd_link_pic (info)
   7607 		  && (r_type == R_PPC_PLTREL24
   7608 		      || r_type == R_PPC_PLT16_LO
   7609 		      || r_type == R_PPC_PLT16_HI
   7610 		      || r_type == R_PPC_PLT16_HA))
   7611 		addend = rel->r_addend;
   7612 	      ent = find_plt_ent (ifunc, got2, addend);
   7613 	    }
   7614 	  if (ent != NULL)
   7615 	    {
   7616 	      if (bfd_link_pic (info)
   7617 		  && ent->sec != got2
   7618 		  && htab->plt_type != PLT_NEW
   7619 		  && (!htab->elf.dynamic_sections_created
   7620 		      || h == NULL
   7621 		      || h->dynindx == -1))
   7622 		{
   7623 		  /* Uh oh, we are going to create a pic glink stub
   7624 		     for an ifunc (here for h == NULL and later in
   7625 		     finish_dynamic_symbol for h != NULL), and
   7626 		     apparently are using code compiled with
   7627 		     -mbss-plt.  The difficulty is that -mbss-plt code
   7628 		     gives no indication via a magic PLTREL24 addend
   7629 		     whether r30 is equal to _GLOBAL_OFFSET_TABLE_ or
   7630 		     is pointing into a .got2 section (and how far
   7631 		     into .got2).  */
   7632 		    info->callbacks->einfo
   7633 		      /* xgettext:c-format */
   7634 		      (_("%X%H: unsupported bss-plt -fPIC ifunc %s\n"),
   7635 		       input_bfd, input_section, rel->r_offset, sym_name);
   7636 		}
   7637 
   7638 	      unresolved_reloc = false;
   7639 	      if (htab->plt_type == PLT_NEW
   7640 		  || !htab->elf.dynamic_sections_created
   7641 		  || h == NULL
   7642 		  || h->dynindx == -1)
   7643 		relocation = (htab->glink->output_section->vma
   7644 			      + htab->glink->output_offset
   7645 			      + (ent->glink_offset & ~1));
   7646 	      else
   7647 		relocation = (htab->elf.splt->output_section->vma
   7648 			      + htab->elf.splt->output_offset
   7649 			      + ent->plt.offset);
   7650 	    }
   7651 	}
   7652 
   7653       addend = rel->r_addend;
   7654       save_unresolved_reloc = unresolved_reloc;
   7655       howto = NULL;
   7656       if (r_type < ARRAY_SIZE (ppc_elf_howto_table))
   7657 	howto = ppc_elf_howto_table[r_type];
   7658 
   7659       tls_type = 0;
   7660       switch (r_type)
   7661 	{
   7662 	default:
   7663 	de_fault:
   7664 	  if (howto)
   7665 	    /* xgettext:c-format */
   7666 	    _bfd_error_handler (_("%pB: %s unsupported"),
   7667 				input_bfd, howto->name);
   7668 	  else
   7669 	    /* xgettext:c-format */
   7670 	    _bfd_error_handler (_("%pB: reloc %#x unsupported"),
   7671 				input_bfd, r_type);
   7672 
   7673 	  bfd_set_error (bfd_error_bad_value);
   7674 	  ret = false;
   7675 	  goto copy_reloc;
   7676 
   7677 	case R_PPC_NONE:
   7678 	case R_PPC_TLS:
   7679 	case R_PPC_TLSGD:
   7680 	case R_PPC_TLSLD:
   7681 	case R_PPC_EMB_MRKREF:
   7682 	case R_PPC_GNU_VTINHERIT:
   7683 	case R_PPC_GNU_VTENTRY:
   7684 	  goto copy_reloc;
   7685 
   7686 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   7687 	     address in the GOT as relocation value instead of the
   7688 	     symbol's value itself.  Also, create a GOT entry for the
   7689 	     symbol and put the symbol value there.  */
   7690 	case R_PPC_GOT_TLSGD16:
   7691 	case R_PPC_GOT_TLSGD16_LO:
   7692 	case R_PPC_GOT_TLSGD16_HI:
   7693 	case R_PPC_GOT_TLSGD16_HA:
   7694 	  tls_type = TLS_TLS | TLS_GD;
   7695 	  goto dogot;
   7696 
   7697 	case R_PPC_GOT_TLSLD16:
   7698 	case R_PPC_GOT_TLSLD16_LO:
   7699 	case R_PPC_GOT_TLSLD16_HI:
   7700 	case R_PPC_GOT_TLSLD16_HA:
   7701 	  tls_type = TLS_TLS | TLS_LD;
   7702 	  goto dogot;
   7703 
   7704 	case R_PPC_GOT_TPREL16:
   7705 	case R_PPC_GOT_TPREL16_LO:
   7706 	case R_PPC_GOT_TPREL16_HI:
   7707 	case R_PPC_GOT_TPREL16_HA:
   7708 	  tls_type = TLS_TLS | TLS_TPREL;
   7709 	  goto dogot;
   7710 
   7711 	case R_PPC_GOT_DTPREL16:
   7712 	case R_PPC_GOT_DTPREL16_LO:
   7713 	case R_PPC_GOT_DTPREL16_HI:
   7714 	case R_PPC_GOT_DTPREL16_HA:
   7715 	  tls_type = TLS_TLS | TLS_DTPREL;
   7716 	  goto dogot;
   7717 
   7718 	case R_PPC_GOT16:
   7719 	case R_PPC_GOT16_LO:
   7720 	case R_PPC_GOT16_HI:
   7721 	case R_PPC_GOT16_HA:
   7722 	  tls_mask = 0;
   7723 	dogot:
   7724 	  {
   7725 	    /* Relocation is to the entry for this symbol in the global
   7726 	       offset table.  */
   7727 	    bfd_vma off;
   7728 	    bfd_vma *offp;
   7729 	    unsigned long indx;
   7730 
   7731 	    if (htab->elf.sgot == NULL)
   7732 	      abort ();
   7733 
   7734 	    indx = 0;
   7735 	    if (tls_type == (TLS_TLS | TLS_LD)
   7736 		&& SYMBOL_REFERENCES_LOCAL (info, h))
   7737 	      offp = &htab->tlsld_got.offset;
   7738 	    else if (h != NULL)
   7739 	      {
   7740 		if (!htab->elf.dynamic_sections_created
   7741 		    || h->dynindx == -1
   7742 		    || SYMBOL_REFERENCES_LOCAL (info, h)
   7743 		    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   7744 		  /* This is actually a static link, or it is a
   7745 		     -Bsymbolic link and the symbol is defined
   7746 		     locally, or the symbol was forced to be local
   7747 		     because of a version file.  */
   7748 		  ;
   7749 		else
   7750 		  {
   7751 		    indx = h->dynindx;
   7752 		    unresolved_reloc = false;
   7753 		  }
   7754 		offp = &h->got.offset;
   7755 	      }
   7756 	    else
   7757 	      {
   7758 		if (local_got_offsets == NULL)
   7759 		  abort ();
   7760 		offp = &local_got_offsets[r_symndx];
   7761 	      }
   7762 
   7763 	    /* The offset must always be a multiple of 4.  We use the
   7764 	       least significant bit to record whether we have already
   7765 	       processed this entry.  */
   7766 	    off = *offp;
   7767 	    if ((off & 1) != 0)
   7768 	      off &= ~1;
   7769 	    else
   7770 	      {
   7771 		unsigned int tls_m = ((tls_mask & TLS_TLS) != 0
   7772 				      ? tls_mask & (TLS_LD | TLS_GD | TLS_DTPREL
   7773 						    | TLS_TPREL | TLS_GDIE)
   7774 				      : 0);
   7775 
   7776 		if (offp == &htab->tlsld_got.offset)
   7777 		  tls_m = TLS_LD;
   7778 		else if ((tls_m & TLS_LD) != 0
   7779 			 && SYMBOL_REFERENCES_LOCAL (info, h))
   7780 		  tls_m &= ~TLS_LD;
   7781 
   7782 		/* We might have multiple got entries for this sym.
   7783 		   Initialize them all.  */
   7784 		do
   7785 		  {
   7786 		    int tls_ty = 0;
   7787 
   7788 		    if ((tls_m & TLS_LD) != 0)
   7789 		      {
   7790 			tls_ty = TLS_TLS | TLS_LD;
   7791 			tls_m &= ~TLS_LD;
   7792 		      }
   7793 		    else if ((tls_m & TLS_GD) != 0)
   7794 		      {
   7795 			tls_ty = TLS_TLS | TLS_GD;
   7796 			tls_m &= ~TLS_GD;
   7797 		      }
   7798 		    else if ((tls_m & TLS_DTPREL) != 0)
   7799 		      {
   7800 			tls_ty = TLS_TLS | TLS_DTPREL;
   7801 			tls_m &= ~TLS_DTPREL;
   7802 		      }
   7803 		    else if ((tls_m & (TLS_TPREL | TLS_GDIE)) != 0)
   7804 		      {
   7805 			tls_ty = TLS_TLS | TLS_TPREL;
   7806 			tls_m = 0;
   7807 		      }
   7808 
   7809 		    /* Generate relocs for the dynamic linker.  */
   7810 		    if (indx != 0
   7811 			|| (bfd_link_pic (info)
   7812 			    && (h == NULL
   7813 				|| !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   7814 			    && !(tls_ty != 0
   7815 				 && bfd_link_executable (info)
   7816 				 && SYMBOL_REFERENCES_LOCAL (info, h))
   7817 			    && (h != NULL
   7818 				? !bfd_is_abs_symbol (&h->root)
   7819 				: sym->st_shndx != SHN_ABS)))
   7820 		      {
   7821 			asection *rsec = htab->elf.srelgot;
   7822 
   7823 			if (ifunc != NULL)
   7824 			  {
   7825 			    rsec = htab->elf.irelplt;
   7826 			    if (indx == 0)
   7827 			      htab->local_ifunc_resolver = 1;
   7828 			    else if (is_static_defined (h))
   7829 			      htab->maybe_local_ifunc_resolver = 1;
   7830 			  }
   7831 			outrel.r_offset = (htab->elf.sgot->output_section->vma
   7832 					   + htab->elf.sgot->output_offset
   7833 					   + off);
   7834 			outrel.r_addend = 0;
   7835 			if (tls_ty & (TLS_LD | TLS_GD))
   7836 			  {
   7837 			    outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
   7838 			    if (tls_ty == (TLS_TLS | TLS_GD))
   7839 			      {
   7840 				BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
   7841 								      &outrel,
   7842 								      rsec));
   7843 				outrel.r_offset += 4;
   7844 				outrel.r_info
   7845 				  = ELF32_R_INFO (indx, R_PPC_DTPREL32);
   7846 			      }
   7847 			  }
   7848 			else if (tls_ty == (TLS_TLS | TLS_DTPREL))
   7849 			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
   7850 			else if (tls_ty == (TLS_TLS | TLS_TPREL))
   7851 			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
   7852 			else if (indx != 0)
   7853 			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
   7854 			else if (ifunc != NULL)
   7855 			  outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   7856 			else
   7857 			  outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   7858 			if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
   7859 			  {
   7860 			    outrel.r_addend += relocation;
   7861 			    if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   7862 			      {
   7863 				if (htab->elf.tls_sec == NULL)
   7864 				  outrel.r_addend = 0;
   7865 				else
   7866 				  outrel.r_addend -= htab->elf.tls_sec->vma;
   7867 			      }
   7868 			  }
   7869 			BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
   7870 							      &outrel, rsec));
   7871 		      }
   7872 
   7873 		    /* Init the .got section contents if we're not
   7874 		       emitting a reloc.  */
   7875 		    else
   7876 		      {
   7877 			bfd_vma value = relocation;
   7878 
   7879 			if (tls_ty != 0)
   7880 			  {
   7881 			    if (htab->elf.tls_sec == NULL)
   7882 			      value = 0;
   7883 			    else
   7884 			      {
   7885 				if (tls_ty & TLS_LD)
   7886 				  value = 0;
   7887 				else
   7888 				  value -= htab->elf.tls_sec->vma + DTP_OFFSET;
   7889 				if (tls_ty & TLS_TPREL)
   7890 				  value += DTP_OFFSET - TP_OFFSET;
   7891 			      }
   7892 
   7893 			    if (tls_ty & (TLS_LD | TLS_GD))
   7894 			      {
   7895 				bfd_put_32 (input_bfd, value,
   7896 					    htab->elf.sgot->contents + off + 4);
   7897 				value = 1;
   7898 			      }
   7899 			  }
   7900 			bfd_put_32 (input_bfd, value,
   7901 				    htab->elf.sgot->contents + off);
   7902 		      }
   7903 
   7904 		    off += 4;
   7905 		    if (tls_ty & (TLS_LD | TLS_GD))
   7906 		      off += 4;
   7907 		  }
   7908 		while (tls_m != 0);
   7909 
   7910 		off = *offp;
   7911 		*offp = off | 1;
   7912 	      }
   7913 
   7914 	    if (off >= (bfd_vma) -2)
   7915 	      abort ();
   7916 
   7917 	    if ((tls_type & TLS_TLS) != 0)
   7918 	      {
   7919 		if (tls_type != (TLS_TLS | TLS_LD))
   7920 		  {
   7921 		    if ((tls_mask & TLS_LD) != 0
   7922 			&& !SYMBOL_REFERENCES_LOCAL (info, h))
   7923 		      off += 8;
   7924 		    if (tls_type != (TLS_TLS | TLS_GD))
   7925 		      {
   7926 			if ((tls_mask & TLS_GD) != 0)
   7927 			  off += 8;
   7928 			if (tls_type != (TLS_TLS | TLS_DTPREL))
   7929 			  {
   7930 			    if ((tls_mask & TLS_DTPREL) != 0)
   7931 			      off += 4;
   7932 			  }
   7933 		      }
   7934 		  }
   7935 	      }
   7936 
   7937 	    /* If here for a picfixup, we're done.  */
   7938 	    if (r_type != ELF32_R_TYPE (rel->r_info))
   7939 	      goto copy_reloc;
   7940 
   7941 	    relocation = (htab->elf.sgot->output_section->vma
   7942 			  + htab->elf.sgot->output_offset
   7943 			  + off
   7944 			  - SYM_VAL (htab->elf.hgot));
   7945 
   7946 	    /* Addends on got relocations don't make much sense.
   7947 	       x+off@got is actually x@got+off, and since the got is
   7948 	       generated by a hash table traversal, the value in the
   7949 	       got at entry m+n bears little relation to the entry m.  */
   7950 	    if (addend != 0)
   7951 	      info->callbacks->einfo
   7952 		/* xgettext:c-format */
   7953 		(_("%H: non-zero addend on %s reloc against `%s'\n"),
   7954 		 input_bfd, input_section, rel->r_offset,
   7955 		 howto->name,
   7956 		 sym_name);
   7957 	  }
   7958 	  break;
   7959 
   7960 	  /* Relocations that need no special processing.  */
   7961 	case R_PPC_LOCAL24PC:
   7962 	  /* It makes no sense to point a local relocation
   7963 	     at a symbol not in this object.  */
   7964 	  if (unresolved_reloc)
   7965 	    {
   7966 	      (*info->callbacks->undefined_symbol) (info,
   7967 						    h->root.root.string,
   7968 						    input_bfd,
   7969 						    input_section,
   7970 						    rel->r_offset,
   7971 						    true);
   7972 	      goto copy_reloc;
   7973 	    }
   7974 	  if (h != NULL && h->type == STT_GNU_IFUNC && bfd_link_pic (info))
   7975 	    {
   7976 	      /* @local on an ifunc does not really make sense since
   7977 		 the ifunc resolver can take you anywhere.  More
   7978 		 seriously, calls to ifuncs must go through a plt call
   7979 		 stub, and for pic the plt call stubs uses r30 to
   7980 		 access the PLT.  The problem is that a call that is
   7981 		 local won't have the +32k reloc addend trick marking
   7982 		 -fPIC code, so the linker won't know whether r30 is
   7983 		 _GLOBAL_OFFSET_TABLE_ or pointing into a .got2 section.  */
   7984 	      /* xgettext:c-format */
   7985 	      info->callbacks->einfo (_("%X%H: @local call to ifunc %s\n"),
   7986 				      input_bfd, input_section, rel->r_offset,
   7987 				      h->root.root.string);
   7988 	    }
   7989 	  break;
   7990 
   7991 	case R_PPC_DTPREL16:
   7992 	case R_PPC_DTPREL16_LO:
   7993 	case R_PPC_DTPREL16_HI:
   7994 	case R_PPC_DTPREL16_HA:
   7995 	  if (htab->elf.tls_sec != NULL)
   7996 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   7997 	  break;
   7998 
   7999 	  /* Relocations that may need to be propagated if this is a shared
   8000 	     object.  */
   8001 	case R_PPC_TPREL16:
   8002 	case R_PPC_TPREL16_LO:
   8003 	case R_PPC_TPREL16_HI:
   8004 	case R_PPC_TPREL16_HA:
   8005 	  if (h != NULL
   8006 	      && h->root.type == bfd_link_hash_undefweak
   8007 	      && h->dynindx == -1
   8008 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   8009 	    {
   8010 	      /* Make this relocation against an undefined weak symbol
   8011 		 resolve to zero.  This is really just a tweak, since
   8012 		 code using weak externs ought to check that they are
   8013 		 defined before using them.  */
   8014 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   8015 	      unsigned int insn = bfd_get_32 (input_bfd, p);
   8016 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
   8017 	      if (insn != 0)
   8018 		bfd_put_32 (input_bfd, insn, p);
   8019 	      break;
   8020 	    }
   8021 	  if (htab->elf.tls_sec != NULL)
   8022 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   8023 	  /* The TPREL16 relocs shouldn't really be used in shared
   8024 	     libs or with non-local symbols as that will result in
   8025 	     DT_TEXTREL being set, but support them anyway.  */
   8026 	  goto dodyn;
   8027 
   8028 	case R_PPC_TPREL32:
   8029 	  if (htab->elf.tls_sec != NULL)
   8030 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   8031 	  goto dodyn;
   8032 
   8033 	case R_PPC_DTPREL32:
   8034 	  if (htab->elf.tls_sec != NULL)
   8035 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   8036 	  goto dodyn;
   8037 
   8038 	case R_PPC_DTPMOD32:
   8039 	  relocation = 1;
   8040 	  addend = 0;
   8041 	  goto dodyn;
   8042 
   8043 	case R_PPC_REL16:
   8044 	case R_PPC_REL16_LO:
   8045 	case R_PPC_REL16_HI:
   8046 	case R_PPC_REL16_HA:
   8047 	case R_PPC_REL16DX_HA:
   8048 	  break;
   8049 
   8050 	case R_PPC_REL32:
   8051 	  if (h == NULL || h == htab->elf.hgot)
   8052 	    break;
   8053 	  /* fall through */
   8054 
   8055 	case R_PPC_ADDR32:
   8056 	case R_PPC_ADDR16:
   8057 	case R_PPC_ADDR16_LO:
   8058 	case R_PPC_ADDR16_HI:
   8059 	case R_PPC_ADDR16_HA:
   8060 	case R_PPC_UADDR32:
   8061 	case R_PPC_UADDR16:
   8062 	  goto dodyn;
   8063 
   8064 	case R_PPC_VLE_REL8:
   8065 	case R_PPC_VLE_REL15:
   8066 	case R_PPC_VLE_REL24:
   8067 	case R_PPC_REL24:
   8068 	case R_PPC_REL14:
   8069 	case R_PPC_REL14_BRTAKEN:
   8070 	case R_PPC_REL14_BRNTAKEN:
   8071 	  /* If these relocations are not to a named symbol, they can be
   8072 	     handled right here, no need to bother the dynamic linker.  */
   8073 	  if (SYMBOL_CALLS_LOCAL (info, h)
   8074 	      || h == htab->elf.hgot)
   8075 	    break;
   8076 	  /* fall through */
   8077 
   8078 	case R_PPC_ADDR24:
   8079 	case R_PPC_ADDR14:
   8080 	case R_PPC_ADDR14_BRTAKEN:
   8081 	case R_PPC_ADDR14_BRNTAKEN:
   8082 	  if (h != NULL && !bfd_link_pic (info))
   8083 	    break;
   8084 	  /* fall through */
   8085 
   8086 	dodyn:
   8087 	  if ((input_section->flags & SEC_ALLOC) == 0
   8088 	      || is_vxworks_tls)
   8089 	    break;
   8090 
   8091 	  if (bfd_link_pic (info)
   8092 	      ? ((h == NULL
   8093 		  || h->dyn_relocs != NULL)
   8094 		 && ((h != NULL && pc_dynrelocs (h))
   8095 		     || must_be_dyn_reloc (info, r_type)))
   8096 	      : (h != NULL
   8097 		 && h->dyn_relocs != NULL))
   8098 	    {
   8099 	      int skip;
   8100 	      asection *sreloc;
   8101 	      long indx = 0;
   8102 
   8103 #ifdef DEBUG
   8104 	      fprintf (stderr, "ppc_elf_relocate_section needs to "
   8105 		       "create relocation for %s\n",
   8106 		       (h && h->root.root.string
   8107 			? h->root.root.string : "<unknown>"));
   8108 #endif
   8109 
   8110 	      /* When generating a shared object, these relocations
   8111 		 are copied into the output file to be resolved at run
   8112 		 time.  */
   8113 	      skip = 0;
   8114 	      outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
   8115 							 input_section,
   8116 							 rel->r_offset);
   8117 	      if (outrel.r_offset == (bfd_vma) -1
   8118 		  || outrel.r_offset == (bfd_vma) -2)
   8119 		skip = (int) outrel.r_offset;
   8120 	      outrel.r_offset += (input_section->output_section->vma
   8121 				  + input_section->output_offset);
   8122 
   8123 	      /* Optimize unaligned reloc use.  */
   8124 	      if ((r_type == R_PPC_ADDR32 && (outrel.r_offset & 3) != 0)
   8125 		  || (r_type == R_PPC_UADDR32 && (outrel.r_offset & 3) == 0))
   8126 		r_type ^= R_PPC_ADDR32 ^ R_PPC_UADDR32;
   8127 	      if ((r_type == R_PPC_ADDR16 && (outrel.r_offset & 1) != 0)
   8128 		  || (r_type == R_PPC_UADDR16 && (outrel.r_offset & 1) == 0))
   8129 		r_type ^= R_PPC_ADDR16 ^ R_PPC_UADDR16;
   8130 
   8131 	      if (skip)
   8132 		memset (&outrel, 0, sizeof outrel);
   8133 	      else if (!SYMBOL_REFERENCES_LOCAL (info, h))
   8134 		{
   8135 		  indx = h->dynindx;
   8136 		  BFD_ASSERT (indx != -1);
   8137 		  unresolved_reloc = false;
   8138 		  outrel.r_info = ELF32_R_INFO (indx, r_type);
   8139 		  outrel.r_addend = rel->r_addend;
   8140 		}
   8141 	      else
   8142 		{
   8143 		  outrel.r_addend = relocation + rel->r_addend;
   8144 
   8145 		  if (r_type != R_PPC_ADDR32)
   8146 		    {
   8147 		      if (ifunc != NULL)
   8148 			{
   8149 			  /* If we get here when building a static
   8150 			     executable, then the libc startup function
   8151 			     responsible for applying indirect function
   8152 			     relocations is going to complain about
   8153 			     the reloc type.
   8154 			     If we get here when building a dynamic
   8155 			     executable, it will be because we have
   8156 			     a text relocation.  The dynamic loader
   8157 			     will set the text segment writable and
   8158 			     non-executable to apply text relocations.
   8159 			     So we'll segfault when trying to run the
   8160 			     indirection function to resolve the reloc.  */
   8161 			  info->callbacks->einfo
   8162 			    /* xgettext:c-format */
   8163 			    (_("%H: relocation %s for indirect "
   8164 			       "function %s unsupported\n"),
   8165 			     input_bfd, input_section, rel->r_offset,
   8166 			     howto->name,
   8167 			     sym_name);
   8168 			  ret = false;
   8169 			}
   8170 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   8171 			;
   8172 		      else if (sec == NULL || sec->owner == NULL)
   8173 			{
   8174 			  bfd_set_error (bfd_error_bad_value);
   8175 			  ret = false;
   8176 			}
   8177 		      else
   8178 			{
   8179 			  asection *osec;
   8180 
   8181 			  /* We are turning this relocation into one
   8182 			     against a section symbol.  It would be
   8183 			     proper to subtract the symbol's value,
   8184 			     osec->vma, from the emitted reloc addend,
   8185 			     but ld.so expects buggy relocs.
   8186 			     FIXME: Why not always use a zero index?  */
   8187 			  osec = sec->output_section;
   8188 			  if ((osec->flags & SEC_THREAD_LOCAL) != 0)
   8189 			    {
   8190 			      osec = htab->elf.tls_sec;
   8191 			      indx = 0;
   8192 			    }
   8193 			  else
   8194 			    {
   8195 			      indx = elf_section_data (osec)->dynindx;
   8196 			      if (indx == 0)
   8197 				{
   8198 				  osec = htab->elf.text_index_section;
   8199 				  indx = elf_section_data (osec)->dynindx;
   8200 				}
   8201 			      BFD_ASSERT (indx != 0);
   8202 			    }
   8203 
   8204 			  /* ld.so doesn't expect buggy TLS relocs.
   8205 			     Don't leave the symbol value in the
   8206 			     addend for them.  */
   8207 			  if (IS_PPC_TLS_RELOC (r_type))
   8208 			    outrel.r_addend -= osec->vma;
   8209 			}
   8210 
   8211 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
   8212 		    }
   8213 		  else if (ifunc != NULL)
   8214 		    outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   8215 		  else
   8216 		    outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   8217 		}
   8218 
   8219 	      sreloc = elf_section_data (input_section)->sreloc;
   8220 	      if (ifunc)
   8221 		{
   8222 		  sreloc = htab->elf.irelplt;
   8223 		  if (indx == 0)
   8224 		    htab->local_ifunc_resolver = 1;
   8225 		  else if (is_static_defined (h))
   8226 		    htab->maybe_local_ifunc_resolver = 1;
   8227 		}
   8228 	      if (sreloc == NULL)
   8229 		return false;
   8230 
   8231 	      BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &outrel,
   8232 						    sreloc));
   8233 
   8234 	      if (skip == -1)
   8235 		goto copy_reloc;
   8236 
   8237 	      /* This reloc will be computed at runtime.  Clear the memory
   8238 		 so that it contains a predictable value for prelink.  */
   8239 	      if (!skip)
   8240 		{
   8241 		  relocation = howto->pc_relative ? outrel.r_offset : 0;
   8242 		  addend = 0;
   8243 		  break;
   8244 		}
   8245 	    }
   8246 	  break;
   8247 
   8248 	case R_PPC_RELAX_PLT:
   8249 	case R_PPC_RELAX_PLTREL24:
   8250 	  if (h != NULL)
   8251 	    {
   8252 	      struct plt_entry *ent;
   8253 	      bfd_vma got2_addend = 0;
   8254 
   8255 	      if (r_type == R_PPC_RELAX_PLTREL24)
   8256 		{
   8257 		  if (bfd_link_pic (info))
   8258 		    got2_addend = addend;
   8259 		  addend = 0;
   8260 		}
   8261 	      ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
   8262 	      if (htab->plt_type == PLT_NEW)
   8263 		relocation = (htab->glink->output_section->vma
   8264 			      + htab->glink->output_offset
   8265 			      + ent->glink_offset);
   8266 	      else
   8267 		relocation = (htab->elf.splt->output_section->vma
   8268 			      + htab->elf.splt->output_offset
   8269 			      + ent->plt.offset);
   8270 	    }
   8271 	  /* Fall through.  */
   8272 
   8273 	case R_PPC_RELAX:
   8274 	  if (bfd_link_pic (info)
   8275 	      ? offset_in_range (input_section, rel->r_offset - 12,
   8276 				 ARRAY_SIZE (shared_stub_entry) * 4)
   8277 	      : offset_in_range (input_section, rel->r_offset,
   8278 				 ARRAY_SIZE (stub_entry) * 4))
   8279 	    {
   8280 	      const int *stub;
   8281 	      size_t size;
   8282 	      size_t insn_offset = rel->r_offset;
   8283 	      unsigned int insn;
   8284 
   8285 	      if (bfd_link_pic (info))
   8286 		{
   8287 		  relocation -= (input_section->output_section->vma
   8288 				 + input_section->output_offset
   8289 				 + rel->r_offset - 4);
   8290 		  stub = shared_stub_entry;
   8291 		  bfd_put_32 (input_bfd, stub[0], contents + insn_offset - 12);
   8292 		  bfd_put_32 (input_bfd, stub[1], contents + insn_offset - 8);
   8293 		  bfd_put_32 (input_bfd, stub[2], contents + insn_offset - 4);
   8294 		  stub += 3;
   8295 		  size = ARRAY_SIZE (shared_stub_entry) - 3;
   8296 		}
   8297 	      else
   8298 		{
   8299 		  stub = stub_entry;
   8300 		  size = ARRAY_SIZE (stub_entry);
   8301 		}
   8302 
   8303 	      relocation += addend;
   8304 	      if (bfd_link_relocatable (info))
   8305 		relocation = 0;
   8306 
   8307 	      /* First insn is HA, second is LO.  */
   8308 	      insn = *stub++;
   8309 	      insn |= ((relocation + 0x8000) >> 16) & 0xffff;
   8310 	      bfd_put_32 (input_bfd, insn, contents + insn_offset);
   8311 	      insn_offset += 4;
   8312 
   8313 	      insn = *stub++;
   8314 	      insn |= relocation & 0xffff;
   8315 	      bfd_put_32 (input_bfd, insn, contents + insn_offset);
   8316 	      insn_offset += 4;
   8317 	      size -= 2;
   8318 
   8319 	      while (size != 0)
   8320 		{
   8321 		  insn = *stub++;
   8322 		  --size;
   8323 		  bfd_put_32 (input_bfd, insn, contents + insn_offset);
   8324 		  insn_offset += 4;
   8325 		}
   8326 
   8327 	      /* Rewrite the reloc and convert one of the trailing nop
   8328 		 relocs to describe this relocation.  */
   8329 	      BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
   8330 	      /* The relocs are at the bottom 2 bytes */
   8331 	      wrel->r_offset = rel->r_offset + d_offset;
   8332 	      wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
   8333 	      wrel->r_addend = rel->r_addend;
   8334 	      memmove (wrel + 1, wrel, (relend - wrel - 1) * sizeof (*wrel));
   8335 	      wrel++, rel++;
   8336 	      wrel->r_offset += 4;
   8337 	      wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
   8338 	    }
   8339 	  else
   8340 	    goto de_fault;
   8341 	  continue;
   8342 
   8343 	  /* Indirect .sdata relocation.  */
   8344 	case R_PPC_EMB_SDAI16:
   8345 	  BFD_ASSERT (htab->sdata[0].section != NULL);
   8346 	  if (!is_static_defined (htab->sdata[0].sym))
   8347 	    {
   8348 	      unresolved_reloc = true;
   8349 	      break;
   8350 	    }
   8351 	  relocation
   8352 	    = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
   8353 						 h, relocation, rel);
   8354 	  addend = 0;
   8355 	  break;
   8356 
   8357 	  /* Indirect .sdata2 relocation.  */
   8358 	case R_PPC_EMB_SDA2I16:
   8359 	  BFD_ASSERT (htab->sdata[1].section != NULL);
   8360 	  if (!is_static_defined (htab->sdata[1].sym))
   8361 	    {
   8362 	      unresolved_reloc = true;
   8363 	      break;
   8364 	    }
   8365 	  relocation
   8366 	    = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
   8367 						 h, relocation, rel);
   8368 	  addend = 0;
   8369 	  break;
   8370 
   8371 	  /* Handle the TOC16 reloc.  We want to use the offset within the .got
   8372 	     section, not the actual VMA.  This is appropriate when generating
   8373 	     an embedded ELF object, for which the .got section acts like the
   8374 	     AIX .toc section.  */
   8375 	case R_PPC_TOC16:			/* phony GOT16 relocations */
   8376 	  if (sec == NULL || sec->output_section == NULL)
   8377 	    {
   8378 	      unresolved_reloc = true;
   8379 	      break;
   8380 	    }
   8381 	  BFD_ASSERT (strcmp (bfd_section_name (sec), ".got") == 0
   8382 		      || strcmp (bfd_section_name (sec), ".cgot") == 0);
   8383 
   8384 	  addend -= sec->output_section->vma + sec->output_offset + 0x8000;
   8385 	  break;
   8386 
   8387 	case R_PPC_PLTREL24:
   8388 	  if (h != NULL && ifunc == NULL)
   8389 	    {
   8390 	      struct plt_entry *ent;
   8391 
   8392 	      ent = find_plt_ent (&h->plt.plist, got2,
   8393 				  bfd_link_pic (info) ? addend : 0);
   8394 	      if (ent == NULL
   8395 		  || htab->elf.splt == NULL)
   8396 		{
   8397 		  /* We didn't make a PLT entry for this symbol.  This
   8398 		     happens when statically linking PIC code, or when
   8399 		     using -Bsymbolic.  */
   8400 		}
   8401 	      else
   8402 		{
   8403 		  /* Relocation is to the entry for this symbol in the
   8404 		     procedure linkage table.  */
   8405 		  unresolved_reloc = false;
   8406 		  if (htab->plt_type == PLT_NEW)
   8407 		    relocation = (htab->glink->output_section->vma
   8408 				  + htab->glink->output_offset
   8409 				  + ent->glink_offset);
   8410 		  else
   8411 		    relocation = (htab->elf.splt->output_section->vma
   8412 				  + htab->elf.splt->output_offset
   8413 				  + ent->plt.offset);
   8414 		}
   8415 	    }
   8416 
   8417 	  /* R_PPC_PLTREL24 is rather special.  If non-zero, the
   8418 	     addend specifies the GOT pointer offset within .got2.
   8419 	     Don't apply it to the relocation field.  */
   8420 	  addend = 0;
   8421 	  break;
   8422 
   8423 	case R_PPC_PLTSEQ:
   8424 	case R_PPC_PLTCALL:
   8425 	case R_PPC_PLT16_LO:
   8426 	case R_PPC_PLT16_HI:
   8427 	case R_PPC_PLT16_HA:
   8428 	  plt_list = NULL;
   8429 	  if (h != NULL)
   8430 	    plt_list = &h->plt.plist;
   8431 	  else if (ifunc != NULL)
   8432 	    plt_list = ifunc;
   8433 	  else if (local_got_offsets != NULL)
   8434 	    {
   8435 	      struct plt_entry **local_plt;
   8436 	      local_plt = (struct plt_entry **) (local_got_offsets
   8437 						 + symtab_hdr->sh_info);
   8438 	      plt_list = local_plt + r_symndx;
   8439 	    }
   8440 	  unresolved_reloc = true;
   8441 	  if (plt_list != NULL)
   8442 	    {
   8443 	      struct plt_entry *ent;
   8444 
   8445 	      ent = find_plt_ent (plt_list, got2,
   8446 				  bfd_link_pic (info) ? addend : 0);
   8447 	      if (ent != NULL && ent->plt.offset != (bfd_vma) -1)
   8448 		{
   8449 		  asection *plt;
   8450 
   8451 		  unresolved_reloc = false;
   8452 		  plt = htab->elf.splt;
   8453 		  if (use_local_plt (info, h))
   8454 		    {
   8455 		      if (ifunc != NULL)
   8456 			plt = htab->elf.iplt;
   8457 		      else
   8458 			plt = htab->pltlocal;
   8459 		    }
   8460 		  relocation = (plt->output_section->vma
   8461 				+ plt->output_offset
   8462 				+ ent->plt.offset);
   8463 		  if (bfd_link_pic (info))
   8464 		    {
   8465 		      bfd_vma got = 0;
   8466 
   8467 		      if (ent->addend >= 32768)
   8468 			got = (ent->addend
   8469 			       + ent->sec->output_section->vma
   8470 			       + ent->sec->output_offset);
   8471 		      else
   8472 			got = SYM_VAL (htab->elf.hgot);
   8473 		      relocation -= got;
   8474 		    }
   8475 		}
   8476 	    }
   8477 	  addend = 0;
   8478 	  break;
   8479 
   8480 	  /* Relocate against _SDA_BASE_.  */
   8481 	case R_PPC_SDAREL16:
   8482 	  {
   8483 	    const char *name;
   8484 	    struct elf_link_hash_entry *sda = htab->sdata[0].sym;
   8485 
   8486 	    if (sec == NULL
   8487 		|| sec->output_section == NULL
   8488 		|| !is_static_defined (sda))
   8489 	      {
   8490 		unresolved_reloc = true;
   8491 		break;
   8492 	      }
   8493 	    addend -= SYM_VAL (sda);
   8494 
   8495 	    name = bfd_section_name (sec->output_section);
   8496 	    if (!(strcmp (name, ".sdata") == 0
   8497 		  || strcmp (name, ".sbss") == 0))
   8498 	      {
   8499 		_bfd_error_handler
   8500 		  /* xgettext:c-format */
   8501 		  (_("%pB: the target (%s) of a %s relocation is "
   8502 		     "in the wrong output section (%s)"),
   8503 		   input_bfd,
   8504 		   sym_name,
   8505 		   howto->name,
   8506 		   name);
   8507 	      }
   8508 	  }
   8509 	  break;
   8510 
   8511 	  /* Relocate against _SDA2_BASE_.  */
   8512 	case R_PPC_EMB_SDA2REL:
   8513 	  {
   8514 	    const char *name;
   8515 	    struct elf_link_hash_entry *sda = htab->sdata[1].sym;
   8516 
   8517 	    if (sec == NULL
   8518 		|| sec->output_section == NULL
   8519 		|| !is_static_defined (sda))
   8520 	      {
   8521 		unresolved_reloc = true;
   8522 		break;
   8523 	      }
   8524 	    addend -= SYM_VAL (sda);
   8525 
   8526 	    name = bfd_section_name (sec->output_section);
   8527 	    if (!(strcmp (name, ".sdata2") == 0
   8528 		  || strcmp (name, ".sbss2") == 0))
   8529 	      {
   8530 		_bfd_error_handler
   8531 		  /* xgettext:c-format */
   8532 		  (_("%pB: the target (%s) of a %s relocation is "
   8533 		     "in the wrong output section (%s)"),
   8534 		   input_bfd,
   8535 		   sym_name,
   8536 		   howto->name,
   8537 		   name);
   8538 	      }
   8539 	  }
   8540 	  break;
   8541 
   8542 	case R_PPC_VLE_LO16A:
   8543 	  relocation = relocation + addend;
   8544 	  r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8545 				   contents + rel->r_offset, relocation,
   8546 				   split16a_type,
   8547 				   htab->params->vle_reloc_fixup);
   8548 	  goto report_reloc;
   8549 
   8550 	case R_PPC_VLE_LO16D:
   8551 	  relocation = relocation + addend;
   8552 	  r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8553 				   contents + rel->r_offset, relocation,
   8554 				   split16d_type,
   8555 				   htab->params->vle_reloc_fixup);
   8556 	  goto report_reloc;
   8557 
   8558 	case R_PPC_VLE_HI16A:
   8559 	  relocation = (relocation + addend) >> 16;
   8560 	  r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8561 				   contents + rel->r_offset, relocation,
   8562 				   split16a_type,
   8563 				   htab->params->vle_reloc_fixup);
   8564 	  goto report_reloc;
   8565 
   8566 	case R_PPC_VLE_HI16D:
   8567 	  relocation = (relocation + addend) >> 16;
   8568 	  r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8569 				   contents + rel->r_offset, relocation,
   8570 				   split16d_type,
   8571 				   htab->params->vle_reloc_fixup);
   8572 	  goto report_reloc;
   8573 
   8574 	case R_PPC_VLE_HA16A:
   8575 	  relocation = (relocation + addend + 0x8000) >> 16;
   8576 	  r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8577 				   contents + rel->r_offset, relocation,
   8578 				   split16a_type,
   8579 				   htab->params->vle_reloc_fixup);
   8580 	  goto report_reloc;
   8581 
   8582 	case R_PPC_VLE_HA16D:
   8583 	  relocation = (relocation + addend + 0x8000) >> 16;
   8584 	  r = ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8585 				   contents + rel->r_offset, relocation,
   8586 				   split16d_type,
   8587 				   htab->params->vle_reloc_fixup);
   8588 	  goto report_reloc;
   8589 
   8590 	  /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
   8591 	case R_PPC_EMB_SDA21:
   8592 	case R_PPC_VLE_SDA21:
   8593 	case R_PPC_EMB_RELSDA:
   8594 	case R_PPC_VLE_SDA21_LO:
   8595 	  if (!offset_in_range (input_section, rel->r_offset, 4))
   8596 	    {
   8597 	      r = bfd_reloc_outofrange;
   8598 	      goto report_reloc;
   8599 	    }
   8600 	  else
   8601 	    {
   8602 	      const char *name;
   8603 	      int reg;
   8604 	      unsigned int insn;
   8605 	      struct elf_link_hash_entry *sda = NULL;
   8606 
   8607 	      if (sec == NULL || sec->output_section == NULL)
   8608 		{
   8609 		  unresolved_reloc = true;
   8610 		  break;
   8611 		}
   8612 
   8613 	      name = bfd_section_name (sec->output_section);
   8614 	      if (strcmp (name, ".sdata") == 0
   8615 		  || strcmp (name, ".sbss") == 0)
   8616 		{
   8617 		  reg = 13;
   8618 		  sda = htab->sdata[0].sym;
   8619 		}
   8620 	      else if (strcmp (name, ".sdata2") == 0
   8621 		       || strcmp (name, ".sbss2") == 0)
   8622 		{
   8623 		  reg = 2;
   8624 		  sda = htab->sdata[1].sym;
   8625 		}
   8626 	      else if (strcmp (name, ".PPC.EMB.sdata0") == 0
   8627 		       || strcmp (name, ".PPC.EMB.sbss0") == 0)
   8628 		{
   8629 		  reg = 0;
   8630 		}
   8631 	      else
   8632 		{
   8633 		  _bfd_error_handler
   8634 		    /* xgettext:c-format */
   8635 		    (_("%pB: the target (%s) of a %s relocation is "
   8636 		       "in the wrong output section (%s)"),
   8637 		     input_bfd,
   8638 		     sym_name,
   8639 		     howto->name,
   8640 		     name);
   8641 
   8642 		  bfd_set_error (bfd_error_bad_value);
   8643 		  ret = false;
   8644 		  goto copy_reloc;
   8645 		}
   8646 
   8647 	      if (sda != NULL)
   8648 		{
   8649 		  if (!is_static_defined (sda))
   8650 		    {
   8651 		      unresolved_reloc = true;
   8652 		      break;
   8653 		    }
   8654 		  addend -= SYM_VAL (sda);
   8655 		}
   8656 
   8657 	      if (r_type == R_PPC_EMB_RELSDA)
   8658 		break;
   8659 
   8660 	      /* The PowerPC Embedded Application Binary Interface
   8661 		 version 1.0 insanely chose to specify R_PPC_EMB_SDA21
   8662 		 operating on a 24-bit field at r_offset.  GNU as and
   8663 		 GNU ld have always assumed R_PPC_EMB_SDA21 operates on
   8664 		 a 32-bit bit insn at r_offset.  Cope with object file
   8665 		 producers that possibly comply with the EABI in
   8666 		 generating an odd r_offset for big-endian objects.  */
   8667 	      if (r_type == R_PPC_EMB_SDA21)
   8668 		rel->r_offset &= ~1;
   8669 
   8670 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   8671 	      if (reg == 0
   8672 		  && (r_type == R_PPC_VLE_SDA21
   8673 		      || r_type == R_PPC_VLE_SDA21_LO))
   8674 		{
   8675 		  relocation = relocation + addend;
   8676 		  addend = 0;
   8677 
   8678 		  /* Force e_li insn, keeping RT from original insn.  */
   8679 		  insn &= 0x1f << 21;
   8680 		  insn |= 28u << 26;
   8681 
   8682 		  /* We have an li20 field, bits 17..20, 11..15, 21..31.  */
   8683 		  /* Top 4 bits of value to 17..20.  */
   8684 		  insn |= (relocation & 0xf0000) >> 5;
   8685 		  /* Next 5 bits of the value to 11..15.  */
   8686 		  insn |= (relocation & 0xf800) << 5;
   8687 		  /* And the final 11 bits of the value to bits 21 to 31.  */
   8688 		  insn |= relocation & 0x7ff;
   8689 
   8690 		  bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   8691 
   8692 		  r = bfd_reloc_ok;
   8693 		  if (r_type == R_PPC_VLE_SDA21
   8694 		      && ((relocation + 0x80000) & 0xffffffff) > 0x100000)
   8695 		    r = bfd_reloc_overflow;
   8696 		  goto report_reloc;
   8697 		}
   8698 	      /* Fill in register field.  */
   8699 	      insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
   8700 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   8701 	    }
   8702 	  break;
   8703 
   8704 	case R_PPC_VLE_SDAREL_LO16A:
   8705 	case R_PPC_VLE_SDAREL_LO16D:
   8706 	case R_PPC_VLE_SDAREL_HI16A:
   8707 	case R_PPC_VLE_SDAREL_HI16D:
   8708 	case R_PPC_VLE_SDAREL_HA16A:
   8709 	case R_PPC_VLE_SDAREL_HA16D:
   8710 	  if (!offset_in_range (input_section, rel->r_offset, 4))
   8711 	    r = bfd_reloc_outofrange;
   8712 	  else
   8713 	    {
   8714 	      bfd_vma value;
   8715 	      const char *name;
   8716 	      struct elf_link_hash_entry *sda = NULL;
   8717 
   8718 	      if (sec == NULL || sec->output_section == NULL)
   8719 		{
   8720 		  unresolved_reloc = true;
   8721 		  break;
   8722 		}
   8723 
   8724 	      name = bfd_section_name (sec->output_section);
   8725 	      if (strcmp (name, ".sdata") == 0
   8726 		  || strcmp (name, ".sbss") == 0)
   8727 		sda = htab->sdata[0].sym;
   8728 	      else if (strcmp (name, ".sdata2") == 0
   8729 		       || strcmp (name, ".sbss2") == 0)
   8730 		sda = htab->sdata[1].sym;
   8731 	      else
   8732 		{
   8733 		  _bfd_error_handler
   8734 		    /* xgettext:c-format */
   8735 		    (_("%pB: the target (%s) of a %s relocation is "
   8736 		       "in the wrong output section (%s)"),
   8737 		     input_bfd,
   8738 		     sym_name,
   8739 		     howto->name,
   8740 		     name);
   8741 
   8742 		  bfd_set_error (bfd_error_bad_value);
   8743 		  ret = false;
   8744 		  goto copy_reloc;
   8745 		}
   8746 
   8747 	      if (sda == NULL || !is_static_defined (sda))
   8748 		{
   8749 		  unresolved_reloc = true;
   8750 		  break;
   8751 		}
   8752 	      value = relocation + addend - SYM_VAL (sda);
   8753 
   8754 	      if (r_type == R_PPC_VLE_SDAREL_LO16A)
   8755 		r = ppc_elf_vle_split16 (input_bfd, input_section,
   8756 					 rel->r_offset,
   8757 					 contents + rel->r_offset, value,
   8758 					 split16a_type,
   8759 					 htab->params->vle_reloc_fixup);
   8760 	      else if (r_type == R_PPC_VLE_SDAREL_LO16D)
   8761 		r = ppc_elf_vle_split16 (input_bfd, input_section,
   8762 					 rel->r_offset,
   8763 					 contents + rel->r_offset, value,
   8764 					 split16d_type,
   8765 					 htab->params->vle_reloc_fixup);
   8766 	      else if (r_type == R_PPC_VLE_SDAREL_HI16A)
   8767 		{
   8768 		  value = value >> 16;
   8769 		  r = ppc_elf_vle_split16 (input_bfd, input_section,
   8770 					   rel->r_offset,
   8771 					   contents + rel->r_offset, value,
   8772 					   split16a_type,
   8773 					   htab->params->vle_reloc_fixup);
   8774 		}
   8775 	      else if (r_type == R_PPC_VLE_SDAREL_HI16D)
   8776 		{
   8777 		  value = value >> 16;
   8778 		  r = ppc_elf_vle_split16 (input_bfd, input_section,
   8779 					   rel->r_offset,
   8780 					   contents + rel->r_offset, value,
   8781 					   split16d_type,
   8782 					   htab->params->vle_reloc_fixup);
   8783 		}
   8784 	      else if (r_type == R_PPC_VLE_SDAREL_HA16A)
   8785 		{
   8786 		  value = (value + 0x8000) >> 16;
   8787 		  r = ppc_elf_vle_split16 (input_bfd, input_section,
   8788 					   rel->r_offset,
   8789 					   contents + rel->r_offset, value,
   8790 					   split16a_type,
   8791 					   htab->params->vle_reloc_fixup);
   8792 		}
   8793 	      else if (r_type == R_PPC_VLE_SDAREL_HA16D)
   8794 		{
   8795 		  value = (value + 0x8000) >> 16;
   8796 		  r = ppc_elf_vle_split16 (input_bfd, input_section,
   8797 					   rel->r_offset,
   8798 					   contents + rel->r_offset, value,
   8799 					   split16d_type,
   8800 					   htab->params->vle_reloc_fixup);
   8801 		}
   8802 	      else
   8803 		abort ();
   8804 	    }
   8805 	  goto report_reloc;
   8806 
   8807 	case R_PPC_VLE_ADDR20:
   8808 	  if (!offset_in_range (input_section, rel->r_offset, 4))
   8809 	    r = bfd_reloc_outofrange;
   8810 	  else
   8811 	    {
   8812 	      ppc_elf_vle_split20 (output_bfd, contents + rel->r_offset,
   8813 				   relocation);
   8814 	      r = bfd_reloc_ok;
   8815 	    }
   8816 	  goto report_reloc;
   8817 
   8818 	  /* Relocate against the beginning of the section.  */
   8819 	case R_PPC_SECTOFF:
   8820 	case R_PPC_SECTOFF_LO:
   8821 	case R_PPC_SECTOFF_HI:
   8822 	case R_PPC_SECTOFF_HA:
   8823 	  if (sec == NULL || sec->output_section == NULL)
   8824 	    {
   8825 	      unresolved_reloc = true;
   8826 	      break;
   8827 	    }
   8828 	  addend -= sec->output_section->vma;
   8829 	  break;
   8830 
   8831 	  /* Negative relocations.  */
   8832 	case R_PPC_EMB_NADDR32:
   8833 	case R_PPC_EMB_NADDR16:
   8834 	case R_PPC_EMB_NADDR16_LO:
   8835 	case R_PPC_EMB_NADDR16_HI:
   8836 	case R_PPC_EMB_NADDR16_HA:
   8837 	  addend -= 2 * relocation;
   8838 	  break;
   8839 
   8840 	case R_PPC_COPY:
   8841 	case R_PPC_GLOB_DAT:
   8842 	case R_PPC_JMP_SLOT:
   8843 	case R_PPC_RELATIVE:
   8844 	case R_PPC_IRELATIVE:
   8845 	case R_PPC_PLT32:
   8846 	case R_PPC_PLTREL32:
   8847 	case R_PPC_ADDR30:
   8848 	case R_PPC_EMB_RELSEC16:
   8849 	case R_PPC_EMB_RELST_LO:
   8850 	case R_PPC_EMB_RELST_HI:
   8851 	case R_PPC_EMB_RELST_HA:
   8852 	case R_PPC_EMB_BIT_FLD:
   8853 	  /* xgettext:c-format */
   8854 	  _bfd_error_handler (_("%pB: %s unsupported"),
   8855 			      input_bfd, howto->name);
   8856 
   8857 	  bfd_set_error (bfd_error_invalid_operation);
   8858 	  ret = false;
   8859 	  goto copy_reloc;
   8860 	}
   8861 
   8862       switch (r_type)
   8863 	{
   8864 	default:
   8865 	  break;
   8866 
   8867 	case R_PPC_TPREL16_HA:
   8868 	  if (htab->do_tls_opt
   8869 	      && relocation + addend + 0x8000 < 0x10000
   8870 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   8871 
   8872 	    {
   8873 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   8874 	      bfd_put_32 (input_bfd, NOP, p);
   8875 	    }
   8876 	  break;
   8877 
   8878 	case R_PPC_TPREL16_LO:
   8879 	  if (htab->do_tls_opt
   8880 	      && relocation + addend + 0x8000 < 0x10000
   8881 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   8882 	    {
   8883 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   8884 	      unsigned int insn = bfd_get_32 (input_bfd, p);
   8885 	      insn &= ~(0x1f << 16);
   8886 	      insn |= 2 << 16;
   8887 	      bfd_put_32 (input_bfd, insn, p);
   8888 	    }
   8889 	  break;
   8890 	}
   8891 
   8892       switch (r_type)
   8893 	{
   8894 	default:
   8895 	  break;
   8896 
   8897 	case R_PPC_PLTCALL:
   8898 	  if (unresolved_reloc)
   8899 	    {
   8900 	      if (offset_in_range (input_section, rel->r_offset, 4))
   8901 		{
   8902 		  bfd_byte *p = contents + rel->r_offset;
   8903 		  unsigned int insn = bfd_get_32 (input_bfd, p);
   8904 		  insn &= 1;
   8905 		  bfd_put_32 (input_bfd, B | insn, p);
   8906 		  unresolved_reloc = save_unresolved_reloc;
   8907 		  r_type = R_PPC_REL24;
   8908 		  howto = ppc_elf_howto_table[r_type];
   8909 		}
   8910 	    }
   8911 	  else if (htab->plt_type != PLT_NEW)
   8912 	    info->callbacks->einfo
   8913 	      (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
   8914 	       input_bfd, input_section, rel->r_offset,
   8915 	       howto->name);
   8916 	  break;
   8917 
   8918 	case R_PPC_PLTSEQ:
   8919 	case R_PPC_PLT16_HA:
   8920 	case R_PPC_PLT16_LO:
   8921 	  if (unresolved_reloc)
   8922 	    {
   8923 	      if (offset_in_range (input_section, rel->r_offset & ~3, 4))
   8924 		{
   8925 		  bfd_byte *p = contents + (rel->r_offset & ~3);
   8926 		  bfd_put_32 (input_bfd, NOP, p);
   8927 		  unresolved_reloc = false;
   8928 		  r_type = R_PPC_NONE;
   8929 		  howto = ppc_elf_howto_table[r_type];
   8930 		}
   8931 	    }
   8932 	  else if (htab->plt_type != PLT_NEW)
   8933 	    info->callbacks->einfo
   8934 	      (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
   8935 	       input_bfd, input_section, rel->r_offset,
   8936 	       howto->name);
   8937 	  break;
   8938 	}
   8939 
   8940       /* Do any further special processing.  */
   8941       switch (r_type)
   8942 	{
   8943 	default:
   8944 	  break;
   8945 
   8946 	case R_PPC_ADDR16_HA:
   8947 	case R_PPC_REL16_HA:
   8948 	case R_PPC_REL16DX_HA:
   8949 	case R_PPC_SECTOFF_HA:
   8950 	case R_PPC_TPREL16_HA:
   8951 	case R_PPC_DTPREL16_HA:
   8952 	case R_PPC_EMB_NADDR16_HA:
   8953 	case R_PPC_EMB_RELST_HA:
   8954 	  /* It's just possible that this symbol is a weak symbol
   8955 	     that's not actually defined anywhere.  In that case,
   8956 	     'sec' would be NULL, and we should leave the symbol
   8957 	     alone (it will be set to zero elsewhere in the link).  */
   8958 	  if (sec == NULL)
   8959 	    break;
   8960 	  /* Fall through.  */
   8961 
   8962 	case R_PPC_PLT16_HA:
   8963 	case R_PPC_GOT16_HA:
   8964 	case R_PPC_GOT_TLSGD16_HA:
   8965 	case R_PPC_GOT_TLSLD16_HA:
   8966 	case R_PPC_GOT_TPREL16_HA:
   8967 	case R_PPC_GOT_DTPREL16_HA:
   8968 	  /* Add 0x10000 if sign bit in 0:15 is set.
   8969 	     Bits 0:15 are not used.  */
   8970 	  addend += 0x8000;
   8971 	  break;
   8972 
   8973 	case R_PPC_ADDR16:
   8974 	case R_PPC_ADDR16_LO:
   8975 	case R_PPC_GOT16:
   8976 	case R_PPC_GOT16_LO:
   8977 	case R_PPC_SDAREL16:
   8978 	case R_PPC_SECTOFF:
   8979 	case R_PPC_SECTOFF_LO:
   8980 	case R_PPC_DTPREL16:
   8981 	case R_PPC_DTPREL16_LO:
   8982 	case R_PPC_TPREL16:
   8983 	case R_PPC_TPREL16_LO:
   8984 	case R_PPC_GOT_TLSGD16:
   8985 	case R_PPC_GOT_TLSGD16_LO:
   8986 	case R_PPC_GOT_TLSLD16:
   8987 	case R_PPC_GOT_TLSLD16_LO:
   8988 	case R_PPC_GOT_DTPREL16:
   8989 	case R_PPC_GOT_DTPREL16_LO:
   8990 	case R_PPC_GOT_TPREL16:
   8991 	case R_PPC_GOT_TPREL16_LO:
   8992 	  if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
   8993 	    {
   8994 	      /* The 32-bit ABI lacks proper relocations to deal with
   8995 		 certain 64-bit instructions.  Prevent damage to bits
   8996 		 that make up part of the insn opcode.  */
   8997 	      unsigned int insn, mask, lobit;
   8998 
   8999 	      insn = bfd_get_32 (input_bfd,
   9000 				 contents + rel->r_offset - d_offset);
   9001 	      mask = 0;
   9002 	      if (is_insn_ds_form (insn))
   9003 		mask = 3;
   9004 	      else if (is_insn_dq_form (insn))
   9005 		mask = 15;
   9006 	      else
   9007 		break;
   9008 	      relocation += addend;
   9009 	      addend = insn & mask;
   9010 	      lobit = mask & relocation;
   9011 	      if (lobit != 0)
   9012 		{
   9013 		  relocation ^= lobit;
   9014 		  info->callbacks->einfo
   9015 		    /* xgettext:c-format */
   9016 		    (_("%H: error: %s against `%s' not a multiple of %u\n"),
   9017 		     input_bfd, input_section, rel->r_offset,
   9018 		     howto->name, sym_name, mask + 1);
   9019 		  bfd_set_error (bfd_error_bad_value);
   9020 		  ret = false;
   9021 		}
   9022 	    }
   9023 	  break;
   9024 	}
   9025 
   9026 #ifdef DEBUG
   9027       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
   9028 	       "offset = %ld, addend = %ld\n",
   9029 	       howto->name,
   9030 	       (int) r_type,
   9031 	       sym_name,
   9032 	       r_symndx,
   9033 	       (long) rel->r_offset,
   9034 	       (long) addend);
   9035 #endif
   9036 
   9037       if (unresolved_reloc
   9038 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   9039 	       && h->def_dynamic)
   9040 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   9041 				      rel->r_offset) != (bfd_vma) -1)
   9042 	{
   9043 	  info->callbacks->einfo
   9044 	    /* xgettext:c-format */
   9045 	    (_("%H: unresolvable %s relocation against symbol `%s'\n"),
   9046 	     input_bfd, input_section, rel->r_offset,
   9047 	     howto->name,
   9048 	     sym_name);
   9049 	  ret = false;
   9050 	}
   9051 
   9052       /* 16-bit fields in insns mostly have signed values, but a
   9053 	 few insns have 16-bit unsigned values.  Really, we should
   9054 	 have different reloc types.  */
   9055       if (howto->complain_on_overflow != complain_overflow_dont
   9056 	  && howto->dst_mask == 0xffff
   9057 	  && (input_section->flags & SEC_CODE) != 0
   9058 	  && offset_in_range (input_section, rel->r_offset & ~3, 4))
   9059 	{
   9060 	  enum complain_overflow complain = complain_overflow_signed;
   9061 
   9062 	  if ((elf_section_flags (input_section) & SHF_PPC_VLE) == 0)
   9063 	    {
   9064 	      unsigned int insn;
   9065 
   9066 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   9067 	      if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
   9068 		complain = complain_overflow_bitfield;
   9069 	      else if ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
   9070 		       || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
   9071 		       || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
   9072 		complain = complain_overflow_unsigned;
   9073 	    }
   9074 	  if (howto->complain_on_overflow != complain)
   9075 	    {
   9076 	      alt_howto = *howto;
   9077 	      alt_howto.complain_on_overflow = complain;
   9078 	      howto = &alt_howto;
   9079 	    }
   9080 	}
   9081 
   9082       if (r_type == R_PPC_REL16DX_HA)
   9083 	{
   9084 	  /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
   9085 	  if (offset_in_range (input_section, rel->r_offset, 4))
   9086 	    r = bfd_reloc_outofrange;
   9087 	  else
   9088 	    {
   9089 	      unsigned int insn;
   9090 
   9091 	      relocation += addend;
   9092 	      relocation -= (rel->r_offset
   9093 			     + input_section->output_offset
   9094 			     + input_section->output_section->vma);
   9095 	      relocation >>= 16;
   9096 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   9097 	      insn &= ~0x1fffc1;
   9098 	      insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
   9099 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   9100 	      r = bfd_reloc_ok;
   9101 	    }
   9102 	}
   9103       else
   9104 	r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
   9105 				      rel->r_offset, relocation, addend);
   9106 
   9107     report_reloc:
   9108       if (r != bfd_reloc_ok)
   9109 	{
   9110 	  if (r == bfd_reloc_overflow)
   9111 	    {
   9112 	      /* On code like "if (foo) foo();" don't report overflow
   9113 		 on a branch to zero when foo is undefined.  */
   9114 	      if (!warned
   9115 		  && !(h != NULL
   9116 		       && (h->root.type == bfd_link_hash_undefweak
   9117 			   || h->root.type == bfd_link_hash_undefined)
   9118 		       && is_branch_reloc (r_type)))
   9119 		info->callbacks->reloc_overflow
   9120 		  (info, (h ? &h->root : NULL), sym_name, howto->name,
   9121 		   rel->r_addend, input_bfd, input_section, rel->r_offset);
   9122 	    }
   9123 	  else
   9124 	    {
   9125 	      info->callbacks->einfo
   9126 		/* xgettext:c-format */
   9127 		(_("%H: %s reloc against `%s': error %d\n"),
   9128 		 input_bfd, input_section, rel->r_offset,
   9129 		 howto->name, sym_name, (int) r);
   9130 	      ret = false;
   9131 	    }
   9132 	}
   9133     copy_reloc:
   9134       if (wrel != rel)
   9135 	*wrel = *rel;
   9136     }
   9137 
   9138   if (wrel != rel)
   9139     {
   9140       Elf_Internal_Shdr *rel_hdr;
   9141       size_t deleted = rel - wrel;
   9142 
   9143       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
   9144       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   9145       relend = wrel;
   9146       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
   9147       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   9148       input_section->reloc_count -= deleted;
   9149     }
   9150 
   9151 #ifdef DEBUG
   9152   fprintf (stderr, "\n");
   9153 #endif
   9154 
   9155   if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET
   9156       && input_section->size != input_section->rawsize
   9157       && (strcmp (input_section->output_section->name, ".init") == 0
   9158 	  || strcmp (input_section->output_section->name, ".fini") == 0))
   9159     {
   9160       /* Branch around the trampolines.  */
   9161       unsigned int insn = B + input_section->size - input_section->rawsize;
   9162       bfd_put_32 (input_bfd, insn, contents + input_section->rawsize);
   9163     }
   9164 
   9165   if (htab->params->ppc476_workaround
   9166       && input_section->sec_info_type == SEC_INFO_TYPE_TARGET
   9167       && (!bfd_link_relocatable (info)
   9168 	  || (input_section->output_section->alignment_power
   9169 	      >= htab->params->pagesize_p2)))
   9170     {
   9171       bfd_vma start_addr, end_addr, addr;
   9172       bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
   9173 
   9174       if (relax_info->workaround_size != 0)
   9175 	{
   9176 	  bfd_byte *p;
   9177 	  unsigned int n;
   9178 	  bfd_byte fill[4];
   9179 
   9180 	  bfd_put_32 (input_bfd, BA, fill);
   9181 	  p = contents + input_section->size - relax_info->workaround_size;
   9182 	  n = relax_info->workaround_size >> 2;
   9183 	  while (n--)
   9184 	    {
   9185 	      memcpy (p, fill, 4);
   9186 	      p += 4;
   9187 	    }
   9188 	}
   9189 
   9190       /* The idea is: Replace the last instruction on a page with a
   9191 	 branch to a patch area.  Put the insn there followed by a
   9192 	 branch back to the next page.  Complicated a little by
   9193 	 needing to handle moved conditional branches, and by not
   9194 	 wanting to touch data-in-text.  */
   9195 
   9196       start_addr = (input_section->output_section->vma
   9197 		    + input_section->output_offset);
   9198       end_addr = (start_addr + input_section->size
   9199 		  - relax_info->workaround_size);
   9200       for (addr = ((start_addr & -pagesize) + pagesize - 4);
   9201 	   addr < end_addr;
   9202 	   addr += pagesize)
   9203 	{
   9204 	  bfd_vma offset = addr - start_addr;
   9205 	  Elf_Internal_Rela *lo, *hi;
   9206 	  bool is_data;
   9207 	  bfd_vma patch_off, patch_addr;
   9208 	  unsigned int insn;
   9209 
   9210 	  /* Do we have a data reloc at this offset?  If so, leave
   9211 	     the word alone.  */
   9212 	  is_data = false;
   9213 	  lo = relocs;
   9214 	  hi = relend;
   9215 	  rel = NULL;
   9216 	  while (lo < hi)
   9217 	    {
   9218 	      rel = lo + (hi - lo) / 2;
   9219 	      if (rel->r_offset < offset)
   9220 		lo = rel + 1;
   9221 	      else if (rel->r_offset > offset + 3)
   9222 		hi = rel;
   9223 	      else
   9224 		{
   9225 		  switch (ELF32_R_TYPE (rel->r_info))
   9226 		    {
   9227 		    case R_PPC_ADDR32:
   9228 		    case R_PPC_UADDR32:
   9229 		    case R_PPC_REL32:
   9230 		    case R_PPC_ADDR30:
   9231 		      is_data = true;
   9232 		      break;
   9233 		    default:
   9234 		      break;
   9235 		    }
   9236 		  break;
   9237 		}
   9238 	    }
   9239 	  if (is_data)
   9240 	    continue;
   9241 
   9242 	  /* Some instructions can be left alone too.  Unconditional
   9243 	     branches, except for bcctr with BO=0x14 (bctr, bctrl),
   9244 	     avoid the icache failure.
   9245 
   9246 	     The problem occurs due to prefetch across a page boundary
   9247 	     where stale instructions can be fetched from the next
   9248 	     page, and the mechanism for flushing these bad
   9249 	     instructions fails under certain circumstances.  The
   9250 	     unconditional branches:
   9251 	     1) Branch: b, bl, ba, bla,
   9252 	     2) Branch Conditional: bc, bca, bcl, bcla,
   9253 	     3) Branch Conditional to Link Register: bclr, bclrl,
   9254 	     where (2) and (3) have BO=0x14 making them unconditional,
   9255 	     prevent the bad prefetch because the prefetch itself is
   9256 	     affected by these instructions.  This happens even if the
   9257 	     instruction is not executed.
   9258 
   9259 	     A bctr example:
   9260 	     .
   9261 	     .	lis 9,new_page@ha
   9262 	     .	addi 9,9,new_page@l
   9263 	     .	mtctr 9
   9264 	     .	bctr
   9265 	     .	nop
   9266 	     .	nop
   9267 	     . new_page:
   9268 	     .
   9269 	     The bctr is not predicted taken due to ctr not being
   9270 	     ready, so prefetch continues on past the bctr into the
   9271 	     new page which might have stale instructions.  If they
   9272 	     fail to be flushed, then they will be executed after the
   9273 	     bctr executes.  Either of the following modifications
   9274 	     prevent the bad prefetch from happening in the first
   9275 	     place:
   9276 	     .
   9277 	     .	lis 9,new_page@ha	 lis 9,new_page@ha
   9278 	     .	addi 9,9,new_page@l	 addi 9,9,new_page@l
   9279 	     .	mtctr 9			 mtctr 9
   9280 	     .	bctr			 bctr
   9281 	     .	nop			 b somewhere_else
   9282 	     .	b somewhere_else	 nop
   9283 	     . new_page:		new_page:
   9284 	     .  */
   9285 	  insn = bfd_get_32 (input_bfd, contents + offset);
   9286 	  if ((insn & (0x3fu << 26)) == (18u << 26)	    /* b,bl,ba,bla */
   9287 	      || ((insn & (0x3fu << 26)) == (16u << 26)	    /* bc,bcl,bca,bcla*/
   9288 		  && (insn & (0x14 << 21)) == (0x14 << 21)) /*	 with BO=0x14 */
   9289 	      || ((insn & (0x3fu << 26)) == (19u << 26)
   9290 		  && (insn & (0x3ff << 1)) == (16u << 1)    /* bclr,bclrl */
   9291 		  && (insn & (0x14 << 21)) == (0x14 << 21)))/*	 with BO=0x14 */
   9292 	    continue;
   9293 
   9294 	  patch_addr = (start_addr + input_section->size
   9295 			- relax_info->workaround_size);
   9296 	  patch_addr = (patch_addr + 15) & -16;
   9297 	  patch_off = patch_addr - start_addr;
   9298 	  bfd_put_32 (input_bfd, B + patch_off - offset, contents + offset);
   9299 
   9300 	  if (rel != NULL
   9301 	      && rel->r_offset >= offset
   9302 	      && rel->r_offset < offset + 4)
   9303 	    {
   9304 	      asection *sreloc;
   9305 
   9306 	      /* If the insn we are patching had a reloc, adjust the
   9307 		 reloc r_offset so that the reloc applies to the moved
   9308 		 location.  This matters for -r and --emit-relocs.  */
   9309 	      if (rel + 1 != relend)
   9310 		{
   9311 		  Elf_Internal_Rela tmp = *rel;
   9312 
   9313 		  /* Keep the relocs sorted by r_offset.  */
   9314 		  memmove (rel, rel + 1, (relend - (rel + 1)) * sizeof (*rel));
   9315 		  relend[-1] = tmp;
   9316 		}
   9317 	      relend[-1].r_offset += patch_off - offset;
   9318 
   9319 	      /* Adjust REL16 addends too.  */
   9320 	      switch (ELF32_R_TYPE (relend[-1].r_info))
   9321 		{
   9322 		case R_PPC_REL16:
   9323 		case R_PPC_REL16_LO:
   9324 		case R_PPC_REL16_HI:
   9325 		case R_PPC_REL16_HA:
   9326 		  relend[-1].r_addend += patch_off - offset;
   9327 		  break;
   9328 		default:
   9329 		  break;
   9330 		}
   9331 
   9332 	      /* If we are building a PIE or shared library with
   9333 		 non-PIC objects, perhaps we had a dynamic reloc too?
   9334 		 If so, the dynamic reloc must move with the insn.  */
   9335 	      sreloc = elf_section_data (input_section)->sreloc;
   9336 	      if (sreloc != NULL)
   9337 		{
   9338 		  Elf32_External_Rela *slo, *shi, *srelend;
   9339 		  bfd_vma soffset;
   9340 
   9341 		  slo = (Elf32_External_Rela *) sreloc->contents;
   9342 		  shi = srelend = slo + sreloc->reloc_count;
   9343 		  soffset = (offset + input_section->output_section->vma
   9344 			     + input_section->output_offset);
   9345 		  while (slo < shi)
   9346 		    {
   9347 		      Elf32_External_Rela *srel = slo + (shi - slo) / 2;
   9348 		      bfd_elf32_swap_reloca_in (output_bfd, (bfd_byte *) srel,
   9349 						&outrel);
   9350 		      if (outrel.r_offset < soffset)
   9351 			slo = srel + 1;
   9352 		      else if (outrel.r_offset > soffset + 3)
   9353 			shi = srel;
   9354 		      else
   9355 			{
   9356 			  if (srel + 1 != srelend)
   9357 			    {
   9358 			      memmove (srel, srel + 1,
   9359 				       (srelend - (srel + 1)) * sizeof (*srel));
   9360 			      srel = srelend - 1;
   9361 			    }
   9362 			  outrel.r_offset += patch_off - offset;
   9363 			  bfd_elf32_swap_reloca_out (output_bfd, &outrel,
   9364 						     (bfd_byte *) srel);
   9365 			  break;
   9366 			}
   9367 		    }
   9368 		}
   9369 	    }
   9370 	  else
   9371 	    rel = NULL;
   9372 
   9373 	  if ((insn & (0x3fu << 26)) == (16u << 26) /* bc */
   9374 	      && (insn & 2) == 0 /* relative */)
   9375 	    {
   9376 	      bfd_vma delta = ((insn & 0xfffc) ^ 0x8000) - 0x8000;
   9377 
   9378 	      delta += offset - patch_off;
   9379 	      if (bfd_link_relocatable (info) && rel != NULL)
   9380 		delta = 0;
   9381 	      if (!bfd_link_relocatable (info) && rel != NULL)
   9382 		{
   9383 		  enum elf_ppc_reloc_type r_type;
   9384 
   9385 		  r_type = ELF32_R_TYPE (relend[-1].r_info);
   9386 		  if (r_type == R_PPC_REL14_BRTAKEN)
   9387 		    insn |= BRANCH_PREDICT_BIT;
   9388 		  else if (r_type == R_PPC_REL14_BRNTAKEN)
   9389 		    insn &= ~BRANCH_PREDICT_BIT;
   9390 		  else
   9391 		    BFD_ASSERT (r_type == R_PPC_REL14);
   9392 
   9393 		  if ((r_type == R_PPC_REL14_BRTAKEN
   9394 		       || r_type == R_PPC_REL14_BRNTAKEN)
   9395 		      && delta + 0x8000 < 0x10000
   9396 		      && (bfd_signed_vma) delta < 0)
   9397 		    insn ^= BRANCH_PREDICT_BIT;
   9398 		}
   9399 	      if (delta + 0x8000 < 0x10000)
   9400 		{
   9401 		  bfd_put_32 (input_bfd,
   9402 			      (insn & ~0xfffc) | (delta & 0xfffc),
   9403 			      contents + patch_off);
   9404 		  patch_off += 4;
   9405 		  bfd_put_32 (input_bfd,
   9406 			      B | ((offset + 4 - patch_off) & 0x3fffffc),
   9407 			      contents + patch_off);
   9408 		  patch_off += 4;
   9409 		}
   9410 	      else
   9411 		{
   9412 		  if (rel != NULL)
   9413 		    {
   9414 		      unsigned int r_sym = ELF32_R_SYM (relend[-1].r_info);
   9415 
   9416 		      relend[-1].r_offset += 8;
   9417 		      relend[-1].r_info = ELF32_R_INFO (r_sym, R_PPC_REL24);
   9418 		    }
   9419 		  bfd_put_32 (input_bfd,
   9420 			      (insn & ~0xfffc) | 8,
   9421 			      contents + patch_off);
   9422 		  patch_off += 4;
   9423 		  bfd_put_32 (input_bfd,
   9424 			      B | ((offset + 4 - patch_off) & 0x3fffffc),
   9425 			      contents + patch_off);
   9426 		  patch_off += 4;
   9427 		  bfd_put_32 (input_bfd,
   9428 			      B | ((delta - 8) & 0x3fffffc),
   9429 			      contents + patch_off);
   9430 		  patch_off += 4;
   9431 		}
   9432 	    }
   9433 	  else
   9434 	    {
   9435 	      bfd_put_32 (input_bfd, insn, contents + patch_off);
   9436 	      patch_off += 4;
   9437 	      bfd_put_32 (input_bfd,
   9438 			  B | ((offset + 4 - patch_off) & 0x3fffffc),
   9439 			  contents + patch_off);
   9440 	      patch_off += 4;
   9441 	    }
   9442 	  BFD_ASSERT (patch_off <= input_section->size);
   9443 	  relax_info->workaround_size = input_section->size - patch_off;
   9444 	}
   9445     }
   9446 
   9447   return ret;
   9448 }
   9449 
   9450 /* Write out the PLT relocs and entries for H.  */
   9452 
   9453 static bool
   9454 write_global_sym_plt (struct elf_link_hash_entry *h, void *inf)
   9455 {
   9456   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   9457   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9458   struct plt_entry *ent;
   9459   bool doneone;
   9460 
   9461   doneone = false;
   9462   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   9463     if (ent->plt.offset != (bfd_vma) -1)
   9464       {
   9465 	bool dyn = !use_local_plt (info, h);
   9466 
   9467 	if (!doneone)
   9468 	  {
   9469 	    Elf_Internal_Rela rela;
   9470 	    bfd_byte *loc;
   9471 	    bfd_vma reloc_index;
   9472 	    asection *plt = htab->elf.splt;
   9473 	    asection *relplt = htab->elf.srelplt;
   9474 
   9475 	    if (htab->plt_type == PLT_NEW || !dyn)
   9476 	      reloc_index = ent->plt.offset / 4;
   9477 	    else
   9478 	      {
   9479 		reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
   9480 			       / htab->plt_slot_size);
   9481 		if (reloc_index > PLT_NUM_SINGLE_ENTRIES
   9482 		    && htab->plt_type == PLT_OLD)
   9483 		  reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
   9484 	      }
   9485 
   9486 	    /* This symbol has an entry in the procedure linkage table.
   9487 	       Set it up.  */
   9488 	    if (htab->plt_type == PLT_VXWORKS && dyn)
   9489 	      {
   9490 		bfd_vma got_offset;
   9491 		const bfd_vma *plt_entry;
   9492 
   9493 		/* The first three entries in .got.plt are reserved.  */
   9494 		got_offset = (reloc_index + 3) * 4;
   9495 
   9496 		/* Use the right PLT. */
   9497 		plt_entry = bfd_link_pic (info) ? ppc_elf_vxworks_pic_plt_entry
   9498 			    : ppc_elf_vxworks_plt_entry;
   9499 
   9500 		/* Fill in the .plt on VxWorks.  */
   9501 		if (bfd_link_pic (info))
   9502 		  {
   9503 		    bfd_put_32 (info->output_bfd,
   9504 				plt_entry[0] | PPC_HA (got_offset),
   9505 				plt->contents + ent->plt.offset + 0);
   9506 		    bfd_put_32 (info->output_bfd,
   9507 				plt_entry[1] | PPC_LO (got_offset),
   9508 				plt->contents + ent->plt.offset + 4);
   9509 		  }
   9510 		else
   9511 		  {
   9512 		    bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
   9513 
   9514 		    bfd_put_32 (info->output_bfd,
   9515 				plt_entry[0] | PPC_HA (got_loc),
   9516 				plt->contents + ent->plt.offset + 0);
   9517 		    bfd_put_32 (info->output_bfd,
   9518 				plt_entry[1] | PPC_LO (got_loc),
   9519 				plt->contents + ent->plt.offset + 4);
   9520 		  }
   9521 
   9522 		bfd_put_32 (info->output_bfd, plt_entry[2],
   9523 			    plt->contents + ent->plt.offset + 8);
   9524 		bfd_put_32 (info->output_bfd, plt_entry[3],
   9525 			    plt->contents + ent->plt.offset + 12);
   9526 
   9527 		/* This instruction is an immediate load.  The value loaded is
   9528 		   the byte offset of the R_PPC_JMP_SLOT relocation from the
   9529 		   start of the .rela.plt section.  The value is stored in the
   9530 		   low-order 16 bits of the load instruction.  */
   9531 		/* NOTE: It appears that this is now an index rather than a
   9532 		   prescaled offset.  */
   9533 		bfd_put_32 (info->output_bfd,
   9534 			    plt_entry[4] | reloc_index,
   9535 			    plt->contents + ent->plt.offset + 16);
   9536 		/* This instruction is a PC-relative branch whose target is
   9537 		   the start of the PLT section.  The address of this branch
   9538 		   instruction is 20 bytes beyond the start of this PLT entry.
   9539 		   The address is encoded in bits 6-29, inclusive.  The value
   9540 		   stored is right-shifted by two bits, permitting a 26-bit
   9541 		   offset.  */
   9542 		bfd_put_32 (info->output_bfd,
   9543 			    (plt_entry[5]
   9544 			     | (-(ent->plt.offset + 20) & 0x03fffffc)),
   9545 			    plt->contents + ent->plt.offset + 20);
   9546 		bfd_put_32 (info->output_bfd, plt_entry[6],
   9547 			    plt->contents + ent->plt.offset + 24);
   9548 		bfd_put_32 (info->output_bfd, plt_entry[7],
   9549 			    plt->contents + ent->plt.offset + 28);
   9550 
   9551 		/* Fill in the GOT entry corresponding to this PLT slot with
   9552 		   the address immediately after the "bctr" instruction
   9553 		   in this PLT entry.  */
   9554 		bfd_put_32 (info->output_bfd, (plt->output_section->vma
   9555 					       + plt->output_offset
   9556 					       + ent->plt.offset + 16),
   9557 			    htab->elf.sgotplt->contents + got_offset);
   9558 
   9559 		if (!bfd_link_pic (info))
   9560 		  {
   9561 		    /* Fill in a couple of entries in .rela.plt.unloaded.  */
   9562 		    loc = htab->srelplt2->contents
   9563 		      + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
   9564 			  * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
   9565 			 * sizeof (Elf32_External_Rela));
   9566 
   9567 		    /* Provide the @ha relocation for the first instruction.  */
   9568 		    rela.r_offset = (plt->output_section->vma
   9569 				     + plt->output_offset
   9570 				     + ent->plt.offset + 2);
   9571 		    rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
   9572 						R_PPC_ADDR16_HA);
   9573 		    rela.r_addend = got_offset;
   9574 		    BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela, loc,
   9575 						htab->srelplt2));
   9576 		    loc += sizeof (Elf32_External_Rela);
   9577 
   9578 		    /* Provide the @l relocation for the second instruction.  */
   9579 		    rela.r_offset = (plt->output_section->vma
   9580 				     + plt->output_offset
   9581 				     + ent->plt.offset + 6);
   9582 		    rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
   9583 						R_PPC_ADDR16_LO);
   9584 		    rela.r_addend = got_offset;
   9585 		    BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela, loc,
   9586 						htab->srelplt2));
   9587 		    loc += sizeof (Elf32_External_Rela);
   9588 
   9589 		    /* Provide a relocation for the GOT entry corresponding to this
   9590 		       PLT slot.  Point it at the middle of the .plt entry.  */
   9591 		    rela.r_offset = (htab->elf.sgotplt->output_section->vma
   9592 				     + htab->elf.sgotplt->output_offset
   9593 				     + got_offset);
   9594 		    rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
   9595 						R_PPC_ADDR32);
   9596 		    rela.r_addend = ent->plt.offset + 16;
   9597 		    BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela, loc,
   9598 						htab->srelplt2));
   9599 		  }
   9600 
   9601 		/* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
   9602 		   In particular, the offset for the relocation is not the
   9603 		   address of the PLT entry for this function, as specified
   9604 		   by the ABI.  Instead, the offset is set to the address of
   9605 		   the GOT slot for this function.  See EABI 4.4.4.1.  */
   9606 		rela.r_offset = (htab->elf.sgotplt->output_section->vma
   9607 				 + htab->elf.sgotplt->output_offset
   9608 				 + got_offset);
   9609 		rela.r_addend = 0;
   9610 	      }
   9611 	    else
   9612 	      {
   9613 		rela.r_addend = 0;
   9614 		if (!dyn)
   9615 		  {
   9616 		    if (h->type == STT_GNU_IFUNC)
   9617 		      {
   9618 			plt = htab->elf.iplt;
   9619 			relplt = htab->elf.irelplt;
   9620 		      }
   9621 		    else
   9622 		      {
   9623 			plt = htab->pltlocal;
   9624 			relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
   9625 		      }
   9626 		    if (h->def_regular
   9627 			&& (h->root.type == bfd_link_hash_defined
   9628 			    || h->root.type == bfd_link_hash_defweak))
   9629 		      rela.r_addend = SYM_VAL (h);
   9630 		  }
   9631 
   9632 		if (relplt == NULL)
   9633 		  {
   9634 		    loc = plt->contents + ent->plt.offset;
   9635 		    bfd_put_32 (info->output_bfd, rela.r_addend, loc);
   9636 		  }
   9637 		else
   9638 		  {
   9639 		    rela.r_offset = (plt->output_section->vma
   9640 				     + plt->output_offset
   9641 				     + ent->plt.offset);
   9642 
   9643 		    if (htab->plt_type == PLT_OLD || !dyn)
   9644 		      {
   9645 			/* We don't need to fill in the .plt.  The ppc dynamic
   9646 			   linker will fill it in.  */
   9647 		      }
   9648 		    else
   9649 		      {
   9650 			bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
   9651 				       + htab->glink->output_section->vma
   9652 				       + htab->glink->output_offset);
   9653 			bfd_put_32 (info->output_bfd, val,
   9654 				    plt->contents + ent->plt.offset);
   9655 		      }
   9656 		  }
   9657 	      }
   9658 
   9659 	    if (relplt != NULL)
   9660 	      {
   9661 		/* Fill in the entry in the .rela.plt section.  */
   9662 		if (!dyn)
   9663 		  {
   9664 		    if (h->type == STT_GNU_IFUNC)
   9665 		      rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   9666 		    else
   9667 		      rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   9668 		    loc = relplt->contents + (relplt->reloc_count++
   9669 					      * sizeof (Elf32_External_Rela));
   9670 		    htab->local_ifunc_resolver = 1;
   9671 		  }
   9672 		else
   9673 		  {
   9674 		    rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
   9675 		    loc = relplt->contents + (reloc_index
   9676 					      * sizeof (Elf32_External_Rela));
   9677 		    if (h->type == STT_GNU_IFUNC && is_static_defined (h))
   9678 		      htab->maybe_local_ifunc_resolver = 1;
   9679 		  }
   9680 		BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
   9681 					    loc, relplt));
   9682 	      }
   9683 	    doneone = true;
   9684 	  }
   9685 
   9686 	if (htab->plt_type == PLT_NEW || !dyn)
   9687 	  {
   9688 	    unsigned char *p;
   9689 	    asection *plt = htab->elf.splt;
   9690 
   9691 	    if (!dyn)
   9692 	      {
   9693 		if (h->type == STT_GNU_IFUNC)
   9694 		  plt = htab->elf.iplt;
   9695 		else
   9696 		  break;
   9697 	      }
   9698 
   9699 	    p = (unsigned char *) htab->glink->contents + ent->glink_offset;
   9700 	    write_glink_stub (h, ent, plt, p, info);
   9701 
   9702 	    if (!bfd_link_pic (info))
   9703 	      /* We only need one non-PIC glink stub.  */
   9704 	      break;
   9705 	  }
   9706 	else
   9707 	  break;
   9708       }
   9709   return true;
   9710 }
   9711 
   9712 /* Finish up PLT handling.  */
   9713 
   9714 bool
   9715 ppc_finish_symbols (struct bfd_link_info *info)
   9716 {
   9717   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9718   bfd *ibfd;
   9719 
   9720   if (!htab)
   9721     return true;
   9722 
   9723   elf_link_hash_traverse (&htab->elf, write_global_sym_plt, info);
   9724 
   9725   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9726     {
   9727       bfd_vma *local_got, *end_local_got;
   9728       struct plt_entry **local_plt, **lplt, **end_local_plt;
   9729       Elf_Internal_Shdr *symtab_hdr;
   9730       bfd_size_type locsymcount;
   9731       Elf_Internal_Sym *local_syms = NULL;
   9732       struct plt_entry *ent;
   9733 
   9734       if (!is_ppc_elf (ibfd))
   9735 	continue;
   9736 
   9737       local_got = elf_local_got_offsets (ibfd);
   9738       if (!local_got)
   9739 	continue;
   9740 
   9741       symtab_hdr = &elf_symtab_hdr (ibfd);
   9742       locsymcount = symtab_hdr->sh_info;
   9743       end_local_got = local_got + locsymcount;
   9744       local_plt = (struct plt_entry **) end_local_got;
   9745       end_local_plt = local_plt + locsymcount;
   9746       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
   9747 	for (ent = *lplt; ent != NULL; ent = ent->next)
   9748 	  {
   9749 	    if (ent->plt.offset != (bfd_vma) -1)
   9750 	      {
   9751 		Elf_Internal_Sym *sym;
   9752 		asection *sym_sec;
   9753 		asection *plt, *relplt;
   9754 		bfd_byte *loc;
   9755 		bfd_vma val;
   9756 		Elf_Internal_Rela rela;
   9757 		unsigned char *p;
   9758 
   9759 		if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
   9760 				lplt - local_plt, ibfd))
   9761 		  {
   9762 		    if (symtab_hdr->contents != (unsigned char *) local_syms)
   9763 		      free (local_syms);
   9764 		    return false;
   9765 		  }
   9766 
   9767 		val = sym->st_value;
   9768 		if (sym_sec != NULL && sym_sec->output_section != NULL)
   9769 		  val += sym_sec->output_offset + sym_sec->output_section->vma;
   9770 
   9771 		if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   9772 		  {
   9773 		    htab->local_ifunc_resolver = 1;
   9774 		    plt = htab->elf.iplt;
   9775 		    relplt = htab->elf.irelplt;
   9776 		    rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   9777 		  }
   9778 		else
   9779 		  {
   9780 		    plt = htab->pltlocal;
   9781 		    if (bfd_link_pic (info))
   9782 		      {
   9783 			relplt = htab->relpltlocal;
   9784 			rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   9785 		      }
   9786 		    else
   9787 		      {
   9788 			loc = plt->contents + ent->plt.offset;
   9789 			bfd_put_32 (info->output_bfd, val, loc);
   9790 			continue;
   9791 		      }
   9792 		  }
   9793 
   9794 		rela.r_offset = (ent->plt.offset
   9795 				 + plt->output_offset
   9796 				 + plt->output_section->vma);
   9797 		rela.r_addend = val;
   9798 		BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
   9799 						      relplt));
   9800 
   9801 		p = (unsigned char *) htab->glink->contents + ent->glink_offset;
   9802 		write_glink_stub (NULL, ent, htab->elf.iplt, p, info);
   9803 	      }
   9804 	  }
   9805 
   9806       if (local_syms != NULL
   9807 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9808 	{
   9809 	  if (!info->keep_memory)
   9810 	    free (local_syms);
   9811 	  else
   9812 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9813 	}
   9814     }
   9815   return true;
   9816 }
   9817 
   9818 /* Finish up dynamic symbol handling.  We set the contents of various
   9819    dynamic sections here.  */
   9820 
   9821 static bool
   9822 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
   9823 			       struct bfd_link_info *info,
   9824 			       struct elf_link_hash_entry *h,
   9825 			       Elf_Internal_Sym *sym)
   9826 {
   9827   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9828   struct plt_entry *ent;
   9829 
   9830 #ifdef DEBUG
   9831   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
   9832 	   h->root.root.string);
   9833 #endif
   9834 
   9835   if (!h->def_regular
   9836       || (h->type == STT_GNU_IFUNC && !bfd_link_pic (info)))
   9837     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   9838       if (ent->plt.offset != (bfd_vma) -1)
   9839 	{
   9840 	  if (!h->def_regular)
   9841 	    {
   9842 	      /* Mark the symbol as undefined, rather than as
   9843 		 defined in the .plt section.  Leave the value if
   9844 		 there were any relocations where pointer equality
   9845 		 matters (this is a clue for the dynamic linker, to
   9846 		 make function pointer comparisons work between an
   9847 		 application and shared library), otherwise set it
   9848 		 to zero.  */
   9849 	      sym->st_shndx = SHN_UNDEF;
   9850 	      if (!h->pointer_equality_needed)
   9851 		sym->st_value = 0;
   9852 	      else if (!h->ref_regular_nonweak)
   9853 		{
   9854 		  /* This breaks function pointer comparisons, but
   9855 		     that is better than breaking tests for a NULL
   9856 		     function pointer.  */
   9857 		  sym->st_value = 0;
   9858 		}
   9859 	    }
   9860 	  else
   9861 	    {
   9862 	      /* Set the value of ifunc symbols in a non-pie
   9863 		 executable to the glink entry.  This is to avoid
   9864 		 text relocations.  We can't do this for ifunc in
   9865 		 allocate_dynrelocs, as we do for normal dynamic
   9866 		 function symbols with plt entries, because we need
   9867 		 to keep the original value around for the ifunc
   9868 		 relocation.  */
   9869 	      sym->st_shndx
   9870 		= (_bfd_elf_section_from_bfd_section
   9871 		   (info->output_bfd, htab->glink->output_section));
   9872 	      sym->st_value = (ent->glink_offset
   9873 			       + htab->glink->output_offset
   9874 			       + htab->glink->output_section->vma);
   9875 	    }
   9876 	  break;
   9877 	}
   9878 
   9879   if (h->needs_copy)
   9880     {
   9881       asection *s;
   9882       Elf_Internal_Rela rela;
   9883 
   9884       /* This symbols needs a copy reloc.  Set it up.  */
   9885 
   9886 #ifdef DEBUG
   9887       fprintf (stderr, ", copy");
   9888 #endif
   9889 
   9890       BFD_ASSERT (h->dynindx != -1);
   9891 
   9892       if (ppc_elf_hash_entry (h)->has_sda_refs)
   9893 	s = htab->relsbss;
   9894       else if (h->root.u.def.section == htab->elf.sdynrelro)
   9895 	s = htab->elf.sreldynrelro;
   9896       else
   9897 	s = htab->elf.srelbss;
   9898       BFD_ASSERT (s != NULL);
   9899 
   9900       rela.r_offset = SYM_VAL (h);
   9901       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
   9902       rela.r_addend = 0;
   9903       BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &rela, s));
   9904     }
   9905 
   9906 #ifdef DEBUG
   9907   fprintf (stderr, "\n");
   9908 #endif
   9909 
   9910   return true;
   9911 }
   9912 
   9913 static enum elf_reloc_type_class
   9915 ppc_elf_reloc_type_class (const struct bfd_link_info *info,
   9916 			  const asection *rel_sec,
   9917 			  const Elf_Internal_Rela *rela)
   9918 {
   9919   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9920 
   9921   if (rel_sec == htab->elf.irelplt)
   9922     return reloc_class_ifunc;
   9923 
   9924   switch (ELF32_R_TYPE (rela->r_info))
   9925     {
   9926     case R_PPC_RELATIVE:
   9927       return reloc_class_relative;
   9928     case R_PPC_JMP_SLOT:
   9929       return reloc_class_plt;
   9930     case R_PPC_COPY:
   9931       return reloc_class_copy;
   9932     default:
   9933       return reloc_class_normal;
   9934     }
   9935 }
   9936 
   9937 /* Finish up the dynamic sections.  */
   9939 
   9940 static bool
   9941 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
   9942 				 struct bfd_link_info *info,
   9943 				 bfd_byte *buf)
   9944 {
   9945   asection *sdyn;
   9946   struct ppc_elf_link_hash_table *htab;
   9947   bfd_vma got;
   9948   bfd *dynobj;
   9949   bool ret = true;
   9950 
   9951 #ifdef DEBUG
   9952   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
   9953 #endif
   9954 
   9955   htab = ppc_elf_hash_table (info);
   9956   dynobj = htab->elf.dynobj;
   9957   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   9958 
   9959   got = 0;
   9960   if (htab->elf.hgot != NULL)
   9961     got = SYM_VAL (htab->elf.hgot);
   9962 
   9963   if (htab->elf.dynamic_sections_created)
   9964     {
   9965       Elf32_External_Dyn *dyncon, *dynconend;
   9966 
   9967       BFD_ASSERT (htab->elf.splt != NULL && sdyn != NULL);
   9968 
   9969       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   9970       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   9971       for (; dyncon < dynconend; dyncon++)
   9972 	{
   9973 	  Elf_Internal_Dyn dyn;
   9974 	  asection *s;
   9975 
   9976 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   9977 
   9978 	  switch (dyn.d_tag)
   9979 	    {
   9980 	    case DT_PLTGOT:
   9981 	      if (htab->elf.target_os == is_vxworks)
   9982 		s = htab->elf.sgotplt;
   9983 	      else
   9984 		s = htab->elf.splt;
   9985 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   9986 	      break;
   9987 
   9988 	    case DT_PLTRELSZ:
   9989 	      dyn.d_un.d_val = htab->elf.srelplt->size;
   9990 	      break;
   9991 
   9992 	    case DT_JMPREL:
   9993 	      s = htab->elf.srelplt;
   9994 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   9995 	      break;
   9996 
   9997 	    case DT_PPC_GOT:
   9998 	      dyn.d_un.d_ptr = got;
   9999 	      break;
   10000 
   10001 	    case DT_TEXTREL:
   10002 	      if (htab->local_ifunc_resolver)
   10003 		info->callbacks->einfo
   10004 		  (_("%X%P: text relocations and GNU indirect "
   10005 		     "functions will result in a segfault at runtime\n"));
   10006 	      else if (htab->maybe_local_ifunc_resolver)
   10007 		info->callbacks->einfo
   10008 		  (_("%P: warning: text relocations and GNU indirect "
   10009 		     "functions may result in a segfault at runtime\n"));
   10010 	      continue;
   10011 
   10012 	    default:
   10013 	      if (htab->elf.target_os == is_vxworks
   10014 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   10015 		break;
   10016 	      continue;
   10017 	    }
   10018 
   10019 	  bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   10020 	}
   10021     }
   10022 
   10023   if (htab->elf.sgot != NULL
   10024       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
   10025     {
   10026       if (htab->elf.hgot->root.u.def.section == htab->elf.sgot
   10027 	  || htab->elf.hgot->root.u.def.section == htab->elf.sgotplt)
   10028 	{
   10029 	  unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
   10030 
   10031 	  p += htab->elf.hgot->root.u.def.value;
   10032 	  if (htab->plt_type == PLT_OLD)
   10033 	    {
   10034 	      /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
   10035 		 so that a function can easily find the address of
   10036 		 _GLOBAL_OFFSET_TABLE_.  */
   10037 	      BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
   10038 			  < htab->elf.hgot->root.u.def.section->size);
   10039 	      bfd_put_32 (output_bfd, 0x4e800021, p - 4);
   10040 	    }
   10041 
   10042 	  if (sdyn != NULL)
   10043 	    {
   10044 	      bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
   10045 	      BFD_ASSERT (htab->elf.hgot->root.u.def.value
   10046 			  < htab->elf.hgot->root.u.def.section->size);
   10047 	      bfd_put_32 (output_bfd, val, p);
   10048 	    }
   10049 	}
   10050       else
   10051 	{
   10052 	  /* xgettext:c-format */
   10053 	  _bfd_error_handler (_("%s not defined in linker created %pA"),
   10054 			      htab->elf.hgot->root.root.string,
   10055 			      (htab->elf.sgotplt != NULL
   10056 			       ? htab->elf.sgotplt : htab->elf.sgot));
   10057 	  bfd_set_error (bfd_error_bad_value);
   10058 	  ret = false;
   10059 	}
   10060 
   10061       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
   10062     }
   10063 
   10064   /* Fill in the first entry in the VxWorks procedure linkage table.  */
   10065   if (htab->elf.target_os == is_vxworks
   10066       && htab->elf.splt != NULL
   10067       && htab->elf.splt->size != 0
   10068       && htab->elf.splt->output_section != bfd_abs_section_ptr)
   10069     {
   10070       asection *splt = htab->elf.splt;
   10071       /* Use the right PLT. */
   10072       const bfd_vma *plt_entry = (bfd_link_pic (info)
   10073 				  ? ppc_elf_vxworks_pic_plt0_entry
   10074 				  : ppc_elf_vxworks_plt0_entry);
   10075 
   10076       if (!bfd_link_pic (info))
   10077 	{
   10078 	  bfd_vma got_value = SYM_VAL (htab->elf.hgot);
   10079 
   10080 	  bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
   10081 		      splt->contents +  0);
   10082 	  bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
   10083 		      splt->contents +  4);
   10084 	}
   10085       else
   10086 	{
   10087 	  bfd_put_32 (output_bfd, plt_entry[0], splt->contents +  0);
   10088 	  bfd_put_32 (output_bfd, plt_entry[1], splt->contents +  4);
   10089 	}
   10090       bfd_put_32 (output_bfd, plt_entry[2], splt->contents +  8);
   10091       bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
   10092       bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
   10093       bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
   10094       bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
   10095       bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
   10096 
   10097       if (! bfd_link_pic (info))
   10098 	{
   10099 	  Elf_Internal_Rela rela;
   10100 	  bfd_byte *loc;
   10101 
   10102 	  loc = htab->srelplt2->contents;
   10103 
   10104 	  /* Output the @ha relocation for the first instruction.  */
   10105 	  rela.r_offset = (htab->elf.splt->output_section->vma
   10106 			   + htab->elf.splt->output_offset
   10107 			   + 2);
   10108 	  rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
   10109 	  rela.r_addend = 0;
   10110 	  BFD_ASSERT (swap_reloc_out (output_bfd, &rela, loc, htab->srelplt2));
   10111 	  loc += sizeof (Elf32_External_Rela);
   10112 
   10113 	  /* Output the @l relocation for the second instruction.  */
   10114 	  rela.r_offset = (htab->elf.splt->output_section->vma
   10115 			   + htab->elf.splt->output_offset
   10116 			   + 6);
   10117 	  rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
   10118 	  rela.r_addend = 0;
   10119 	  BFD_ASSERT (swap_reloc_out (output_bfd, &rela, loc, htab->srelplt2));
   10120 	  loc += sizeof (Elf32_External_Rela);
   10121 
   10122 	  /* Fix up the remaining relocations.  They may have the wrong
   10123 	     symbol index for _G_O_T_ or _P_L_T_ depending on the order
   10124 	     in which symbols were output.  */
   10125 	  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
   10126 	    {
   10127 	      Elf_Internal_Rela rel;
   10128 
   10129 	      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
   10130 	      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
   10131 	      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
   10132 	      loc += sizeof (Elf32_External_Rela);
   10133 
   10134 	      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
   10135 	      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
   10136 	      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
   10137 	      loc += sizeof (Elf32_External_Rela);
   10138 
   10139 	      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
   10140 	      rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
   10141 	      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
   10142 	      loc += sizeof (Elf32_External_Rela);
   10143 	    }
   10144 	}
   10145     }
   10146 
   10147   if (htab->glink != NULL
   10148       && htab->glink->contents != NULL
   10149       && htab->elf.dynamic_sections_created)
   10150     {
   10151       unsigned char *p;
   10152       unsigned char *endp;
   10153       bfd_vma res0;
   10154 
   10155       /*
   10156        * PIC glink code is the following:
   10157        *
   10158        * # ith PLT code stub.
   10159        *   addis 11,30,(plt+(i-1)*4-got)@ha
   10160        *   lwz 11,(plt+(i-1)*4-got)@l(11)
   10161        *   mtctr 11
   10162        *   bctr
   10163        *
   10164        * # A table of branches, one for each plt entry.
   10165        * # The idea is that the plt call stub loads ctr and r11 with these
   10166        * # addresses, so (r11 - res_0) gives the plt index * 4.
   10167        * res_0:	b PLTresolve
   10168        * res_1:	b PLTresolve
   10169        * .
   10170        * # Some number of entries towards the end can be nops
   10171        * res_n_m3: nop
   10172        * res_n_m2: nop
   10173        * res_n_m1:
   10174        *
   10175        * PLTresolve:
   10176        *    addis 11,11,(1f-res_0)@ha
   10177        *    mflr 0
   10178        *    bcl 20,31,1f
   10179        * 1: addi 11,11,(1b-res_0)@l
   10180        *    mflr 12
   10181        *    mtlr 0
   10182        *    sub 11,11,12		# r11 = index * 4
   10183        *    addis 12,12,(got+4-1b)@ha
   10184        *    lwz 0,(got+4-1b)@l(12)	# got[1] address of dl_runtime_resolve
   10185        *    lwz 12,(got+8-1b)@l(12)	# got[2] contains the map address
   10186        *    mtctr 0
   10187        *    add 0,11,11
   10188        *    add 11,0,11			# r11 = index * 12 = reloc offset.
   10189        *    bctr
   10190        *
   10191        * Non-PIC glink code is a little simpler.
   10192        *
   10193        * # ith PLT code stub.
   10194        *   lis 11,(plt+(i-1)*4)@ha
   10195        *   lwz 11,(plt+(i-1)*4)@l(11)
   10196        *   mtctr 11
   10197        *   bctr
   10198        *
   10199        * The branch table is the same, then comes
   10200        *
   10201        * PLTresolve:
   10202        *    lis 12,(got+4)@ha
   10203        *    addis 11,11,(-res_0)@ha
   10204        *    lwz 0,(got+4)@l(12)		# got[1] address of dl_runtime_resolve
   10205        *    addi 11,11,(-res_0)@l	# r11 = index * 4
   10206        *    mtctr 0
   10207        *    add 0,11,11
   10208        *    lwz 12,(got+8)@l(12)	# got[2] contains the map address
   10209        *    add 11,0,11			# r11 = index * 12 = reloc offset.
   10210        *    bctr
   10211        */
   10212 
   10213       /* Build the branch table, one for each plt entry (less one),
   10214 	 and perhaps some padding.  */
   10215       p = htab->glink->contents;
   10216       p += htab->glink_pltresolve;
   10217       endp = htab->glink->contents;
   10218       endp += htab->glink->size - GLINK_PLTRESOLVE;
   10219       while (p < endp - (htab->params->ppc476_workaround ? 0 : 8 * 4))
   10220 	{
   10221 	  bfd_put_32 (output_bfd, B + endp - p, p);
   10222 	  p += 4;
   10223 	}
   10224       while (p < endp)
   10225 	{
   10226 	  bfd_put_32 (output_bfd, NOP, p);
   10227 	  p += 4;
   10228 	}
   10229 
   10230       res0 = (htab->glink_pltresolve
   10231 	      + htab->glink->output_section->vma
   10232 	      + htab->glink->output_offset);
   10233 
   10234       if (htab->params->ppc476_workaround)
   10235 	{
   10236 	  /* Ensure that a call stub at the end of a page doesn't
   10237 	     result in prefetch over the end of the page into the
   10238 	     glink branch table.  */
   10239 	  bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
   10240 	  bfd_vma page_addr;
   10241 	  bfd_vma glink_start = (htab->glink->output_section->vma
   10242 				 + htab->glink->output_offset);
   10243 
   10244 	  for (page_addr = res0 & -pagesize;
   10245 	       page_addr > glink_start;
   10246 	       page_addr -= pagesize)
   10247 	    {
   10248 	      /* We have a plt call stub that may need fixing.  */
   10249 	      bfd_byte *loc;
   10250 	      unsigned int insn;
   10251 
   10252 	      loc = htab->glink->contents + page_addr - 4 - glink_start;
   10253 	      insn = bfd_get_32 (output_bfd, loc);
   10254 	      if (insn == BCTR)
   10255 		{
   10256 		  /* By alignment, we know that there must be at least
   10257 		     one other call stub before this one.  */
   10258 		  insn = bfd_get_32 (output_bfd, loc - 16);
   10259 		  if (insn == BCTR)
   10260 		    bfd_put_32 (output_bfd, B | (-16 & 0x3fffffc), loc);
   10261 		  else
   10262 		    bfd_put_32 (output_bfd, B | (-20 & 0x3fffffc), loc);
   10263 		}
   10264 	    }
   10265 	}
   10266 
   10267       /* Last comes the PLTresolve stub.  */
   10268       endp = p + GLINK_PLTRESOLVE;
   10269       if (bfd_link_pic (info))
   10270 	{
   10271 	  bfd_vma bcl;
   10272 
   10273 	  bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
   10274 		 + htab->glink->output_section->vma
   10275 		 + htab->glink->output_offset);
   10276 
   10277 	  bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (bcl - res0), p);
   10278 	  p += 4;
   10279 	  bfd_put_32 (output_bfd, MFLR_0, p);
   10280 	  p += 4;
   10281 	  bfd_put_32 (output_bfd, BCL_20_31, p);
   10282 	  p += 4;
   10283 	  bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (bcl - res0), p);
   10284 	  p += 4;
   10285 	  bfd_put_32 (output_bfd, MFLR_12, p);
   10286 	  p += 4;
   10287 	  bfd_put_32 (output_bfd, MTLR_0, p);
   10288 	  p += 4;
   10289 	  bfd_put_32 (output_bfd, SUB_11_11_12, p);
   10290 	  p += 4;
   10291 	  bfd_put_32 (output_bfd, ADDIS_12_12 + PPC_HA (got + 4 - bcl), p);
   10292 	  p += 4;
   10293 	  if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
   10294 	    {
   10295 	      bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4 - bcl), p);
   10296 	      p += 4;
   10297 	      bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8 - bcl), p);
   10298 	      p += 4;
   10299 	    }
   10300 	  else
   10301 	    {
   10302 	      bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4 - bcl), p);
   10303 	      p += 4;
   10304 	      bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
   10305 	      p += 4;
   10306 	    }
   10307 	  bfd_put_32 (output_bfd, MTCTR_0, p);
   10308 	  p += 4;
   10309 	  bfd_put_32 (output_bfd, ADD_0_11_11, p);
   10310 	}
   10311       else
   10312 	{
   10313 	  bfd_put_32 (output_bfd, LIS_12 + PPC_HA (got + 4), p);
   10314 	  p += 4;
   10315 	  bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (-res0), p);
   10316 	  p += 4;
   10317 	  if (PPC_HA (got + 4) == PPC_HA (got + 8))
   10318 	    bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4), p);
   10319 	  else
   10320 	    bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4), p);
   10321 	  p += 4;
   10322 	  bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (-res0), p);
   10323 	  p += 4;
   10324 	  bfd_put_32 (output_bfd, MTCTR_0, p);
   10325 	  p += 4;
   10326 	  bfd_put_32 (output_bfd, ADD_0_11_11, p);
   10327 	  p += 4;
   10328 	  if (PPC_HA (got + 4) == PPC_HA (got + 8))
   10329 	    bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8), p);
   10330 	  else
   10331 	    bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
   10332 	}
   10333       p += 4;
   10334       bfd_put_32 (output_bfd, ADD_11_0_11, p);
   10335       p += 4;
   10336       bfd_put_32 (output_bfd, BCTR, p);
   10337       p += 4;
   10338       while (p < endp)
   10339 	{
   10340 	  bfd_put_32 (output_bfd,
   10341 		      htab->params->ppc476_workaround ? BA : NOP, p);
   10342 	  p += 4;
   10343 	}
   10344       BFD_ASSERT (p == endp);
   10345     }
   10346 
   10347   if (htab->glink_eh_frame != NULL
   10348       && htab->glink_eh_frame->contents != NULL)
   10349     {
   10350       unsigned char *p = htab->glink_eh_frame->contents;
   10351       bfd_vma val;
   10352 
   10353       p += sizeof (glink_eh_frame_cie);
   10354       /* FDE length.  */
   10355       p += 4;
   10356       /* CIE pointer.  */
   10357       p += 4;
   10358       /* Offset to .glink.  */
   10359       val = (htab->glink->output_section->vma
   10360 	     + htab->glink->output_offset);
   10361       val -= (htab->glink_eh_frame->output_section->vma
   10362 	      + htab->glink_eh_frame->output_offset);
   10363       val -= p - htab->glink_eh_frame->contents;
   10364       bfd_put_32 (htab->elf.dynobj, val, p);
   10365 
   10366       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   10367 	  && !_bfd_elf_write_linker_section_eh_frame (output_bfd, info,
   10368 						      htab->glink_eh_frame,
   10369 						      buf))
   10370 	return false;
   10371     }
   10372 
   10373   return ret;
   10374 }
   10375 
   10376 #define TARGET_LITTLE_SYM	powerpc_elf32_le_vec
   10378 #define TARGET_LITTLE_NAME	"elf32-powerpcle"
   10379 #define TARGET_BIG_SYM		powerpc_elf32_vec
   10380 #define TARGET_BIG_NAME		"elf32-powerpc"
   10381 #define ELF_ARCH		bfd_arch_powerpc
   10382 #define ELF_TARGET_ID		PPC32_ELF_DATA
   10383 #define ELF_MACHINE_CODE	EM_PPC
   10384 #define ELF_MAXPAGESIZE		0x10000
   10385 #define ELF_COMMONPAGESIZE	0x1000
   10386 #define elf_info_to_howto	ppc_elf_info_to_howto
   10387 
   10388 #ifdef  EM_CYGNUS_POWERPC
   10389 #define ELF_MACHINE_ALT1	EM_CYGNUS_POWERPC
   10390 #endif
   10391 
   10392 #ifdef EM_PPC_OLD
   10393 #define ELF_MACHINE_ALT2	EM_PPC_OLD
   10394 #endif
   10395 
   10396 #define elf_backend_plt_not_loaded	1
   10397 #define elf_backend_want_dynrelro	1
   10398 #define elf_backend_can_gc_sections	1
   10399 #define elf_backend_can_refcount	1
   10400 #define elf_backend_rela_normal		1
   10401 #define elf_backend_caches_rawsize	1
   10402 
   10403 #define bfd_elf32_mkobject			ppc_elf_mkobject
   10404 #define bfd_elf32_bfd_merge_private_bfd_data	ppc_elf_merge_private_bfd_data
   10405 #define bfd_elf32_bfd_relax_section		ppc_elf_relax_section
   10406 #define bfd_elf32_bfd_reloc_type_lookup		ppc_elf_reloc_type_lookup
   10407 #define bfd_elf32_bfd_reloc_name_lookup		ppc_elf_reloc_name_lookup
   10408 #define bfd_elf32_bfd_set_private_flags		ppc_elf_set_private_flags
   10409 #define bfd_elf32_bfd_link_hash_table_create	ppc_elf_link_hash_table_create
   10410 #define bfd_elf32_get_synthetic_symtab		ppc_elf_get_synthetic_symtab
   10411 
   10412 #define elf_backend_object_p			ppc_elf_object_p
   10413 #define elf_backend_gc_mark_hook		ppc_elf_gc_mark_hook
   10414 #define elf_backend_section_from_shdr		ppc_elf_section_from_shdr
   10415 #define elf_backend_relocate_section		ppc_elf_relocate_section
   10416 #define elf_backend_create_dynamic_sections	ppc_elf_create_dynamic_sections
   10417 #define elf_backend_check_relocs		ppc_elf_check_relocs
   10418 #define elf_backend_relocs_compatible		_bfd_elf_relocs_compatible
   10419 #define elf_backend_copy_indirect_symbol	ppc_elf_copy_indirect_symbol
   10420 #define elf_backend_adjust_dynamic_symbol	ppc_elf_adjust_dynamic_symbol
   10421 #define elf_backend_add_symbol_hook		ppc_elf_add_symbol_hook
   10422 #define elf_backend_late_size_sections		ppc_elf_late_size_sections
   10423 #define elf_backend_hash_symbol			ppc_elf_hash_symbol
   10424 #define elf_backend_finish_dynamic_symbol	ppc_elf_finish_dynamic_symbol
   10425 #define elf_backend_finish_dynamic_sections	ppc_elf_finish_dynamic_sections
   10426 #define elf_backend_fake_sections		ppc_elf_fake_sections
   10427 #define elf_backend_additional_program_headers	ppc_elf_additional_program_headers
   10428 #define elf_backend_modify_segment_map		ppc_elf_modify_segment_map
   10429 #define elf_backend_grok_prstatus		ppc_elf_grok_prstatus
   10430 #define elf_backend_grok_psinfo			ppc_elf_grok_psinfo
   10431 #define elf_backend_write_core_note		ppc_elf_write_core_note
   10432 #define elf_backend_reloc_type_class		ppc_elf_reloc_type_class
   10433 #define elf_backend_begin_write_processing	ppc_elf_begin_write_processing
   10434 #define elf_backend_final_write_processing	ppc_elf_final_write_processing
   10435 #define elf_backend_write_section		ppc_elf_write_section
   10436 #define elf_backend_get_sec_type_attr		ppc_elf_get_sec_type_attr
   10437 #define elf_backend_plt_sym_val			ppc_elf_plt_sym_val
   10438 #define elf_backend_action_discarded		ppc_elf_action_discarded
   10439 #define elf_backend_init_index_section		_bfd_elf_init_1_index_section
   10440 #define elf_backend_lookup_section_flags_hook	ppc_elf_lookup_section_flags
   10441 
   10442 #include "elf32-target.h"
   10443 
   10444 /* FreeBSD Target */
   10445 
   10446 #undef  TARGET_LITTLE_SYM
   10447 #undef  TARGET_LITTLE_NAME
   10448 
   10449 #undef  TARGET_BIG_SYM
   10450 #define TARGET_BIG_SYM  powerpc_elf32_fbsd_vec
   10451 #undef  TARGET_BIG_NAME
   10452 #define TARGET_BIG_NAME "elf32-powerpc-freebsd"
   10453 
   10454 #undef  ELF_OSABI
   10455 #define ELF_OSABI	ELFOSABI_FREEBSD
   10456 #undef	ELF_OSABI_EXACT
   10457 #define	ELF_OSABI_EXACT	1
   10458 
   10459 #undef  elf32_bed
   10460 #define elf32_bed	elf32_powerpc_fbsd_bed
   10461 
   10462 #include "elf32-target.h"
   10463 
   10464 /* VxWorks Target */
   10465 
   10466 #undef TARGET_LITTLE_SYM
   10467 #undef TARGET_LITTLE_NAME
   10468 
   10469 #undef TARGET_BIG_SYM
   10470 #define TARGET_BIG_SYM		powerpc_elf32_vxworks_vec
   10471 #undef TARGET_BIG_NAME
   10472 #define TARGET_BIG_NAME		"elf32-powerpc-vxworks"
   10473 
   10474 #undef  ELF_OSABI
   10475 #undef	ELF_OSABI_EXACT
   10476 
   10477 #undef ELF_TARGET_OS
   10478 #define ELF_TARGET_OS		is_vxworks
   10479 
   10480 /* VxWorks uses the elf default section flags for .plt.  */
   10481 static const struct bfd_elf_special_section *
   10482 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd, asection *sec)
   10483 {
   10484   if (sec->name == NULL)
   10485     return NULL;
   10486 
   10487   if (strcmp (sec->name, ".plt") == 0)
   10488     return _bfd_elf_get_sec_type_attr (abfd, sec);
   10489 
   10490   return ppc_elf_get_sec_type_attr (abfd, sec);
   10491 }
   10492 
   10493 /* Like ppc_elf_link_hash_table_create, but overrides
   10494    appropriately for VxWorks.  */
   10495 static struct bfd_link_hash_table *
   10496 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
   10497 {
   10498   struct bfd_link_hash_table *ret;
   10499 
   10500   ret = ppc_elf_link_hash_table_create (abfd);
   10501   if (ret)
   10502     {
   10503       struct ppc_elf_link_hash_table *htab
   10504 	= (struct ppc_elf_link_hash_table *)ret;
   10505       htab->plt_type = PLT_VXWORKS;
   10506       htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
   10507       htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
   10508       htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
   10509     }
   10510   return ret;
   10511 }
   10512 
   10513 /* Tweak magic VxWorks symbols as they are loaded.  */
   10514 static bool
   10515 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
   10516 				 struct bfd_link_info *info,
   10517 				 Elf_Internal_Sym *sym,
   10518 				 const char **namep,
   10519 				 flagword *flagsp,
   10520 				 asection **secp,
   10521 				 bfd_vma *valp)
   10522 {
   10523   if (!elf_vxworks_add_symbol_hook (abfd, info, sym, namep, flagsp, secp,
   10524 				    valp))
   10525     return false;
   10526 
   10527   return ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp);
   10528 }
   10529 
   10530 static bool
   10531 ppc_elf_vxworks_final_write_processing (bfd *abfd)
   10532 {
   10533   ppc_final_write_processing (abfd);
   10534   return elf_vxworks_final_write_processing (abfd);
   10535 }
   10536 
   10537 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
   10538    define it.  */
   10539 #undef elf_backend_want_plt_sym
   10540 #define elf_backend_want_plt_sym		1
   10541 #undef elf_backend_want_got_plt
   10542 #define elf_backend_want_got_plt		1
   10543 #undef elf_backend_got_symbol_offset
   10544 #define elf_backend_got_symbol_offset		0
   10545 #undef elf_backend_plt_not_loaded
   10546 #define elf_backend_plt_not_loaded		0
   10547 #undef elf_backend_plt_readonly
   10548 #define elf_backend_plt_readonly		1
   10549 #undef elf_backend_got_header_size
   10550 #define elf_backend_got_header_size		12
   10551 #undef elf_backend_dtrel_excludes_plt
   10552 #define elf_backend_dtrel_excludes_plt		1
   10553 
   10554 #undef bfd_elf32_get_synthetic_symtab
   10555 
   10556 #undef bfd_elf32_bfd_link_hash_table_create
   10557 #define bfd_elf32_bfd_link_hash_table_create \
   10558   ppc_elf_vxworks_link_hash_table_create
   10559 #undef elf_backend_add_symbol_hook
   10560 #define elf_backend_add_symbol_hook \
   10561   ppc_elf_vxworks_add_symbol_hook
   10562 #undef elf_backend_link_output_symbol_hook
   10563 #define elf_backend_link_output_symbol_hook \
   10564   elf_vxworks_link_output_symbol_hook
   10565 #undef elf_backend_final_write_processing
   10566 #define elf_backend_final_write_processing \
   10567   ppc_elf_vxworks_final_write_processing
   10568 #undef elf_backend_get_sec_type_attr
   10569 #define elf_backend_get_sec_type_attr \
   10570   ppc_elf_vxworks_get_sec_type_attr
   10571 #undef elf_backend_emit_relocs
   10572 #define elf_backend_emit_relocs \
   10573   elf_vxworks_emit_relocs
   10574 
   10575 #undef elf32_bed
   10576 #define elf32_bed				ppc_elf_vxworks_bed
   10577 
   10578 #include "elf32-target.h"
   10579