Blame view

tools/prelink-riscv.inc 4.12 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
42ac26f2b   Rick Chen   riscv: tools: Pre...
2
3
4
5
  /*
   * Copyright (C) 2017 Andes Technology
   * Chih-Mao Chen <cmchen@andestech.com>
   *
42ac26f2b   Rick Chen   riscv: tools: Pre...
6
7
8
9
10
11
12
13
   * Statically process runtime relocations on RISC-V ELF images
   * so that it can be directly executed when loaded at LMA
   * without fixup. Both RV32 and RV64 are supported.
   */
  
  #define CONCAT_IMPL(x, y) x##y
  #define CONCAT(x, y) CONCAT_IMPL(x, y)
  #define CONCAT3(x, y, z) CONCAT(CONCAT(x, y), z)
4539926a9   Marcus Comstedt   riscv: tools: Add...
14
  #define prelink_bonn    CONCAT3(prelink_, PRELINK_BYTEORDER, PRELINK_INC_BITS)
42ac26f2b   Rick Chen   riscv: tools: Pre...
15
  #define uintnn_t        CONCAT3(uint, PRELINK_INC_BITS, _t)
4539926a9   Marcus Comstedt   riscv: tools: Add...
16
  #define get_offset_bonn CONCAT3(get_offset_, PRELINK_BYTEORDER, PRELINK_INC_BITS)
42ac26f2b   Rick Chen   riscv: tools: Pre...
17
18
19
20
21
22
23
24
  #define Elf_Ehdr        CONCAT3(Elf, PRELINK_INC_BITS, _Ehdr)
  #define Elf_Phdr        CONCAT3(Elf, PRELINK_INC_BITS, _Phdr)
  #define Elf_Rela        CONCAT3(Elf, PRELINK_INC_BITS, _Rela)
  #define Elf_Sym         CONCAT3(Elf, PRELINK_INC_BITS, _Sym)
  #define Elf_Dyn         CONCAT3(Elf, PRELINK_INC_BITS, _Dyn)
  #define Elf_Addr        CONCAT3(Elf, PRELINK_INC_BITS, _Addr)
  #define ELF_R_TYPE      CONCAT3(ELF, PRELINK_INC_BITS, _R_TYPE)
  #define ELF_R_SYM       CONCAT3(ELF, PRELINK_INC_BITS, _R_SYM)
4539926a9   Marcus Comstedt   riscv: tools: Add...
25
26
27
28
  #define target16_to_cpu CONCAT(PRELINK_BYTEORDER, 16_to_cpu)
  #define target32_to_cpu CONCAT(PRELINK_BYTEORDER, 32_to_cpu)
  #define target64_to_cpu CONCAT(PRELINK_BYTEORDER, 64_to_cpu)
  #define targetnn_to_cpu CONCAT3(PRELINK_BYTEORDER, PRELINK_INC_BITS, _to_cpu)
71bdfcb21   Marcus Comstedt   riscv: tools: Han...
29
30
  #define cpu_to_target32 CONCAT3(cpu_to_, PRELINK_BYTEORDER, 32)
  #define cpu_to_target64 CONCAT3(cpu_to_, PRELINK_BYTEORDER, 64)
42ac26f2b   Rick Chen   riscv: tools: Pre...
31

4539926a9   Marcus Comstedt   riscv: tools: Add...
32
  static void* get_offset_bonn (void* data, Elf_Phdr* phdrs, size_t phnum, Elf_Addr addr)
42ac26f2b   Rick Chen   riscv: tools: Pre...
33
34
35
36
  {
  	Elf_Phdr *p;
  
  	for (p = phdrs; p < phdrs + phnum; ++p)
4539926a9   Marcus Comstedt   riscv: tools: Add...
37
38
  		if (targetnn_to_cpu(p->p_vaddr) <= addr && targetnn_to_cpu(p->p_vaddr) + targetnn_to_cpu(p->p_memsz) > addr)
  			return data + targetnn_to_cpu(p->p_offset) + (addr - targetnn_to_cpu(p->p_vaddr));
42ac26f2b   Rick Chen   riscv: tools: Pre...
39
40
41
  
  	return NULL;
  }
4539926a9   Marcus Comstedt   riscv: tools: Add...
42
  static void prelink_bonn(void *data)
