summaryrefslogtreecommitdiff
path: root/libebl/ppc_symbol.c
blob: e865eaabc7022a0f7ca66135dc0d24ed2f1305ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* PPC specific symbolic name handling.
   Copyright (C) 2004, 2005 Red Hat, Inc.
   Written by Ulrich Drepper <drepper@redhat.com>, 2004.

   This program is Open Source software; you can redistribute it and/or
   modify it under the terms of the Open Software License version 1.0 as
   published by the Open Source Initiative.

   You should have received a copy of the Open Software License along
   with this program; if not, you may obtain a copy of the Open Software
   License version 1.0 from http://www.opensource.org/licenses/osl.php or
   by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
   3001 King Ranch Road, Ukiah, CA 95482.   */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <assert.h>
#include <elf.h>
#include <stddef.h>

#include <libebl_ppc.h>


/* Return of the backend.  */
const char *
ppc_backend_name (void)
{
  return "ppc";
}


/* Relocation mapping table.  */
static struct
{
  const char *name;
  enum { both = 0, rel = 1, exec = 2 } appear;
} reloc_map_table[] =
  {
    // XXX Check all the appear values.
    [R_PPC_NONE] = { "R_PPC_NONE", both },
    [R_PPC_ADDR32] = { "R_PPC_ADDR32", both },
    [R_PPC_ADDR24] = { "R_PPC_ADDR24", both },
    [R_PPC_ADDR16] = { "R_PPC_ADDR16", both },
    [R_PPC_ADDR16_LO] = { "R_PPC_ADDR16_LO", both },
    [R_PPC_ADDR16_HI] = { "R_PPC_ADDR16_HI", both },
    [R_PPC_ADDR16_HA] = { "R_PPC_ADDR16_HA", both },
    [R_PPC_ADDR14] = { "R_PPC_ADDR14", exec },
    [R_PPC_ADDR14_BRTAKEN] = { "R_PPC_ADDR14_BRTAKEN", exec },
    [R_PPC_ADDR14_BRNTAKEN] = { "R_PPC_ADDR14_BRNTAKEN", exec },
    [R_PPC_REL24] = { "R_PPC_REL24", both },
    [R_PPC_REL14] = { "R_PPC_REL14", both },
    [R_PPC_REL14_BRTAKEN] = { "R_PPC_REL14_BRTAKEN", exec },
    [R_PPC_REL14_BRNTAKEN] = { "R_PPC_REL14_BRNTAKEN", exec },
    [R_PPC_GOT16] = { "R_PPC_GOT16", rel },
    [R_PPC_GOT16_LO] = { "R_PPC_GOT16_LO", rel },
    [R_PPC_GOT16_HI] = { "R_PPC_GOT16_HI", rel },
    [R_PPC_GOT16_HA] = { "R_PPC_GOT16_HA", rel },
    [R_PPC_PLTREL24] = { "R_PPC_PLTREL24", rel },
    [R_PPC_COPY] = { "R_PPC_COPY", exec },
    [R_PPC_GLOB_DAT] = { "R_PPC_GLOB_DAT", exec },
    [R_PPC_JMP_SLOT] = { "R_PPC_JMP_SLOT", exec },
    [R_PPC_RELATIVE] = { "R_PPC_RELATIVE", exec },
    [R_PPC_LOCAL24PC] = { "R_PPC_LOCAL24PC", rel },
    [R_PPC_UADDR32] = { "R_PPC_UADDR32", exec },
    [R_PPC_UADDR16] = { "R_PPC_UADDR16", exec },
    [R_PPC_REL32] = { "R_PPC_REL32", exec },
    [R_PPC_PLT32] = { "R_PPC_PLT32", exec },
    [R_PPC_PLTREL32] = { "R_PPC_PLTREL32", both },
    [R_PPC_PLT16_LO] = { "R_PPC_PLT16_LO", both },
    [R_PPC_PLT16_HI] = { "R_PPC_PLT16_HI", both },
    [R_PPC_PLT16_HA] = { "R_PPC_PLT16_HA", both },
    [R_PPC_SDAREL16] = { "R_PPC_SDAREL16", both },
    [R_PPC_SECTOFF] = { "R_PPC_SECTOFF", both },
    [R_PPC_SECTOFF_LO] = { "R_PPC_SECTOFF_LO", both },
    [R_PPC_SECTOFF_HI] = { "R_PPC_SECTOFF_HI", both },
    [R_PPC_SECTOFF_HA] = { "R_PPC_SECTOFF_HA", both },
    [R_PPC_TLS] = { "R_PPC_TLS", both },
    [R_PPC_DTPMOD32] = { "R_PPC_DTPMOD32", exec },
    [R_PPC_TPREL16] = { "R_PPC_TPREL16", rel },
    [R_PPC_TPREL16_LO] = { "R_PPC_TPREL16_LO", rel },
    [R_PPC_TPREL16_HI] = { "R_PPC_TPREL16_HI", rel },
    [R_PPC_TPREL16_HA] = { "R_PPC_TPREL16_HA", rel },
    [R_PPC_TPREL32] = { "R_PPC_TPREL32", exec },
    [R_PPC_DTPREL16] = { "R_PPC_DTPREL16", rel },
    [R_PPC_DTPREL16_LO] = { "R_PPC_DTPREL16_LO", rel },
    [R_PPC_DTPREL16_HI] = { "R_PPC_DTPREL16_HI", rel },
    [R_PPC_DTPREL16_HA] = { "R_PPC_DTPREL16_HA", rel },
    [R_PPC_DTPREL32] = { "R_PPC_DTPREL32", exec },
    [R_PPC_GOT_TLSGD16] = { "R_PPC_GOT_TLSGD16", exec },
    [R_PPC_GOT_TLSGD16_LO] = { "R_PPC_GOT_TLSGD16_LO", exec },
    [R_PPC_GOT_TLSGD16_HI] = { "R_PPC_GOT_TLSGD16_HI", exec },
    [R_PPC_GOT_TLSGD16_HA] = { "R_PPC_GOT_TLSGD16_HA", exec },
    [R_PPC_GOT_TLSLD16] = { "R_PPC_GOT_TLSLD16", exec },
    [R_PPC_GOT_TLSLD16_LO] = { "R_PPC_GOT_TLSLD16_LO", exec },
    [R_PPC_GOT_TLSLD16_HI] = { "R_PPC_GOT_TLSLD16_HI", exec },
    [R_PPC_GOT_TLSLD16_HA] = { "R_PPC_GOT_TLSLD16_HA", exec },
    [R_PPC_GOT_TPREL16] = { "R_PPC_GOT_TPREL16", exec },
    [R_PPC_GOT_TPREL16_LO] = { "R_PPC_GOT_TPREL16_LO", exec },
    [R_PPC_GOT_TPREL16_HI] = { "R_PPC_GOT_TPREL16_HI", exec },
    [R_PPC_GOT_TPREL16_HA] = { "R_PPC_GOT_TPREL16_HA", exec },
    [R_PPC_GOT_DTPREL16] = { "R_PPC_GOT_DTPREL16", exec },
    [R_PPC_GOT_DTPREL16_LO] = { "R_PPC_GOT_DTPREL16_LO", exec },
    [R_PPC_GOT_DTPREL16_HI] = { "R_PPC_GOT_DTPREL16_HI", exec },
    [R_PPC_GOT_DTPREL16_HA] = { "R_PPC_GOT_DTPREL16_HA", exec },
    [R_PPC_REL16] = { "R_PPC_REL16", rel },
    [R_PPC_REL16_LO] = { "R_PPC_REL16_LO", rel },
    [R_PPC_REL16_HI] = { "R_PPC_REL16_HI", rel },
    [R_PPC_REL16_HA] = { "R_PPC_REL16_HA", rel },
  };
