1/* NFP-specific support for 64-bit ELF
2   Copyright (C) 2017-2022 Free Software Foundation, Inc.
3   Contributed by Francois H. Theron <francois.theron@netronome.com>
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 3 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., 51 Franklin Street - Fifth Floor,
20   Boston, MA 02110-1301, USA.  */
21
22#include "sysdep.h"
23#include "bfd.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/nfp.h"
27
28
29static bfd_reloc_status_type
30elf64_nfp_reloc (bfd * abfd ATTRIBUTE_UNUSED,
31		 arelent * reloc_entry,
32		 asymbol * symbol,
33		 void *data ATTRIBUTE_UNUSED,
34		 asection * input_section,
35		 bfd * output_bfd,
36		 char **error_message ATTRIBUTE_UNUSED);
37
38/* We don't actually apply any relocations in this toolset
39   so we make them all do nothing, but at least display useful
40   names.
41   Most of these are mainly used by the NFP toolchain to resolve things
42   before the final ELF file is created.  */
43#define NFP_HOWTO(type) \
44  HOWTO (type, 0, 0, 0, false, 0, complain_overflow_dont, elf64_nfp_reloc, \
45	 #type, false, 0, 0, false)
46static reloc_howto_type elf_nfp_howto_table[] =
47{
48  NFP_HOWTO (R_NFP_NOTYPE),
49  NFP_HOWTO (R_NFP_W32LE),
50  NFP_HOWTO (R_NFP_SRC8_A),
51  NFP_HOWTO (R_NFP_SRC8_B),
52  NFP_HOWTO (R_NFP_IMMED8_I),
53  NFP_HOWTO (R_NFP_SC),
54  NFP_HOWTO (R_NFP_IMMED_LO16_I_A),
55  NFP_HOWTO (R_NFP_IMMED_LO16_I_B),
56  NFP_HOWTO (R_NFP_SRC7_B),
57  NFP_HOWTO (R_NFP_SRC7_A),
58  NFP_HOWTO (R_NFP_SRC8_I_B),
59  NFP_HOWTO (R_NFP_SRC8_I_A),
60  NFP_HOWTO (R_NFP_IMMED_HI16_I_A),
61  NFP_HOWTO (R_NFP_IMMED_HI16_I_B),
62  NFP_HOWTO (R_NFP_W64LE),
63  NFP_HOWTO (R_NFP_SH_INFO),
64  NFP_HOWTO (R_NFP_W32BE),
65  NFP_HOWTO (R_NFP_W64BE),
66  NFP_HOWTO (R_NFP_W32_29_24),
67  NFP_HOWTO (R_NFP_W32LE_AND),
68  NFP_HOWTO (R_NFP_W32BE_AND),
69  NFP_HOWTO (R_NFP_W32LE_OR),
70  NFP_HOWTO (R_NFP_W32BE_OR),
71  NFP_HOWTO (R_NFP_W64LE_AND),
72  NFP_HOWTO (R_NFP_W64BE_AND),
73  NFP_HOWTO (R_NFP_W64LE_OR),
74  NFP_HOWTO (R_NFP_W64BE_OR)
75};
76
77static bool
78elf64_nfp_object_p (bfd * abfd)
79{
80  /* If the e_machine value is one of the unofficial ones, we convert
81     it first and set e_flags accordingly for later consistency.  */
82  if (elf_elfheader (abfd)->e_machine == E_NFP_MACH_3200)
83    {
84      elf_elfheader (abfd)->e_machine = EM_NFP;
85      elf_elfheader (abfd)->e_flags &= ~EF_NFP_SET_MACH (~0);
86      elf_elfheader (abfd)->e_flags |= EF_NFP_SET_MACH (E_NFP_MACH_3200);
87    }
88  else if (elf_elfheader (abfd)->e_machine == E_NFP_MACH_6000)
89    {
90      elf_elfheader (abfd)->e_machine = EM_NFP;
91      elf_elfheader (abfd)->e_flags &= ~EF_NFP_SET_MACH (~0);
92      elf_elfheader (abfd)->e_flags |= EF_NFP_SET_MACH (E_NFP_MACH_6000);
93    }
94
95  if (elf_elfheader (abfd)->e_machine == EM_NFP)
96    {
97      int e_mach = EF_NFP_MACH (elf_elfheader (abfd)->e_flags);
98
99      switch (e_mach)
100	{
101	case E_NFP_MACH_3200:
102	case E_NFP_MACH_6000:
103	  if (!bfd_default_set_arch_mach (abfd, bfd_arch_nfp, e_mach))
104	    return false;
105	default:
106	  break;
107	}
108    }
109
110  return true;
111}
112
113static bool
114elf64_nfp_section_from_shdr (bfd * abfd,
115			     Elf_Internal_Shdr * hdr,
116			     const char *name, int shindex)
117{
118  switch (hdr->sh_type)
119    {
120    case SHT_NFP_INITREG:
121    case SHT_NFP_MECONFIG:
122    case SHT_NFP_UDEBUG:
123      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
124    default:
125      return false;
126    }
127}
128
129bfd_reloc_status_type
130elf64_nfp_reloc (bfd * abfd ATTRIBUTE_UNUSED,
131		 arelent * reloc_entry ATTRIBUTE_UNUSED,
132		 asymbol * symbol ATTRIBUTE_UNUSED,
133		 void *data ATTRIBUTE_UNUSED,
134		 asection * input_section ATTRIBUTE_UNUSED,
135		 bfd * output_bfd ATTRIBUTE_UNUSED,
136		 char **error_message ATTRIBUTE_UNUSED)
137{
138  return bfd_reloc_ok;
139}
140
141static bool
142elf64_nfp_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
143			 arelent * cache_ptr, Elf_Internal_Rela * dst)
144{
145  unsigned int r_type;
146
147  r_type = ELF64_R_TYPE (dst->r_info);
148  if (r_type >= R_NFP_MAX)
149    {
150      /* xgettext:c-format */
151      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
152			  abfd, r_type);
153      bfd_set_error (bfd_error_bad_value);
154      return false;
155    }
156  cache_ptr->howto = &elf_nfp_howto_table[r_type];
157  return true;
158}
159
160static reloc_howto_type *
161elf64_nfp_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
162			     bfd_reloc_code_real_type code ATTRIBUTE_UNUSED)
163{
164  return NULL;
165}
166
167static reloc_howto_type *
168elf64_nfp_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
169			     const char *r_name ATTRIBUTE_UNUSED)
170{
171  return NULL;
172}
173
174#define ELF_ARCH		bfd_arch_nfp
175#define ELF_MACHINE_CODE	EM_NFP
176#define ELF_MACHINE_ALT1	E_NFP_MACH_6000
177#define ELF_MACHINE_ALT2	E_NFP_MACH_3200
178#define ELF_MAXPAGESIZE		1
179#define TARGET_LITTLE_NAME	"elf64-nfp"
180#define TARGET_LITTLE_SYM       nfp_elf64_vec
181
182#define elf_backend_object_p		elf64_nfp_object_p
183#define elf_backend_section_from_shdr   elf64_nfp_section_from_shdr
184#define elf_info_to_howto		elf64_nfp_info_to_howto
185#define bfd_elf64_bfd_reloc_type_lookup	     elf64_nfp_reloc_type_lookup
186#define bfd_elf64_bfd_reloc_name_lookup      elf64_nfp_reloc_name_lookup
187
188#include "elf64-target.h"
189