Path: blob/master/arch/loongarch/kernel/module-sections.c
26424 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2020-2022 Loongson Technology Corporation Limited3*/45#include <linux/elf.h>6#include <linux/kernel.h>7#include <linux/module.h>8#include <linux/moduleloader.h>9#include <linux/ftrace.h>10#include <linux/sort.h>1112Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val)13{14struct mod_section *got_sec = &mod->arch.got;15int i = got_sec->num_entries;16struct got_entry *got = get_got_entry(val, sechdrs, got_sec);1718if (got)19return (Elf_Addr)got;2021/* There is no GOT entry for val yet, create a new one. */22got = (struct got_entry *)sechdrs[got_sec->shndx].sh_addr;23got[i] = emit_got_entry(val);2425got_sec->num_entries++;26if (got_sec->num_entries > got_sec->max_entries) {27/*28* This may happen when the module contains a GOT_HI20 without29* a paired GOT_LO12. Such a module is broken, reject it.30*/31pr_err("%s: module contains bad GOT relocation\n", mod->name);32return 0;33}3435return (Elf_Addr)&got[i];36}3738Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val)39{40int nr;41struct mod_section *plt_sec = &mod->arch.plt;42struct mod_section *plt_idx_sec = &mod->arch.plt_idx;43struct plt_entry *plt = get_plt_entry(val, sechdrs, plt_sec, plt_idx_sec);44struct plt_idx_entry *plt_idx;4546if (plt)47return (Elf_Addr)plt;4849nr = plt_sec->num_entries;5051/* There is no duplicate entry, create a new one */52plt = (struct plt_entry *)sechdrs[plt_sec->shndx].sh_addr;53plt[nr] = emit_plt_entry(val);54plt_idx = (struct plt_idx_entry *)sechdrs[plt_idx_sec->shndx].sh_addr;55plt_idx[nr] = emit_plt_idx_entry(val);5657plt_sec->num_entries++;58plt_idx_sec->num_entries++;59BUG_ON(plt_sec->num_entries > plt_sec->max_entries);6061return (Elf_Addr)&plt[nr];62}6364#define cmp_3way(a, b) ((a) < (b) ? -1 : (a) > (b))6566static int compare_rela(const void *x, const void *y)67{68int ret;69const Elf_Rela *rela_x = x, *rela_y = y;7071ret = cmp_3way(rela_x->r_info, rela_y->r_info);72if (ret == 0)73ret = cmp_3way(rela_x->r_addend, rela_y->r_addend);7475return ret;76}7778static void count_max_entries(Elf_Rela *relas, int num,79unsigned int *plts, unsigned int *gots)80{81unsigned int i;8283sort(relas, num, sizeof(Elf_Rela), compare_rela, NULL);8485for (i = 0; i < num; i++) {86if (i && !compare_rela(&relas[i-1], &relas[i]))87continue;8889switch (ELF_R_TYPE(relas[i].r_info)) {90case R_LARCH_SOP_PUSH_PLT_PCREL:91case R_LARCH_B26:92(*plts)++;93break;94case R_LARCH_GOT_PC_HI20:95(*gots)++;96break;97default:98break; /* Do nothing. */99}100}101}102103int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,104char *secstrings, struct module *mod)105{106unsigned int i, num_plts = 0, num_gots = 0;107Elf_Shdr *got_sec, *plt_sec, *plt_idx_sec, *tramp = NULL;108109/*110* Find the empty .plt sections.111*/112for (i = 0; i < ehdr->e_shnum; i++) {113if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))114mod->arch.got.shndx = i;115else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))116mod->arch.plt.shndx = i;117else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt.idx"))118mod->arch.plt_idx.shndx = i;119else if (!strcmp(secstrings + sechdrs[i].sh_name, ".ftrace_trampoline"))120tramp = sechdrs + i;121}122123if (!mod->arch.got.shndx) {124pr_err("%s: module GOT section(s) missing\n", mod->name);125return -ENOEXEC;126}127if (!mod->arch.plt.shndx) {128pr_err("%s: module PLT section(s) missing\n", mod->name);129return -ENOEXEC;130}131if (!mod->arch.plt_idx.shndx) {132pr_err("%s: module PLT.IDX section(s) missing\n", mod->name);133return -ENOEXEC;134}135136/* Calculate the maxinum number of entries */137for (i = 0; i < ehdr->e_shnum; i++) {138int num_rela = sechdrs[i].sh_size / sizeof(Elf_Rela);139Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;140Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;141142if (sechdrs[i].sh_type != SHT_RELA)143continue;144145/* ignore relocations that operate on non-exec sections */146if (!(dst_sec->sh_flags & SHF_EXECINSTR))147continue;148149count_max_entries(relas, num_rela, &num_plts, &num_gots);150}151152got_sec = sechdrs + mod->arch.got.shndx;153got_sec->sh_type = SHT_NOBITS;154got_sec->sh_flags = SHF_ALLOC;155got_sec->sh_addralign = L1_CACHE_BYTES;156got_sec->sh_size = (num_gots + 1) * sizeof(struct got_entry);157mod->arch.got.num_entries = 0;158mod->arch.got.max_entries = num_gots;159160plt_sec = sechdrs + mod->arch.plt.shndx;161plt_sec->sh_type = SHT_NOBITS;162plt_sec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;163plt_sec->sh_addralign = L1_CACHE_BYTES;164plt_sec->sh_size = (num_plts + 1) * sizeof(struct plt_entry);165mod->arch.plt.num_entries = 0;166mod->arch.plt.max_entries = num_plts;167168plt_idx_sec = sechdrs + mod->arch.plt_idx.shndx;169plt_idx_sec->sh_type = SHT_NOBITS;170plt_idx_sec->sh_flags = SHF_ALLOC;171plt_idx_sec->sh_addralign = L1_CACHE_BYTES;172plt_idx_sec->sh_size = (num_plts + 1) * sizeof(struct plt_idx_entry);173mod->arch.plt_idx.num_entries = 0;174mod->arch.plt_idx.max_entries = num_plts;175176if (tramp) {177tramp->sh_type = SHT_NOBITS;178tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;179tramp->sh_addralign = __alignof__(struct plt_entry);180tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry);181}182183return 0;184}185186187