1
0
Files
kernel-49/arch/mips/vdso/genvdso.c
Greg Kroah-Hartman 85df2cd9ff Merge 4.9.235 into android-4.9-q
Changes in 4.9.235
	bonding: fix a potential double-unregister
	bonding: show saner speed for broadcast mode
	net: Fix potential wrong skb->protocol in skb_vlan_untag()
	tipc: fix uninit skb->data in tipc_nl_compat_dumpit()
	ipvlan: fix device features
	gre6: Fix reception with IP6_TNL_F_RCV_DSCP_COPY
	ALSA: pci: delete repeated words in comments
	ASoC: tegra: Fix reference count leaks.
	arm64: dts: qcom: msm8916: Pull down PDM GPIOs during sleep
	media: pci: ttpci: av7110: fix possible buffer overflow caused by bad DMA value in debiirq()
	scsi: target: tcmu: Fix crash on ARM during cmd completion
	iommu/iova: Don't BUG on invalid PFNs
	drm/amdkfd: Fix reference count leaks.
	drm/radeon: fix multiple reference count leak
	drm/amdgpu: fix ref count leak in amdgpu_driver_open_kms
	drm/amd/display: fix ref count leak in amdgpu_drm_ioctl
	drm/amdgpu: fix ref count leak in amdgpu_display_crtc_set_config
	drm/amdgpu/display: fix ref count leak when pm_runtime_get_sync fails
	scsi: lpfc: Fix shost refcount mismatch when deleting vport
	selftests/powerpc: Purge extra count_pmc() calls of ebb selftests
	omapfb: fix multiple reference count leaks due to pm_runtime_get_sync
	PCI: Fix pci_create_slot() reference count leak
	rtlwifi: rtl8192cu: Prevent leaking urb
	mips/vdso: Fix resource leaks in genvdso.c
	cec-api: prevent leaking memory through hole in structure
	drm/nouveau/drm/noveau: fix reference count leak in nouveau_fbcon_open
	drm/nouveau: Fix reference count leak in nouveau_connector_detect
	locking/lockdep: Fix overflow in presentation of average lock-time
	scsi: iscsi: Do not put host in iscsi_set_flashnode_param()
	ceph: fix potential mdsc use-after-free crash
	scsi: fcoe: Memory leak fix in fcoe_sysfs_fcf_del()
	EDAC/ie31200: Fallback if host bridge device is already initialized
	media: davinci: vpif_capture: fix potential double free
	KVM: arm64: Fix symbol dependency in __hyp_call_panic_nvhe
	powerpc/spufs: add CONFIG_COREDUMP dependency
	USB: sisusbvga: Fix a potential UB casued by left shifting a negative value
	efi: provide empty efi_enter_virtual_mode implementation
	Revert "ath10k: fix DMA related firmware crashes on multiple devices"
	i2c: rcar: in slave mode, clear NACK earlier
	usb: gadget: f_tcm: Fix some resource leaks in some error paths
	jbd2: make sure jh have b_transaction set in refile/unfile_buffer
	jbd2: abort journal if free a async write error metadata buffer
	fs: prevent BUG_ON in submit_bh_wbc()
	s390/cio: add cond_resched() in the slow_eval_known_fn() loop
	scsi: ufs: Fix possible infinite loop in ufshcd_hold
	scsi: ufs: Improve interrupt handling for shared interrupts
	net: gianfar: Add of_node_put() before goto statement
	powerpc/perf: Fix soft lockups due to missed interrupt accounting
	HID: i2c-hid: Always sleep 60ms after I2C_HID_PWR_ON commands
	btrfs: fix space cache memory leak after transaction abort
	fbcon: prevent user font height or width change from causing potential out-of-bounds access
	USB: lvtest: return proper error code in probe
	vt: defer kfree() of vc_screenbuf in vc_do_resize()
	vt_ioctl: change VT_RESIZEX ioctl to check for error return from vc_resize()
	serial: samsung: Removes the IRQ not found warning
	serial: pl011: Fix oops on -EPROBE_DEFER
	serial: pl011: Don't leak amba_ports entry on driver register error
	serial: 8250: change lock order in serial8250_do_startup()
	writeback: Protect inode->i_io_list with inode->i_lock
	writeback: Avoid skipping inode writeback
	writeback: Fix sync livelock due to b_dirty_time processing
	XEN uses irqdesc::irq_data_common::handler_data to store a per interrupt XEN data pointer which contains XEN specific information.
	xhci: Do warm-reset when both CAS and XDEV_RESUME are set
	PM: sleep: core: Fix the handling of pending runtime resume requests
	device property: Fix the secondary firmware node handling in set_primary_fwnode()
	USB: yurex: Fix bad gfp argument
	usb: uas: Add quirk for PNY Pro Elite
	USB: quirks: Add no-lpm quirk for another Raydium touchscreen
	USB: Ignore UAS for JMicron JMS567 ATA/ATAPI Bridge
	usb: host: ohci-exynos: Fix error handling in exynos_ohci_probe()
	overflow.h: Add allocation size calculation helpers
	USB: gadget: u_f: add overflow checks to VLA macros
	USB: gadget: f_ncm: add bounds checks to ncm_unwrap_ntb()
	USB: gadget: u_f: Unbreak offset calculation in VLAs
	usb: storage: Add unusual_uas entry for Sony PSZ drives
	btrfs: check the right error variable in btrfs_del_dir_entries_in_log
	HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
	ALSA: usb-audio: Update documentation comment for MS2109 quirk
	Linux 4.9.235

Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
Change-Id: I06c5a72573ab236503da67fc4c730d52d9d73eb4
2020-09-09 21:40:03 +03:00

