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