summaryrefslogtreecommitdiffstats
path: root/trunk/src/arch-sh.c
blob: 1b11312fe278fc34b33b221c9f839b324b62e61d (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* Copyright (C) 2001, 2002, 2003, 2004, 2009 Red Hat, Inc.
   Written by Jakub Jelinek <jakub@redhat.com>, 2001.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <config.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <error.h>
#include <argp.h>
#include <stdlib.h>

#include "prelink.h"

static int
sh_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
	       GElf_Addr adjust)
{
  if (dyn->d_tag == DT_PLTGOT)
    {
      int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
      Elf32_Addr data;

      if (sec == -1)
	return 0;

      data = read_une32 (dso, dyn->d_un.d_ptr);
      /* If .got.plt[0] points to _DYNAMIC, it needs to be adjusted.  */
      if (data == dso->shdr[n].sh_addr && data >= start)
	write_ne32 (dso, dyn->d_un.d_ptr, data + adjust);

      data = read_une32 (dso, dyn->d_un.d_ptr + 4);
      /* If .got.plt[1] points to .plt + 36, it needs to be adjusted.  */
      if (data && data >= start)
	{
	  int i;

	  for (i = 1; i < dso->ehdr.e_shnum; i++)
	    if (data == dso->shdr[i].sh_addr + 36
		&& dso->shdr[i].sh_type == SHT_PROGBITS
		&& strcmp (strptr (dso, dso->ehdr.e_shstrndx,
					dso->shdr[i].sh_name), ".plt") == 0)
	      {
		write_ne32 (dso, dyn->d_un.d_ptr + 4, data + adjust);
		break;
	      }
	}
    }
  return 0;
}

static int
sh_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
	       GElf_Addr adjust)
{
  error (0, 0, "%s: SH doesn't support REL relocs", dso->filename);
  return 1;
}

static int
sh_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
		GElf_Addr adjust)
{
  Elf32_Addr data;

  switch (GELF_R_TYPE (rela->r_info))
    {
    case R_SH_RELATIVE:
      if (rela->r_addend && (Elf32_Addr) rela->r_addend >= start)
	{
	  rela->r_addend += (Elf32_Sword) adjust;
	  break;
	}
      /* FALLTHROUGH */
    case R_SH_JMP_SLOT:
      data = read_une32 (dso, rela->r_offset);
      if (data >= start)
	write_ne32 (dso, rela->r_offset, data + adjust);
      break;
      break;
    }
  return 0;
}

static int
sh_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
{
  error (0, 0, "%s: SH doesn't support REL relocs", info->dso->filename);
  return 1;
}

static int
sh_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
		 GElf_Addr relaaddr)
{
  DSO *dso;
  GElf_Addr value;

  dso = info->dso;
  if (GELF_R_TYPE (rela->r_info) == R_SH_NONE)
    /* Fast path: nothing to do.  */
    return 0;
  else if (GELF_R_TYPE (rela->r_info) == R_SH_RELATIVE)
    {
      if (rela->r_addend)
	write_ne32 (dso, rela->r_offset, rela->r_addend);
      return 0;
    }
  value = info->resolve (info, GELF_R_SYM (rela->r_info),
			 GELF_R_TYPE (rela->r_info));
  value += rela->r_addend;
  switch (GELF_R_TYPE (rela->r_info))
    {
    case R_SH_GLOB_DAT:
    case R_SH_JMP_SLOT:
    case R_SH_DIR32:
      write_ne32 (dso, rela->r_offset, value);
      break;
    case R_SH_REL32:
      write_ne32 (dso, rela->r_offset, value - rela->r_addend);
      break;
    case R_SH_COPY:
      if (dso->ehdr.e_type == ET_EXEC)
	/* COPY relocs are handled specially in generic code.  */
	return 0;
      error (0, 0, "%s: R_SH_COPY reloc in shared library?", dso->filename);
      return 1;
    default:
      error (0, 0, "%s: Unknown sh relocation type %d", dso->filename,
	     (int) GELF_R_TYPE (rela->r_info));
      return 1;
    }
  return 0;
}

static int
sh_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
			char *buf, GElf_Addr dest_addr)
{
  switch (GELF_R_TYPE (rela->r_info))
    {
    case R_SH_GLOB_DAT:
    case R_SH_JMP_SLOT:
    case R_SH_DIR32:
      buf_write_ne32 (info->dso, buf, rela->r_addend);
      break;
    default:
      abort ();
    }
  return 0;
}