304 lines
7.5 KiB
C

/*
* Copyright (C) 2015 Imagination Technologies
* Author: Alex Smith <alex.smith@imgtec.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
/*
* This tool is used to generate the real VDSO images from the raw image. It
* first patches up the MIPS ABI flags and GNU attributes sections defined in
* elf.S to have the correct name and type. It then generates a C source file
* to be compiled into the kernel containing the VDSO image data and a
* mips_vdso_image struct for it, including symbol offsets extracted from the
* image.
*
* We need to be passed both a stripped and unstripped VDSO image. The stripped
* image is compiled into the kernel, but we must also patch up the unstripped
* image's ABI flags sections so that it can be installed and used for
* debugging.
*/
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <byteswap.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Define these in case the system elf.h is not new enough to have them. */
#ifndef SHT_GNU_ATTRIBUTES
# define SHT_GNU_ATTRIBUTES 0x6ffffff5
#endif
#ifndef SHT_MIPS_ABIFLAGS
# define SHT_MIPS_ABIFLAGS 0x7000002a
#endif
enum {
ABI_O32 = (1 << 0),
ABI_N32 = (1 << 1),
ABI_N64 = (1 << 2),
ABI_ALL = ABI_O32 | ABI_N32 | ABI_N64,
};
/* Symbols the kernel requires offsets for. */
static struct {
const char *name;
const char *offset_name;
unsigned int abis;
} vdso_symbols[] = {
{ "__vdso_sigreturn", "off_sigreturn", ABI_O32 },
{ "__vdso_rt_sigreturn", "off_rt_sigreturn", ABI_ALL },
{}
};
static const char *program_name;
static const char *vdso_name;
static unsigned char elf_class;
static unsigned int elf_abi;
static bool need_swap;
static FILE *out_file;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define HOST_ORDER ELFDATA2LSB
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define HOST_ORDER ELFDATA2MSB
#endif
#define BUILD_SWAP(bits) \
static uint##bits##_t swap_uint##bits(uint##bits##_t val) \
{ \
return need_swap ? bswap_##bits(val) : val; \
}
BUILD_SWAP(16)
BUILD_SWAP(32)
BUILD_SWAP(64)
#define __FUNC(name, bits) name##bits
#define _FUNC(name, bits) __FUNC(name, bits)
#define FUNC(name) _FUNC(name, ELF_BITS)
#define __ELF(x, bits) Elf##bits##_##x
#define _ELF(x, bits) __ELF(x, bits)
#define ELF(x) _ELF(x, ELF_BITS)
/*
* Include genvdso.h twice with ELF_BITS defined differently to get functions
* for both ELF32 and ELF64.
*/
#define ELF_BITS 64
#include "genvdso.h"
#undef ELF_BITS
#define ELF_BITS 32
#include "genvdso.h"
#undef ELF_BITS
static void *map_vdso(const char *path, size_t *_size)
{
int fd;
struct stat stat;
void *addr;
const Elf32_Ehdr *ehdr;
fd = open(path, O_RDWR);
if (fd < 0) {
fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name,
path, strerror(errno));
return NULL;
}
if (fstat(fd, &stat) != 0) {
fprintf(stderr, "%s: Failed to stat '%s': %s\n", program_name,
path, strerror(errno));
close(fd);
return NULL;
}
addr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
0);
if (addr == MAP_FAILED) {
fprintf(stderr, "%s: Failed to map '%s': %s\n", program_name,
path, strerror(errno));
close(fd);
return NULL;
}
/* ELF32/64 header formats are the same for the bits we're checking. */
ehdr = addr;
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
fprintf(stderr, "%s: '%s' is not an ELF file\n", program_name,
path);
close(fd);
return NULL;
}
elf_class = ehdr->e_ident[EI_CLASS];
switch (elf_class) {
case ELFCLASS32:
case ELFCLASS64:
break;
default:
fprintf(stderr, "%s: '%s' has invalid ELF class\n",
program_name, path);
close(fd);
return NULL;
}
switch (ehdr->e_ident[EI_DATA]) {
case ELFDATA2LSB:
case ELFDATA2MSB:
need_swap = ehdr->e_ident[EI_DATA] != HOST_ORDER;
break;
default:
fprintf(stderr, "%s: '%s' has invalid ELF data order\n",
program_name, path);
close(fd);
return NULL;
}
if (swap_uint16(ehdr->e_machine) != EM_MIPS) {
fprintf(stderr,
"%s: '%s' has invalid ELF machine (expected EM_MIPS)\n",
program_name, path);
close(fd);
return NULL;
} else if (swap_uint16(ehdr->e_type) != ET_DYN) {
fprintf(stderr,
"%s: '%s' has invalid ELF type (expected ET_DYN)\n",
program_name, path);
close(fd);
return NULL;
}
*_size = stat.st_size;
close(fd);
return addr;
}
static bool patch_vdso(const char *path, void *vdso)
{
if (elf_class == ELFCLASS64)
return patch_vdso64(path, vdso);
else
return patch_vdso32(path, vdso);
}
static bool get_symbols(const char *path, void *vdso)
{
if (elf_class == ELFCLASS64)
return get_symbols64(path, vdso);
else
return get_symbols32(path, vdso);
}
int main(int argc, char **argv)
{
const char *dbg_vdso_path, *vdso_path, *out_path;
void *dbg_vdso, *vdso;
size_t dbg_vdso_size, vdso_size, i;
program_name = argv[0];
if (argc < 4 || argc > 5) {
fprintf(stderr,
"Usage: %s <debug VDSO> <stripped VDSO> <output file> [<name>]\n",
program_name);
return EXIT_FAILURE;
}
dbg_vdso_path = argv[1];
vdso_path = argv[2];
out_path = argv[3];
vdso_name = (argc > 4) ? argv[4] : "";
dbg_vdso = map_vdso(dbg_vdso_path, &dbg_vdso_size);
if (!dbg_vdso)
return EXIT_FAILURE;
vdso = map_vdso(vdso_path, &vdso_size);
if (!vdso)
return EXIT_FAILURE;
/* Patch both the VDSOs' ABI flags sections. */
if (!patch_vdso(dbg_vdso_path, dbg_vdso))
return EXIT_FAILURE;
if (!patch_vdso(vdso_path, vdso))
return EXIT_FAILURE;
if (msync(dbg_vdso, dbg_vdso_size, MS_SYNC) != 0) {
fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
dbg_vdso_path, strerror(errno));
return EXIT_FAILURE;
} else if (msync(vdso, vdso_size, MS_SYNC) != 0) {
fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
vdso_path, strerror(errno));
return EXIT_FAILURE;
}
out_file = fopen(out_path, "w");
if (!out_file) {
fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name,
out_path, strerror(errno));
return EXIT_FAILURE;
}
fprintf(out_file, "/* Automatically generated - do not edit */\n");
fprintf(out_file, "#include <linux/linkage.h>\n");
fprintf(out_file, "#include <linux/mm.h>\n");
fprintf(out_file, "#include <asm/vdso.h>\n");
/* Write out the stripped VDSO data. */
fprintf(out_file,
"static unsigned char vdso_data[PAGE_ALIGN(%zu)] __page_aligned_data = {\n\t",
vdso_size);
for (i = 0; i < vdso_size; i++) {
if (!(i % 10))
fprintf(out_file, "\n\t");
fprintf(out_file, "0x%02x, ", ((unsigned char *)vdso)[i]);
}
fprintf(out_file, "\n};\n");
/* Preallocate a page array. */
fprintf(out_file,
"static struct page *vdso_pages[PAGE_ALIGN(%zu) / PAGE_SIZE];\n",
vdso_size);
fprintf(out_file, "struct mips_vdso_image vdso_image%s%s = {\n",
(vdso_name[0]) ? "_" : "", vdso_name);
fprintf(out_file, "\t.data = vdso_data,\n");
fprintf(out_file, "\t.size = PAGE_ALIGN(%zu),\n", vdso_size);
fprintf(out_file, "\t.mapping = {\n");
fprintf(out_file, "\t\t.name = \"[vdso]\",\n");
fprintf(out_file, "\t\t.pages = vdso_pages,\n");
fprintf(out_file, "\t},\n");
/* Calculate and write symbol offsets to <output file> */
if (!get_symbols(dbg_vdso_path, dbg_vdso)) {
unlink(out_path);
fclose(out_file);
return EXIT_FAILURE;
}
fprintf(out_file, "};\n");
fclose(out_file);
return EXIT_SUCCESS;
}