OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [bfd/] [elf32-sparc.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 104 markom
/* SPARC-specific support for 32-bit ELF
2
   Copyright (C) 1993, 94, 95, 96, 97, 98, 99, 2000
3
   Free Software Foundation, Inc.
4
 
5
This file is part of BFD, the Binary File Descriptor library.
6
 
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
 
21
#include "bfd.h"
22
#include "sysdep.h"
23
#include "bfdlink.h"
24
#include "libbfd.h"
25
#include "elf-bfd.h"
26
#include "elf/sparc.h"
27
 
28
static reloc_howto_type *elf32_sparc_reloc_type_lookup
29
  PARAMS ((bfd *, bfd_reloc_code_real_type));
30
static void elf32_sparc_info_to_howto
31
  PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
32
static boolean elf32_sparc_check_relocs
33
  PARAMS ((bfd *, struct bfd_link_info *, asection *,
34
           const Elf_Internal_Rela *));
35
static boolean elf32_sparc_adjust_dynamic_symbol
36
  PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
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
static boolean elf32_sparc_merge_private_bfd_data PARAMS ((bfd *, bfd *));
48
static boolean elf32_sparc_object_p
49
  PARAMS ((bfd *));
50
static void elf32_sparc_final_write_processing
51
  PARAMS ((bfd *, boolean));
52
 
53
/* The relocation "howto" table.  */
54
 
55
static bfd_reloc_status_type sparc_elf_notsupported_reloc
56
  PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
57
static bfd_reloc_status_type sparc_elf_wdisp16_reloc
58
  PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59
 
60
reloc_howto_type _bfd_sparc_elf_howto_table[] =
61
{
62
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
63
  HOWTO(R_SPARC_8,         0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       false,0,0x000000ff,true),
64
  HOWTO(R_SPARC_16,        0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      false,0,0x0000ffff,true),
65
  HOWTO(R_SPARC_32,        0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      false,0,0xffffffff,true),
66
  HOWTO(R_SPARC_DISP8,     0,0, 8,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   false,0,0x000000ff,true),
67
  HOWTO(R_SPARC_DISP16,    0,1,16,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  false,0,0x0000ffff,true),
68
  HOWTO(R_SPARC_DISP32,    0,2,32,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  false,0,0x00ffffff,true),
69
  HOWTO(R_SPARC_WDISP30,   2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", false,0,0x3fffffff,true),
70
  HOWTO(R_SPARC_WDISP22,   2,2,22,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", false,0,0x003fffff,true),
71
  HOWTO(R_SPARC_HI22,     10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    false,0,0x003fffff,true),
72
  HOWTO(R_SPARC_22,        0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      false,0,0x003fffff,true),
73
  HOWTO(R_SPARC_13,        0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      false,0,0x00001fff,true),
74
  HOWTO(R_SPARC_LO10,      0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    false,0,0x000003ff,true),
75
  HOWTO(R_SPARC_GOT10,     0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   false,0,0x000003ff,true),
76
  HOWTO(R_SPARC_GOT13,     0,2,13,false,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   false,0,0x00001fff,true),
77
  HOWTO(R_SPARC_GOT22,    10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   false,0,0x003fffff,true),
78
  HOWTO(R_SPARC_PC10,      0,2,10,true, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    false,0,0x000003ff,true),
79
  HOWTO(R_SPARC_PC22,     10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    false,0,0x003fffff,true),
80
  HOWTO(R_SPARC_WPLT30,    2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  false,0,0x3fffffff,true),
81
  HOWTO(R_SPARC_COPY,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    false,0,0x00000000,true),
82
  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),
83
  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),
84
  HOWTO(R_SPARC_RELATIVE,  0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",false,0,0x00000000,true),
85
  HOWTO(R_SPARC_UA32,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_UA32",    false,0,0x00000000,true),
86
  HOWTO(R_SPARC_PLT32,     0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PLT32",    false,0,0x00000000,true),
87
  HOWTO(R_SPARC_HIPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HIPLT22",  false,0,0x00000000,true),
88
  HOWTO(R_SPARC_LOPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LOPLT10",  false,0,0x00000000,true),
89
  HOWTO(R_SPARC_PCPLT32,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT32",  false,0,0x00000000,true),
90
  HOWTO(R_SPARC_PCPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT22",  false,0,0x00000000,true),
91
  HOWTO(R_SPARC_PCPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT10",  false,0,0x00000000,true),
92
  HOWTO(R_SPARC_10,        0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      false,0,0x000003ff,true),
93
  HOWTO(R_SPARC_11,        0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      false,0,0x000007ff,true),
94
  /* These are for sparc64 in a 64 bit environment.
95
     Values need to be here because the table is indexed by reloc number.  */
96
  HOWTO(R_SPARC_64,        0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_64",      false,0,0x00000000,true),
97
  HOWTO(R_SPARC_OLO10,     0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_OLO10",   false,0,0x00000000,true),
98
  HOWTO(R_SPARC_HH22,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HH22",    false,0,0x00000000,true),
99
  HOWTO(R_SPARC_HM10,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HM10",    false,0,0x00000000,true),
100
  HOWTO(R_SPARC_LM22,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LM22",    false,0,0x00000000,true),
101
  HOWTO(R_SPARC_PC_HH22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PC_HH22", false,0,0x00000000,true),
102
  HOWTO(R_SPARC_PC_HM10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PC_HM10", false,0,0x00000000,true),
103
  HOWTO(R_SPARC_PC_LM22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PC_LM22", false,0,0x00000000,true),
104
  /* End sparc64 in 64 bit environment values.
105
     The following are for sparc64 in a 32 bit environment.  */
106
  HOWTO(R_SPARC_WDISP16,   2,2,16,true, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
107
  HOWTO(R_SPARC_WDISP19,   2,2,19,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", false,0,0x0007ffff,true),
108
  HOWTO(R_SPARC_UNUSED_42, 0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_UNUSED_42",false,0,0x00000000,true),
109
  HOWTO(R_SPARC_7,         0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       false,0,0x0000007f,true),
110
  HOWTO(R_SPARC_5,         0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       false,0,0x0000001f,true),
111
  HOWTO(R_SPARC_6,         0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       false,0,0x0000003f,true),
112
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
113
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
114
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
115
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
116
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
117
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
118
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
119
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
120
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
121
  HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
122
  HOWTO(R_SPARC_REV32,      0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_REV32",      false,0,0xffffffff,true),
123
};
124
static reloc_howto_type elf32_sparc_vtinherit_howto =
125
  HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
126
static reloc_howto_type elf32_sparc_vtentry_howto =
127
  HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,false,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", false,0,0, false);
128
 
129
 
130
struct elf_reloc_map {
131
  bfd_reloc_code_real_type bfd_reloc_val;
132
  unsigned char elf_reloc_val;
133
};
134
 
135
static CONST struct elf_reloc_map sparc_reloc_map[] =
136
{
137
  { BFD_RELOC_NONE, R_SPARC_NONE, },
138
  { BFD_RELOC_16, R_SPARC_16, },
139
  { BFD_RELOC_8, R_SPARC_8 },
140
  { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
141
  { BFD_RELOC_CTOR, R_SPARC_32 },
142
  { BFD_RELOC_32, R_SPARC_32 },
143
  { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
144
  { BFD_RELOC_HI22, R_SPARC_HI22 },
145
  { BFD_RELOC_LO10, R_SPARC_LO10, },
146
  { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
147
  { BFD_RELOC_SPARC22, R_SPARC_22 },
148
  { BFD_RELOC_SPARC13, R_SPARC_13 },
149
  { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
150
  { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
151
  { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
152
  { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
153
  { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
154
  { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
155
  { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
156
  { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
157
  { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
158
  { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
159
  { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
160
  /* ??? Doesn't dwarf use this?  */
161
/*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
162
  {BFD_RELOC_SPARC_10, R_SPARC_10},
163
  {BFD_RELOC_SPARC_11, R_SPARC_11},
164
  {BFD_RELOC_SPARC_64, R_SPARC_64},
165
  {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10},
166
  {BFD_RELOC_SPARC_HH22, R_SPARC_HH22},
167
  {BFD_RELOC_SPARC_HM10, R_SPARC_HM10},
168
  {BFD_RELOC_SPARC_LM22, R_SPARC_LM22},
169
  {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22},
170
  {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10},
171
  {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22},
172
  {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16},
173
  {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19},
174
  {BFD_RELOC_SPARC_7, R_SPARC_7},
175
  {BFD_RELOC_SPARC_5, R_SPARC_5},
176
  {BFD_RELOC_SPARC_6, R_SPARC_6},
177
  {BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
178
  {BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT},
179
  {BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY},
180
};
181
 
182
static reloc_howto_type *
183
elf32_sparc_reloc_type_lookup (abfd, code)
184
     bfd *abfd ATTRIBUTE_UNUSED;
185
     bfd_reloc_code_real_type code;
186
{
187
  unsigned int i;
188
 
189
  switch (code)
190
    {
191
    case BFD_RELOC_VTABLE_INHERIT:
192
      return &elf32_sparc_vtinherit_howto;
193
 
194
    case BFD_RELOC_VTABLE_ENTRY:
195
      return &elf32_sparc_vtentry_howto;
196
 
197
    default:
198
      for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
199
        {
200
          if (sparc_reloc_map[i].bfd_reloc_val == code)
201
            return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
202
        }
203
    }
204
    bfd_set_error (bfd_error_bad_value);
205
    return NULL;
206
}
207
 
208
/* We need to use ELF32_R_TYPE so we have our own copy of this function,
209
   and elf64-sparc.c has its own copy.  */
210
 
211
static void
212
elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
213
     bfd *abfd ATTRIBUTE_UNUSED;
214
     arelent *cache_ptr;
215
     Elf_Internal_Rela *dst;
216
{
217
  switch (ELF32_R_TYPE(dst->r_info))
218
    {
219
    case R_SPARC_GNU_VTINHERIT:
220
      cache_ptr->howto = &elf32_sparc_vtinherit_howto;
221
      break;
222
 
223
    case R_SPARC_GNU_VTENTRY:
224
      cache_ptr->howto = &elf32_sparc_vtentry_howto;
225
      break;
226
 
227
    default:
228
      BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
229
      cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
230
    }
231
}
232
 
233
/* For unsupported relocs.  */
234
 
235
static bfd_reloc_status_type
236
sparc_elf_notsupported_reloc (abfd,
237
                             reloc_entry,
238
                             symbol,
239
                             data,
240
                             input_section,
241
                             output_bfd,
242
                             error_message)
243
     bfd *abfd ATTRIBUTE_UNUSED;
244
     arelent *reloc_entry ATTRIBUTE_UNUSED;
245
     asymbol *symbol ATTRIBUTE_UNUSED;
246
     PTR data ATTRIBUTE_UNUSED;
247
     asection *input_section ATTRIBUTE_UNUSED;
248
     bfd *output_bfd ATTRIBUTE_UNUSED;
249
     char **error_message ATTRIBUTE_UNUSED;
250
{
251
  return bfd_reloc_notsupported;
252
}
253
 
254
/* Handle the WDISP16 reloc.  */
255
 
256
static bfd_reloc_status_type
257
sparc_elf_wdisp16_reloc (abfd,
258
                         reloc_entry,
259
                         symbol,
260
                         data,
261
                         input_section,
262
                         output_bfd,
263
                         error_message)
264
     bfd *abfd;
265
     arelent *reloc_entry;
266
     asymbol *symbol;
267
     PTR data;
268
     asection *input_section;
269
     bfd *output_bfd;
270
     char **error_message ATTRIBUTE_UNUSED;
271
{
272
  bfd_vma relocation;
273
  bfd_vma x;
274
 
275
  if (output_bfd != (bfd *) NULL
276
      && (symbol->flags & BSF_SECTION_SYM) == 0
277
      && (! reloc_entry->howto->partial_inplace
278
          || reloc_entry->addend == 0))
279
    {
280
      reloc_entry->address += input_section->output_offset;
281
      return bfd_reloc_ok;
282
    }
283
 
284
  if (output_bfd != NULL)
285
    return bfd_reloc_continue;
286
 
287
  if (reloc_entry->address > input_section->_cooked_size)
288
    return bfd_reloc_outofrange;
289
 
290
  relocation = (symbol->value
291
                + symbol->section->output_section->vma
292
                + symbol->section->output_offset);
293
  relocation += reloc_entry->addend;
294
  relocation -= (input_section->output_section->vma
295
                 + input_section->output_offset);
296
  relocation -= reloc_entry->address;
297
 
298
  x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
299
  x |= ((((relocation >> 2) & 0xc000) << 6)
300
        | ((relocation >> 2) & 0x3fff));
301
  bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
302
 
303
  if ((bfd_signed_vma) relocation < - 0x40000
304
      || (bfd_signed_vma) relocation > 0x3ffff)
305
    return bfd_reloc_overflow;
306
  else
307
    return bfd_reloc_ok;
308
}
309
 
310
/* Functions for the SPARC ELF linker.  */
311
 
312
/* The name of the dynamic interpreter.  This is put in the .interp
313
   section.  */
314
 
315
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
316
 
317
/* The nop opcode we use.  */
318
 
319
#define SPARC_NOP 0x01000000
320
 
321
/* The size in bytes of an entry in the procedure linkage table.  */
322
 
323
#define PLT_ENTRY_SIZE 12
324
 
325
/* The first four entries in a procedure linkage table are reserved,
326
   and the initial contents are unimportant (we zero them out).
327
   Subsequent entries look like this.  See the SVR4 ABI SPARC
328
   supplement to see how this works.  */
329
 
330
/* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
331
#define PLT_ENTRY_WORD0 0x03000000
332
/* b,a .plt0.  We fill in the offset later.  */
333
#define PLT_ENTRY_WORD1 0x30800000
334
/* nop.  */
335
#define PLT_ENTRY_WORD2 SPARC_NOP
336
 
337
/* Look through the relocs for a section during the first phase, and
338
   allocate space in the global offset table or procedure linkage
339
   table.  */
340
 
341
static boolean
342
elf32_sparc_check_relocs (abfd, info, sec, relocs)
343
     bfd *abfd;
344
     struct bfd_link_info *info;
345
     asection *sec;
346
     const Elf_Internal_Rela *relocs;
347
{
348
  bfd *dynobj;
349
  Elf_Internal_Shdr *symtab_hdr;
350
  struct elf_link_hash_entry **sym_hashes;
351
  bfd_vma *local_got_offsets;
352
  const Elf_Internal_Rela *rel;
353
  const Elf_Internal_Rela *rel_end;
354
  asection *sgot;
355
  asection *srelgot;
356
  asection *sreloc;
357
 
358
  if (info->relocateable)
359
    return true;
360
 
361
  dynobj = elf_hash_table (info)->dynobj;
362
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
363
  sym_hashes = elf_sym_hashes (abfd);
364
  local_got_offsets = elf_local_got_offsets (abfd);
365
 
366
  sgot = NULL;
367
  srelgot = NULL;
368
  sreloc = NULL;
369
 
370
  rel_end = relocs + sec->reloc_count;
371
  for (rel = relocs; rel < rel_end; rel++)
372
    {
373
      unsigned long r_symndx;
374
      struct elf_link_hash_entry *h;
375
 
376
      r_symndx = ELF32_R_SYM (rel->r_info);
377
      if (r_symndx < symtab_hdr->sh_info)
378
        h = NULL;
379
      else
380
        h = sym_hashes[r_symndx - symtab_hdr->sh_info];
381
 
382
      switch (ELF32_R_TYPE (rel->r_info))
383
        {
384
        case R_SPARC_GOT10:
385
        case R_SPARC_GOT13:
386
        case R_SPARC_GOT22:
387
          /* This symbol requires a global offset table entry.  */
388
 
389
          if (dynobj == NULL)
390
            {
391
              /* Create the .got section.  */
392
              elf_hash_table (info)->dynobj = dynobj = abfd;
393
              if (! _bfd_elf_create_got_section (dynobj, info))
394
                return false;
395
            }
396
 
397
          if (sgot == NULL)
398
            {
399
              sgot = bfd_get_section_by_name (dynobj, ".got");
400
              BFD_ASSERT (sgot != NULL);
401
            }
402
 
403
          if (srelgot == NULL
404
              && (h != NULL || info->shared))
405
            {
406
              srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
407
              if (srelgot == NULL)
408
                {
409
                  srelgot = bfd_make_section (dynobj, ".rela.got");
410
                  if (srelgot == NULL
411
                      || ! bfd_set_section_flags (dynobj, srelgot,
412
                                                  (SEC_ALLOC
413
                                                   | SEC_LOAD
414
                                                   | SEC_HAS_CONTENTS
415
                                                   | SEC_IN_MEMORY
416
                                                   | SEC_LINKER_CREATED
417
                                                   | SEC_READONLY))
418
                      || ! bfd_set_section_alignment (dynobj, srelgot, 2))
419
                    return false;
420
                }
421
            }
422
 
423
          if (h != NULL)
424
            {
425
              if (h->got.offset != (bfd_vma) -1)
426
                {
427
                  /* We have already allocated space in the .got.  */
428
                  break;
429
                }
430
              h->got.offset = sgot->_raw_size;
431
 
432
              /* Make sure this symbol is output as a dynamic symbol.  */
433
              if (h->dynindx == -1)
434
                {
435
                  if (! bfd_elf32_link_record_dynamic_symbol (info, h))
436
                    return false;
437
                }
438
 
439
              srelgot->_raw_size += sizeof (Elf32_External_Rela);
440
            }
441
          else
442
            {
443
              /* This is a global offset table entry for a local
444
                 symbol.  */
445
              if (local_got_offsets == NULL)
446
                {
447
                  size_t size;
448
                  register unsigned int i;
449
 
450
                  size = symtab_hdr->sh_info * sizeof (bfd_vma);
451
                  local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
452
                  if (local_got_offsets == NULL)
453
                    return false;
454
                  elf_local_got_offsets (abfd) = local_got_offsets;
455
                  for (i = 0; i < symtab_hdr->sh_info; i++)
456
                    local_got_offsets[i] = (bfd_vma) -1;
457
                }
458
              if (local_got_offsets[r_symndx] != (bfd_vma) -1)
459
                {
460
                  /* We have already allocated space in the .got.  */
461
                  break;
462
                }
463
              local_got_offsets[r_symndx] = sgot->_raw_size;
464
 
465
              if (info->shared)
466
                {
467
                  /* If we are generating a shared object, we need to
468
                     output a R_SPARC_RELATIVE reloc so that the
469
                     dynamic linker can adjust this GOT entry.  */
470
                  srelgot->_raw_size += sizeof (Elf32_External_Rela);
471
                }
472
            }
473
 
474
          sgot->_raw_size += 4;
475
 
476
          /* If the .got section is more than 0x1000 bytes, we add
477
             0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
478
             bit relocations have a greater chance of working.  */
479
          if (sgot->_raw_size >= 0x1000
480
              && elf_hash_table (info)->hgot->root.u.def.value == 0)
481
            elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
482
 
483
          break;
484
 
485
        case R_SPARC_WPLT30:
486
          /* This symbol requires a procedure linkage table entry.  We
487
             actually build the entry in adjust_dynamic_symbol,
488
             because this might be a case of linking PIC code without
489
             linking in any dynamic objects, in which case we don't
490
             need to generate a procedure linkage table after all.  */
491
 
492
          if (h == NULL)
493
            {
494
              /* The Solaris native assembler will generate a WPLT30
495
                 reloc for a local symbol if you assemble a call from
496
                 one section to another when using -K pic.  We treat
497
                 it as WDISP30.  */
498
              break;
499
            }
500
 
501
          /* Make sure this symbol is output as a dynamic symbol.  */
502
          if (h->dynindx == -1)
503
            {
504
              if (! bfd_elf32_link_record_dynamic_symbol (info, h))
505
                return false;
506
            }
507
 
508
          h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
509
 
510
          break;
511
 
512
        case R_SPARC_PC10:
513
        case R_SPARC_PC22:
514
          if (h != NULL)
515
            h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
516
 
517
          if (h != NULL
518
              && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
519
            break;
520
          /* Fall through.  */
521
        case R_SPARC_DISP8:
522
        case R_SPARC_DISP16:
523
        case R_SPARC_DISP32:
524
        case R_SPARC_WDISP30:
525
        case R_SPARC_WDISP22:
526
        case R_SPARC_WDISP19:
527
        case R_SPARC_WDISP16:
528
          if (h != NULL)
529
            h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
530
 
531
          /* If we are linking with -Bsymbolic, we do not need to copy
532
             a PC relative reloc against a global symbol which is
533
             defined in an object we are including in the link (i.e.,
534
             DEF_REGULAR is set).  FIXME: At this point we have not
535
             seen all the input files, so it is possible that
536
             DEF_REGULAR is not set now but will be set later (it is
537
             never cleared).  This needs to be handled as in
538
             elf32-i386.c.  */
539
          if (h == NULL
540
              || (info->symbolic
541
                  && (h->elf_link_hash_flags
542
                      & ELF_LINK_HASH_DEF_REGULAR) != 0))
543
            break;
544
          /* Fall through.  */
545
        case R_SPARC_8:
546
        case R_SPARC_16:
547
        case R_SPARC_32:
548
        case R_SPARC_HI22:
549
        case R_SPARC_22:
550
        case R_SPARC_13:
551
        case R_SPARC_LO10:
552
        case R_SPARC_UA32:
553
          if (h != NULL)
554
            h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
555
 
556
          if (info->shared)
557
            {
558
              /* When creating a shared object, we must copy these
559
                 relocs into the output file.  We create a reloc
560
                 section in dynobj and make room for the reloc.  */
561
              if (sreloc == NULL)
562
                {
563
                  const char *name;
564
 
565
                  name = (bfd_elf_string_from_elf_section
566
                          (abfd,
567
                           elf_elfheader (abfd)->e_shstrndx,
568
                           elf_section_data (sec)->rel_hdr.sh_name));
569
                  if (name == NULL)
570
                    return false;
571
 
572
                  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
573
                              && strcmp (bfd_get_section_name (abfd, sec),
574
                                         name + 5) == 0);
575
 
576
                  sreloc = bfd_get_section_by_name (dynobj, name);
577
                  if (sreloc == NULL)
578
                    {
579
                      flagword flags;
580
 
581
                      sreloc = bfd_make_section (dynobj, name);
582
                      flags = (SEC_HAS_CONTENTS | SEC_READONLY
583
                               | SEC_IN_MEMORY | SEC_LINKER_CREATED);
584
                      if ((sec->flags & SEC_ALLOC) != 0)
585
                        flags |= SEC_ALLOC | SEC_LOAD;
586
                      if (sreloc == NULL
587
                          || ! bfd_set_section_flags (dynobj, sreloc, flags)
588
                          || ! bfd_set_section_alignment (dynobj, sreloc, 2))
589
                        return false;
590
                    }
591
                }
592
 
593
              sreloc->_raw_size += sizeof (Elf32_External_Rela);
594
            }
595
 
596
          break;
597
 
598
        case R_SPARC_GNU_VTINHERIT:
599
          if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
600
            return false;
601
          break;
602
 
603
        case R_SPARC_GNU_VTENTRY:
604
          if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
605
            return false;
606
          break;
607
 
608
        default:
609
          break;
610
        }
611
    }
612
 
613
  return true;
614
}
615
 
616
static asection *
617
elf32_sparc_gc_mark_hook (abfd, info, rel, h, sym)
618
       bfd *abfd;
619
       struct bfd_link_info *info ATTRIBUTE_UNUSED;
620
       Elf_Internal_Rela *rel;
621
       struct elf_link_hash_entry *h;
622
       Elf_Internal_Sym *sym;
623
{
624
 
625
  if (h != NULL)
626
    {
627
      switch (ELF32_R_TYPE (rel->r_info))
628
      {
629
      case R_SPARC_GNU_VTINHERIT:
630
      case R_SPARC_GNU_VTENTRY:
631
        break;
632
 
633
      default:
634
        switch (h->root.type)
635
          {
636
          case bfd_link_hash_defined:
637
          case bfd_link_hash_defweak:
638
            return h->root.u.def.section;
639
 
640
          case bfd_link_hash_common:
641
            return h->root.u.c.p->section;
642
 
643
          default:
644
            break;
645
          }
646
       }
647
     }
648
   else
649
     {
650
       if (!(elf_bad_symtab (abfd)
651
           && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
652
         && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
653
                && sym->st_shndx != SHN_COMMON))
654
          {
655
            return bfd_section_from_elf_index (abfd, sym->st_shndx);
656
          }
657
      }
658
 
659
  return NULL;
660
}
661
 
662
/* Update the got entry reference counts for the section being removed.  */
663
static boolean
664
elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
665
     bfd *abfd;
666
     struct bfd_link_info *info ATTRIBUTE_UNUSED;
667
     asection *sec;
668
     const Elf_Internal_Rela *relocs;
669
{
670
 
671
  Elf_Internal_Shdr *symtab_hdr;
672
  struct elf_link_hash_entry **sym_hashes;
673
  bfd_signed_vma *local_got_refcounts;
674
  const Elf_Internal_Rela *rel, *relend;
675
  unsigned long r_symndx;
676
  struct elf_link_hash_entry *h;
677
 
678
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
679
  sym_hashes = elf_sym_hashes (abfd);
680
  local_got_refcounts = elf_local_got_refcounts (abfd);
681
 
682
  relend = relocs + sec->reloc_count;
683
  for (rel = relocs; rel < relend; rel++)
684
    switch (ELF32_R_TYPE (rel->r_info))
685
      {
686
      case R_SPARC_GOT10:
687
      case R_SPARC_GOT13:
688
      case R_SPARC_GOT22:
689
        r_symndx = ELF32_R_SYM (rel->r_info);
690
        if (r_symndx >= symtab_hdr->sh_info)
691
          {
692
            h = sym_hashes[r_symndx - symtab_hdr->sh_info];
693
            if (h->got.refcount > 0)
694
              h->got.refcount--;
695
          }
696
        else
697
          {
698
            if (local_got_refcounts[r_symndx] > 0)
699
              local_got_refcounts[r_symndx]--;
700
          }
701
        break;
702
 
703
      case R_SPARC_PLT32:
704
      case R_SPARC_HIPLT22:
705
      case R_SPARC_LOPLT10:
706
      case R_SPARC_PCPLT32:
707
      case R_SPARC_PCPLT10:
708
        r_symndx = ELF32_R_SYM (rel->r_info);
709
        if (r_symndx >= symtab_hdr->sh_info)
710
          {
711
            h = sym_hashes[r_symndx - symtab_hdr->sh_info];
712
            if (h->plt.refcount > 0)
713
              h->plt.refcount--;
714
          }
715
        break;
716
 
717
      default:
718
        break;
719
      }
720
 
721
  return true;
722
}
723
 
724
/* Adjust a symbol defined by a dynamic object and referenced by a
725
   regular object.  The current definition is in some section of the
726
   dynamic object, but we're not including those sections.  We have to
727
   change the definition to something the rest of the link can
728
   understand.  */
729
 
730
static boolean
731
elf32_sparc_adjust_dynamic_symbol (info, h)
732
     struct bfd_link_info *info;
733
     struct elf_link_hash_entry *h;
734
{
735
  bfd *dynobj;
736
  asection *s;
737
  unsigned int power_of_two;
738
 
739
  dynobj = elf_hash_table (info)->dynobj;
740
 
741
  /* Make sure we know what is going on here.  */
742
  BFD_ASSERT (dynobj != NULL
743
              && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
744
                  || h->weakdef != NULL
745
                  || ((h->elf_link_hash_flags
746
                       & ELF_LINK_HASH_DEF_DYNAMIC) != 0
747
                      && (h->elf_link_hash_flags
748
                          & ELF_LINK_HASH_REF_REGULAR) != 0
749
                      && (h->elf_link_hash_flags
750
                          & ELF_LINK_HASH_DEF_REGULAR) == 0)));
751
 
752
  /* If this is a function, put it in the procedure linkage table.  We
753
     will fill in the contents of the procedure linkage table later
754
     (although we could actually do it here).  The STT_NOTYPE
755
     condition is a hack specifically for the Oracle libraries
756
     delivered for Solaris; for some inexplicable reason, they define
757
     some of their functions as STT_NOTYPE when they really should be
758
     STT_FUNC.  */
759
  if (h->type == STT_FUNC
760
      || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
761
      || (h->type == STT_NOTYPE
762
          && (h->root.type == bfd_link_hash_defined
763
              || h->root.type == bfd_link_hash_defweak)
764
          && (h->root.u.def.section->flags & SEC_CODE) != 0))
765
    {
766
      if (! elf_hash_table (info)->dynamic_sections_created
767
          || ((!info->shared || info->symbolic || h->dynindx == -1)
768
              && (h->elf_link_hash_flags
769
                  & ELF_LINK_HASH_DEF_REGULAR) != 0))
770
        {
771
          /* This case can occur if we saw a WPLT30 reloc in an input
772
             file, but none of the input files were dynamic objects.
773
             Or, when linking the main application or a -Bsymbolic
774
             shared library against PIC code.  Or when a global symbol
775
             has been made private, e.g. via versioning.
776
 
777
             In these cases we know what value the symbol will resolve
778
             to, so we don't actually need to build a procedure linkage
779
             table, and we can just do a WDISP30 reloc instead.  */
780
 
781
          h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
782
          return true;
783
        }
784
 
785
      s = bfd_get_section_by_name (dynobj, ".plt");
786
      BFD_ASSERT (s != NULL);
787
 
788
      /* The first four entries in .plt are reserved.  */
789
      if (s->_raw_size == 0)
790
        s->_raw_size = 4 * PLT_ENTRY_SIZE;
791
 
792
      /* The procedure linkage table has a maximum size.  */
793
      if (s->_raw_size >= 0x400000)
794
        {
795
          bfd_set_error (bfd_error_bad_value);
796
          return false;
797
        }
798
 
799
     /* If this symbol is not defined in a regular file, and we are
800
       not generating a shared library, then set the symbol to this
801
       location in the .plt.  This is required to make function
802
       pointers compare as equal between the normal executable and
803
       the shared library.  */
804
     if (! info->shared
805
        && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
806
      {
807
        h->root.u.def.section = s;
808
        h->root.u.def.value = s->_raw_size;
809
      }
810
 
811
      h->plt.offset = s->_raw_size;
812
 
813
      /* Make room for this entry.  */
814
      s->_raw_size += PLT_ENTRY_SIZE;
815
 
816
      /* We also need to make an entry in the .rela.plt section.  */
817
 
818
      s = bfd_get_section_by_name (dynobj, ".rela.plt");
819
      BFD_ASSERT (s != NULL);
820
      s->_raw_size += sizeof (Elf32_External_Rela);
821
 
822
      return true;
823
    }
824
 
825
  /* If this is a weak symbol, and there is a real definition, the
826
     processor independent code will have arranged for us to see the
827
     real definition first, and we can just use the same value.  */
828
  if (h->weakdef != NULL)
829
    {
830
      BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
831
                  || h->weakdef->root.type == bfd_link_hash_defweak);
832
      h->root.u.def.section = h->weakdef->root.u.def.section;
833
      h->root.u.def.value = h->weakdef->root.u.def.value;
834
      return true;
835
    }
836
 
837
  /* This is a reference to a symbol defined by a dynamic object which
838
     is not a function.  */
839
 
840
  /* If we are creating a shared library, we must presume that the
841
     only references to the symbol are via the global offset table.
842
     For such cases we need not do anything here; the relocations will
843
     be handled correctly by relocate_section.  */
844
  if (info->shared)
845
    return true;
846
 
847
  /* If there are no references to this symbol that do not use the
848
     GOT, we don't need to generate a copy reloc.  */
849
  if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
850
    return true;
851
 
852
  /* We must allocate the symbol in our .dynbss section, which will
853
     become part of the .bss section of the executable.  There will be
854
     an entry for this symbol in the .dynsym section.  The dynamic
855
     object will contain position independent code, so all references
856
     from the dynamic object to this symbol will go through the global
857
     offset table.  The dynamic linker will use the .dynsym entry to
858
     determine the address it must put in the global offset table, so
859
     both the dynamic object and the regular object will refer to the
860
     same memory location for the variable.  */
861
 
862
  s = bfd_get_section_by_name (dynobj, ".dynbss");
863
  BFD_ASSERT (s != NULL);
864
 
865
  /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
866
     to copy the initial value out of the dynamic object and into the
867
     runtime process image.  We need to remember the offset into the
868
     .rel.bss section we are going to use.  */
869
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
870
    {
871
      asection *srel;
872
 
873
      srel = bfd_get_section_by_name (dynobj, ".rela.bss");
874
      BFD_ASSERT (srel != NULL);
875
      srel->_raw_size += sizeof (Elf32_External_Rela);
876
      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
877
    }
878
 
879
  /* We need to figure out the alignment required for this symbol.  I
880
     have no idea how ELF linkers handle this.  */
881
  power_of_two = bfd_log2 (h->size);
882
  if (power_of_two > 3)
883
    power_of_two = 3;
884
 
885
  /* Apply the required alignment.  */
886
  s->_raw_size = BFD_ALIGN (s->_raw_size,
887
                            (bfd_size_type) (1 << power_of_two));
888
  if (power_of_two > bfd_get_section_alignment (dynobj, s))
889
    {
890
      if (! bfd_set_section_alignment (dynobj, s, power_of_two))
891
        return false;
892
    }
893
 
894
  /* Define the symbol as being at this point in the section.  */
895
  h->root.u.def.section = s;
896
  h->root.u.def.value = s->_raw_size;
897
 
898
  /* Increment the section size to make room for the symbol.  */
899
  s->_raw_size += h->size;
900
 
901
  return true;
902
}
903
 
904
/* Set the sizes of the dynamic sections.  */
905
 
906
static boolean
907
elf32_sparc_size_dynamic_sections (output_bfd, info)
908
     bfd *output_bfd;
909
     struct bfd_link_info *info;
910
{
911
  bfd *dynobj;
912
  asection *s;
913
  boolean reltext;
914
  boolean relplt;
915
 
916
  dynobj = elf_hash_table (info)->dynobj;
917
  BFD_ASSERT (dynobj != NULL);
918
 
919
  if (elf_hash_table (info)->dynamic_sections_created)
920
    {
921
      /* Set the contents of the .interp section to the interpreter.  */
922
      if (! info->shared)
923
        {
924
          s = bfd_get_section_by_name (dynobj, ".interp");
925
          BFD_ASSERT (s != NULL);
926
          s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
927
          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
928
        }
929
 
930
      /* Make space for the trailing nop in .plt.  */
931
      s = bfd_get_section_by_name (dynobj, ".plt");
932
      BFD_ASSERT (s != NULL);
933
      if (s->_raw_size > 0)
934
        s->_raw_size += 4;
935
    }
936
  else
937
    {
938
      /* We may have created entries in the .rela.got section.
939
         However, if we are not creating the dynamic sections, we will
940
         not actually use these entries.  Reset the size of .rela.got,
941
         which will cause it to get stripped from the output file
942
         below.  */
943
      s = bfd_get_section_by_name (dynobj, ".rela.got");
944
      if (s != NULL)
945
        s->_raw_size = 0;
946
    }
947
 
948
  /* The check_relocs and adjust_dynamic_symbol entry points have
949
     determined the sizes of the various dynamic sections.  Allocate
950
     memory for them.  */
951
  reltext = false;
952
  relplt = false;
953
  for (s = dynobj->sections; s != NULL; s = s->next)
954
    {
955
      const char *name;
956
      boolean strip;
957
 
958
      if ((s->flags & SEC_LINKER_CREATED) == 0)
959
        continue;
960
 
961
      /* It's OK to base decisions on the section name, because none
962
         of the dynobj section names depend upon the input files.  */
963
      name = bfd_get_section_name (dynobj, s);
964
 
965
      strip = false;
966
 
967
      if (strncmp (name, ".rela", 5) == 0)
968
        {
969
          if (s->_raw_size == 0)
970
            {
971
              /* If we don't need this section, strip it from the
972
                 output file.  This is to handle .rela.bss and
973
                 .rel.plt.  We must create it in
974
                 create_dynamic_sections, because it must be created
975
                 before the linker maps input sections to output
976
                 sections.  The linker does that before
977
                 adjust_dynamic_symbol is called, and it is that
978
                 function which decides whether anything needs to go
979
                 into these sections.  */
980
              strip = true;
981
            }
982
          else
983
            {
984
              const char *outname;
985
              asection *target;
986
 
987
              /* If this relocation section applies to a read only
988
                 section, then we probably need a DT_TEXTREL entry.  */
989
              outname = bfd_get_section_name (output_bfd,
990
                                              s->output_section);
991
              target = bfd_get_section_by_name (output_bfd, outname + 5);
992
              if (target != NULL
993
                  && (target->flags & SEC_READONLY) != 0
994
                  && (target->flags & SEC_ALLOC) != 0)
995
                reltext = true;
996
 
997
              if (strcmp (name, ".rela.plt") == 0)
998
                relplt = true;
999
 
1000
              /* We use the reloc_count field as a counter if we need
1001
                 to copy relocs into the output file.  */
1002
              s->reloc_count = 0;
1003
            }
1004
        }
1005
      else if (strcmp (name, ".plt") != 0
1006
               && strcmp (name, ".got") != 0)
1007
        {
1008
          /* It's not one of our sections, so don't allocate space.  */
1009
          continue;
1010
        }
1011
 
1012
      if (strip)
1013
        {
1014
          _bfd_strip_section_from_output (info, s);
1015
          continue;
1016
        }
1017
 
1018
      /* Allocate memory for the section contents.  */
1019
      s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1020
      if (s->contents == NULL && s->_raw_size != 0)
1021
        return false;
1022
    }
1023
 
1024
  if (elf_hash_table (info)->dynamic_sections_created)
1025
    {
1026
      /* Add some entries to the .dynamic section.  We fill in the
1027
         values later, in elf32_sparc_finish_dynamic_sections, but we
1028
         must add the entries now so that we get the correct size for
1029
         the .dynamic section.  The DT_DEBUG entry is filled in by the
1030
         dynamic linker and used by the debugger.  */
1031
      if (! info->shared)
1032
        {
1033
          if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1034
            return false;
1035
        }
1036
 
1037
      if (relplt)
1038
        {
1039
          if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1040
              || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1041
              || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1042
              || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1043
            return false;
1044
        }
1045
 
1046
      if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1047
          || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1048
          || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1049
                                            sizeof (Elf32_External_Rela)))
1050
        return false;
1051
 
1052
      if (reltext)
1053
        {
1054
          if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1055
            return false;
1056
        }
1057
    }
1058
 
1059
  return true;
1060
}
1061
 
1062
/* Relocate a SPARC ELF section.  */
1063
 
1064
static boolean
1065
elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1066
                              contents, relocs, local_syms, local_sections)
1067
     bfd *output_bfd;
1068
     struct bfd_link_info *info;
1069
     bfd *input_bfd;
1070
     asection *input_section;
1071
     bfd_byte *contents;
1072
     Elf_Internal_Rela *relocs;
1073
     Elf_Internal_Sym *local_syms;
1074
     asection **local_sections;
1075
{
1076
  bfd *dynobj;
1077
  Elf_Internal_Shdr *symtab_hdr;
1078
  struct elf_link_hash_entry **sym_hashes;
1079
  bfd_vma *local_got_offsets;
1080
  bfd_vma got_base;
1081
  asection *sgot;
1082
  asection *splt;
1083
  asection *sreloc;
1084
  Elf_Internal_Rela *rel;
1085
  Elf_Internal_Rela *relend;
1086
 
1087
  dynobj = elf_hash_table (info)->dynobj;
1088
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1089
  sym_hashes = elf_sym_hashes (input_bfd);
1090
  local_got_offsets = elf_local_got_offsets (input_bfd);
1091
 
1092
  if (elf_hash_table (info)->hgot == NULL)
1093
    got_base = 0;
1094
  else
1095
    got_base = elf_hash_table (info)->hgot->root.u.def.value;
1096
 
1097
  sgot = NULL;
1098
  splt = NULL;
1099
  sreloc = NULL;
1100
 
1101
  rel = relocs;
1102
  relend = relocs + input_section->reloc_count;
1103
  for (; rel < relend; rel++)
1104
    {
1105
      int r_type;
1106
      reloc_howto_type *howto;
1107
      unsigned long r_symndx;
1108
      struct elf_link_hash_entry *h;
1109
      Elf_Internal_Sym *sym;
1110
      asection *sec;
1111
      bfd_vma relocation;
1112
      bfd_reloc_status_type r;
1113
 
1114
      r_type = ELF32_R_TYPE (rel->r_info);
1115
 
1116
      if (r_type == R_SPARC_GNU_VTINHERIT
1117
          || r_type == R_SPARC_GNU_VTENTRY)
1118
        continue;
1119
 
1120
      if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1121
        {
1122
          bfd_set_error (bfd_error_bad_value);
1123
          return false;
1124
        }
1125
      howto = _bfd_sparc_elf_howto_table + r_type;
1126
 
1127
      r_symndx = ELF32_R_SYM (rel->r_info);
1128
 
1129
      if (info->relocateable)
1130
        {
1131
          /* This is a relocateable link.  We don't have to change
1132
             anything, unless the reloc is against a section symbol,
1133
             in which case we have to adjust according to where the
1134
             section symbol winds up in the output section.  */
1135
          if (r_symndx < symtab_hdr->sh_info)
1136
            {
1137
              sym = local_syms + r_symndx;
1138
              if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1139
                {
1140
                  sec = local_sections[r_symndx];
1141
                  rel->r_addend += sec->output_offset + sym->st_value;
1142
                }
1143
            }
1144
 
1145
          continue;
1146
        }
1147
 
1148
      /* This is a final link.  */
1149
      h = NULL;
1150
      sym = NULL;
1151
      sec = NULL;
1152
      if (r_symndx < symtab_hdr->sh_info)
1153
        {
1154
          sym = local_syms + r_symndx;
1155
          sec = local_sections[r_symndx];
1156
          relocation = (sec->output_section->vma
1157
                        + sec->output_offset
1158
                        + sym->st_value);
1159
        }
1160
      else
1161
        {
1162
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1163
          while (h->root.type == bfd_link_hash_indirect
1164
                 || h->root.type == bfd_link_hash_warning)
1165
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
1166
          if (h->root.type == bfd_link_hash_defined
1167
              || h->root.type == bfd_link_hash_defweak)
1168
            {
1169
              sec = h->root.u.def.section;
1170
              if ((r_type == R_SPARC_WPLT30
1171
                   && h->plt.offset != (bfd_vma) -1)
1172
                  || ((r_type == R_SPARC_GOT10
1173
                       || r_type == R_SPARC_GOT13
1174
                       || r_type == R_SPARC_GOT22)
1175
                      && elf_hash_table (info)->dynamic_sections_created
1176
                      && (! info->shared
1177
                          || (! info->symbolic && h->dynindx != -1)
1178
                          || (h->elf_link_hash_flags
1179
                              & ELF_LINK_HASH_DEF_REGULAR) == 0))
1180
                  || (info->shared
1181
                      && ((! info->symbolic && h->dynindx != -1)
1182
                          || (h->elf_link_hash_flags
1183
                              & ELF_LINK_HASH_DEF_REGULAR) == 0)
1184
                      && (r_type == R_SPARC_8
1185
                          || r_type == R_SPARC_16
1186
                          || r_type == R_SPARC_32
1187
                          || r_type == R_SPARC_DISP8
1188
                          || r_type == R_SPARC_DISP16
1189
                          || r_type == R_SPARC_DISP32
1190
                          || r_type == R_SPARC_WDISP30
1191
                          || r_type == R_SPARC_WDISP22
1192
                          || r_type == R_SPARC_WDISP19
1193
                          || r_type == R_SPARC_WDISP16
1194
                          || r_type == R_SPARC_HI22
1195
                          || r_type == R_SPARC_22
1196
                          || r_type == R_SPARC_13
1197
                          || r_type == R_SPARC_LO10
1198
                          || r_type == R_SPARC_UA32
1199
                          || ((r_type == R_SPARC_PC10
1200
                               || r_type == R_SPARC_PC22)
1201
                              && strcmp (h->root.root.string,
1202
                                         "_GLOBAL_OFFSET_TABLE_") != 0))))
1203
                {
1204
                  /* In these cases, we don't need the relocation
1205
                     value.  We check specially because in some
1206
                     obscure cases sec->output_section will be NULL.  */
1207
                  relocation = 0;
1208
                }
1209
              else
1210
                relocation = (h->root.u.def.value
1211
                              + sec->output_section->vma
1212
                              + sec->output_offset);
1213
            }
1214
          else if (h->root.type == bfd_link_hash_undefweak)
1215
            relocation = 0;
1216
          else if (info->shared && !info->symbolic && !info->no_undefined)
1217
            relocation = 0;
1218
          else
1219
            {
1220
              if (! ((*info->callbacks->undefined_symbol)
1221
                     (info, h->root.root.string, input_bfd,
1222
                      input_section, rel->r_offset,
1223
                      (!info->shared || info->no_undefined))))
1224
                return false;
1225
              relocation = 0;
1226
            }
1227
        }
1228
 
1229
      switch (r_type)
1230
        {
1231
        case R_SPARC_GOT10:
1232
        case R_SPARC_GOT13:
1233
        case R_SPARC_GOT22:
1234
          /* Relocation is to the entry for this symbol in the global
1235
             offset table.  */
1236
          if (sgot == NULL)
1237
            {
1238
              sgot = bfd_get_section_by_name (dynobj, ".got");
1239
              BFD_ASSERT (sgot != NULL);
1240
            }
1241
 
1242
          if (h != NULL)
1243
            {
1244
              bfd_vma off;
1245
 
1246
              off = h->got.offset;
1247
              BFD_ASSERT (off != (bfd_vma) -1);
1248
 
1249
              if (! elf_hash_table (info)->dynamic_sections_created
1250
                  || (info->shared
1251
                      && (info->symbolic || h->dynindx == -1)
1252
                      && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1253
                {
1254
                  /* This is actually a static link, or it is a
1255
                     -Bsymbolic link and the symbol is defined
1256
                     locally, or the symbol was forced to be local
1257
                     because of a version file.  We must initialize
1258
                     this entry in the global offset table.  Since the
1259
                     offset must always be a multiple of 4, we use the
1260
                     least significant bit to record whether we have
1261
                     initialized it already.
1262
 
1263
                     When doing a dynamic link, we create a .rela.got
1264
                     relocation entry to initialize the value.  This
1265
                     is done in the finish_dynamic_symbol routine.  */
1266
                  if ((off & 1) != 0)
1267
                    off &= ~1;
1268
                  else
1269
                    {
1270
                      bfd_put_32 (output_bfd, relocation,
1271
                                  sgot->contents + off);
1272
                      h->got.offset |= 1;
1273
                    }
1274
                }
1275
 
1276
              relocation = sgot->output_offset + off - got_base;
1277
            }
1278
          else
1279
            {
1280
              bfd_vma off;
1281
 
1282
              BFD_ASSERT (local_got_offsets != NULL
1283
                          && local_got_offsets[r_symndx] != (bfd_vma) -1);
1284
 
1285
              off = local_got_offsets[r_symndx];
1286
 
1287
              /* The offset must always be a multiple of 4.  We use
1288
                 the least significant bit to record whether we have
1289
                 already processed this entry.  */
1290
              if ((off & 1) != 0)
1291
                off &= ~1;
1292
              else
1293
                {
1294
                  bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1295
 
1296
                  if (info->shared)
1297
                    {
1298
                      asection *srelgot;
1299
                      Elf_Internal_Rela outrel;
1300
 
1301
                      /* We need to generate a R_SPARC_RELATIVE reloc
1302
                         for the dynamic linker.  */
1303
                      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1304
                      BFD_ASSERT (srelgot != NULL);
1305
 
1306
                      outrel.r_offset = (sgot->output_section->vma
1307
                                         + sgot->output_offset
1308
                                         + off);
1309
                      outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1310
                      outrel.r_addend = 0;
1311
                      bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1312
                                                 (((Elf32_External_Rela *)
1313
                                                   srelgot->contents)
1314
                                                  + srelgot->reloc_count));
1315
                      ++srelgot->reloc_count;
1316
                    }
1317
 
1318
                  local_got_offsets[r_symndx] |= 1;
1319
                }
1320
 
1321
              relocation = sgot->output_offset + off - got_base;
1322
            }
1323
 
1324
          break;
1325
 
1326
        case R_SPARC_WPLT30:
1327
          /* Relocation is to the entry for this symbol in the
1328
             procedure linkage table.  */
1329
 
1330
          /* The Solaris native assembler will generate a WPLT30 reloc
1331
             for a local symbol if you assemble a call from one
1332
             section to another when using -K pic.  We treat it as
1333
             WDISP30.  */
1334
          if (h == NULL)
1335
            break;
1336
 
1337
          if (h->plt.offset == (bfd_vma) -1)
1338
            {
1339
              /* We didn't make a PLT entry for this symbol.  This
1340
                 happens when statically linking PIC code, or when
1341
                 using -Bsymbolic.  */
1342
              break;
1343
            }
1344
 
1345
          if (splt == NULL)
1346
            {
1347
              splt = bfd_get_section_by_name (dynobj, ".plt");
1348
              BFD_ASSERT (splt != NULL);
1349
            }
1350
 
1351
          relocation = (splt->output_section->vma
1352
                        + splt->output_offset
1353
                        + h->plt.offset);
1354
          break;
1355
 
1356
        case R_SPARC_PC10:
1357
        case R_SPARC_PC22:
1358
          if (h != NULL
1359
              && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1360
            break;
1361
          /* Fall through.  */
1362
        case R_SPARC_DISP8:
1363
        case R_SPARC_DISP16:
1364
        case R_SPARC_DISP32:
1365
        case R_SPARC_WDISP30:
1366
        case R_SPARC_WDISP22:
1367
        case R_SPARC_WDISP19:
1368
        case R_SPARC_WDISP16:
1369
          if (h == NULL
1370
              || (info->symbolic
1371
                  && (h->elf_link_hash_flags
1372
                      & ELF_LINK_HASH_DEF_REGULAR) != 0))
1373
            break;
1374
          /* Fall through.  */
1375
        case R_SPARC_8:
1376
        case R_SPARC_16:
1377
        case R_SPARC_32:
1378
        case R_SPARC_HI22:
1379
        case R_SPARC_22:
1380
        case R_SPARC_13:
1381
        case R_SPARC_LO10:
1382
        case R_SPARC_UA32:
1383
          if (info->shared)
1384
            {
1385
              Elf_Internal_Rela outrel;
1386
              boolean skip;
1387
 
1388
              /* When generating a shared object, these relocations
1389
                 are copied into the output file to be resolved at run
1390
                 time.  */
1391
 
1392
              if (sreloc == NULL)
1393
                {
1394
                  const char *name;
1395
 
1396
                  name = (bfd_elf_string_from_elf_section
1397
                          (input_bfd,
1398
                           elf_elfheader (input_bfd)->e_shstrndx,
1399
                           elf_section_data (input_section)->rel_hdr.sh_name));
1400
                  if (name == NULL)
1401
                    return false;
1402
 
1403
                  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1404
                              && strcmp (bfd_get_section_name (input_bfd,
1405
                                                               input_section),
1406
                                         name + 5) == 0);
1407
 
1408
                  sreloc = bfd_get_section_by_name (dynobj, name);
1409
                  BFD_ASSERT (sreloc != NULL);
1410
                }
1411
 
1412
              skip = false;
1413
 
1414
              if (elf_section_data (input_section)->stab_info == NULL)
1415
                outrel.r_offset = rel->r_offset;
1416
              else
1417
                {
1418
                  bfd_vma off;
1419
 
1420
                  off = (_bfd_stab_section_offset
1421
                         (output_bfd, &elf_hash_table (info)->stab_info,
1422
                          input_section,
1423
                          &elf_section_data (input_section)->stab_info,
1424
                          rel->r_offset));
1425
                  if (off == (bfd_vma) -1)
1426
                    skip = true;
1427
                  outrel.r_offset = off;
1428
                }
1429
 
1430
              outrel.r_offset += (input_section->output_section->vma
1431
                                  + input_section->output_offset);
1432
 
1433
              if (skip)
1434
                memset (&outrel, 0, sizeof outrel);
1435
              /* h->dynindx may be -1 if the symbol was marked to
1436
                 become local.  */
1437
              else if (h != NULL
1438
                       && ((! info->symbolic && h->dynindx != -1)
1439
                           || (h->elf_link_hash_flags
1440
                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
1441
                {
1442
                  BFD_ASSERT (h->dynindx != -1);
1443
                  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1444
                  outrel.r_addend = rel->r_addend;
1445
                }
1446
              else
1447
                {
1448
                  if (r_type == R_SPARC_32)
1449
                    {
1450
                      outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1451
                      outrel.r_addend = relocation + rel->r_addend;
1452
                    }
1453
                  else
1454
                    {
1455
                      long indx;
1456
 
1457
                      if (h == NULL)
1458
                        sec = local_sections[r_symndx];
1459
                      else
1460
                        {
1461
                          BFD_ASSERT (h->root.type == bfd_link_hash_defined
1462
                                      || (h->root.type
1463
                                          == bfd_link_hash_defweak));
1464
                          sec = h->root.u.def.section;
1465
                        }
1466
                      if (sec != NULL && bfd_is_abs_section (sec))
1467
                        indx = 0;
1468
                      else if (sec == NULL || sec->owner == NULL)
1469
                        {
1470
                          bfd_set_error (bfd_error_bad_value);
1471
                          return false;
1472
                        }
1473
                      else
1474
                        {
1475
                          asection *osec;
1476
 
1477
                          osec = sec->output_section;
1478
                          indx = elf_section_data (osec)->dynindx;
1479
 
1480
                          /* FIXME: we really should be able to link non-pic
1481
                             shared libraries.  */
1482
                          if (indx == 0)
1483
                            {
1484
                              BFD_FAIL ();
1485
                              (*_bfd_error_handler)
1486
                                (_("%s: probably compiled without -fPIC?"),
1487
                                 bfd_get_filename (input_bfd));
1488
                              bfd_set_error (bfd_error_bad_value);
1489
                              return false;
1490
                            }
1491
                        }
1492
 
1493
                      outrel.r_info = ELF32_R_INFO (indx, r_type);
1494
                      outrel.r_addend = relocation + rel->r_addend;
1495
                    }
1496
                }
1497
 
1498
              bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1499
                                         (((Elf32_External_Rela *)
1500
                                           sreloc->contents)
1501
                                          + sreloc->reloc_count));
1502
              ++sreloc->reloc_count;
1503
 
1504
              /* This reloc will be computed at runtime, so there's no
1505
                 need to do anything now, unless this is a RELATIVE
1506
                 reloc in an unallocated section.  */
1507
              if (skip
1508
                  || (input_section->flags & SEC_ALLOC) != 0
1509
                  || ELF32_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1510
                continue;
1511
            }
1512
          break;
1513
 
1514
        default:
1515
          break;
1516
        }
1517
 
1518
      if (r_type == R_SPARC_WDISP16)
1519
        {
1520
          bfd_vma x;
1521
 
1522
          relocation += rel->r_addend;
1523
          relocation -= (input_section->output_section->vma
1524
                         + input_section->output_offset);
1525
          relocation -= rel->r_offset;
1526
 
1527
          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1528
          x |= ((((relocation >> 2) & 0xc000) << 6)
1529
                | ((relocation >> 2) & 0x3fff));
1530
          bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1531
 
1532
          if ((bfd_signed_vma) relocation < - 0x40000
1533
              || (bfd_signed_vma) relocation > 0x3ffff)
1534
            r = bfd_reloc_overflow;
1535
          else
1536
            r = bfd_reloc_ok;
1537
        }
1538
      else if (r_type == R_SPARC_REV32)
1539
        {
1540
          bfd_vma x;
1541
 
1542
          relocation = relocation + rel->r_addend;
1543
 
1544
          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1545
          x = x + relocation;
1546
          bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1547
          r = bfd_reloc_ok;
1548
        }
1549
      else
1550
        r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1551
                                      contents, rel->r_offset,
1552
                                      relocation, rel->r_addend);
1553
 
1554
 
1555
      if (r != bfd_reloc_ok)
1556
        {
1557
          switch (r)
1558
            {
1559
            default:
1560
            case bfd_reloc_outofrange:
1561
              abort ();
1562
            case bfd_reloc_overflow:
1563
              {
1564
                const char *name;
1565
 
1566
                if (h != NULL)
1567
                  name = h->root.root.string;
1568
                else
1569
                  {
1570
                    name = bfd_elf_string_from_elf_section (input_bfd,
1571
                                                            symtab_hdr->sh_link,
1572
                                                            sym->st_name);
1573
                    if (name == NULL)
1574
                      return false;
1575
                    if (*name == '\0')
1576
                      name = bfd_section_name (input_bfd, sec);
1577
                  }
1578
                if (! ((*info->callbacks->reloc_overflow)
1579
                       (info, name, howto->name, (bfd_vma) 0,
1580
                        input_bfd, input_section, rel->r_offset)))
1581
                  return false;
1582
              }
1583
              break;
1584
            }
1585
        }
1586
    }
1587
 
1588
  return true;
1589
}
1590
 
1591
/* Finish up dynamic symbol handling.  We set the contents of various
1592
   dynamic sections here.  */
1593
 
1594
static boolean
1595
elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1596
     bfd *output_bfd;
1597
     struct bfd_link_info *info;
1598
     struct elf_link_hash_entry *h;
1599
     Elf_Internal_Sym *sym;
1600
{
1601
  bfd *dynobj;
1602
 
1603
  dynobj = elf_hash_table (info)->dynobj;
1604
 
1605
  if (h->plt.offset != (bfd_vma) -1)
1606
    {
1607
      asection *splt;
1608
      asection *srela;
1609
      Elf_Internal_Rela rela;
1610
 
1611
      /* This symbol has an entry in the procedure linkage table.  Set
1612
         it up.  */
1613
 
1614
      BFD_ASSERT (h->dynindx != -1);
1615
 
1616
      splt = bfd_get_section_by_name (dynobj, ".plt");
1617
      srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1618
      BFD_ASSERT (splt != NULL && srela != NULL);
1619
 
1620
      /* Fill in the entry in the procedure linkage table.  */
1621
      bfd_put_32 (output_bfd,
1622
                  PLT_ENTRY_WORD0 + h->plt.offset,
1623
                  splt->contents + h->plt.offset);
1624
      bfd_put_32 (output_bfd,
1625
                  (PLT_ENTRY_WORD1
1626
                   + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1627
                  splt->contents + h->plt.offset + 4);
1628
      bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1629
                  splt->contents + h->plt.offset + 8);
1630
 
1631
      /* Fill in the entry in the .rela.plt section.  */
1632
      rela.r_offset = (splt->output_section->vma
1633
                       + splt->output_offset
1634
                       + h->plt.offset);
1635
      rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1636
      rela.r_addend = 0;
1637
      bfd_elf32_swap_reloca_out (output_bfd, &rela,
1638
                                 ((Elf32_External_Rela *) srela->contents
1639
                                  + h->plt.offset / PLT_ENTRY_SIZE - 4));
1640
 
1641
      if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1642
        {
1643
          /* Mark the symbol as undefined, rather than as defined in
1644
             the .plt section.  Leave the value alone.  */
1645
          sym->st_shndx = SHN_UNDEF;
1646
        }
1647
    }
1648
 
1649
  if (h->got.offset != (bfd_vma) -1)
1650
    {
1651
      asection *sgot;
1652
      asection *srela;
1653
      Elf_Internal_Rela rela;
1654
 
1655
      /* This symbol has an entry in the global offset table.  Set it
1656
         up.  */
1657
 
1658
      sgot = bfd_get_section_by_name (dynobj, ".got");
1659
      srela = bfd_get_section_by_name (dynobj, ".rela.got");
1660
      BFD_ASSERT (sgot != NULL && srela != NULL);
1661
 
1662
      rela.r_offset = (sgot->output_section->vma
1663
                       + sgot->output_offset
1664
                       + (h->got.offset &~ 1));
1665
 
1666
      /* If this is a -Bsymbolic link, and the symbol is defined
1667
         locally, we just want to emit a RELATIVE reloc.  Likewise if
1668
         the symbol was forced to be local because of a version file.
1669
         The entry in the global offset table will already have been
1670
         initialized in the relocate_section function.  */
1671
      if (info->shared
1672
          && (info->symbolic || h->dynindx == -1)
1673
          && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1674
        rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1675
      else
1676
        {
1677
          bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1678
          rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1679
        }
1680
 
1681
      rela.r_addend = 0;
1682
      bfd_elf32_swap_reloca_out (output_bfd, &rela,
1683
                                 ((Elf32_External_Rela *) srela->contents
1684
                                  + srela->reloc_count));
1685
      ++srela->reloc_count;
1686
    }
1687
 
1688
  if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1689
    {
1690
      asection *s;
1691
      Elf_Internal_Rela rela;
1692
 
1693
      /* This symbols needs a copy reloc.  Set it up.  */
1694
 
1695
      BFD_ASSERT (h->dynindx != -1);
1696
 
1697
      s = bfd_get_section_by_name (h->root.u.def.section->owner,
1698
                                   ".rela.bss");
1699
      BFD_ASSERT (s != NULL);
1700
 
1701
      rela.r_offset = (h->root.u.def.value
1702
                       + h->root.u.def.section->output_section->vma
1703
                       + h->root.u.def.section->output_offset);
1704
      rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1705
      rela.r_addend = 0;
1706
      bfd_elf32_swap_reloca_out (output_bfd, &rela,
1707
                                 ((Elf32_External_Rela *) s->contents
1708
                                  + s->reloc_count));
1709
      ++s->reloc_count;
1710
    }
1711
 
1712
  /* Mark some specially defined symbols as absolute.  */
1713
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1714
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1715
      || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1716
    sym->st_shndx = SHN_ABS;
1717
 
1718
  return true;
1719
}
1720
 
1721
/* Finish up the dynamic sections.  */
1722
 
1723
static boolean
1724
elf32_sparc_finish_dynamic_sections (output_bfd, info)
1725
     bfd *output_bfd;
1726
     struct bfd_link_info *info;
1727
{
1728
  bfd *dynobj;
1729
  asection *sdyn;
1730
  asection *sgot;
1731
 
1732
  dynobj = elf_hash_table (info)->dynobj;
1733
 
1734
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1735
 
1736
  if (elf_hash_table (info)->dynamic_sections_created)
1737
    {
1738
      asection *splt;
1739
      Elf32_External_Dyn *dyncon, *dynconend;
1740
 
1741
      splt = bfd_get_section_by_name (dynobj, ".plt");
1742
      BFD_ASSERT (splt != NULL && sdyn != NULL);
1743
 
1744
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
1745
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1746
      for (; dyncon < dynconend; dyncon++)
1747
        {
1748
          Elf_Internal_Dyn dyn;
1749
          const char *name;
1750
          boolean size;
1751
 
1752
          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1753
 
1754
          switch (dyn.d_tag)
1755
            {
1756
            case DT_PLTGOT:   name = ".plt"; size = false; break;
1757
            case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1758
            case DT_JMPREL:   name = ".rela.plt"; size = false; break;
1759
            default:      name = NULL; size = false; break;
1760
            }
1761
 
1762
          if (name != NULL)
1763
            {
1764
              asection *s;
1765
 
1766
              s = bfd_get_section_by_name (output_bfd, name);
1767
              if (s == NULL)
1768
                dyn.d_un.d_val = 0;
1769
              else
1770
                {
1771
                  if (! size)
1772
                    dyn.d_un.d_ptr = s->vma;
1773
                  else
1774
                    {
1775
                      if (s->_cooked_size != 0)
1776
                        dyn.d_un.d_val = s->_cooked_size;
1777
                      else
1778
                        dyn.d_un.d_val = s->_raw_size;
1779
                    }
1780
                }
1781
              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1782
            }
1783
        }
1784
 
1785
      /* Clear the first four entries in the procedure linkage table,
1786
         and put a nop in the last four bytes.  */
1787
      if (splt->_raw_size > 0)
1788
        {
1789
          memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1790
          bfd_put_32 (output_bfd, SPARC_NOP,
1791
                      splt->contents + splt->_raw_size - 4);
1792
        }
1793
 
1794
      elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1795
        PLT_ENTRY_SIZE;
1796
    }
1797
 
1798
  /* Set the first entry in the global offset table to the address of
1799
     the dynamic section.  */
1800
  sgot = bfd_get_section_by_name (dynobj, ".got");
1801
  BFD_ASSERT (sgot != NULL);
1802
  if (sgot->_raw_size > 0)
1803
    {
1804
      if (sdyn == NULL)
1805
        bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1806
      else
1807
        bfd_put_32 (output_bfd,
1808
                    sdyn->output_section->vma + sdyn->output_offset,
1809
                    sgot->contents);
1810
    }
1811
 
1812
  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1813
 
1814
  return true;
1815
}
1816
 
1817
/* Functions for dealing with the e_flags field.
1818
 
1819
   We don't define set_private_flags or copy_private_bfd_data because
1820
   the only currently defined values are based on the bfd mach number,
1821
   so we use the latter instead and defer setting e_flags until the
1822
   file is written out.  */
1823
 
1824
/* Merge backend specific data from an object file to the output
1825
   object file when linking.  */
1826
 
1827
static boolean
1828
elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1829
     bfd *ibfd;
1830
     bfd *obfd;
1831
{
1832
  boolean error;
1833
  /* FIXME: This should not be static.  */
1834
  static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1835
 
1836
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1837
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1838
    return true;
1839
 
1840
  error = false;
1841
 
1842
#if 0
1843
  /* ??? The native linker doesn't do this so we can't (otherwise gcc would
1844
     have to know which linker is being used).  Instead, the native linker
1845
     bumps up the architecture level when it has to.  However, I still think
1846
     warnings like these are good, so it would be nice to have them turned on
1847
     by some option.  */
1848
 
1849
  /* If the output machine is normal sparc, we can't allow v9 input files.  */
1850
  if (bfd_get_mach (obfd) == bfd_mach_sparc
1851
      && (bfd_get_mach (ibfd) == bfd_mach_sparc_v8plus
1852
          || bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa))
1853
    {
1854
      error = true;
1855
      (*_bfd_error_handler)
1856
        (_("%s: compiled for a v8plus system and target is v8"),
1857
         bfd_get_filename (ibfd));
1858
    }
1859
  /* If the output machine is v9, we can't allow v9+vis input files.  */
1860
  if (bfd_get_mach (obfd) == bfd_mach_sparc_v8plus
1861
      && bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa)
1862
    {
1863
      error = true;
1864
      (*_bfd_error_handler)
1865
        (_("%s: compiled for a v8plusa system and target is v8plus"),
1866
         bfd_get_filename (ibfd));
1867
    }
1868
#else
1869
  if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1870
    {
1871
      error = true;
1872
      (*_bfd_error_handler)
1873
        (_("%s: compiled for a 64 bit system and target is 32 bit"),
1874
         bfd_get_filename (ibfd));
1875
    }
1876
  else if ((ibfd->flags & DYNAMIC) == 0)
1877
    {
1878
      if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1879
        bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
1880
    }
1881
#endif
1882
 
1883
  if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
1884
       != previous_ibfd_e_flags)
1885
      && previous_ibfd_e_flags != (unsigned long) -1)
1886
    {
1887
      (*_bfd_error_handler)
1888
        (_("%s: linking little endian files with big endian files"),
1889
         bfd_get_filename (ibfd));
1890
      error = true;
1891
    }
1892
  previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
1893
 
1894
  if (error)
1895
    {
1896
      bfd_set_error (bfd_error_bad_value);
1897
      return false;
1898
    }
1899
 
1900
  return true;
1901
}
1902
 
1903
/* Set the right machine number.  */
1904
 
1905
static boolean
1906
elf32_sparc_object_p (abfd)
1907
     bfd *abfd;
1908
{
1909
  if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
1910
    {
1911
      if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
1912
        return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1913
                                          bfd_mach_sparc_v8plusa);
1914
      else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
1915
        return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1916
                                          bfd_mach_sparc_v8plus);
1917
      else
1918
        return false;
1919
    }
1920
  else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
1921
    return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
1922
                                      bfd_mach_sparc_sparclite_le);
1923
  else
1924
    return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
1925
}
1926
 
1927
/* The final processing done just before writing out the object file.
1928
   We need to set the e_machine field appropriately.  */
1929
 
1930
static void
1931
elf32_sparc_final_write_processing (abfd, linker)
1932
     bfd *abfd;
1933
     boolean linker ATTRIBUTE_UNUSED;
1934
{
1935
  switch (bfd_get_mach (abfd))
1936
    {
1937
    case bfd_mach_sparc :
1938
      break; /* nothing to do */
1939
    case bfd_mach_sparc_v8plus :
1940
      elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
1941
      elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
1942
      elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
1943
      break;
1944
    case bfd_mach_sparc_v8plusa :
1945
      elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
1946
      elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
1947
      elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
1948
      break;
1949
    case bfd_mach_sparc_sparclite_le :
1950
      elf_elfheader (abfd)->e_machine = EM_SPARC;
1951
      elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
1952
      break;
1953
    default :
1954
      abort ();
1955
      break;
1956
    }
1957
}
1958
 
1959
#define TARGET_BIG_SYM  bfd_elf32_sparc_vec
1960
#define TARGET_BIG_NAME "elf32-sparc"
1961
#define ELF_ARCH        bfd_arch_sparc
1962
#define ELF_MACHINE_CODE EM_SPARC
1963
#define ELF_MACHINE_ALT1 EM_SPARC32PLUS
1964
#define ELF_MAXPAGESIZE 0x10000
1965
 
1966
#define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
1967
#define elf_info_to_howto               elf32_sparc_info_to_howto
1968
#define elf_backend_create_dynamic_sections \
1969
                                        _bfd_elf_create_dynamic_sections
1970
#define elf_backend_check_relocs        elf32_sparc_check_relocs
1971
#define elf_backend_adjust_dynamic_symbol \
1972
                                        elf32_sparc_adjust_dynamic_symbol
1973
#define elf_backend_size_dynamic_sections \
1974
                                        elf32_sparc_size_dynamic_sections
1975
#define elf_backend_relocate_section    elf32_sparc_relocate_section
1976
#define elf_backend_finish_dynamic_symbol \
1977
                                        elf32_sparc_finish_dynamic_symbol
1978
#define elf_backend_finish_dynamic_sections \
1979
                                        elf32_sparc_finish_dynamic_sections
1980
#define bfd_elf32_bfd_merge_private_bfd_data \
1981
                                        elf32_sparc_merge_private_bfd_data
1982
#define elf_backend_object_p            elf32_sparc_object_p
1983
#define elf_backend_final_write_processing \
1984
                                        elf32_sparc_final_write_processing
1985
#define elf_backend_gc_mark_hook        elf32_sparc_gc_mark_hook
1986
#define elf_backend_gc_sweep_hook       elf32_sparc_gc_sweep_hook
1987
 
1988
#define elf_backend_can_gc_sections 1
1989
#define elf_backend_want_got_plt 0
1990
#define elf_backend_plt_readonly 0
1991
#define elf_backend_want_plt_sym 1
1992
#define elf_backend_got_header_size 4
1993
#define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
1994
 
1995
#include "elf32-target.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.