1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 Elf_Addr module_emit_got_entry(struct module *mod, Elf_Addr val)
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);
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);
23 got_sec->num_entries++;
24 if (got_sec->num_entries > got_sec->max_entries) {
26 * This may happen when the module contains a GOT_HI20 without
27 * a paired GOT_LO12. Such a module is broken, reject it.
29 pr_err("%s: module contains bad GOT relocation\n", mod->name);
33 return (Elf_Addr)&got[i];
36 Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Addr val)
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;
47 nr = plt_sec->num_entries;
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);
55 plt_sec->num_entries++;
56 plt_idx_sec->num_entries++;
57 BUG_ON(plt_sec->num_entries > plt_sec->max_entries);
59 return (Elf_Addr)&plt[nr];
62 static int is_rela_equal(const Elf_Rela *x, const Elf_Rela *y)
64 return x->r_info == y->r_info && x->r_addend == y->r_addend;
67 static bool duplicate_rela(const Elf_Rela *rela, int idx)
71 for (i = 0; i < idx; i++) {
72 if (is_rela_equal(&rela[i], &rela[idx]))
79 static void count_max_entries(Elf_Rela *relas, int num,
80 unsigned int *plts, unsigned int *gots)
84 for (i = 0; i < num; i++) {
85 type = ELF_R_TYPE(relas[i].r_info);
87 case R_LARCH_SOP_PUSH_PLT_PCREL:
89 if (!duplicate_rela(relas, i))
92 case R_LARCH_GOT_PC_HI20:
93 if (!duplicate_rela(relas, i))
97 break; /* Do nothing. */
102 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
103 char *secstrings, struct module *mod)
105 unsigned int i, num_plts = 0, num_gots = 0;
108 * Find the empty .plt sections.
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;
119 if (!mod->arch.got.shdr) {
120 pr_err("%s: module GOT section(s) missing\n", mod->name);
123 if (!mod->arch.plt.shdr) {
124 pr_err("%s: module PLT section(s) missing\n", mod->name);
127 if (!mod->arch.plt_idx.shdr) {
128 pr_err("%s: module PLT.IDX section(s) missing\n", mod->name);
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;
138 if (sechdrs[i].sh_type != SHT_RELA)
141 /* ignore relocations that operate on non-exec sections */
142 if (!(dst_sec->sh_flags & SHF_EXECINSTR))
145 count_max_entries(relas, num_rela, &num_plts, &num_gots);
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;
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;
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;