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