Imported Upstream version 0.155
[platform/upstream/elfutils.git] / backends / sparc_symbol.c
1 /* SPARC specific symbolic name handling.
2    Copyright (C) 2002, 2003, 2005, 2007, 2008 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Jakub Jelinek <jakub@redhat.com>, 2002.
5
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8
9      * the GNU Lesser General Public License as published by the Free
10        Software Foundation; either version 3 of the License, or (at
11        your option) any later version
12
13    or
14
15      * the GNU General Public License as published by the Free
16        Software Foundation; either version 2 of the License, or (at
17        your option) any later version
18
19    or both in parallel, as here.
20
21    elfutils is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    General Public License for more details.
25
26    You should have received copies of the GNU General Public License and
27    the GNU Lesser General Public License along with this program.  If
28    not, see <http://www.gnu.org/licenses/>.  */
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include <elf.h>
35 #include <stddef.h>
36
37 #define BACKEND         sparc_
38 #include "libebl_CPU.h"
39
40 /* Check for the simple reloc types.  */
41 Elf_Type
42 sparc_reloc_simple_type (Ebl *ebl __attribute__ ((unused)), int type)
43 {
44   switch (type)
45     {
46     case R_SPARC_8:
47       return ELF_T_BYTE;
48     case R_SPARC_16:
49     case R_SPARC_UA16:
50       return ELF_T_HALF;
51     case R_SPARC_32:
52     case R_SPARC_UA32:
53       return ELF_T_WORD;
54     case R_SPARC_64:
55     case R_SPARC_UA64:
56       return ELF_T_XWORD;
57     default:
58       return ELF_T_NUM;
59     }
60 }
61
62 /* Check whether machine flags are valid.  */
63 bool
64 sparc_machine_flag_check (GElf_Word flags)
65 {
66   return ((flags &~ (EF_SPARCV9_MM
67                      | EF_SPARC_LEDATA
68                      | EF_SPARC_32PLUS
69                      | EF_SPARC_SUN_US1
70                      | EF_SPARC_SUN_US3)) == 0);
71 }
72
73 bool
74 sparc_check_special_section (Ebl *ebl,
75                              int ndx __attribute__ ((unused)),
76                              const GElf_Shdr *shdr,
77                              const char *sname __attribute__ ((unused)))
78 {
79   if ((shdr->sh_flags & (SHF_WRITE | SHF_EXECINSTR))
80       == (SHF_WRITE | SHF_EXECINSTR))
81     {
82       /* This is ordinarily flagged, but is valid for a PLT on SPARC.
83
84          Look for the SHT_DYNAMIC section and the DT_PLTGOT tag in it.
85          Its d_ptr should match the .plt section's sh_addr.  */
86
87       Elf_Scn *scn = NULL;
88       while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
89         {
90           GElf_Shdr scn_shdr;
91           if (likely (gelf_getshdr (scn, &scn_shdr) != NULL)
92               && scn_shdr.sh_type == SHT_DYNAMIC
93               && scn_shdr.sh_entsize != 0)
94             {
95               Elf_Data *data = elf_getdata (scn, NULL);
96               if (data != NULL)
97                 for (size_t i = 0; i < data->d_size / scn_shdr.sh_entsize; ++i)
98                   {
99                     GElf_Dyn dyn;
100                     if (unlikely (gelf_getdyn (data, i, &dyn) == NULL))
101                       break;
102                     if (dyn.d_tag == DT_PLTGOT)
103                       return dyn.d_un.d_ptr == shdr->sh_addr;
104                   }
105               break;
106             }
107         }
108     }
109
110   return false;
111 }
112
113 const char *
114 sparc_symbol_type_name (int type,
115                         char *buf __attribute__ ((unused)),
116                         size_t len __attribute__ ((unused)))
117 {
118   switch (type)
119     {
120     case STT_SPARC_REGISTER:
121       return "SPARC_REGISTER";
122     }
123   return NULL;
124 }
125
126 const char *
127 sparc_dynamic_tag_name (int64_t tag,
128                         char *buf __attribute__ ((unused)),
129                         size_t len __attribute__ ((unused)))
130 {
131   switch (tag)
132     {
133     case DT_SPARC_REGISTER:
134       return "SPARC_REGISTER";
135     }
136   return NULL;
137 }
138
139 bool
140 sparc_dynamic_tag_check (int64_t tag)
141 {
142   switch (tag)
143     {
144     case DT_SPARC_REGISTER:
145       return true;
146     }
147   return false;
148 }