1 /* ARC-specific support for 32-bit ELF
2 Copyright (C) 1994-2018 Free Software Foundation, Inc.
3 Contributed by Cupertino Miranda (cmiranda@synopsys.com).
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
46 struct got_entry *next;
49 bfd_boolean processed;
50 bfd_boolean created_dyn_relocation;
51 enum tls_got_entries existing_entries;
54 static struct got_entry **
55 arc_get_local_got_ents (bfd * abfd)
57 static struct got_entry **local_got_ents = NULL;
59 if (local_got_ents == NULL)
62 Elf_Internal_Shdr *symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
64 size = symtab_hdr->sh_info * sizeof (bfd_vma);
65 local_got_ents = (struct got_entry **)
66 bfd_alloc (abfd, sizeof (struct got_entry *) * size);
67 if (local_got_ents == NULL)
70 memset (local_got_ents, 0, sizeof (struct got_entry *) * size);
71 elf_local_got_ents (abfd) = local_got_ents;
74 return local_got_ents;
77 static struct got_entry *
78 got_entry_for_type (struct got_entry **list,
81 struct got_entry **p = list;
85 if ((*p)->type == type)
93 new_got_entry_to_list (struct got_entry **list,
96 enum tls_got_entries existing_entries)
98 /* Find list end. Avoid having multiple entries of the same
100 struct got_entry **p = list;
101 struct got_entry *entry;
105 if ((*p)->type == type)
110 entry = (struct got_entry *) xmalloc (sizeof (struct got_entry));
113 entry->offset = offset;
115 entry->processed = FALSE;
116 entry->created_dyn_relocation = FALSE;
117 entry->existing_entries = existing_entries;
119 ARC_DEBUG ("New GOT got entry added to list: "
120 "type: %d, offset: %ld, existing_entries: %d\n",
121 type, (long) offset, existing_entries);
123 /* Add the entry to the end of the list. */
127 static enum tls_type_e
128 tls_type_for_reloc (reloc_howto_type *howto)
130 enum tls_type_e ret = GOT_UNKNOWN;
132 if (is_reloc_for_GOT (howto))
137 case R_ARC_TLS_GD_GOT:
140 case R_ARC_TLS_IE_GOT:
143 case R_ARC_TLS_LE_32:
154 static struct got_entry **
155 get_got_entry_list_for_symbol (bfd *abfd,
156 unsigned long r_symndx,
157 struct elf_link_hash_entry *h)
159 struct elf_arc_link_hash_entry *h1 =
160 ((struct elf_arc_link_hash_entry *) h);
163 return &h1->got_ents;
167 struct got_entry **local_got_ents
168 = arc_get_local_got_ents (abfd);
169 return &local_got_ents[r_symndx];
174 static enum tls_type_e
175 arc_got_entry_type_for_reloc (reloc_howto_type *howto)
177 enum tls_type_e type = GOT_UNKNOWN;
179 if (is_reloc_for_GOT (howto))
182 if (is_reloc_for_TLS (howto))
186 case R_ARC_TLS_GD_GOT:
189 case R_ARC_TLS_IE_GOT:
199 #define ADD_SYMBOL_REF_SEC_AND_RELOC(SECNAME, COND_FOR_RELOC, H) \
200 htab->s##SECNAME->size; \
202 if (COND_FOR_RELOC) \
204 htab->srel##SECNAME->size += sizeof (Elf32_External_Rela); \
205 ARC_DEBUG ("arc_info: Added reloc space in " \
206 #SECNAME " section at " __FILE__ \
207 ":%d for symbol %s\n", \
208 __LINE__, name_for_global_symbol (H)); \
211 if (h->dynindx == -1 && !h->forced_local) \
212 if (! bfd_elf_link_record_dynamic_symbol (info, H)) \
214 htab->s##SECNAME->size += 4; \
218 arc_fill_got_info_for_reloc (enum tls_type_e type,
219 struct got_entry **list,
220 struct bfd_link_info * info,
221 struct elf_link_hash_entry *h)
223 struct elf_link_hash_table *htab = elf_hash_table (info);
225 if (got_entry_for_type (list, type) != NULL)
233 = ADD_SYMBOL_REF_SEC_AND_RELOC (got, bfd_link_pic (info)
235 new_got_entry_to_list (list, type, offset, TLS_GOT_NONE);
243 = ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
244 bfd_vma ATTRIBUTE_UNUSED notneeded
245 = ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
246 new_got_entry_to_list (list, type, offset, TLS_GOT_MOD_AND_OFF);
253 = ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
254 new_got_entry_to_list (list, type, offset, TLS_GOT_OFF);
267 relocate_fix_got_relocs_for_got_info (struct got_entry ** list_p,
268 enum tls_type_e type,
269 struct bfd_link_info * info,
271 unsigned long r_symndx,
272 Elf_Internal_Sym * local_syms,
273 asection ** local_sections,
274 struct elf_link_hash_entry * h,
275 struct arc_relocation_data * reloc_data)
277 struct elf_link_hash_table *htab = elf_hash_table (info);
278 struct got_entry *entry = NULL;
280 if (list_p == NULL || type == GOT_UNKNOWN || type == GOT_TLS_LE)
283 entry = got_entry_for_type (list_p, type);
287 || (! elf_hash_table (info)->dynamic_sections_created
288 || (bfd_link_pic (info)
289 && SYMBOL_REFERENCES_LOCAL (info, h))))
291 const char ATTRIBUTE_UNUSED *symbol_name;
292 static const char local_name[] = "(local)";
293 asection *tls_sec = NULL;
294 bfd_vma sym_value = 0;
298 // TODO: This should not be here.
299 reloc_data->sym_value = h->root.u.def.value;
300 reloc_data->sym_section = h->root.u.def.section;
302 sym_value = h->root.u.def.value
303 + h->root.u.def.section->output_section->vma
304 + h->root.u.def.section->output_offset;
306 tls_sec = elf_hash_table (info)->tls_sec;
308 symbol_name = h->root.root.string;
312 Elf_Internal_Sym *sym = local_syms + r_symndx;
313 asection *sec = local_sections[r_symndx];
315 sym_value = sym->st_value
316 + sec->output_section->vma
317 + sec->output_offset;
319 tls_sec = elf_hash_table (info)->tls_sec;
321 symbol_name = local_name;
325 if (entry && !entry->processed)
331 BFD_ASSERT (tls_sec && tls_sec->output_section);
332 bfd_vma sec_vma = tls_sec->output_section->vma;
334 bfd_put_32 (output_bfd,
336 htab->sgot->contents + entry->offset
337 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
340 ARC_DEBUG ("arc_info: FIXED -> %s value = %#lx "
341 "@ %lx, for symbol %s\n",
342 (entry->type == GOT_TLS_GD ? "GOT_TLS_GD" :
344 (long) (sym_value - sec_vma),
345 (long) (htab->sgot->output_section->vma
346 + htab->sgot->output_offset->vma
348 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
356 BFD_ASSERT (tls_sec && tls_sec->output_section);
357 bfd_vma ATTRIBUTE_UNUSED sec_vma
358 = tls_sec->output_section->vma;
360 bfd_put_32 (output_bfd,
362 + (elf_hash_table (info)->dynamic_sections_created ? 0 : TCB_SIZE),
363 htab->sgot->contents + entry->offset
364 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
367 ARC_DEBUG ("arc_info: FIXED -> %s value = %#lx "
368 "@ %p, for symbol %s\n",
369 (entry->type == GOT_TLS_GD ? "GOT_TLS_GD" :
371 (long) (sym_value - sec_vma),
372 (long) (htab->sgot->output_section->vma
373 + htab->sgot->output_offset->vma
375 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF
384 = reloc_data->sym_section->output_section->vma
385 + reloc_data->sym_section->output_offset;
388 && h->root.type == bfd_link_hash_undefweak)
389 ARC_DEBUG ("arc_info: PATCHED: NOT_PATCHED "
390 "@ %#08lx for sym %s in got offset %#lx "
392 (long) (htab->sgot->output_section->vma
393 + htab->sgot->output_offset
396 (long) entry->offset);
399 bfd_put_32 (output_bfd,
400 reloc_data->sym_value + sec_vma,
401 htab->sgot->contents + entry->offset);
402 ARC_DEBUG ("arc_info: PATCHED: %#08lx "
403 "@ %#08lx for sym %s in got offset %#lx\n",
404 (long) (reloc_data->sym_value + sec_vma),
405 (long) (htab->sgot->output_section->vma
406 + htab->sgot->output_offset + entry->offset),
408 (long) entry->offset);
416 entry->processed = TRUE;
420 return entry->offset;
424 create_got_dynrelocs_for_single_entry (struct got_entry *list,
426 struct bfd_link_info * info,
427 struct elf_link_hash_entry *h)
432 bfd_vma got_offset = list->offset;
434 if (list->type == GOT_NORMAL
435 && !list->created_dyn_relocation)
437 if (bfd_link_pic (info)
439 && (info->symbolic || h->dynindx == -1)
442 ADD_RELA (output_bfd, got, got_offset, 0, R_ARC_RELATIVE, 0);
444 /* Do not fully understand the side effects of this condition.
445 The relocation space might still being reserved. Perhaps
446 I should clear its value. */
447 else if (h != NULL && h->dynindx != -1)
449 ADD_RELA (output_bfd, got, got_offset, h->dynindx, R_ARC_GLOB_DAT, 0);
451 list->created_dyn_relocation = TRUE;
453 else if (list->existing_entries != TLS_GOT_NONE
454 && !list->created_dyn_relocation)
456 /* TODO TLS: This is not called for local symbols.
457 In order to correctly implement TLS, this should also
458 be called for all local symbols with tls got entries.
459 Should be moved to relocate_section in order to make it
460 work for local symbols. */
461 struct elf_link_hash_table *htab = elf_hash_table (info);
462 enum tls_got_entries e = list->existing_entries;
464 BFD_ASSERT (list->type != GOT_TLS_GD
465 || list->existing_entries == TLS_GOT_MOD_AND_OFF);
467 bfd_vma dynindx = (h == NULL || h->dynindx == -1) ? 0 : h->dynindx;
469 if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_MOD)
471 ADD_RELA (output_bfd, got, got_offset, dynindx,
472 R_ARC_TLS_DTPMOD, 0);
473 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
474 GOT_OFFSET = %#lx, GOT_VMA = %#lx, INDEX = %ld, ADDEND = 0x0\n",
477 (long) (htab->sgot->output_section->vma
478 + htab->sgot->output_offset + got_offset),
482 if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_OFF)
485 if (list->type == GOT_TLS_IE)
487 addend = bfd_get_32 (output_bfd,
488 htab->sgot->contents + got_offset);
491 ADD_RELA (output_bfd, got,
492 got_offset + (e == TLS_GOT_MOD_AND_OFF ? 4 : 0),
494 (list->type == GOT_TLS_IE ? R_ARC_TLS_TPOFF
498 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
499 GOT_OFFSET = %#lx, GOT_VMA = %#lx, INDEX = %ld, ADDEND = %#lx\n",
502 (long) (htab->sgot->output_section->vma
503 + htab->sgot->output_offset + got_offset),
504 (long) dynindx, (long) addend);
506 list->created_dyn_relocation = TRUE;
511 create_got_dynrelocs_for_got_info (struct got_entry **list_p,
513 struct bfd_link_info * info,
514 struct elf_link_hash_entry *h)
519 struct got_entry *list = *list_p;
520 /* Traverse the list of got entries for this symbol. */
523 create_got_dynrelocs_for_single_entry (list, output_bfd, info, h);
528 #undef ADD_SYMBOL_REF_SEC_AND_RELOC
530 #endif /* ARC_GOT_H */