aboutsummaryrefslogtreecommitdiffstats
path: root/src/reloc-info.c
blob: d51cfc179f5722e6332e87758e12e58eadcd2cba (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
204
/* Copyright (C) 2008 CodeSourcery
   Written by Maciej W. Rozycki <macro@codesourcery.com>, 2008.

* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <config.h>

#include "prelink.h"
#include "reloc-info.h"

/* A structure to lay out generic relocation information
 * in a way specific to 64-bit MIPS.  */
union mips64_r_info
{
  /* Generic r_info interpretation.  */
  Elf64_Xword r_info;

  /* 64-bit MIPS r_info interpretation.  */
  struct
    {
      /* Symbol index for the first relocation.  */
      Elf64_Word r_sym;

      /* Special symbol for the second relocation.  */
      Elf64_Byte r_ssym;

      /* Third relocation.  */
      Elf64_Byte r_type3;

      /* Second relocation.  */
      Elf64_Byte r_type2;

      /* First relocation.  */
      Elf64_Byte r_type;
    }
  s_info;
};

/* Extract the symbol index from 64-bit MIPS reloc info.  */

static GElf_Xword
mips64_r_sym (DSO *dso, GElf_Xword r_info)
{
  union mips64_r_info mips64_r_info;

  buf_write_ne64 (dso, (unsigned char *) &mips64_r_info.r_info, r_info);
  return buf_read_une32 (dso, (unsigned char *) &mips64_r_info.s_info.r_sym);
}

/* Extract the special symbol index from 64-bit MIPS reloc info.  */

static GElf_Xword
mips64_r_ssym (DSO *dso, GElf_Xword r_info)
{
  union mips64_r_info mips64_r_info;

  buf_write_ne64 (dso, (unsigned char *) &mips64_r_info.r_info, r_info);
  return mips64_r_info.s_info.r_ssym;
}

/* Extract the first reloc type from 64-bit MIPS reloc info.  */

static GElf_Xword
mips64_r_type (DSO *dso, GElf_Xword r_info)
{
  union mips64_r_info mips64_r_info;

  buf_write_ne64 (dso, (unsigned char *) &mips64_r_info.r_info, r_info);
  return mips64_r_info.s_info.r_type;
}

/* Extract the second reloc type from 64-bit MIPS reloc info.  */

static GElf_Xword
mips64_r_type2 (DSO *dso, GElf_Xword r_info)
{
  union mips64_r_info mips64_r_info;

  buf_write_ne64 (dso, (unsigned char *) &mips64_r_info.r_info, r_info);
  return mips64_r_info.s_info.r_type2;
}

/* Extract the third reloc type from 64-bit MIPS reloc info.  */

static GElf_Xword
mips64_r_type3 (DSO *dso, GElf_Xword r_info)
{
  union mips64_r_info mips64_r_info;

  buf_write_ne64 (dso, (unsigned char *) &mips64_r_info.r_info, r_info);
  return mips64_r_info.s_info.r_type3;
}

/* Construct 64-bit MIPS reloc info from symbol indices and reloc types.  */

static GElf_Xword
mips64_r_info_ext (DSO *dso, GElf_Word r_sym, Elf64_Byte r_ssym,
		   Elf64_Byte r_type, Elf64_Byte r_type2, Elf64_Byte r_type3)
{
  union mips64_r_info mips64_r_info;

  buf_write_ne32 (dso, (unsigned char *) &mips64_r_info.s_info.r_sym, r_sym);
  mips64_r_info.s_info.r_ssym = r_ssym;
  mips64_r_info.s_info.r_type = r_type;
  mips64_r_info.s_info.r_type2 = r_type2;
  mips64_r_info.s_info.r_type3 = r_type3;
  return buf_read_une64 (dso, (unsigned char *) &mips64_r_info.r_info);
}


/* Extract the symbol index from reloc info.  */

GElf_Xword
reloc_r_sym (DSO *dso, GElf_Xword r_info)
{
  if (dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64
      && dso->ehdr.e_machine == EM_MIPS)
    return mips64_r_sym (dso, r_info);
  else
    return GELF_R_SYM (r_info);
}

/* Extract the special symbol index from reloc info.  */

GElf_Xword
reloc_r_ssym (DSO *dso, GElf_Xword r_info)
{
  if (dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64
      && dso->ehdr.e_machine == EM_MIPS)
    return mips64_r_ssym (dso, r_info);
  else
    return RSS_UNDEF;
}

/* Extract the first reloc type from reloc info.  */

GElf_Xword
reloc_r_type (DSO *dso, GElf_Xword r_info)
{
  if (dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64
      && dso->ehdr.e_machine == EM_MIPS)
    return mips64_r_type (dso, r_info);
  else
    return GELF_R_TYPE (r_info);
}

/* Extract the second reloc type from reloc info.  */

GElf_Xword
reloc_r_type2 (DSO *dso, GElf_Xword r_info)
{
  if (dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64
      && dso->ehdr.e_machine == EM_MIPS)
    return mips64_r_type2 (dso, r_info);
  else
    return 0;
}

/* Extract the third reloc type from reloc info.  */

GElf_Xword
reloc_r_type3 (DSO *dso, GElf_Xword r_info)
{
  if (dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64
      && dso->ehdr.e_machine == EM_MIPS)
    return mips64_r_type3 (dso, r_info);
  else
    return 0;
}

/* Construct reloc info from symbol index and reloc type.  */

GElf_Xword
reloc_r_info (DSO *dso, GElf_Word r_sym, GElf_Word r_type)
{
  if (dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64
      && dso->ehdr.e_machine == EM_MIPS)
    switch (r_type)
      {
      case R_MIPS_REL32:
      case R_MIPS_GLOB_DAT:
	return mips64_r_info_ext (dso, r_sym, RSS_UNDEF,
				  r_type, R_MIPS_64, R_MIPS_NONE);
      default:
	return mips64_r_info_ext (dso, r_sym, RSS_UNDEF,
				  r_type, R_MIPS_NONE, R_MIPS_NONE);
      }
  else
    return GELF_R_INFO (r_sym, r_type);
}

/* Construct reloc info from symbol index and reloc type.  */

GElf_Xword
reloc_r_info_ext (DSO *dso, GElf_Word r_sym, Elf64_Byte r_ssym,
		  Elf64_Byte r_type, Elf64_Byte r_type2, Elf64_Byte r_type3)
{
  if (dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64
      && dso->ehdr.e_machine == EM_MIPS)
    return mips64_r_info_ext (dso, r_sym, r_ssym, r_type, r_type2, r_type3);
  else
    return GELF_R_INFO (r_sym, r_type);
}