18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Imagination Technologies
48c2ecf20Sopenharmony_ci * Author: Alex Smith <alex.smith@imgtec.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_cistatic inline bool FUNC(patch_vdso)(const char *path, void *vdso)
88c2ecf20Sopenharmony_ci{
98c2ecf20Sopenharmony_ci	const ELF(Ehdr) *ehdr = vdso;
108c2ecf20Sopenharmony_ci	void *shdrs;
118c2ecf20Sopenharmony_ci	ELF(Shdr) *shdr;
128c2ecf20Sopenharmony_ci	char *shstrtab, *name;
138c2ecf20Sopenharmony_ci	uint16_t sh_count, sh_entsize, i;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	shdrs = vdso + FUNC(swap_uint)(ehdr->e_shoff);
168c2ecf20Sopenharmony_ci	sh_count = swap_uint16(ehdr->e_shnum);
178c2ecf20Sopenharmony_ci	sh_entsize = swap_uint16(ehdr->e_shentsize);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	shdr = shdrs + (sh_entsize * swap_uint16(ehdr->e_shstrndx));
208c2ecf20Sopenharmony_ci	shstrtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	for (i = 0; i < sh_count; i++) {
238c2ecf20Sopenharmony_ci		shdr = shdrs + (i * sh_entsize);
248c2ecf20Sopenharmony_ci		name = shstrtab + swap_uint32(shdr->sh_name);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci		/*
278c2ecf20Sopenharmony_ci		 * Ensure there are no relocation sections - ld.so does not
288c2ecf20Sopenharmony_ci		 * relocate the VDSO so if there are relocations things will
298c2ecf20Sopenharmony_ci		 * break.
308c2ecf20Sopenharmony_ci		 */
318c2ecf20Sopenharmony_ci		switch (swap_uint32(shdr->sh_type)) {
328c2ecf20Sopenharmony_ci		case SHT_REL:
338c2ecf20Sopenharmony_ci		case SHT_RELA:
348c2ecf20Sopenharmony_ci			fprintf(stderr,
358c2ecf20Sopenharmony_ci				"%s: '%s' contains relocation sections\n",
368c2ecf20Sopenharmony_ci				program_name, path);
378c2ecf20Sopenharmony_ci			return false;
388c2ecf20Sopenharmony_ci		}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci		/* Check for existing sections. */
418c2ecf20Sopenharmony_ci		if (strcmp(name, ".MIPS.abiflags") == 0) {
428c2ecf20Sopenharmony_ci			fprintf(stderr,
438c2ecf20Sopenharmony_ci				"%s: '%s' already contains a '.MIPS.abiflags' section\n",
448c2ecf20Sopenharmony_ci				program_name, path);
458c2ecf20Sopenharmony_ci			return false;
468c2ecf20Sopenharmony_ci		}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci		if (strcmp(name, ".mips_abiflags") == 0) {
498c2ecf20Sopenharmony_ci			strcpy(name, ".MIPS.abiflags");
508c2ecf20Sopenharmony_ci			shdr->sh_type = swap_uint32(SHT_MIPS_ABIFLAGS);
518c2ecf20Sopenharmony_ci			shdr->sh_entsize = shdr->sh_size;
528c2ecf20Sopenharmony_ci		}
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return true;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline bool FUNC(get_symbols)(const char *path, void *vdso)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	const ELF(Ehdr) *ehdr = vdso;
618c2ecf20Sopenharmony_ci	void *shdrs, *symtab;
628c2ecf20Sopenharmony_ci	ELF(Shdr) *shdr;
638c2ecf20Sopenharmony_ci	const ELF(Sym) *sym;
648c2ecf20Sopenharmony_ci	char *strtab, *name;
658c2ecf20Sopenharmony_ci	uint16_t sh_count, sh_entsize, st_count, st_entsize, i, j;
668c2ecf20Sopenharmony_ci	uint64_t offset;
678c2ecf20Sopenharmony_ci	uint32_t flags;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	shdrs = vdso + FUNC(swap_uint)(ehdr->e_shoff);
708c2ecf20Sopenharmony_ci	sh_count = swap_uint16(ehdr->e_shnum);
718c2ecf20Sopenharmony_ci	sh_entsize = swap_uint16(ehdr->e_shentsize);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	for (i = 0; i < sh_count; i++) {
748c2ecf20Sopenharmony_ci		shdr = shdrs + (i * sh_entsize);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		if (swap_uint32(shdr->sh_type) == SHT_SYMTAB)
778c2ecf20Sopenharmony_ci			break;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (i == sh_count) {
818c2ecf20Sopenharmony_ci		fprintf(stderr, "%s: '%s' has no symbol table\n", program_name,
828c2ecf20Sopenharmony_ci			path);
838c2ecf20Sopenharmony_ci		return false;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* Get flags */
878c2ecf20Sopenharmony_ci	flags = swap_uint32(ehdr->e_flags);
888c2ecf20Sopenharmony_ci	if (elf_class == ELFCLASS64)
898c2ecf20Sopenharmony_ci		elf_abi = ABI_N64;
908c2ecf20Sopenharmony_ci	else if (flags & EF_MIPS_ABI2)
918c2ecf20Sopenharmony_ci		elf_abi = ABI_N32;
928c2ecf20Sopenharmony_ci	else
938c2ecf20Sopenharmony_ci		elf_abi = ABI_O32;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* Get symbol table. */
968c2ecf20Sopenharmony_ci	symtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
978c2ecf20Sopenharmony_ci	st_entsize = FUNC(swap_uint)(shdr->sh_entsize);
988c2ecf20Sopenharmony_ci	st_count = FUNC(swap_uint)(shdr->sh_size) / st_entsize;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* Get string table. */
1018c2ecf20Sopenharmony_ci	shdr = shdrs + (swap_uint32(shdr->sh_link) * sh_entsize);
1028c2ecf20Sopenharmony_ci	strtab = vdso + FUNC(swap_uint)(shdr->sh_offset);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* Write offsets for symbols needed by the kernel. */
1058c2ecf20Sopenharmony_ci	for (i = 0; vdso_symbols[i].name; i++) {
1068c2ecf20Sopenharmony_ci		if (!(vdso_symbols[i].abis & elf_abi))
1078c2ecf20Sopenharmony_ci			continue;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		for (j = 0; j < st_count; j++) {
1108c2ecf20Sopenharmony_ci			sym = symtab + (j * st_entsize);
1118c2ecf20Sopenharmony_ci			name = strtab + swap_uint32(sym->st_name);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci			if (!strcmp(name, vdso_symbols[i].name)) {
1148c2ecf20Sopenharmony_ci				offset = FUNC(swap_uint)(sym->st_value);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci				fprintf(out_file,
1178c2ecf20Sopenharmony_ci					"\t.%s = 0x%" PRIx64 ",\n",
1188c2ecf20Sopenharmony_ci					vdso_symbols[i].offset_name, offset);
1198c2ecf20Sopenharmony_ci				break;
1208c2ecf20Sopenharmony_ci			}
1218c2ecf20Sopenharmony_ci		}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		if (j == st_count) {
1248c2ecf20Sopenharmony_ci			fprintf(stderr,
1258c2ecf20Sopenharmony_ci				"%s: '%s' is missing required symbol '%s'\n",
1268c2ecf20Sopenharmony_ci				program_name, path, vdso_symbols[i].name);
1278c2ecf20Sopenharmony_ci			return false;
1288c2ecf20Sopenharmony_ci		}
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return true;
1328c2ecf20Sopenharmony_ci}
133