#define nreloc_map_table \
     (sizeof (reloc_map_table) / sizeof (reloc_map_table[0]))


/* Determine relocation type string for PPC.  */
const char *
ppc_reloc_type_name (int type, char *buf __attribute__ ((unused)),
		     size_t len __attribute__ ((unused)))
{
  if (type < 0 || (size_t) type >= nreloc_map_table)
    return NULL;

  return reloc_map_table[type].name;
}


/* Check for correct relocation type.  */
bool
ppc_reloc_type_check (int type)
{
  return (type >= R_PPC_NONE && (size_t) type < nreloc_map_table
	  && reloc_map_table[type].name != NULL) ? true : false;
}


/* Check for correct relocation type use.  */
bool
ppc_reloc_valid_use (Elf *elf, int type)
{
  if (type < R_PPC_NONE || type >= R_PPC_NUM
      || reloc_map_table[type].name == NULL)
    return false;

  Elf32_Ehdr *ehdr = elf32_getehdr (elf);
  assert (ehdr != NULL);

  if (reloc_map_table[type].appear == rel)
    return ehdr->e_type == ET_REL;

  if (reloc_map_table[type].appear == exec)
    return ehdr->e_type != ET_REL;

  assert (reloc_map_table[type].appear == both);
  return true;
}


/* Check for the simple reloc types.  */
Elf_Type
ppc_reloc_simple_type (Elf *elf __attribute__ ((unused)), int type)
{
  switch (type)
    {
    case R_PPC_ADDR32:
    case R_PPC_UADDR32:
      return ELF_T_WORD;
    case R_PPC_UADDR16:
      return ELF_T_HALF;
    default:
      return ELF_T_NUM;
    }
}


const char *
ppc_dynamic_tag_name (int64_t tag, char *buf __attribute__ ((unused)),
		      size_t len __attribute__ ((unused)))
{
  switch (tag)
    {
    case DT_PPC_GOT:
      return "PPC_GOT";
    default:
      break;
    }
  return NULL;
}


bool
ppc_dynamic_tag_check (int64_t tag)
{
  return tag == DT_PPC_GOT;
}


/* Check whether given relocation is a copy relocation.  */
bool
ppc_copy_reloc_p (int reloc)
{
  return reloc == R_PPC_COPY;
}