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