42ac26f2b   Rick Chen   riscv: tools: Pre...
43
44
45
46
47
  {
  	Elf_Ehdr *ehdr = data;
  	Elf_Phdr *p;
  	Elf_Dyn *dyn;
  	Elf_Rela *r;
4539926a9   Marcus Comstedt   riscv: tools: Add...
48
  	if (target16_to_cpu(ehdr->e_machine) != EM_RISCV)
42ac26f2b   Rick Chen   riscv: tools: Pre...
49
  		die("Machine type is not RISC-V");
4539926a9   Marcus Comstedt   riscv: tools: Add...
50
  	Elf_Phdr *phdrs = data + targetnn_to_cpu(ehdr->e_phoff);
42ac26f2b   Rick Chen   riscv: tools: Pre...
51
52
  
  	Elf_Dyn *dyns = NULL;
4539926a9   Marcus Comstedt   riscv: tools: Add...
53
54
55
  	for (p = phdrs; p < phdrs + target16_to_cpu(ehdr->e_phnum); ++p) {
  		if (target32_to_cpu(p->p_type) == PT_DYNAMIC) {
  			dyns = data + targetnn_to_cpu(p->p_offset);
42ac26f2b   Rick Chen   riscv: tools: Pre...
56
57
58
59
60
61
62
63
64
65
66
  			break;
  		}
  	}
  
  	if (dyns == NULL)
  		die("No dynamic section found");
  
  	Elf_Rela *rela_dyn = NULL;
  	size_t rela_count = 0;
  	Elf_Sym *dynsym = NULL;
  	for (dyn = dyns;; ++dyn) {
4539926a9   Marcus Comstedt   riscv: tools: Add...
67
  		if (targetnn_to_cpu(dyn->d_tag) == DT_NULL)
42ac26f2b   Rick Chen   riscv: tools: Pre...
68
  			break;
4539926a9   Marcus Comstedt   riscv: tools: Add...
69
70
71
72
73
74
  		else if (targetnn_to_cpu(dyn->d_tag) == DT_RELA)
  			rela_dyn = get_offset_bonn(data, phdrs, target16_to_cpu(ehdr->e_phnum), + targetnn_to_cpu(dyn->d_un.d_ptr));
  		else if (targetnn_to_cpu(dyn->d_tag) == DT_RELASZ)
  		  rela_count = targetnn_to_cpu(dyn->d_un.d_val) / sizeof(Elf_Rela);
  		else if (targetnn_to_cpu(dyn->d_tag) == DT_SYMTAB)
  			dynsym = get_offset_bonn(data, phdrs, target16_to_cpu(ehdr->e_phnum), + targetnn_to_cpu(dyn->d_un.d_ptr));
42ac26f2b   Rick Chen   riscv: tools: Pre...
75
76
77
78
79
80
81
82
83
84
  
  	}
  
  	if (rela_dyn == NULL)
  		die("No .rela.dyn found");
  
  	if (dynsym == NULL)
  		die("No .dynsym found");
  
  	for (r = rela_dyn; r < rela_dyn + rela_count; ++r) {
4539926a9   Marcus Comstedt   riscv: tools: Add...
85
  		void* buf = get_offset_bonn(data, phdrs, target16_to_cpu(ehdr->e_phnum), targetnn_to_cpu(r->r_offset));
42ac26f2b   Rick Chen   riscv: tools: Pre...
86
87
88
  
  		if (buf == NULL)
  			continue;
4539926a9   Marcus Comstedt   riscv: tools: Add...
89
  		if (ELF_R_TYPE(targetnn_to_cpu(r->r_info)) == R_RISCV_RELATIVE)
42ac26f2b   Rick Chen   riscv: tools: Pre...
90
  			*((uintnn_t*) buf) = r->r_addend;
4539926a9   Marcus Comstedt   riscv: tools: Add...
91
  		else if (ELF_R_TYPE(targetnn_to_cpu(r->r_info)) == R_RISCV_32)
71bdfcb21   Marcus Comstedt   riscv: tools: Han...
92
  			*((uint32_t*) buf) = cpu_to_target32(targetnn_to_cpu(dynsym[ELF_R_SYM(targetnn_to_cpu(r->r_info))].st_value) + targetnn_to_cpu(r->r_addend));
4539926a9   Marcus Comstedt   riscv: tools: Add...
93
  		else if (ELF_R_TYPE(targetnn_to_cpu(r->r_info)) == R_RISCV_64)
71bdfcb21   Marcus Comstedt   riscv: tools: Han...
94
  			*((uint64_t*) buf) = cpu_to_target64(targetnn_to_cpu(dynsym[ELF_R_SYM(targetnn_to_cpu(r->r_info))].st_value) + targetnn_to_cpu(r->r_addend));
42ac26f2b   Rick Chen   riscv: tools: Pre...
95
96
  	}
  }
4539926a9   Marcus Comstedt   riscv: tools: Add...
97
  #undef prelink_bonn
42ac26f2b   Rick Chen   riscv: tools: Pre...
98
  #undef uintnn_t
4539926a9   Marcus Comstedt   riscv: tools: Add...
99
  #undef get_offset_bonn
42ac26f2b   Rick Chen   riscv: tools: Pre...
100
101
102
103
104
105
106
107
  #undef Elf_Ehdr
  #undef Elf_Phdr
  #undef Elf_Rela
  #undef Elf_Sym
  #undef Elf_Dyn
  #undef Elf_Addr
  #undef ELF_R_TYPE
  #undef ELF_R_SYM
4539926a9   Marcus Comstedt   riscv: tools: Add...
108
109
110
111
  #undef target16_to_cpu
  #undef target32_to_cpu
  #undef target64_to_cpu
  #undef targetnn_to_cpu
71bdfcb21   Marcus Comstedt   riscv: tools: Han...
112
113
  #undef cpu_to_target32
  #undef cpu_to_target64
42ac26f2b   Rick Chen   riscv: tools: Pre...
114
115
116
117
  
  #undef CONCAT_IMPL
  #undef CONCAT
  #undef CONCAT3