arch/riscv/kernel/module-sections.c

Source file repositories/reference/linux-study-clean/arch/riscv/kernel/module-sections.c

File Facts

System
Linux kernel
Corpus path
arch/riscv/kernel/module-sections.c
Extension
.c
Size
5976 bytes
Lines
212
Domain
Architecture Layer
Bucket
arch/riscv
Inferred role
Architecture Layer: implementation source
Status
source implementation candidate

Why This File Exists

CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.

Dependency Surface

Detected Declarations

Annotated Snippet

switch (ELF_R_TYPE(relas[i].r_info)) {
		case R_RISCV_CALL_PLT:
		case R_RISCV_PLT32:
			(*plts)++;
			break;
		case R_RISCV_GOT_HI20:
			(*gots)++;
			break;
		default:
			unreachable();
		}
	}
}

static bool rela_needs_plt_got_entry(const Elf_Rela *rela)
{
	switch (ELF_R_TYPE(rela->r_info)) {
	case R_RISCV_CALL_PLT:
	case R_RISCV_GOT_HI20:
	case R_RISCV_PLT32:
		return true;
	default:
		return false;
	}
}

int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
			      char *secstrings, struct module *mod)
{
	size_t num_scratch_relas = 0;
	unsigned int num_plts = 0;
	unsigned int num_gots = 0;
	Elf_Rela *scratch = NULL;
	Elf_Rela *new_scratch;
	size_t scratch_size = 0;
	int i;

	/*
	 * Find the empty .got and .plt sections.
	 */
	for (i = 0; i < ehdr->e_shnum; i++) {
		if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
			mod->arch.plt.shdr = sechdrs + i;
		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
			mod->arch.got.shdr = sechdrs + i;
		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got.plt"))
			mod->arch.got_plt.shdr = sechdrs + i;
	}

	if (!mod->arch.plt.shdr) {
		pr_err("%s: module PLT section(s) missing\n", mod->name);
		return -ENOEXEC;
	}
	if (!mod->arch.got.shdr) {
		pr_err("%s: module GOT section(s) missing\n", mod->name);
		return -ENOEXEC;
	}
	if (!mod->arch.got_plt.shdr) {
		pr_err("%s: module GOT.PLT section(s) missing\n", mod->name);
		return -ENOEXEC;
	}

	/* Calculate the maximum number of entries */
	for (i = 0; i < ehdr->e_shnum; i++) {
		size_t num_relas = sechdrs[i].sh_size / sizeof(Elf_Rela);
		Elf_Rela *relas = (void *)ehdr + sechdrs[i].sh_offset;
		Elf_Shdr *dst_sec = sechdrs + sechdrs[i].sh_info;
		size_t scratch_size_needed;

		if (sechdrs[i].sh_type != SHT_RELA)
			continue;

		/* ignore relocations that operate on non-exec sections */
		if (!(dst_sec->sh_flags & SHF_EXECINSTR))
			continue;

		/*
		 * apply_relocate_add() relies on HI20 and LO12 relocation pairs being
		 * close together, so sort a copy of the section to avoid interfering.
		 */
		scratch_size_needed = (num_scratch_relas + num_relas) * sizeof(*scratch);
		if (scratch_size_needed > scratch_size) {
			scratch_size = scratch_size_needed;
			new_scratch = kvrealloc(scratch, scratch_size, GFP_KERNEL);
			if (!new_scratch) {
				kvfree(scratch);
				return -ENOMEM;
			}
			scratch = new_scratch;
		}

Annotation

Implementation Notes