2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * Copyright (C) 2017 Zihao Yu
15 #include <linux/elf.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
18 #include <linux/moduleloader.h>
20 static int apply_r_riscv_64_rela(struct module
*me
, u32
*location
, Elf_Addr v
)
26 static int apply_r_riscv_branch_rela(struct module
*me
, u32
*location
,
29 s64 offset
= (void *)v
- (void *)location
;
30 u32 imm12
= (offset
& 0x1000) << (31 - 12);
31 u32 imm11
= (offset
& 0x800) >> (11 - 7);
32 u32 imm10_5
= (offset
& 0x7e0) << (30 - 10);
33 u32 imm4_1
= (offset
& 0x1e) << (11 - 4);
35 *location
= (*location
& 0x1fff07f) | imm12
| imm11
| imm10_5
| imm4_1
;
39 static int apply_r_riscv_jal_rela(struct module
*me
, u32
*location
,
42 s64 offset
= (void *)v
- (void *)location
;
43 u32 imm20
= (offset
& 0x100000) << (31 - 20);
44 u32 imm19_12
= (offset
& 0xff000);
45 u32 imm11
= (offset
& 0x800) << (20 - 11);
46 u32 imm10_1
= (offset
& 0x7fe) << (30 - 10);
48 *location
= (*location
& 0xfff) | imm20
| imm19_12
| imm11
| imm10_1
;
52 static int apply_r_riscv_pcrel_hi20_rela(struct module
*me
, u32
*location
,
55 s64 offset
= (void *)v
- (void *)location
;
58 if (offset
!= (s32
)offset
) {
60 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
61 me
->name
, v
, location
);
65 hi20
= (offset
+ 0x800) & 0xfffff000;
66 *location
= (*location
& 0xfff) | hi20
;
70 static int apply_r_riscv_pcrel_lo12_i_rela(struct module
*me
, u32
*location
,
74 * v is the lo12 value to fill. It is calculated before calling this
77 *location
= (*location
& 0xfffff) | ((v
& 0xfff) << 20);
81 static int apply_r_riscv_pcrel_lo12_s_rela(struct module
*me
, u32
*location
,
85 * v is the lo12 value to fill. It is calculated before calling this
88 u32 imm11_5
= (v
& 0xfe0) << (31 - 11);
89 u32 imm4_0
= (v
& 0x1f) << (11 - 4);
91 *location
= (*location
& 0x1fff07f) | imm11_5
| imm4_0
;
95 static int apply_r_riscv_call_plt_rela(struct module
*me
, u32
*location
,
98 s64 offset
= (void *)v
- (void *)location
;
102 if (offset
!= fill_v
) {
104 "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
105 me
->name
, v
, location
);
109 hi20
= (offset
+ 0x800) & 0xfffff000;
110 lo12
= (offset
- hi20
) & 0xfff;
111 *location
= (*location
& 0xfff) | hi20
;
112 *(location
+ 1) = (*(location
+ 1) & 0xfffff) | (lo12
<< 20);
116 static int apply_r_riscv_relax_rela(struct module
*me
, u32
*location
,
122 static int (*reloc_handlers_rela
[]) (struct module
*me
, u32
*location
,
124 [R_RISCV_64
] = apply_r_riscv_64_rela
,
125 [R_RISCV_BRANCH
] = apply_r_riscv_branch_rela
,
126 [R_RISCV_JAL
] = apply_r_riscv_jal_rela
,
127 [R_RISCV_PCREL_HI20
] = apply_r_riscv_pcrel_hi20_rela
,
128 [R_RISCV_PCREL_LO12_I
] = apply_r_riscv_pcrel_lo12_i_rela
,
129 [R_RISCV_PCREL_LO12_S
] = apply_r_riscv_pcrel_lo12_s_rela
,
130 [R_RISCV_CALL_PLT
] = apply_r_riscv_call_plt_rela
,
131 [R_RISCV_RELAX
] = apply_r_riscv_relax_rela
,
134 int apply_relocate_add(Elf_Shdr
*sechdrs
, const char *strtab
,
135 unsigned int symindex
, unsigned int relsec
,
138 Elf_Rela
*rel
= (void *) sechdrs
[relsec
].sh_addr
;
139 int (*handler
)(struct module
*me
, u32
*location
, Elf_Addr v
);
142 unsigned int i
, type
;
146 pr_debug("Applying relocate section %u to %u\n", relsec
,
147 sechdrs
[relsec
].sh_info
);
149 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
150 /* This is where to make the change */
151 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
153 /* This is the symbol it is referring to */
154 sym
= (Elf_Sym
*)sechdrs
[symindex
].sh_addr
155 + ELF_RISCV_R_SYM(rel
[i
].r_info
);
156 if (IS_ERR_VALUE(sym
->st_value
)) {
157 /* Ignore unresolved weak symbol */
158 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
)
160 pr_warning("%s: Unknown symbol %s\n",
161 me
->name
, strtab
+ sym
->st_name
);
165 type
= ELF_RISCV_R_TYPE(rel
[i
].r_info
);
167 if (type
< ARRAY_SIZE(reloc_handlers_rela
))
168 handler
= reloc_handlers_rela
[type
];
173 pr_err("%s: Unknown relocation type %u\n",
178 v
= sym
->st_value
+ rel
[i
].r_addend
;
180 if (type
== R_RISCV_PCREL_LO12_I
|| type
== R_RISCV_PCREL_LO12_S
) {
183 for (j
= 0; j
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); j
++) {
185 sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
187 /* Find the corresponding HI20 PC-relative relocation entry */
188 if (hi20_loc
== sym
->st_value
) {
190 (Elf_Sym
*)sechdrs
[symindex
].sh_addr
191 + ELF_RISCV_R_SYM(rel
[j
].r_info
);
196 s64 offset
= hi20_sym_val
- hi20_loc
;
197 s32 hi20
= (offset
+ 0x800) & 0xfffff000;
198 s32 lo12
= offset
- hi20
;
203 if (j
== sechdrs
[relsec
].sh_size
/ sizeof(*rel
)) {
205 "%s: Can not find HI20 PC-relative relocation information\n",
211 res
= handler(me
, location
, v
);