* elf32-i386.c (elf_backend_plt_readonly): Set correctly, to 1.
[external/binutils.git] / bfd / elf32-sparc.c
1 /* SPARC-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25
26 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
27   PARAMS ((bfd *, bfd_reloc_code_real_type));
28 static void elf_info_to_howto
29   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
30 static boolean elf32_sparc_check_relocs
31   PARAMS ((bfd *, struct bfd_link_info *, asection *,
32            const Elf_Internal_Rela *));
33 static boolean elf32_sparc_adjust_dynamic_symbol
34   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
35 static boolean elf32_sparc_adjust_dynindx
36   PARAMS ((struct elf_link_hash_entry *, PTR));
37 static boolean elf32_sparc_size_dynamic_sections
38   PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf32_sparc_relocate_section
40   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
41            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
42 static boolean elf32_sparc_finish_dynamic_symbol
43   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
44            Elf_Internal_Sym *));
45 static boolean elf32_sparc_finish_dynamic_sections
46   PARAMS ((bfd *, struct bfd_link_info *));
47
48 enum reloc_type
49   {
50     R_SPARC_NONE = 0,
51     R_SPARC_8,          R_SPARC_16,             R_SPARC_32, 
52     R_SPARC_DISP8,      R_SPARC_DISP16,         R_SPARC_DISP32, 
53     R_SPARC_WDISP30,    R_SPARC_WDISP22,
54     R_SPARC_HI22,       R_SPARC_22,
55     R_SPARC_13,         R_SPARC_LO10,
56     R_SPARC_GOT10,      R_SPARC_GOT13,          R_SPARC_GOT22,
57     R_SPARC_PC10,       R_SPARC_PC22,
58     R_SPARC_WPLT30,
59     R_SPARC_COPY,
60     R_SPARC_GLOB_DAT,   R_SPARC_JMP_SLOT,
61     R_SPARC_RELATIVE,
62     R_SPARC_UA32,
63     R_SPARC_max
64   };
65
66 #if 0
67 static CONST char *CONST reloc_type_names[] =
68 {
69   "R_SPARC_NONE",
70   "R_SPARC_8",          "R_SPARC_16",           "R_SPARC_32",
71   "R_SPARC_DISP8",      "R_SPARC_DISP16",       "R_SPARC_DISP32",
72   "R_SPARC_WDISP30",    "R_SPARC_WDISP22",
73   "R_SPARC_HI22",       "R_SPARC_22",
74   "R_SPARC_13",         "R_SPARC_LO10",
75   "R_SPARC_GOT10",      "R_SPARC_GOT13",        "R_SPARC_GOT22",
76   "R_SPARC_PC10",       "R_SPARC_PC22",
77   "R_SPARC_WPLT30",
78   "R_SPARC_COPY",
79   "R_SPARC_GLOB_DAT",   "R_SPARC_JMP_SLOT",
80   "R_SPARC_RELATIVE",
81   "R_SPARC_UA32",
82 };
83 #endif
84
85 static reloc_howto_type elf_sparc_howto_table[] = 
86 {
87   HOWTO(R_SPARC_NONE,    0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_NONE",    false,0,0x00000000,true),
88   HOWTO(R_SPARC_8,       0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_8",       false,0,0x000000ff,true),
89   HOWTO(R_SPARC_16,      0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_16",      false,0,0x0000ffff,true),
90   HOWTO(R_SPARC_32,      0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_32",      false,0,0xffffffff,true),
91   HOWTO(R_SPARC_DISP8,   0,0, 8,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_DISP8",   false,0,0x000000ff,true),
92   HOWTO(R_SPARC_DISP16,  0,1,16,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_DISP16",  false,0,0x0000ffff,true),
93   HOWTO(R_SPARC_DISP32,  0,2,32,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_DISP32",  false,0,0x00ffffff,true),
94   HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_WDISP30", false,0,0x3fffffff,true),
95   HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_WDISP22", false,0,0x003fffff,true),
96   HOWTO(R_SPARC_HI22,   10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_HI22",    false,0,0x003fffff,true),
97   HOWTO(R_SPARC_22,      0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_22",      false,0,0x003fffff,true),
98   HOWTO(R_SPARC_13,      0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_13",      false,0,0x00001fff,true),
99   HOWTO(R_SPARC_LO10,    0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_LO10",    false,0,0x000003ff,true),
100   HOWTO(R_SPARC_GOT10,   0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_GOT10",   false,0,0x000003ff,true),
101   HOWTO(R_SPARC_GOT13,   0,2,13,false,0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_GOT13",   false,0,0x00001fff,true),
102   HOWTO(R_SPARC_GOT22,  10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_GOT22",   false,0,0x003fffff,true),
103   HOWTO(R_SPARC_PC10,    0,2,10,true, 0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_PC10",    false,0,0x000003ff,true),
104   HOWTO(R_SPARC_PC22,   10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_PC22",    false,0,0x003fffff,true),
105   HOWTO(R_SPARC_WPLT30,  2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_WPLT30",  false,0,0x3fffffff,true),
106   HOWTO(R_SPARC_COPY,    0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_COPY",    false,0,0x00000000,true),
107   HOWTO(R_SPARC_GLOB_DAT,0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_GLOB_DAT",false,0,0x00000000,true),
108   HOWTO(R_SPARC_JMP_SLOT,0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_JMP_SLOT",false,0,0x00000000,true),
109   HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_RELATIVE",false,0,0x00000000,true),
110   HOWTO(R_SPARC_UA32,    0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_UA32",    false,0,0x00000000,true),
111 };
112
113 struct elf_reloc_map {
114   unsigned char bfd_reloc_val;
115   unsigned char elf_reloc_val;
116 };
117
118 static CONST struct elf_reloc_map sparc_reloc_map[] =
119 {
120   { BFD_RELOC_NONE, R_SPARC_NONE, },
121   { BFD_RELOC_16, R_SPARC_16, },
122   { BFD_RELOC_8, R_SPARC_8 },
123   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
124   { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits.  */
125   { BFD_RELOC_32, R_SPARC_32 },
126   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
127   { BFD_RELOC_HI22, R_SPARC_HI22 },
128   { BFD_RELOC_LO10, R_SPARC_LO10, },
129   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
130   { BFD_RELOC_SPARC22, R_SPARC_22 },
131   { BFD_RELOC_SPARC13, R_SPARC_13 },
132   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
133   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
134   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
135   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
136   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
137   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
138   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
139   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
140   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
141   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
142   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
143 /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
144 };
145
146 static reloc_howto_type *
147 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
148      bfd *abfd;
149      bfd_reloc_code_real_type code;
150 {
151   unsigned int i;
152   for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
153     {
154       if (sparc_reloc_map[i].bfd_reloc_val == code)
155         return &elf_sparc_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
156     }
157   return 0;
158 }
159
160 static void
161 elf_info_to_howto (abfd, cache_ptr, dst)
162      bfd *abfd;
163      arelent *cache_ptr;
164      Elf_Internal_Rela *dst;
165 {
166   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max);
167   cache_ptr->howto = &elf_sparc_howto_table[ELF32_R_TYPE(dst->r_info)];
168 }
169
170 \f
171 /* Functions for the SPARC ELF linker.  */
172
173 /* The name of the dynamic interpreter.  This is put in the .interp
174    section.  */
175
176 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
177
178 /* The nop opcode we use.  */
179
180 #define SPARC_NOP 0x01000000
181
182 /* The size in bytes of an entry in the procedure linkage table.  */
183
184 #define PLT_ENTRY_SIZE 12
185
186 /* The first four entries in a procedure linkage table are reserved,
187    and the initial contents are unimportant (we zero them out).
188    Subsequent entries look like this.  See the SVR4 ABI SPARC
189    supplement to see how this works.  */
190
191 /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
192 #define PLT_ENTRY_WORD0 0x03000000
193 /* b,a .plt0.  We fill in the offset later.  */
194 #define PLT_ENTRY_WORD1 0x30800000
195 /* nop.  */
196 #define PLT_ENTRY_WORD2 SPARC_NOP
197
198 /* Look through the relocs for a section during the first phase, and
199    allocate space in the global offset table or procedure linkage
200    table.  */
201
202 static boolean
203 elf32_sparc_check_relocs (abfd, info, sec, relocs)
204      bfd *abfd;
205      struct bfd_link_info *info;
206      asection *sec;
207      const Elf_Internal_Rela *relocs;
208 {
209   bfd *dynobj;
210   Elf_Internal_Shdr *symtab_hdr;
211   struct elf_link_hash_entry **sym_hashes;
212   bfd_vma *local_got_offsets;
213   const Elf_Internal_Rela *rel;
214   const Elf_Internal_Rela *rel_end;
215   asection *sgot;
216   asection *srelgot;
217   asection *sreloc;
218
219   if (info->relocateable)
220     return true;
221
222   dynobj = elf_hash_table (info)->dynobj;
223   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
224   sym_hashes = elf_sym_hashes (abfd);
225   local_got_offsets = elf_local_got_offsets (abfd);
226
227   sgot = NULL;
228   srelgot = NULL;
229   sreloc = NULL;
230
231   rel_end = relocs + sec->reloc_count;
232   for (rel = relocs; rel < rel_end; rel++)
233     {
234       unsigned long r_symndx;
235       struct elf_link_hash_entry *h;
236
237       r_symndx = ELF32_R_SYM (rel->r_info);
238       if (r_symndx < symtab_hdr->sh_info)
239         h = NULL;
240       else
241         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
242
243       switch (ELF32_R_TYPE (rel->r_info))
244         {
245         case R_SPARC_GOT10:
246         case R_SPARC_GOT13:
247         case R_SPARC_GOT22:
248           /* This symbol requires a global offset table entry.  */
249
250           if (dynobj == NULL)
251             {
252               /* Create the .got section.  */
253               elf_hash_table (info)->dynobj = dynobj = abfd;
254               if (! _bfd_elf_create_got_section (dynobj, info))
255                 return false;
256             }
257
258           if (sgot == NULL)
259             {
260               sgot = bfd_get_section_by_name (dynobj, ".got");
261               BFD_ASSERT (sgot != NULL);
262             }
263
264           if (srelgot == NULL
265               && (h != NULL || info->shared))
266             {
267               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
268               if (srelgot == NULL)
269                 {
270                   srelgot = bfd_make_section (dynobj, ".rela.got");
271                   if (srelgot == NULL
272                       || ! bfd_set_section_flags (dynobj, srelgot,
273                                                   (SEC_ALLOC
274                                                    | SEC_LOAD
275                                                    | SEC_HAS_CONTENTS
276                                                    | SEC_IN_MEMORY
277                                                    | SEC_READONLY))
278                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
279                     return false;
280                 }
281             }
282
283           if (h != NULL)
284             {
285               if (h->got_offset != (bfd_vma) -1)
286                 {
287                   /* We have already allocated space in the .got.  */
288                   break;
289                 }
290               h->got_offset = sgot->_raw_size;
291
292               /* Make sure this symbol is output as a dynamic symbol.  */
293               if (h->dynindx == -1)
294                 {
295                   if (! bfd_elf32_link_record_dynamic_symbol (info, h))
296                     return false;
297                 }
298
299               srelgot->_raw_size += sizeof (Elf32_External_Rela);
300             }
301           else
302             {
303               /* This is a global offset table entry for a local
304                  symbol.  */
305               if (local_got_offsets == NULL)
306                 {
307                   size_t size;
308                   register unsigned int i;
309
310                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
311                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
312                   if (local_got_offsets == NULL)
313                     {
314                       bfd_set_error (bfd_error_no_memory);
315                       return false;
316                     }
317                   elf_local_got_offsets (abfd) = local_got_offsets;
318                   for (i = 0; i < symtab_hdr->sh_info; i++)
319                     local_got_offsets[i] = (bfd_vma) -1;
320                 }
321               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
322                 {
323                   /* We have already allocated space in the .got.  */
324                   break;
325                 }
326               local_got_offsets[r_symndx] = sgot->_raw_size;
327
328               if (info->shared)
329                 {
330                   /* If we are generating a shared object, we need to
331                      output a R_SPARC_RELATIVE reloc so that the
332                      dynamic linker can adjust this GOT entry.  */
333                   srelgot->_raw_size += sizeof (Elf32_External_Rela);
334                 }
335             }
336
337           sgot->_raw_size += 4;
338
339           break;
340
341         case R_SPARC_WPLT30:
342           /* This symbol requires a procedure linkage table entry.  We
343              actually build the entry in adjust_dynamic_symbol,
344              because this might be a case of linking PIC code without
345              linking in any dynamic objects, in which case we don't
346              need to generate a procedure linkage table after all.  */
347
348           if (h == NULL)
349             {
350               /* It does not make sense to have a procedure linkage
351                  table entry for a local symbol.  */
352               bfd_set_error (bfd_error_bad_value);
353               return false;
354             }
355
356           /* Make sure this symbol is output as a dynamic symbol.  */
357           if (h->dynindx == -1)
358             {
359               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
360                 return false;
361             }
362
363           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
364
365           break;
366
367         case R_SPARC_PC10:
368         case R_SPARC_PC22:
369           if (h != NULL
370               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
371             break;
372           /* Fall through.  */
373         case R_SPARC_DISP8:
374         case R_SPARC_DISP16:
375         case R_SPARC_DISP32:
376         case R_SPARC_WDISP30:
377         case R_SPARC_WDISP22:
378           if (h == NULL)
379             break;
380           /* Fall through.  */
381         case R_SPARC_8:
382         case R_SPARC_16:
383         case R_SPARC_32:
384         case R_SPARC_HI22:
385         case R_SPARC_22:
386         case R_SPARC_13:
387         case R_SPARC_LO10:
388         case R_SPARC_UA32:
389           if (info->shared
390               && (sec->flags & SEC_ALLOC) != 0)
391             {
392               /* When creating a shared object, we must copy these
393                  relocs into the output file.  We create a reloc
394                  section in dynobj and make room for the reloc.  */
395               if (sreloc == NULL)
396                 {
397                   const char *name;
398
399                   name = (bfd_elf_string_from_elf_section
400                           (abfd,
401                            elf_elfheader (abfd)->e_shstrndx,
402                            elf_section_data (sec)->rel_hdr.sh_name));
403                   if (name == NULL)
404                     return false;
405
406                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
407                               && strcmp (bfd_get_section_name (abfd, sec),
408                                          name + 5) == 0);
409
410                   sreloc = bfd_get_section_by_name (dynobj, name);
411                   if (sreloc == NULL)
412                     {
413                       sreloc = bfd_make_section (dynobj, name);
414                       if (sreloc == NULL
415                           || ! bfd_set_section_flags (dynobj, sreloc,
416                                                       (SEC_ALLOC
417                                                        | SEC_LOAD
418                                                        | SEC_HAS_CONTENTS
419                                                        | SEC_IN_MEMORY
420                                                        | SEC_READONLY))
421                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
422                         return false;
423                     }
424                 }
425
426               sreloc->_raw_size += sizeof (Elf32_External_Rela);
427             }
428
429           break;
430
431         default:
432           break;
433         }
434     }
435
436   return true;
437 }
438
439 /* Adjust a symbol defined by a dynamic object and referenced by a
440    regular object.  The current definition is in some section of the
441    dynamic object, but we're not including those sections.  We have to
442    change the definition to something the rest of the link can
443    understand.  */
444
445 static boolean
446 elf32_sparc_adjust_dynamic_symbol (info, h)
447      struct bfd_link_info *info;
448      struct elf_link_hash_entry *h;
449 {
450   bfd *dynobj;
451   asection *s;
452   unsigned int power_of_two;
453
454   dynobj = elf_hash_table (info)->dynobj;
455
456   /* Make sure we know what is going on here.  */
457   BFD_ASSERT (dynobj != NULL
458               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
459                   || h->weakdef != NULL
460                   || ((h->elf_link_hash_flags
461                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
462                       && (h->elf_link_hash_flags
463                           & ELF_LINK_HASH_REF_REGULAR) != 0
464                       && (h->elf_link_hash_flags
465                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
466
467   /* If this is a function, put it in the procedure linkage table.  We
468      will fill in the contents of the procedure linkage table later
469      (although we could actually do it here).  */
470   if (h->type == STT_FUNC
471       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
472     {
473       if (! elf_hash_table (info)->dynamic_sections_created)
474         {
475           /* This case can occur if we saw a WPLT30 reloc in an input
476              file, but none of the input files were dynamic objects.
477              In such a case, we don't actually need to build a
478              procedure linkage table, and we can just do a WDISP30
479              reloc instead.  */
480           BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
481           return true;
482         }
483
484       s = bfd_get_section_by_name (dynobj, ".plt");
485       BFD_ASSERT (s != NULL);
486
487       /* The first four entries in .plt are reserved.  */
488       if (s->_raw_size == 0)
489         s->_raw_size = 4 * PLT_ENTRY_SIZE;
490
491       /* The procedure linkage table has a maximum size.  */
492       if (s->_raw_size >= 0x400000)
493         {
494           bfd_set_error (bfd_error_bad_value);
495           return false;
496         }
497
498       /* If this symbol is not defined in a regular file, and we are
499          not generating a shared library, then set the symbol to this
500          location in the .plt.  This is required to make function
501          pointers compare as equal between the normal executable and
502          the shared library.  */
503       if (! info->shared
504           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
505         {
506           h->root.u.def.section = s;
507           h->root.u.def.value = s->_raw_size;
508         }
509
510       h->plt_offset = s->_raw_size;
511
512       /* Make room for this entry.  */
513       s->_raw_size += PLT_ENTRY_SIZE;
514
515       /* We also need to make an entry in the .rela.plt section.  */
516
517       s = bfd_get_section_by_name (dynobj, ".rela.plt");
518       BFD_ASSERT (s != NULL);
519       s->_raw_size += sizeof (Elf32_External_Rela);
520
521       return true;
522     }
523
524   /* If this is a weak symbol, and there is a real definition, the
525      processor independent code will have arranged for us to see the
526      real definition first, and we can just use the same value.  */
527   if (h->weakdef != NULL)
528     {
529       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
530                   || h->weakdef->root.type == bfd_link_hash_defweak);
531       h->root.u.def.section = h->weakdef->root.u.def.section;
532       h->root.u.def.value = h->weakdef->root.u.def.value;
533       return true;
534     }
535
536   /* This is a reference to a symbol defined by a dynamic object which
537      is not a function.  */
538
539   /* If we are creating a shared library, we must presume that the
540      only references to the symbol are via the global offset table.
541      For such cases we need not do anything here; the relocations will
542      be handled correctly by relocate_section.  */
543   if (info->shared)
544     return true;
545
546   /* We must allocate the symbol in our .dynbss section, which will
547      become part of the .bss section of the executable.  There will be
548      an entry for this symbol in the .dynsym section.  The dynamic
549      object will contain position independent code, so all references
550      from the dynamic object to this symbol will go through the global
551      offset table.  The dynamic linker will use the .dynsym entry to
552      determine the address it must put in the global offset table, so
553      both the dynamic object and the regular object will refer to the
554      same memory location for the variable.  */
555
556   s = bfd_get_section_by_name (dynobj, ".dynbss");
557   BFD_ASSERT (s != NULL);
558
559   /* If the symbol is currently defined in the .bss section of the
560      dynamic object, then it is OK to simply initialize it to zero.
561      If the symbol is in some other section, we must generate a
562      R_SPARC_COPY reloc to tell the dynamic linker to copy the initial
563      value out of the dynamic object and into the runtime process
564      image.  We need to remember the offset into the .rel.bss section
565      we are going to use.  */
566   if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
567     {
568       asection *srel;
569
570       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
571       BFD_ASSERT (srel != NULL);
572       srel->_raw_size += sizeof (Elf32_External_Rela);
573       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
574     }
575
576   /* We need to figure out the alignment required for this symbol.  I
577      have no idea how ELF linkers handle this.  */
578   power_of_two = bfd_log2 (h->size);
579   if (power_of_two > 3)
580     power_of_two = 3;
581
582   /* Apply the required alignment.  */
583   s->_raw_size = BFD_ALIGN (s->_raw_size,
584                             (bfd_size_type) (1 << power_of_two));
585   if (power_of_two > bfd_get_section_alignment (dynobj, s))
586     {
587       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
588         return false;
589     }
590
591   /* Define the symbol as being at this point in the section.  */
592   h->root.u.def.section = s;
593   h->root.u.def.value = s->_raw_size;
594
595   /* Increment the section size to make room for the symbol.  */
596   s->_raw_size += h->size;
597
598   return true;
599 }
600
601 /* Set the sizes of the dynamic sections.  */
602
603 static boolean
604 elf32_sparc_size_dynamic_sections (output_bfd, info)
605      bfd *output_bfd;
606      struct bfd_link_info *info;
607 {
608   bfd *dynobj;
609   asection *s;
610   boolean reltext;
611   boolean relplt;
612
613   dynobj = elf_hash_table (info)->dynobj;
614   BFD_ASSERT (dynobj != NULL);
615
616   if (elf_hash_table (info)->dynamic_sections_created)
617     {
618       /* Set the contents of the .interp section to the interpreter.  */
619       if (! info->shared)
620         {
621           s = bfd_get_section_by_name (dynobj, ".interp");
622           BFD_ASSERT (s != NULL);
623           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
624           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
625         }
626
627       /* Make space for the trailing nop in .plt.  */
628       s = bfd_get_section_by_name (dynobj, ".plt");
629       BFD_ASSERT (s != NULL);
630       if (s->_raw_size > 0)
631         s->_raw_size += 4;
632     }
633   else
634     {
635       /* We may have created entries in the .rela.got section.
636          However, if we are not creating the dynamic sections, we will
637          not actually use these entries.  Reset the size of .rela.got,
638          which will cause it to get stripped from the output file
639          below.  */
640       s = bfd_get_section_by_name (dynobj, ".rela.got");
641       if (s != NULL)
642         s->_raw_size = 0;
643     }
644
645   /* The check_relocs and adjust_dynamic_symbol entry points have
646      determined the sizes of the various dynamic sections.  Allocate
647      memory for them.  */
648   reltext = false;
649   relplt = false;
650   for (s = dynobj->sections; s != NULL; s = s->next)
651     {
652       const char *name;
653       boolean strip;
654
655       if ((s->flags & SEC_IN_MEMORY) == 0)
656         continue;
657
658       /* It's OK to base decisions on the section name, because none
659          of the dynobj section names depend upon the input files.  */
660       name = bfd_get_section_name (dynobj, s);
661
662       strip = false;
663
664       if (strncmp (name, ".rela", 5) == 0)
665         {
666           if (s->_raw_size == 0)
667             {
668               /* If we don't need this section, strip it from the
669                  output file.  This is to handle .rela.bss and
670                  .rel.plt.  We must create it in
671                  create_dynamic_sections, because it must be created
672                  before the linker maps input sections to output
673                  sections.  The linker does that before
674                  adjust_dynamic_symbol is called, and it is that
675                  function which decides whether anything needs to go
676                  into these sections.  */
677               strip = true;
678             }
679           else
680             {
681               asection *target;
682
683               /* If this relocation section applies to a read only
684                  section, then we probably need a DT_TEXTREL entry.  */
685               target = bfd_get_section_by_name (output_bfd, name + 5);
686               if (target != NULL
687                   && (target->flags & SEC_READONLY) != 0)
688                 reltext = true;
689
690               if (strcmp (name, ".rela.plt") == 0)
691                 relplt = true;
692
693               /* We use the reloc_count field as a counter if we need
694                  to copy relocs into the output file.  */
695               s->reloc_count = 0;
696             }
697         }
698       else if (strcmp (name, ".plt") != 0
699                && strcmp (name, ".got") != 0)
700         {
701           /* It's not one of our sections, so don't allocate space.  */
702           continue;
703         }
704
705       if (strip)
706         {
707           asection **spp;
708
709           for (spp = &s->output_section->owner->sections;
710                *spp != s->output_section;
711                spp = &(*spp)->next)
712             ;
713           *spp = s->output_section->next;
714           --s->output_section->owner->section_count;
715
716           continue;
717         }
718
719       /* Allocate memory for the section contents.  */
720       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
721       if (s->contents == NULL && s->_raw_size != 0)
722         {
723           bfd_set_error (bfd_error_no_memory);
724           return false;
725         }
726     }
727
728   if (elf_hash_table (info)->dynamic_sections_created)
729     {
730       /* Add some entries to the .dynamic section.  We fill in the
731          values later, in elf32_sparc_finish_dynamic_sections, but we
732          must add the entries now so that we get the correct size for
733          the .dynamic section.  The DT_DEBUG entry is filled in by the
734          dynamic linker and used by the debugger.  */
735       if (! info->shared)
736         {
737           if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
738             return false;
739         }
740
741       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
742         return false;
743
744       if (relplt)
745         {
746           if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
747               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
748               || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
749             return false;
750         }
751
752       if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
753           || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
754           || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
755                                             sizeof (Elf32_External_Rela)))
756         return false;
757
758       if (reltext)
759         {
760           if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
761             return false;
762         }
763     }
764
765   /* If we are generating a shared library, we generate a section
766      symbol for each output section.  These are local symbols, which
767      means that they must come first in the dynamic symbol table.
768      That means we must increment the dynamic symbol index of every
769      other dynamic symbol.  */
770   if (info->shared)
771     {
772       int c, i;
773
774       c = bfd_count_sections (output_bfd);
775       elf_link_hash_traverse (elf_hash_table (info),
776                               elf32_sparc_adjust_dynindx,
777                               (PTR) &c);
778       elf_hash_table (info)->dynsymcount += c;
779
780       for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
781         {
782           elf_section_data (s)->dynindx = i;
783           /* These symbols will have no names, so we don't need to
784              fiddle with dynstr_index.  */
785         }
786     }
787
788   return true;
789 }
790
791 /* Increment the index of a dynamic symbol by a given amount.  Called
792    via elf_link_hash_traverse.  */
793
794 static boolean
795 elf32_sparc_adjust_dynindx (h, cparg)
796      struct elf_link_hash_entry *h;
797      PTR cparg;
798 {
799   int *cp = (int *) cparg;
800
801   if (h->dynindx != -1)
802     h->dynindx += *cp;
803   return true;
804 }
805
806 /* Relocate a SPARC ELF section.  */
807
808 static boolean
809 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
810                               contents, relocs, local_syms, local_sections)
811      bfd *output_bfd;
812      struct bfd_link_info *info;
813      bfd *input_bfd;
814      asection *input_section;
815      bfd_byte *contents;
816      Elf_Internal_Rela *relocs;
817      Elf_Internal_Sym *local_syms;
818      asection **local_sections;
819 {
820   bfd *dynobj;
821   Elf_Internal_Shdr *symtab_hdr;
822   struct elf_link_hash_entry **sym_hashes;
823   bfd_vma *local_got_offsets;
824   asection *sgot;
825   asection *splt;
826   asection *sreloc;
827   Elf_Internal_Rela *rel;
828   Elf_Internal_Rela *relend;
829
830   dynobj = elf_hash_table (info)->dynobj;
831   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
832   sym_hashes = elf_sym_hashes (input_bfd);
833   local_got_offsets = elf_local_got_offsets (input_bfd);
834
835   sgot = NULL;
836   splt = NULL;
837   sreloc = NULL;
838
839   rel = relocs;
840   relend = relocs + input_section->reloc_count;
841   for (; rel < relend; rel++)
842     {
843       int r_type;
844       reloc_howto_type *howto;
845       unsigned long r_symndx;
846       struct elf_link_hash_entry *h;
847       Elf_Internal_Sym *sym;
848       asection *sec;
849       bfd_vma relocation;
850       bfd_reloc_status_type r;
851
852       r_type = ELF32_R_TYPE (rel->r_info);
853       if (r_type < 0 || r_type >= (int) R_SPARC_max)
854         {
855           bfd_set_error (bfd_error_bad_value);
856           return false;
857         }
858       howto = elf_sparc_howto_table + r_type;
859
860       r_symndx = ELF32_R_SYM (rel->r_info);
861
862       if (info->relocateable)
863         {
864           /* This is a relocateable link.  We don't have to change
865              anything, unless the reloc is against a section symbol,
866              in which case we have to adjust according to where the
867              section symbol winds up in the output section.  */
868           if (r_symndx < symtab_hdr->sh_info)
869             {
870               sym = local_syms + r_symndx;
871               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
872                 {
873                   sec = local_sections[r_symndx];
874                   rel->r_addend += sec->output_offset + sym->st_value;
875                 }
876             }
877
878           continue;
879         }
880
881       /* This is a final link.  */
882       h = NULL;
883       sym = NULL;
884       sec = NULL;
885       if (r_symndx < symtab_hdr->sh_info)
886         {
887           sym = local_syms + r_symndx;
888           sec = local_sections[r_symndx];
889           relocation = (sec->output_section->vma
890                         + sec->output_offset
891                         + sym->st_value);
892         }
893       else
894         {
895           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
896           if (h->root.type == bfd_link_hash_defined
897               || h->root.type == bfd_link_hash_defweak)
898             {
899               sec = h->root.u.def.section;
900               if ((r_type == R_SPARC_WPLT30
901                    && h->plt_offset != (bfd_vma) -1)
902                   || ((r_type == R_SPARC_GOT10
903                        || r_type == R_SPARC_GOT13
904                        || r_type == R_SPARC_GOT22)
905                       && elf_hash_table (info)->dynamic_sections_created
906                       && (! info->shared
907                           || ! info->symbolic
908                           || (h->elf_link_hash_flags
909                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
910                   || (info->shared
911                       && (input_section->flags & SEC_ALLOC) != 0
912                       && (r_type == R_SPARC_8
913                           || r_type == R_SPARC_16
914                           || r_type == R_SPARC_32
915                           || r_type == R_SPARC_DISP8
916                           || r_type == R_SPARC_DISP16
917                           || r_type == R_SPARC_DISP32
918                           || r_type == R_SPARC_WDISP30
919                           || r_type == R_SPARC_WDISP22
920                           || r_type == R_SPARC_HI22
921                           || r_type == R_SPARC_22
922                           || r_type == R_SPARC_13
923                           || r_type == R_SPARC_LO10
924                           || r_type == R_SPARC_UA32
925                           || ((r_type == R_SPARC_PC10
926                                || r_type == R_SPARC_PC22)
927                               && strcmp (h->root.root.string,
928                                          "_GLOBAL_OFFSET_TABLE_") != 0))))
929                 {
930                   /* In these cases, we don't need the relocation
931                      value.  We check specially because in some
932                      obscure cases sec->output_section will be NULL.  */
933                   relocation = 0;
934                 }
935               else
936                 relocation = (h->root.u.def.value
937                               + sec->output_section->vma
938                               + sec->output_offset);
939             }
940           else if (h->root.type == bfd_link_hash_undefweak)
941             relocation = 0;
942           else if (info->shared && !info->symbolic)
943             relocation = 0;
944           else
945             {
946               if (! ((*info->callbacks->undefined_symbol)
947                      (info, h->root.root.string, input_bfd,
948                       input_section, rel->r_offset)))
949                 return false;
950               relocation = 0;
951             }
952         }
953
954       switch (r_type)
955         {
956         case R_SPARC_GOT10:
957         case R_SPARC_GOT13:
958         case R_SPARC_GOT22:
959           /* Relocation is to the entry for this symbol in the global
960              offset table.  */
961           if (sgot == NULL)
962             {
963               sgot = bfd_get_section_by_name (dynobj, ".got");
964               BFD_ASSERT (sgot != NULL);
965             }
966
967           if (h != NULL)
968             {
969               bfd_vma off;
970
971               off = h->got_offset;
972               BFD_ASSERT (off != (bfd_vma) -1);
973
974               if (! elf_hash_table (info)->dynamic_sections_created
975                   || (info->shared
976                       && info->symbolic
977                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
978                 {
979                   /* This is actually a static link, or it is a
980                      -Bsymbolic link and the symbol is defined
981                      locally.  We must initialize this entry in the
982                      global offset table.  Since the offset must
983                      always be a multiple of 4, we use the least
984                      significant bit to record whether we have
985                      initialized it already.
986
987                      When doing a dynamic link, we create a .rela.got
988                      relocation entry to initialize the value.  This
989                      is done in the finish_dynamic_symbol routine.  */
990                   if ((off & 1) != 0)
991                     off &= ~1;
992                   else
993                     {
994                       bfd_put_32 (output_bfd, relocation,
995                                   sgot->contents + off);
996                       h->got_offset |= 1;
997                     }
998                 }
999
1000               relocation = sgot->output_offset + off;
1001             }
1002           else
1003             {
1004               bfd_vma off;
1005
1006               BFD_ASSERT (local_got_offsets != NULL
1007                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1008
1009               off = local_got_offsets[r_symndx];
1010
1011               /* The offset must always be a multiple of 4.  We use
1012                  the least significant bit to record whether we have
1013                  already processed this entry.  */
1014               if ((off & 1) != 0)
1015                 off &= ~1;
1016               else
1017                 {
1018                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1019
1020                   if (info->shared)
1021                     {
1022                       asection *srelgot;
1023                       Elf_Internal_Rela outrel;
1024
1025                       /* We need to generate a R_SPARC_RELATIVE reloc
1026                          for the dynamic linker.  */
1027                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1028                       BFD_ASSERT (srelgot != NULL);
1029
1030                       outrel.r_offset = (sgot->output_section->vma
1031                                          + sgot->output_offset
1032                                          + off);
1033                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1034                       outrel.r_addend = 0;
1035                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1036                                                  (((Elf32_External_Rela *)
1037                                                    srelgot->contents)
1038                                                   + srelgot->reloc_count));
1039                       ++srelgot->reloc_count;
1040                     }
1041
1042                   local_got_offsets[r_symndx] |= 1;
1043                 }
1044
1045               relocation = sgot->output_offset + off;
1046             }
1047
1048           break;
1049
1050         case R_SPARC_WPLT30:
1051           /* Relocation is to the entry for this symbol in the
1052              procedure linkage table.  */
1053           BFD_ASSERT (h != NULL);
1054
1055           if (h->plt_offset == (bfd_vma) -1)
1056             {
1057               /* We didn't make a PLT entry for this symbol.  This
1058                  happens when statically linking PIC code, or when
1059                  using -Bsymbolic.  */
1060               break;
1061             }
1062
1063           if (splt == NULL)
1064             {
1065               splt = bfd_get_section_by_name (dynobj, ".plt");
1066               BFD_ASSERT (splt != NULL);
1067             }
1068
1069           relocation = (splt->output_section->vma
1070                         + splt->output_offset
1071                         + h->plt_offset);
1072           break;
1073
1074         case R_SPARC_PC10:
1075         case R_SPARC_PC22:
1076           if (h != NULL
1077               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1078             break;
1079           /* Fall through.  */
1080         case R_SPARC_DISP8:
1081         case R_SPARC_DISP16:
1082         case R_SPARC_DISP32:
1083         case R_SPARC_WDISP30:
1084         case R_SPARC_WDISP22:
1085           if (h == NULL)
1086             break;
1087           /* Fall through.  */
1088         case R_SPARC_8:
1089         case R_SPARC_16:
1090         case R_SPARC_32:
1091         case R_SPARC_HI22:
1092         case R_SPARC_22:
1093         case R_SPARC_13:
1094         case R_SPARC_LO10:
1095         case R_SPARC_UA32:
1096           if (info->shared
1097               && (input_section->flags & SEC_ALLOC) != 0)
1098             {
1099               Elf_Internal_Rela outrel;
1100
1101               /* When generating a shared object, these relocations
1102                  are copied into the output file to be resolved at run
1103                  time.  */
1104
1105               if (sreloc == NULL)
1106                 {
1107                   const char *name;
1108
1109                   name = (bfd_elf_string_from_elf_section
1110                           (input_bfd,
1111                            elf_elfheader (input_bfd)->e_shstrndx,
1112                            elf_section_data (input_section)->rel_hdr.sh_name));
1113                   if (name == NULL)
1114                     return false;
1115
1116                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1117                               && strcmp (bfd_get_section_name (input_bfd,
1118                                                                input_section),
1119                                          name + 5) == 0);
1120
1121                   sreloc = bfd_get_section_by_name (dynobj, name);
1122                   BFD_ASSERT (sreloc != NULL);
1123                 }
1124
1125               outrel.r_offset = (rel->r_offset
1126                                  + input_section->output_section->vma
1127                                  + input_section->output_offset);
1128               if (h != NULL)
1129                 {
1130                   BFD_ASSERT (h->dynindx != -1);
1131                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1132                   outrel.r_addend = rel->r_addend;
1133                 }
1134               else
1135                 {
1136                   if (r_type == R_SPARC_32)
1137                     {
1138                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1139                       outrel.r_addend = relocation + rel->r_addend;
1140                     }
1141                   else
1142                     {
1143                       long indx;
1144
1145                       sym = local_syms + r_symndx;
1146
1147                       BFD_ASSERT (ELF_ST_TYPE (sym->st_info) == STT_SECTION);
1148
1149                       sec = local_sections[r_symndx];
1150                       if (sec != NULL && bfd_is_abs_section (sec))
1151                         indx = 0;
1152                       else if (sec == NULL || sec->owner == NULL)
1153                         {
1154                           bfd_set_error (bfd_error_bad_value);
1155                           return false;
1156                         }
1157                       else
1158                         {
1159                           asection *osec;
1160
1161                           osec = sec->output_section;
1162                           indx = elf_section_data (osec)->dynindx;
1163                           if (indx == 0)
1164                             abort ();
1165                         }
1166
1167                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1168                       outrel.r_addend = relocation + rel->r_addend;
1169                     }
1170                 }
1171
1172               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1173                                          (((Elf32_External_Rela *)
1174                                            sreloc->contents)
1175                                           + sreloc->reloc_count));
1176               ++sreloc->reloc_count;
1177
1178               /* This reloc will be computed at runtime, so there's no
1179                  need to do anything now.  */
1180               continue;
1181             }
1182
1183         default:
1184           break;
1185         }               
1186
1187       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1188                                     contents, rel->r_offset,
1189                                     relocation, rel->r_addend);
1190
1191       if (r != bfd_reloc_ok)
1192         {
1193           switch (r)
1194             {
1195             default:
1196             case bfd_reloc_outofrange:
1197               abort ();
1198             case bfd_reloc_overflow:
1199               {
1200                 const char *name;
1201
1202                 if (h != NULL)
1203                   name = h->root.root.string;
1204                 else
1205                   {
1206                     name = bfd_elf_string_from_elf_section (input_bfd,
1207                                                             symtab_hdr->sh_link,
1208                                                             sym->st_name);
1209                     if (name == NULL)
1210                       return false;
1211                     if (*name == '\0')
1212                       name = bfd_section_name (input_bfd, sec);
1213                   }
1214                 if (! ((*info->callbacks->reloc_overflow)
1215                        (info, name, howto->name, (bfd_vma) 0,
1216                         input_bfd, input_section, rel->r_offset)))
1217                   return false;
1218               }
1219               break;
1220             }
1221         }
1222     }
1223
1224   return true;
1225 }
1226
1227 /* Finish up dynamic symbol handling.  We set the contents of various
1228    dynamic sections here.  */
1229
1230 static boolean
1231 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1232      bfd *output_bfd;
1233      struct bfd_link_info *info;
1234      struct elf_link_hash_entry *h;
1235      Elf_Internal_Sym *sym;
1236 {
1237   bfd *dynobj;
1238
1239   dynobj = elf_hash_table (info)->dynobj;
1240
1241   if (h->plt_offset != (bfd_vma) -1)
1242     {
1243       asection *splt;
1244       asection *srela;
1245       Elf_Internal_Rela rela;
1246
1247       /* This symbol has an entry in the procedure linkage table.  Set
1248          it up.  */
1249
1250       BFD_ASSERT (h->dynindx != -1);
1251
1252       splt = bfd_get_section_by_name (dynobj, ".plt");
1253       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1254       BFD_ASSERT (splt != NULL && srela != NULL);
1255
1256       /* Fill in the entry in the procedure linkage table.  */
1257       bfd_put_32 (output_bfd,
1258                   PLT_ENTRY_WORD0 + h->plt_offset,
1259                   splt->contents + h->plt_offset);
1260       bfd_put_32 (output_bfd,
1261                   (PLT_ENTRY_WORD1
1262                    + (((- (h->plt_offset + 4)) >> 2) & 0x3fffff)),
1263                   splt->contents + h->plt_offset + 4);
1264       bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1265                   splt->contents + h->plt_offset + 8);
1266
1267       /* Fill in the entry in the .rela.plt section.  */
1268       rela.r_offset = (splt->output_section->vma
1269                        + splt->output_offset
1270                        + h->plt_offset);
1271       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1272       rela.r_addend = 0;
1273       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1274                                  ((Elf32_External_Rela *) srela->contents
1275                                   + h->plt_offset / PLT_ENTRY_SIZE - 4));
1276
1277       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1278         {
1279           /* Mark the symbol as undefined, rather than as defined in
1280              the .plt section.  Leave the value alone.  */
1281           sym->st_shndx = SHN_UNDEF;
1282         }
1283     }
1284
1285   if (h->got_offset != (bfd_vma) -1)
1286     {
1287       asection *sgot;
1288       asection *srela;
1289       Elf_Internal_Rela rela;
1290
1291       /* This symbol has an entry in the global offset table.  Set it
1292          up.  */
1293
1294       BFD_ASSERT (h->dynindx != -1);
1295
1296       sgot = bfd_get_section_by_name (dynobj, ".got");
1297       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1298       BFD_ASSERT (sgot != NULL && srela != NULL);
1299
1300       rela.r_offset = (sgot->output_section->vma
1301                        + sgot->output_offset
1302                        + (h->got_offset &~ 1));
1303
1304       /* If this is a -Bsymbolic link, and the symbol is defined
1305          locally, we just want to emit a RELATIVE reloc.  The entry in
1306          the global offset table will already have been initialized in
1307          the relocate_section function.  */
1308       if (info->shared
1309           && info->symbolic
1310           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1311         rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1312       else
1313         {
1314           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1315           rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1316         }
1317
1318       rela.r_addend = 0;
1319       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1320                                  ((Elf32_External_Rela *) srela->contents
1321                                   + srela->reloc_count));
1322       ++srela->reloc_count;
1323     }
1324
1325   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1326     {
1327       asection *s;
1328       Elf_Internal_Rela rela;
1329
1330       /* This symbols needs a copy reloc.  Set it up.  */
1331
1332       BFD_ASSERT (h->dynindx != -1);
1333
1334       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1335                                    ".rela.bss");
1336       BFD_ASSERT (s != NULL);
1337
1338       rela.r_offset = (h->root.u.def.value
1339                        + h->root.u.def.section->output_section->vma
1340                        + h->root.u.def.section->output_offset);
1341       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1342       rela.r_addend = 0;
1343       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1344                                  ((Elf32_External_Rela *) s->contents
1345                                   + s->reloc_count));
1346       ++s->reloc_count;
1347     }
1348
1349   /* Mark some specially defined symbols as absolute.  */
1350   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1351       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1352       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1353     sym->st_shndx = SHN_ABS;
1354
1355   return true;
1356 }
1357
1358 /* Finish up the dynamic sections.  */
1359
1360 static boolean
1361 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1362      bfd *output_bfd;
1363      struct bfd_link_info *info;
1364 {
1365   bfd *dynobj;
1366   asection *sdyn;
1367   asection *sgot;
1368
1369   dynobj = elf_hash_table (info)->dynobj;
1370
1371   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1372
1373   if (elf_hash_table (info)->dynamic_sections_created)
1374     {
1375       asection *splt;
1376       Elf32_External_Dyn *dyncon, *dynconend;
1377
1378       splt = bfd_get_section_by_name (dynobj, ".plt");
1379       BFD_ASSERT (splt != NULL && sdyn != NULL);
1380
1381       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1382       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1383       for (; dyncon < dynconend; dyncon++)
1384         {
1385           Elf_Internal_Dyn dyn;
1386           const char *name;
1387           boolean size;
1388
1389           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1390
1391           switch (dyn.d_tag)
1392             {
1393             case DT_PLTGOT:   name = ".plt"; size = false; break;
1394             case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1395             case DT_JMPREL:   name = ".rela.plt"; size = false; break;
1396             default:      name = NULL; size = false; break;
1397             }
1398
1399           if (name != NULL)
1400             {
1401               asection *s;
1402
1403               s = bfd_get_section_by_name (output_bfd, name);
1404               if (s == NULL)
1405                 dyn.d_un.d_val = 0;
1406               else
1407                 {
1408                   if (! size)
1409                     dyn.d_un.d_ptr = s->vma;
1410                   else
1411                     {
1412                       if (s->_cooked_size != 0)
1413                         dyn.d_un.d_val = s->_cooked_size;
1414                       else
1415                         dyn.d_un.d_val = s->_raw_size;
1416                     }
1417                 }
1418               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1419             }
1420         }
1421
1422       /* Clear the first four entries in the procedure linkage table,
1423          and put a nop in the last four bytes.  */
1424       if (splt->_raw_size > 0)
1425         {
1426           memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1427           bfd_put_32 (output_bfd, SPARC_NOP,
1428                       splt->contents + splt->_raw_size - 4);
1429         }
1430
1431       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1432         PLT_ENTRY_SIZE;
1433     }
1434
1435   /* Set the first entry in the global offset table to the address of
1436      the dynamic section.  */
1437   sgot = bfd_get_section_by_name (dynobj, ".got");
1438   BFD_ASSERT (sgot != NULL);
1439   if (sgot->_raw_size > 0)
1440     {
1441       if (sdyn == NULL)
1442         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1443       else
1444         bfd_put_32 (output_bfd,
1445                     sdyn->output_section->vma + sdyn->output_offset,
1446                     sgot->contents);
1447     }
1448
1449   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1450
1451   if (info->shared)
1452     {
1453       asection *sdynsym;
1454       asection *s;
1455       Elf_Internal_Sym sym;
1456
1457       /* Set up the section symbols for the output sections.  */
1458
1459       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
1460       BFD_ASSERT (sdynsym != NULL);
1461
1462       sym.st_size = 0;
1463       sym.st_name = 0;
1464       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1465       sym.st_other = 0;
1466
1467       for (s = output_bfd->sections; s != NULL; s = s->next)
1468         {
1469           int indx;
1470
1471           sym.st_value = s->vma;
1472
1473           indx = elf_section_data (s)->this_idx;
1474           BFD_ASSERT (indx > 0);
1475           sym.st_shndx = indx;
1476
1477           bfd_elf32_swap_symbol_out (output_bfd, &sym,
1478                                      (PTR) (((Elf32_External_Sym *)
1479                                              sdynsym->contents)
1480                                             + elf_section_data (s)->dynindx));
1481         }
1482
1483       /* Set the sh_info field of the output .dynsym section to the
1484          index of the first global symbol.  */
1485       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
1486         bfd_count_sections (output_bfd) + 1;
1487     }
1488
1489   return true;
1490 }
1491
1492 #define TARGET_BIG_SYM  bfd_elf32_sparc_vec
1493 #define TARGET_BIG_NAME "elf32-sparc"
1494 #define ELF_ARCH        bfd_arch_sparc
1495 #define ELF_MACHINE_CODE EM_SPARC
1496 #define ELF_MAXPAGESIZE 0x10000
1497 #define elf_backend_create_dynamic_sections \
1498                                         _bfd_elf_create_dynamic_sections
1499 #define elf_backend_check_relocs        elf32_sparc_check_relocs
1500 #define elf_backend_adjust_dynamic_symbol \
1501                                         elf32_sparc_adjust_dynamic_symbol
1502 #define elf_backend_size_dynamic_sections \
1503                                         elf32_sparc_size_dynamic_sections
1504 #define elf_backend_relocate_section    elf32_sparc_relocate_section
1505 #define elf_backend_finish_dynamic_symbol \
1506                                         elf32_sparc_finish_dynamic_symbol
1507 #define elf_backend_finish_dynamic_sections \
1508                                         elf32_sparc_finish_dynamic_sections
1509 #define elf_backend_want_got_plt 0
1510 #define elf_backend_plt_readonly 0
1511 #define elf_backend_want_plt_sym 1
1512
1513 #include "elf32-target.h"