1/* RISC-V specific symbolic name handling.
2   This file is part of elfutils.
3
4   This file is free software; you can redistribute it and/or modify
5   it under the terms of either
6
7     * the GNU Lesser General Public License as published by the Free
8       Software Foundation; either version 3 of the License, or (at
9       your option) any later version
10
11   or
12
13     * the GNU General Public License as published by the Free
14       Software Foundation; either version 2 of the License, or (at
15       your option) any later version
16
17   or both in parallel, as here.
18
19   elfutils is distributed in the hope that it will be useful, but
20   WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   General Public License for more details.
23
24   You should have received copies of the GNU General Public License and
25   the GNU Lesser General Public License along with this program.  If
26   not, see <http://www.gnu.org/licenses/>.  */
27
28#ifdef HAVE_CONFIG_H
29# include <config.h>
30#endif
31
32#include <assert.h>
33#include <elf.h>
34#include <stddef.h>
35#include <string.h>
36
37#define BACKEND riscv_
38#include "libebl_CPU.h"
39
40
41/* Check for the simple reloc types.  */
42Elf_Type
43riscv_reloc_simple_type (Ebl *ebl __attribute__ ((unused)), int type,
44			 int *addsub)
45{
46  switch (type)
47    {
48    case R_RISCV_SET8:
49      return ELF_T_BYTE;
50    case R_RISCV_SET16:
51      return ELF_T_HALF;
52    case R_RISCV_32:
53    case R_RISCV_SET32:
54      return ELF_T_WORD;
55    case R_RISCV_64:
56      return ELF_T_XWORD;
57    case R_RISCV_ADD16:
58      *addsub = 1;
59      return ELF_T_HALF;
60    case R_RISCV_SUB16:
61      *addsub = -1;
62      return ELF_T_HALF;
63    case R_RISCV_ADD32:
64      *addsub = 1;
65      return ELF_T_WORD;
66    case R_RISCV_SUB32:
67      *addsub = -1;
68      return ELF_T_WORD;
69    case R_RISCV_ADD64:
70      *addsub = 1;
71      return ELF_T_XWORD;
72    case R_RISCV_SUB64:
73      *addsub = -1;
74      return ELF_T_XWORD;
75    default:
76      return ELF_T_NUM;
77    }
78}
79
80/* Check whether machine flags are valid.  */
81bool
82riscv_machine_flag_check (GElf_Word flags)
83{
84  return ((flags &~ (EF_RISCV_RVC
85		     | EF_RISCV_FLOAT_ABI)) == 0);
86}
87
88/* Check whether given symbol's st_value and st_size are OK despite failing
89   normal checks.  */
90bool
91riscv_check_special_symbol (Elf *elf, const GElf_Sym *sym,
92			    const char *name, const GElf_Shdr *destshdr)
93{
94  if (name == NULL)
95    return false;
96
97  size_t shstrndx;
98  if (elf_getshdrstrndx (elf, &shstrndx) != 0)
99    return false;
100  const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
101  if (sname == NULL)
102    return false;
103
104  /* _GLOBAL_OFFSET_TABLE_ points to the start of the .got section, but it
105     is preceded by the .got.plt section in the output .got section.  */
106  if (strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
107    return (strcmp (sname, ".got") == 0
108	    && sym->st_value >= destshdr->sh_addr
109	    && sym->st_value < destshdr->sh_addr + destshdr->sh_size);
110
111  /* __global_pointer$ points to the .sdata section with an offset of
112     0x800.  It might however fall in the .got section, in which case we
113     cannot check the offset.  The size always should be zero.  */
114  if (strcmp (name, "__global_pointer$") == 0)
115    return (((strcmp (sname, ".sdata") == 0
116	      && sym->st_value == destshdr->sh_addr + 0x800)
117	     || strcmp (sname, ".got") == 0)
118	    && sym->st_size == 0);
119
120  return false;
121}
122
123const char *
124riscv_segment_type_name (int segment, char *buf __attribute__ ((unused)),
125			 size_t len __attribute__ ((unused)))
126{
127  switch (segment)
128    {
129    case PT_RISCV_ATTRIBUTES:
130      return "RISCV_ATTRIBUTES";
131    }
132  return NULL;
133}
134
135/* Return symbolic representation of section type.  */
136const char *
137riscv_section_type_name (int type,
138			 char *buf __attribute__ ((unused)),
139			 size_t len __attribute__ ((unused)))
140{
141  switch (type)
142    {
143    case SHT_RISCV_ATTRIBUTES:
144      return "RISCV_ATTRIBUTES";
145    }
146
147  return NULL;
148}
149
150const char *
151riscv_dynamic_tag_name (int64_t tag, char *buf __attribute__ ((unused)),
152			size_t len __attribute__ ((unused)))
153{
154  switch (tag)
155    {
156    case DT_RISCV_VARIANT_CC:
157      return "RISCV_VARIANT_CC";
158    }
159  return NULL;
160}
161
162bool
163riscv_dynamic_tag_check (int64_t tag)
164{
165  return tag == DT_RISCV_VARIANT_CC;
166}
167