1 /* SPDX-License-Identifier: GPL-2.0 2 * 3 * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org> 4 * 5 * Copyright (C) 2018 Andes Technology Corporation <zong@andestech.com> 6 */ 7 8 #include <linux/elf.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 12 u64 module_emit_got_entry(struct module *mod, u64 val) 13 { 14 struct mod_section *got_sec = &mod->arch.got; 15 int i = got_sec->num_entries; 16 struct got_entry *got = get_got_entry(val, got_sec); 17 18 if (got) 19 return (u64)got; 20 21 /* There is no duplicate entry, create a new one */ 22 got = (struct got_entry *)got_sec->shdr->sh_addr; 23 got[i] = emit_got_entry(val); 24 25 got_sec->num_entries++; 26 BUG_ON(got_sec->num_entries > got_sec->max_entries); 27 28 return (u64)&got[i]; 29 } 30 31 u64 module_emit_plt_entry(struct module *mod, u64 val) 32 { 33 struct mod_section *plt_sec = &mod->arch.plt; 34 struct plt_entry *plt = get_plt_entry(val, plt_sec); 35 int i = plt_sec->num_entries; 36 37 if (plt) 38 return (u64)plt; 39 40 /* There is no duplicate entry, create a new one */ 41 plt = (struct plt_entry *)plt_sec->shdr->sh_addr; 42 plt[i] = emit_plt_entry(val); 43 44 plt_sec->num_entries++; 45 BUG_ON(plt_sec->num_entries > plt_sec->max_entries); 46 47 return (u64)&plt[i]; 48 } 49 50 static int is_rela_equal(const Elf64_Rela *x, const Elf64_Rela *y) 51 { 52 return x->r_info == y->r_info && x->r_addend == y->r_addend; 53 } 54 55 static bool duplicate_rela(const Elf64_Rela *rela, int idx) 56 { 57 int i; 58 for (i = 0; i < idx; i++) { 59 if (is_rela_equal(&rela[i], &rela[idx])) 60 return true; 61 } 62 return false; 63 } 64 65 static void count_max_entries(Elf64_Rela *relas, int num, 66 unsigned int *plts, unsigned int *gots) 67 { 68 unsigned int type, i; 69 70 for (i = 0; i < num; i++) { 71 type = ELF64_R_TYPE(relas[i].r_info); 72 if (type == R_RISCV_CALL_PLT) { 73 if (!duplicate_rela(relas, i)) 74 (*plts)++; 75 } else if (type == R_RISCV_GOT_HI20) { 76 if (!duplicate_rela(relas, i)) 77 (*gots)++; 78 } 79 } 80 } 81 82 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, 83 char *secstrings, struct module *mod) 84 { 85 unsigned int num_plts = 0; 86 unsigned int num_gots = 0; 87 int i; 88 89 /* 90 * Find the empty .got and .plt sections. 91 */ 92 for (i = 0; i < ehdr->e_shnum; i++) { 93 if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt")) 94 mod->arch.plt.shdr = sechdrs + i; 95 else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got")) 96 mod->arch.got.shdr = sechdrs + i; 97 } 98 99 if (!mod->arch.plt.shdr) { 100 pr_err("%s: module PLT section(s) missing\n", mod->name); 101 return -ENOEXEC; 102 } 103 if (!mod->arch.got.shdr) { 104 pr_err("%s: module GOT section(s) missing\n", mod->name); 105 return -ENOEXEC; 106 } 107 108 /* Calculate the maxinum number of entries */ 109 for (i = 0; i < ehdr->e_shnum; i++) { 110 Elf64_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset; 111 int num_rela = sechdrs[i].sh_size / sizeof(Elf64_Rela); 112 Elf64_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info; 113 114 if (sechdrs[i].sh_type != SHT_RELA) 115 continue; 116 117 /* ignore relocations that operate on non-exec sections */ 118 if (!(dst_sec->sh_flags & SHF_EXECINSTR)) 119 continue; 120 121 count_max_entries(relas, num_rela, &num_plts, &num_gots); 122 } 123 124 mod->arch.plt.shdr->sh_type = SHT_NOBITS; 125 mod->arch.plt.shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC; 126 mod->arch.plt.shdr->sh_addralign = L1_CACHE_BYTES; 127 mod->arch.plt.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_entry); 128 mod->arch.plt.num_entries = 0; 129 mod->arch.plt.max_entries = num_plts; 130 131 mod->arch.got.shdr->sh_type = SHT_NOBITS; 132 mod->arch.got.shdr->sh_flags = SHF_ALLOC; 133 mod->arch.got.shdr->sh_addralign = L1_CACHE_BYTES; 134 mod->arch.got.shdr->sh_size = (num_gots + 1) * sizeof(struct got_entry); 135 mod->arch.got.num_entries = 0; 136 mod->arch.got.max_entries = num_gots; 137 138 return 0; 139 } 140