static int
sh_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
{
  error (0, 0, "%s: SH doesn't support REL relocs", info->dso->filename);
  return 1;
}

static int
sh_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
{
  GElf_Addr value;

  value = info->resolve (info, GELF_R_SYM (rela->r_info),
			 GELF_R_TYPE (rela->r_info));
  value += rela->r_addend;
  switch (GELF_R_TYPE (rela->r_info))
    {
    case R_SH_NONE:
      break;
    case R_SH_GLOB_DAT:
    case R_SH_JMP_SLOT:
    case R_SH_DIR32:
      buf_write_ne32 (info->dso, buf, value);
      break;
    case R_SH_REL32:
      buf_write_ne32 (info->dso, buf, value - rela->r_offset);
      break;
    case R_SH_COPY:
      abort ();
    case R_SH_RELATIVE:
      error (0, 0, "%s: R_SH_RELATIVE in ET_EXEC object?", info->dso->filename);
      return 1;
    default:
      return 1;
    }
  return 0;
}

static int
sh_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
			 GElf_Addr reladdr)
{
  error (0, 0, "%s: SH doesn't support REL relocs", dso->filename);
  return 1;
}

static int
sh_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
			  GElf_Rela *rela, GElf_Addr relaaddr)
{
  GElf_Addr value;
  struct prelink_conflict *conflict;
  GElf_Rela *ret;

  if (GELF_R_TYPE (rela->r_info) == R_SH_RELATIVE
      || GELF_R_TYPE (rela->r_info) == R_SH_NONE
      || info->dso == dso)
    /* Fast path: nothing to do.  */
    return 0;
  conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
			       GELF_R_TYPE (rela->r_info));
  if (conflict == NULL)
    return 0;
  else if (conflict->ifunc)
    {
      error (0, 0, "%s: STT_GNU_IFUNC not handled on SuperH yet",
	     dso->filename);
      return 1;
    }
  value = conflict_lookup_value (conflict);
  ret = prelink_conflict_add_rela (info);
  if (ret == NULL)
    return 1;
  ret->r_offset = rela->r_offset;
  ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
  value += rela->r_addend;
  switch (GELF_R_TYPE (rela->r_info))
    {
    case R_SH_REL32:
      value -= rela->r_offset;
      ret->r_info = GELF_R_INFO (0, R_SH_DIR32);
      /* FALLTHROUGH */
    case R_SH_DIR32:
      if ((rela->r_offset & 3) == 0)
	ret->r_info = GELF_R_INFO (0, R_SH_GLOB_DAT);
      /* FALLTHROUGH */
    case R_SH_GLOB_DAT:
    case R_SH_JMP_SLOT:
      ret->r_addend = (Elf32_Sword) (value + rela->r_addend);
      break;
    case R_SH_COPY:
      error (0, 0, "R_SH_COPY should not be present in shared libraries");
      return 1;
    default:
      error (0, 0, "%s: Unknown sh relocation type %d", dso->filename,
	     (int) GELF_R_TYPE (rela->r_info));
      return 1;
    }
  return 0;
}

static int
sh_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
{
  return 0;
}

static int
sh_need_rel_to_rela (DSO *dso, int first, int last)
{
  return 0;
}

static int
sh_arch_prelink (struct prelink_info *info)
{
  DSO *dso;
  int i;

  dso = info->dso;
  if (dso->info[DT_PLTGOT])
    {
      /* Write address of .plt + 36 into got[1].
	 .plt + 36 is what got[3] contains unless prelinking.  */
      int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
      Elf32_Addr data;

      if (sec == -1)
	return 1;

      for (i = 1; i < dso->ehdr.e_shnum; i++)
	if (dso->shdr[i].sh_type == SHT_PROGBITS
	    && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
				 dso->shdr[i].sh_name),
			 ".plt"))
	break;

      if (i == dso->ehdr.e_shnum)
	return 0;
      data = dso->shdr[i].sh_addr + 36;
      write_ne32 (dso, dso->info[DT_PLTGOT] + 4, data);
    }

  return 0;
}

static int
sh_arch_undo_prelink (DSO *dso)
{
  int i;

  if (dso->info[DT_PLTGOT])
    {
      /* Clear got[1] if it contains address of .plt + 36.  */
      int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
      Elf32_Addr data;

      if (sec == -1)
	return 1;

      for (i = 1; i < dso->ehdr.e_shnum; i++)
	if (dso->shdr[i].sh_type == SHT_PROGBITS
	    && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
				 dso->shdr[i].sh_name),
			 ".plt"))
	break;

      if (i == dso->ehdr.e_shnum)
	return 0;
      data = read_une32 (dso, dso->info[DT_PLTGOT] + 4);
      if (data == dso->shdr[i].sh_addr + 36)
	write_ne32 (dso, dso->info[DT_PLTGOT] + 4, 0);
    }

  return 0;
}

