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