1 /* AArch64 specific symbolic name handling.
2 Copyright (C) 2013, 2015, 2017 Red Hat, Inc.
3 This file is part of elfutils.
4
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
7
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
11
12 or
13
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
17
18 or both in parallel, as here.
19
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include <elf.h>
34 #include <stddef.h>
35 #include <string.h>
36
37 #define BACKEND aarch64_
38 #include "libebl_CPU.h"
39
40
41 /* Check for the simple reloc types. */
42 Elf_Type
aarch64_reloc_simple_type(Ebl * ebl,int type,int * addsub)43 aarch64_reloc_simple_type (Ebl *ebl __attribute__ ((unused)), int type,
44 int *addsub __attribute__ ((unused)))
45 {
46 switch (type)
47 {
48 case R_AARCH64_ABS64:
49 return ELF_T_XWORD;
50 case R_AARCH64_ABS32:
51 return ELF_T_WORD;
52 case R_AARCH64_ABS16:
53 return ELF_T_HALF;
54
55 default:
56 return ELF_T_NUM;
57 }
58 }
59
60 /* If this is the _GLOBAL_OFFSET_TABLE_ symbol, then it should point in
61 the .got even if there is a .got.plt section.
62 https://sourceware.org/ml/libc-ports/2013-06/msg00057.html
63 https://bugzilla.redhat.com/show_bug.cgi?id=1201778
64 */
65 bool
aarch64_check_special_symbol(Elf * elf,const GElf_Sym * sym,const char * name,const GElf_Shdr * destshdr)66 aarch64_check_special_symbol (Elf *elf, const GElf_Sym *sym,
67 const char *name, const GElf_Shdr *destshdr)
68 {
69 if (name != NULL
70 && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
71 {
72 size_t shstrndx;
73 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
74 return false;
75 const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
76 if (sname != NULL
77 && (strcmp (sname, ".got") == 0 || strcmp (sname, ".got.plt") == 0))
78 {
79 Elf_Scn *scn = NULL;
80 while ((scn = elf_nextscn (elf, scn)) != NULL)
81 {
82 GElf_Shdr shdr_mem;
83 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
84 if (shdr != NULL)
85 {
86 sname = elf_strptr (elf, shstrndx, shdr->sh_name);
87 if (sname != NULL && strcmp (sname, ".got") == 0)
88 return (sym->st_value >= shdr->sh_addr
89 && sym->st_value < shdr->sh_addr + shdr->sh_size);
90 }
91 }
92 }
93 }
94
95 return false;
96 }
97
98 /* A data mapping symbol is a symbol with "$d" name or "$d.<any...>" name,
99 STT_NOTYPE, STB_LOCAL and st_size of zero. The indicate the stat of a
100 sequence of data items. */
101 bool
aarch64_data_marker_symbol(const GElf_Sym * sym,const char * sname)102 aarch64_data_marker_symbol (const GElf_Sym *sym, const char *sname)
103 {
104 return (sym != NULL && sname != NULL
105 && sym->st_size == 0 && GELF_ST_BIND (sym->st_info) == STB_LOCAL
106 && GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
107 && (strcmp (sname, "$d") == 0 || strncmp (sname, "$d.", 3) == 0));
108 }
109
110 const char *
aarch64_dynamic_tag_name(int64_t tag,char * buf,size_t len)111 aarch64_dynamic_tag_name (int64_t tag, char *buf __attribute__ ((unused)),
112 size_t len __attribute__ ((unused)))
113 {
114 switch (tag)
115 {
116 case DT_AARCH64_BTI_PLT:
117 return "AARCH64_BTI_PLT";
118 case DT_AARCH64_PAC_PLT:
119 return "AARCH64_PAC_PLT";
120 case DT_AARCH64_VARIANT_PCS:
121 return "AARCH64_VARIANT_PCS";
122 default:
123 break;
124 }
125 return NULL;
126 }
127
128 bool
aarch64_dynamic_tag_check(int64_t tag)129 aarch64_dynamic_tag_check (int64_t tag)
130 {
131 return (tag == DT_AARCH64_BTI_PLT
132 || tag == DT_AARCH64_PAC_PLT
133 || tag == DT_AARCH64_VARIANT_PCS);
134 }
135