static int
sh_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
{
  int sec;
  const char *name;

  switch (GELF_R_TYPE (rela->r_info))
    {
    case R_SH_NONE:
      break;
    case R_SH_RELATIVE:
      if (rela->r_addend)
	write_ne32 (dso, rela->r_offset, 0);
      break;
    case R_SH_JMP_SLOT:
      sec = addr_to_sec (dso, rela->r_offset);
      name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[sec].sh_name);
      if (sec == -1 || (strcmp (name, ".got") && strcmp (name, ".got.plt")))
	{
	  error (0, 0, "%s: R_SH_JMP_SLOT not pointing into .got section",
		 dso->filename);
	  return 1;
	}
      else
	{
	  Elf32_Addr data = read_une32 (dso, dso->shdr[sec].sh_addr + 4);

	  assert (rela->r_offset >= dso->shdr[sec].sh_addr + 12);
	  assert (((rela->r_offset - dso->shdr[sec].sh_addr) & 3) == 0);
	  write_ne32 (dso, rela->r_offset,
		      7 * (rela->r_offset - dso->shdr[sec].sh_addr - 12)
		      + data);
	}
      break;
    case R_SH_GLOB_DAT:
    case R_SH_DIR32:
    case R_SH_REL32:
      write_ne32 (dso, rela->r_offset, 0);
      break;
    case R_SH_COPY:
      if (dso->ehdr.e_type == ET_EXEC)
	/* COPY relocs are handled specially in generic code.  */
	return 0;
      error (0, 0, "%s: R_SH_COPY reloc in shared library?", dso->filename);
      return 1;
    default:
      error (0, 0, "%s: Unknown sh relocation type %d", dso->filename,
	     (int) GELF_R_TYPE (rela->r_info));
      return 1;
    }
  return 0;
}

static int
sh_reloc_size (int reloc_type)
{
  return 4;
}

static int
sh_reloc_class (int reloc_type)
{
  switch (reloc_type)
    {
    case R_SH_COPY: return RTYPE_CLASS_COPY;
    case R_SH_JMP_SLOT: return RTYPE_CLASS_PLT;
    default: return RTYPE_CLASS_VALID;
    }
}

PL_ARCH(sh) = {
  .name = "SuperH",
  .class = ELFCLASS32,
  .machine = EM_SH,
  .alternate_machine = { EM_NONE },
  .R_JMP_SLOT = R_SH_JMP_SLOT,
  .R_COPY = R_SH_COPY,
  .R_RELATIVE = R_SH_RELATIVE,
  .rtype_class_valid = RTYPE_CLASS_VALID,
  .dynamic_linker = "/lib/ld-linux.so.2",
  .adjust_dyn = sh_adjust_dyn,
  .adjust_rel = sh_adjust_rel,
  .adjust_rela = sh_adjust_rela,
  .prelink_rel = sh_prelink_rel,
  .prelink_rela = sh_prelink_rela,
  .prelink_conflict_rel = sh_prelink_conflict_rel,
  .prelink_conflict_rela = sh_prelink_conflict_rela,
  .apply_conflict_rela = sh_apply_conflict_rela,
  .apply_rel = sh_apply_rel,
  .apply_rela = sh_apply_rela,
  .rel_to_rela = sh_rel_to_rela,
  .need_rel_to_rela = sh_need_rel_to_rela,
  .reloc_size = sh_reloc_size,
  .reloc_class = sh_reloc_class,
  .max_reloc_size = 4,
  .arch_prelink = sh_arch_prelink,
  .arch_undo_prelink = sh_arch_undo_prelink,
  .undo_prelink_rela = sh_undo_prelink_rela,
  /* Although TASK_UNMAPPED_BASE is 0x29555000, we leave some
     area so that mmap of /etc/ld.so.cache and ld.so's malloc
     does not take some library's VA slot.
     Also, if this guard area isn't too small, typically
     even dlopened libraries will get the slots they desire.  */
  .mmap_base = 0x30000000,
  .mmap_end =  0x40000000,
  .max_page_size = 0x10000,
  .page_size = 0x1000
};