Commit | Line | Data |
---|---|---|
fcdfe9d2 HC |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2020-2022 Loongson Technology Corporation Limited | |
4 | */ | |
5 | ||
6 | #include <linux/elf.h> | |
7 | #include <linux/kernel.h> | |
8 | #include <linux/module.h> | |
28ac0a9e | 9 | #include <linux/ftrace.h> |
fcdfe9d2 | 10 | |
9151dde4 | 11 | Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val) |
59b3d4a9 XR |
12 | { |
13 | struct mod_section *got_sec = &mod->arch.got; | |
14 | int i = got_sec->num_entries; | |
9151dde4 | 15 | struct got_entry *got = get_got_entry(val, sechdrs, got_sec); |
59b3d4a9 XR |
16 | |
17 | if (got) | |
18 | return (Elf_Addr)got; | |
19 | ||
20 | /* There is no GOT entry for val yet, create a new one. */ | |
9151dde4 | 21 | got = (struct got_entry *)sechdrs[got_sec->shndx].sh_addr; |
59b3d4a9 XR |
22 | got[i] = emit_got_entry(val); |
23 | ||
24 | got_sec->num_entries++; | |
25 | if (got_sec->num_entries > got_sec->max_entries) { | |
26 | /* | |
27 | * This may happen when the module contains a GOT_HI20 without | |
28 | * a paired GOT_LO12. Such a module is broken, reject it. | |
29 | */ | |
30 | pr_err("%s: module contains bad GOT relocation\n", mod->name); | |
31 | return 0; | |
32 | } | |
33 | ||
34 | return (Elf_Addr)&got[i]; | |
35 | } | |
36 | ||
9151dde4 | 37 | Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val) |
fcdfe9d2 HC |
38 | { |
39 | int nr; | |
40 | struct mod_section *plt_sec = &mod->arch.plt; | |
41 | struct mod_section *plt_idx_sec = &mod->arch.plt_idx; | |
9151dde4 | 42 | struct plt_entry *plt = get_plt_entry(val, sechdrs, plt_sec, plt_idx_sec); |
fcdfe9d2 HC |
43 | struct plt_idx_entry *plt_idx; |
44 | ||
45 | if (plt) | |
46 | return (Elf_Addr)plt; | |
47 | ||
48 | nr = plt_sec->num_entries; | |
49 | ||
50 | /* There is no duplicate entry, create a new one */ | |
9151dde4 | 51 | plt = (struct plt_entry *)sechdrs[plt_sec->shndx].sh_addr; |
fcdfe9d2 | 52 | plt[nr] = emit_plt_entry(val); |
9151dde4 | 53 | plt_idx = (struct plt_idx_entry *)sechdrs[plt_idx_sec->shndx].sh_addr; |
fcdfe9d2 HC |
54 | plt_idx[nr] = emit_plt_idx_entry(val); |
55 | ||
56 | plt_sec->num_entries++; | |
57 | plt_idx_sec->num_entries++; | |
58 | BUG_ON(plt_sec->num_entries > plt_sec->max_entries); | |
59 | ||
60 | return (Elf_Addr)&plt[nr]; | |
61 | } | |
62 | ||
63 | static int is_rela_equal(const Elf_Rela *x, const Elf_Rela *y) | |
64 | { | |
65 | return x->r_info == y->r_info && x->r_addend == y->r_addend; | |
66 | } | |
67 | ||
68 | static bool duplicate_rela(const Elf_Rela *rela, int idx) | |
69 | { | |
70 | int i; | |
71 | ||
72 | for (i = 0; i < idx; i++) { | |
73 | if (is_rela_equal(&rela[i], &rela[idx])) | |
74 | return true; | |
75 | } | |
76 | ||
77 | return false; | |
78 | } | |
79 | ||
59b3d4a9 XR |
80 | static void count_max_entries(Elf_Rela *relas, int num, |
81 | unsigned int *plts, unsigned int *gots) | |
fcdfe9d2 HC |
82 | { |
83 | unsigned int i, type; | |
84 | ||
85 | for (i = 0; i < num; i++) { | |
86 | type = ELF_R_TYPE(relas[i].r_info); | |
9bd1e380 XR |
87 | switch (type) { |
88 | case R_LARCH_SOP_PUSH_PLT_PCREL: | |
89 | case R_LARCH_B26: | |
fcdfe9d2 HC |
90 | if (!duplicate_rela(relas, i)) |
91 | (*plts)++; | |
9bd1e380 | 92 | break; |
59b3d4a9 XR |
93 | case R_LARCH_GOT_PC_HI20: |
94 | if (!duplicate_rela(relas, i)) | |
95 | (*gots)++; | |
96 | break; | |
9bd1e380 XR |
97 | default: |
98 | break; /* Do nothing. */ | |
fcdfe9d2 HC |
99 | } |
100 | } | |
101 | } | |
102 | ||
103 | int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, | |
104 | char *secstrings, struct module *mod) | |
105 | { | |
59b3d4a9 | 106 | unsigned int i, num_plts = 0, num_gots = 0; |
28ac0a9e | 107 | Elf_Shdr *got_sec, *plt_sec, *plt_idx_sec, *tramp = NULL; |
fcdfe9d2 HC |
108 | |
109 | /* | |
110 | * Find the empty .plt sections. | |
111 | */ | |
112 | for (i = 0; i < ehdr->e_shnum; i++) { | |
59b3d4a9 | 113 | if (!strcmp(secstrings + sechdrs[i].sh_name, ".got")) |
9151dde4 | 114 | mod->arch.got.shndx = i; |
59b3d4a9 | 115 | else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt")) |
9151dde4 | 116 | mod->arch.plt.shndx = i; |
fcdfe9d2 | 117 | else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt.idx")) |
9151dde4 | 118 | mod->arch.plt_idx.shndx = i; |
28ac0a9e QZ |
119 | else if (!strcmp(secstrings + sechdrs[i].sh_name, ".ftrace_trampoline")) |
120 | tramp = sechdrs + i; | |
fcdfe9d2 HC |
121 | } |
122 | ||
9151dde4 | 123 | if (!mod->arch.got.shndx) { |
59b3d4a9 XR |
124 | pr_err("%s: module GOT section(s) missing\n", mod->name); |
125 | return -ENOEXEC; | |
126 | } | |
9151dde4 | 127 | if (!mod->arch.plt.shndx) { |
fcdfe9d2 HC |
128 | pr_err("%s: module PLT section(s) missing\n", mod->name); |
129 | return -ENOEXEC; | |
130 | } | |
9151dde4 | 131 | if (!mod->arch.plt_idx.shndx) { |
fcdfe9d2 HC |
132 | pr_err("%s: module PLT.IDX section(s) missing\n", mod->name); |
133 | return -ENOEXEC; | |
134 | } | |
135 | ||
136 | /* Calculate the maxinum number of entries */ | |
137 | for (i = 0; i < ehdr->e_shnum; i++) { | |
138 | int num_rela = sechdrs[i].sh_size / sizeof(Elf_Rela); | |
139 | Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset; | |
140 | Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info; | |
141 | ||
142 | if (sechdrs[i].sh_type != SHT_RELA) | |
143 | continue; | |
144 | ||
145 | /* ignore relocations that operate on non-exec sections */ | |
146 | if (!(dst_sec->sh_flags & SHF_EXECINSTR)) | |
147 | continue; | |
148 | ||
59b3d4a9 | 149 | count_max_entries(relas, num_rela, &num_plts, &num_gots); |
fcdfe9d2 HC |
150 | } |
151 | ||
9151dde4 HC |
152 | got_sec = sechdrs + mod->arch.got.shndx; |
153 | got_sec->sh_type = SHT_NOBITS; | |
154 | got_sec->sh_flags = SHF_ALLOC; | |
155 | got_sec->sh_addralign = L1_CACHE_BYTES; | |
156 | got_sec->sh_size = (num_gots + 1) * sizeof(struct got_entry); | |
59b3d4a9 XR |
157 | mod->arch.got.num_entries = 0; |
158 | mod->arch.got.max_entries = num_gots; | |
159 | ||
9151dde4 HC |
160 | plt_sec = sechdrs + mod->arch.plt.shndx; |
161 | plt_sec->sh_type = SHT_NOBITS; | |
162 | plt_sec->sh_flags = SHF_EXECINSTR | SHF_ALLOC; | |
163 | plt_sec->sh_addralign = L1_CACHE_BYTES; | |
164 | plt_sec->sh_size = (num_plts + 1) * sizeof(struct plt_entry); | |
fcdfe9d2 HC |
165 | mod->arch.plt.num_entries = 0; |
166 | mod->arch.plt.max_entries = num_plts; | |
167 | ||
9151dde4 HC |
168 | plt_idx_sec = sechdrs + mod->arch.plt_idx.shndx; |
169 | plt_idx_sec->sh_type = SHT_NOBITS; | |
170 | plt_idx_sec->sh_flags = SHF_ALLOC; | |
171 | plt_idx_sec->sh_addralign = L1_CACHE_BYTES; | |
172 | plt_idx_sec->sh_size = (num_plts + 1) * sizeof(struct plt_idx_entry); | |
fcdfe9d2 HC |
173 | mod->arch.plt_idx.num_entries = 0; |
174 | mod->arch.plt_idx.max_entries = num_plts; | |
175 | ||
28ac0a9e QZ |
176 | if (tramp) { |
177 | tramp->sh_type = SHT_NOBITS; | |
178 | tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC; | |
179 | tramp->sh_addralign = __alignof__(struct plt_entry); | |
180 | tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry); | |
181 | } | |
182 | ||
fcdfe9d2 HC |
183 | return 0; |
184 | } |