From patchwork Tue Feb 27 18:48:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776277 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308728wro; Tue, 27 Feb 2024 10:50:24 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWX2N9Eq5Wzy/xhTgrSUQRySV74NpPwFOKLzl+mC1QYO6GaZdTjBcPoqauk5q3XEROoMcXv/r3tEiZ+BkGABG63 X-Google-Smtp-Source: AGHT+IEd5TRk+SDPFFEqPKjh0P4jDQ+cDy+nROqKDh0aqESO32y4sXUIO0BBQ20IBAItDmh2fdkp X-Received: by 2002:a05:6214:2b0c:b0:68f:e19a:7633 with SMTP id jx12-20020a0562142b0c00b0068fe19a7633mr4129817qvb.50.1709059823826; Tue, 27 Feb 2024 10:50:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059823; cv=none; d=google.com; s=arc-20160816; b=bCGDJ3k90068bCv/uTJUvQC8pX2b5xG3Q5kNftKyE6aJHJFRQ9JnDXSBjyYL3Yjb+E Q3kYS9HX2RQhchOrnQENFYaMwdcxc48C41Yvo6fbyO/IfcNw1Y7jc6yzn7VO8DnskSn6 tzPc25h3FMZZgHf/Q3UqCHh37cffiQEiqXDP27L4WHCRZctz0n5QPvSVJObYwnkzYVtb dXs2RI+l5gtsLfiy2Wyb0BSCf3+yoHSSsDVaKlGx/7mR12aRjMlu7etGvPBCjY/iE56G INDLwM6ah80+XUODrx7nvSBVyFlTXkrzoa/3sMu4eAl/hkN7NA9XYqjuPb6nOJRa4VKN 8GVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WPZgpp/cCi7t1E47Pxo1jS8aeNgwNI8GgVuo9d5SLyc=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=BeGF3mxSM6HuUfSOrr4ZSiS5iNDyvMcqu0pEu80JTrsXDh+aoIq99ZG+Fcihq4nyjl t6O2P8o2jUA4tHqrr04vUpiOjVNfjOwYgShKN/fetMLwP0D5HQgvFnSxi8bDMxQeAvLH hneH6CQHD26cPRpdD8lZNjL0mo1SKF37d7xA4APkhyxU8pIDZ3PXsHVvQWpFp1ArxmqD a0iBoHEiAV1QZDwzFp7H7ewGs/wZmZ9etEscMOex6mV1eSDB40fk63uHjcyRHm9QGvBd PhOsWsISVYV5LsOkUtm+bpq1khd3vlu9qNEeD9M+FyWJYXqAa+zpzDz7OCYUwVDBhTGs 9URQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D1czXhJ6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d5-20020ac85345000000b0042e78c37d6csi7091619qto.318.2024.02.27.10.50.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:23 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D1czXhJ6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2Vc-0006Zl-2J; Tue, 27 Feb 2024 13:48:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2Va-0006ZP-1W for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:54 -0500 Received: from mail-oi1-x231.google.com ([2607:f8b0:4864:20::231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VX-0001VN-GQ for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:53 -0500 Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3c19bc08f96so2645611b6e.2 for ; Tue, 27 Feb 2024 10:48:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059730; x=1709664530; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WPZgpp/cCi7t1E47Pxo1jS8aeNgwNI8GgVuo9d5SLyc=; b=D1czXhJ6QU+VZohBRAcyBBcPjE1DJEf8IiKz+Ng2jlX2W2ixIU3JhM2sIlUk5i4qLQ QmrDib3d5eE2lf/jRJoE6y/uKvtode5pZRsBbJgm6bXqCu3L6gRvNcOaK8wDf7i/yB2/ zfevhoFt6iQgj43+QFRrWUaZLeI/JIla8y59w2FImSyIysMWanM3Sk3O+SpJn8ujOyK9 fL8YleYkFhPOXqrvhiOXe8SUq2PSXePAPjlbVWNGHUw2CFPK7c1O0cVBTjpW5zIhLei5 mWZNFP1ItJrmZuUw89zEtp9hyZaoIw/Kv0n1UT/hdrd7qKU8T1Fpgc9BTSqLFerLRpcn dMYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059730; x=1709664530; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WPZgpp/cCi7t1E47Pxo1jS8aeNgwNI8GgVuo9d5SLyc=; b=NEhNg4yZU6S+d84zjNJdcxXToP4Jd/thF0cfLAo/7+IgYGiMJJc+xGNTs3fHnm4Lun DhX+ewprJTPESHtKOC03yC013Z+ZY/zLg3rpjQNCWNl13BaZ36/TmLiiW0Z6txG1Xini xCjiMVNusURhMy4i54uCfAWRcy6avlhYRNnJzCLOoukfVQTeA0OjgGIJoCPPQ6jrwmcL 1KhbN1ADSMOHoxzdeHwl+cXpBdpPxe8Xzs0+xt8zCgWeaseSBYazOuoDlJRRKnWl8SRE mgM1D2OYVXWD9Fjz2NIBQJ5lZcFyMU7C/LfF05jdXA5b3gOykoIhDY1zvJ+6V8bW/2TI BECg== X-Gm-Message-State: AOJu0YwV+YCdTexgsc9Q7EzfEfK2PsCTjWNS5Znde4N+jse1XyuTTUvm jOqSrYM6oZmbfXtbthqjGl88dkhWU2J9HAYT0KjQnL2ODqpsRM8tA5V9qHHSHCcENOG4v+KaSEr w X-Received: by 2002:a05:6808:1143:b0:3c1:9a60:9248 with SMTP id u3-20020a056808114300b003c19a609248mr3180500oiu.14.1709059730367; Tue, 27 Feb 2024 10:48:50 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:49 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 10/14] linux-user/elfload: Write corefile elf header in one block Date: Tue, 27 Feb 2024 08:48:29 -1000 Message-Id: <20240227184833.193836-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::231; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x231.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Fixes a bug in which write_note() wrote namesz_rounded and datasz_rounded bytes, even though name and data pointers contain only the unrounded number of bytes. Instead of many small writes, allocate a block to contain all of the elf headers and all of the notes. Copy the data into the block piecemeal and the write it to the file as a chunk. This also avoids the need to lseek forward for alignment. --- linux-user/elfload.c | 410 ++++++++++++------------------------------- 1 file changed, 113 insertions(+), 297 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index bad01bd2ef..b8d07d8054 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4002,18 +4002,6 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) * Example for ARM target is provided in this file. */ -/* An ELF note in memory */ -struct memelfnote { - const char *name; - size_t namesz; - size_t namesz_rounded; - int type; - size_t datasz; - size_t datasz_rounded; - void *data; - size_t notesz; -}; - struct target_elf_siginfo { abi_int si_signo; /* signal number */ abi_int si_code; /* extra code */ @@ -4053,40 +4041,6 @@ struct target_elf_prpsinfo { char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ }; -/* Here is the structure in which status of each thread is captured. */ -struct elf_thread_status { - QTAILQ_ENTRY(elf_thread_status) ets_link; - struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ -#if 0 - elf_fpregset_t fpu; /* NT_PRFPREG */ - struct task_struct *thread; - elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */ -#endif - struct memelfnote notes[1]; - int num_notes; -}; - -#define NUMNOTES 3 - -struct elf_note_info { - struct memelfnote notes[NUMNOTES]; - struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ - struct target_elf_prpsinfo psinfo; /* NT_PRPSINFO */ - - QTAILQ_HEAD(, elf_thread_status) thread_list; -#if 0 - /* - * Current version of ELF coredump doesn't support - * dumping fp regs etc. - */ - elf_fpregset_t *fpu; - elf_fpxregset_t *xfpu; - int thread_status_size; -#endif - int notes_size; - int numnote; -}; - struct vm_area_struct { target_ulong vma_start; /* start vaddr of memory region */ target_ulong vma_end; /* end vaddr of memory region */ @@ -4110,22 +4064,6 @@ static abi_ulong vma_dump_size(const struct vm_area_struct *); static int vma_walker(void *priv, target_ulong start, target_ulong end, unsigned long flags); -static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t); -static void fill_note(struct memelfnote *, const char *, int, - unsigned int, void *); -static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int); -static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *); -static void fill_auxv_note(struct memelfnote *, const TaskState *); -static void fill_elf_note_phdr(struct elf_phdr *, int, off_t); -static size_t note_size(const struct memelfnote *); -static void free_note_info(struct elf_note_info *); -static void fill_note_info(struct elf_note_info *, int, const CPUArchState *); -static void fill_thread_info(struct elf_note_info *, const CPUArchState *); - -static int dump_write(int, const void *, size_t); -static int write_note(struct memelfnote *, int); -static int write_note_info(struct elf_note_info *, int); - #ifdef BSWAP_NEEDED static void bswap_prstatus(struct target_elf_prstatus *prstatus) { @@ -4280,35 +4218,32 @@ static size_t size_note(const char *name, size_t datasz) return sizeof(struct elf_note) + namesz + datasz; } -static void fill_note(struct memelfnote *note, const char *name, int type, - unsigned int sz, void *data) +static void *fill_note(void **pptr, int type, const char *name, size_t datasz) { - unsigned int namesz; + void *ptr = *pptr; + struct elf_note *n = ptr; + size_t namesz = strlen(name) + 1; - namesz = strlen(name) + 1; - note->name = name; - note->namesz = namesz; - note->namesz_rounded = roundup(namesz, sizeof (int32_t)); - note->type = type; - note->datasz = sz; - note->datasz_rounded = roundup(sz, sizeof (int32_t)); + n->n_namesz = namesz; + n->n_descsz = datasz; + n->n_type = type; + bswap_note(n); - note->data = data; + ptr += sizeof(*n); + memcpy(ptr, name, namesz); - /* - * We calculate rounded up note size here as specified by - * ELF document. - */ - note->notesz = sizeof (struct elf_note) + - note->namesz_rounded + note->datasz_rounded; + namesz = ROUND_UP(namesz, 4); + datasz = ROUND_UP(datasz, 4); + + *pptr = ptr + namesz + datasz; + return ptr + namesz; } static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, uint32_t flags) { - (void) memset(elf, 0, sizeof(*elf)); + memcpy(elf->e_ident, ELFMAG, SELFMAG); - (void) memcpy(elf->e_ident, ELFMAG, SELFMAG); elf->e_ident[EI_CLASS] = ELF_CLASS; elf->e_ident[EI_DATA] = ELF_DATA; elf->e_ident[EI_VERSION] = EV_CURRENT; @@ -4326,95 +4261,79 @@ static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, bswap_ehdr(elf); } -static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset) +static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset) { phdr->p_type = PT_NOTE; phdr->p_offset = offset; - phdr->p_vaddr = 0; - phdr->p_paddr = 0; phdr->p_filesz = sz; - phdr->p_memsz = 0; - phdr->p_flags = 0; - phdr->p_align = 0; bswap_phdr(phdr, 1); } -static size_t note_size(const struct memelfnote *note) +static void fill_prstatus_note(void *data, const TaskState *ts, + CPUState *cpu, int signr) { - return (note->notesz); + /* + * Because note memory is only aligned to 4, and target_elf_prstatus + * may well have higher alignment requirements, fill locally and + * memcpy to the destination afterward. + */ + struct target_elf_prstatus prstatus = { + .pr_info.si_signo = signr, + .pr_cursig = signr, + .pr_pid = ts->ts_tid, + .pr_ppid = getppid(), + .pr_pgrp = getpgrp(), + .pr_sid = getsid(0), + }; + + elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu)); + bswap_prstatus(&prstatus); + memcpy(data, &prstatus, sizeof(prstatus)); } -static void fill_prstatus(struct target_elf_prstatus *prstatus, - const TaskState *ts, int signr) -{ - (void) memset(prstatus, 0, sizeof (*prstatus)); - prstatus->pr_info.si_signo = prstatus->pr_cursig = signr; - prstatus->pr_pid = ts->ts_tid; - prstatus->pr_ppid = getppid(); - prstatus->pr_pgrp = getpgrp(); - prstatus->pr_sid = getsid(0); - - bswap_prstatus(prstatus); -} - -static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts) +static void fill_prpsinfo_note(void *data, const TaskState *ts) { + /* + * Because note memory is only aligned to 4, and target_elf_prpsinfo + * may well have higher alignment requirements, fill locally and + * memcpy to the destination afterward. + */ + struct target_elf_prpsinfo psinfo; char *base_filename; - unsigned int i, len; - - (void) memset(psinfo, 0, sizeof (*psinfo)); + size_t len; len = ts->info->env_strings - ts->info->arg_strings; - if (len >= ELF_PRARGSZ) - len = ELF_PRARGSZ - 1; - if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) { - return -EFAULT; + len = MIN(len, ELF_PRARGSZ); + memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len); + for (size_t i = 0; i < len; i++) { + if (psinfo.pr_psargs[i] == 0) { + psinfo.pr_psargs[i] = ' '; + } } - for (i = 0; i < len; i++) - if (psinfo->pr_psargs[i] == 0) - psinfo->pr_psargs[i] = ' '; - psinfo->pr_psargs[len] = 0; - psinfo->pr_pid = getpid(); - psinfo->pr_ppid = getppid(); - psinfo->pr_pgrp = getpgrp(); - psinfo->pr_sid = getsid(0); - psinfo->pr_uid = getuid(); - psinfo->pr_gid = getgid(); + psinfo.pr_pid = getpid(); + psinfo.pr_ppid = getppid(); + psinfo.pr_pgrp = getpgrp(); + psinfo.pr_sid = getsid(0); + psinfo.pr_uid = getuid(); + psinfo.pr_gid = getgid(); base_filename = g_path_get_basename(ts->bprm->filename); /* * Using strncpy here is fine: at max-length, * this field is not NUL-terminated. */ - (void) strncpy(psinfo->pr_fname, base_filename, - sizeof(psinfo->pr_fname)); - + strncpy(psinfo.pr_fname, base_filename, sizeof(psinfo.pr_fname)); g_free(base_filename); - bswap_psinfo(psinfo); - return (0); + + bswap_psinfo(&psinfo); + memcpy(data, &psinfo, sizeof(psinfo)); } -static void fill_auxv_note(struct memelfnote *note, const TaskState *ts) +static void fill_auxv_note(void *data, const TaskState *ts) { - elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv; - elf_addr_t orig_auxv = auxv; - void *ptr; - int len = ts->info->auxv_len; - - /* - * Auxiliary vector is stored in target process stack. It contains - * {type, value} pairs that we need to dump into note. This is not - * strictly necessary but we do it here for sake of completeness. - */ - - /* read in whole auxv vector and copy it to memelfnote */ - ptr = lock_user(VERIFY_READ, orig_auxv, len, 0); - if (ptr != NULL) { - fill_note(note, "CORE", NT_AUXV, len, ptr); - unlock_user(ptr, auxv, len); - } + memcpy(data, g2h_untagged(ts->info->saved_auxv), ts->info->auxv_len); } /* @@ -4462,111 +4381,6 @@ static int dump_write(int fd, const void *ptr, size_t size) return (0); } -static int write_note(struct memelfnote *men, int fd) -{ - struct elf_note en; - - en.n_namesz = men->namesz; - en.n_type = men->type; - en.n_descsz = men->datasz; - - bswap_note(&en); - - if (dump_write(fd, &en, sizeof(en)) != 0) - return (-1); - if (dump_write(fd, men->name, men->namesz_rounded) != 0) - return (-1); - if (dump_write(fd, men->data, men->datasz_rounded) != 0) - return (-1); - - return (0); -} - -static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env) -{ - CPUState *cpu = env_cpu((CPUArchState *)env); - TaskState *ts = (TaskState *)cpu->opaque; - struct elf_thread_status *ets; - - ets = g_malloc0(sizeof (*ets)); - ets->num_notes = 1; /* only prstatus is dumped */ - fill_prstatus(&ets->prstatus, ts, 0); - elf_core_copy_regs(&ets->prstatus.pr_reg, env); - fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus), - &ets->prstatus); - - QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link); - - info->notes_size += note_size(&ets->notes[0]); -} - -static void fill_note_info(struct elf_note_info *info, - int signr, const CPUArchState *env) -{ - CPUState *cpu = env_cpu((CPUArchState *)env); - TaskState *ts = (TaskState *)cpu->opaque; - - memset(info, 0, sizeof (*info)); - QTAILQ_INIT(&info->thread_list); - - /* - * First fill in status (and registers) of current thread - * including process info & aux vector. - */ - fill_prstatus(&info->prstatus, ts, signr); - elf_core_copy_regs(&info->prstatus.pr_reg, env); - fill_note(&info->notes[0], "CORE", NT_PRSTATUS, - sizeof(info->prstatus), &info->prstatus); - fill_psinfo(&info->psinfo, ts); - fill_note(&info->notes[1], "CORE", NT_PRPSINFO, - sizeof(info->psinfo), &info->psinfo); - fill_auxv_note(&info->notes[2], ts); - info->numnote = 3; - - info->notes_size = 0; - for (int i = 0; i < info->numnote; i++) { - info->notes_size += note_size(&info->notes[i]); - } - - /* read and fill status of all threads */ - CPU_FOREACH(cpu) { - if (cpu == thread_cpu) { - continue; - } - fill_thread_info(info, cpu_env(cpu)); - } -} - -static void free_note_info(struct elf_note_info *info) -{ - struct elf_thread_status *ets; - - while (!QTAILQ_EMPTY(&info->thread_list)) { - ets = QTAILQ_FIRST(&info->thread_list); - QTAILQ_REMOVE(&info->thread_list, ets, ets_link); - g_free(ets); - } -} - -static int write_note_info(struct elf_note_info *info, int fd) -{ - struct elf_thread_status *ets; - int i, error = 0; - - /* write prstatus, psinfo and auxv for current thread */ - for (i = 0; i < info->numnote; i++) - if ((error = write_note(&info->notes[i], fd)) != 0) - return (error); - - /* write prstatus for each thread */ - QTAILQ_FOREACH(ets, &info->thread_list, ets_link) { - if ((error = write_note(&ets->notes[0], fd)) != 0) - return (error); - } - - return (0); -} - /* * Write out ELF coredump. * @@ -4615,14 +4429,13 @@ static int elf_core_dump(int signr, const CPUArchState *env) const CPUState *cpu = env_cpu((CPUArchState *)env); const TaskState *ts = (const TaskState *)cpu->opaque; struct vm_area_struct *vma; - struct elf_note_info info; - struct elfhdr elf; - struct elf_phdr phdr; struct rlimit dumpsize; struct mm_struct mm; off_t offset, note_offset, data_offset; + size_t note_size; int segs, cpus, ret; int fd = -1; + CPUState *cpu_iter; if (prctl(PR_GET_DUMPABLE) == 0) { return 0; @@ -4644,7 +4457,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) segs = vma_get_mapping_count(&mm); cpus = 0; - CPU_FOREACH(cpu) { + CPU_FOREACH(cpu_iter) { cpus++; } @@ -4655,6 +4468,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) offset += size_note("CORE", ts->info->auxv_len); offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; + note_size = offset - note_offset; offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); data_offset = offset; @@ -4678,61 +4492,64 @@ static int elf_core_dump(int signr, const CPUArchState *env) } /* - * Construct valid coredump ELF header. We also - * add one more segment for notes. + * There is a fair amount of alignment padding within the notes + * as well as preceeding the process memory. Allocate a zeroed + * block to hold it all. Write all of the headers directly into + * this buffer and then write it out as a block. */ - fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0); - if (dump_write(fd, &elf, sizeof (elf)) != 0) - goto out; + { + g_autofree void *header = g_malloc0(data_offset); + void *hptr, *dptr; - /* fill in the in-memory version of notes */ - fill_note_info(&info, signr, env); + /* Create elf file header. */ + hptr = header; + fill_elf_header(hptr, segs + 1, ELF_MACHINE, 0); + hptr += sizeof(struct elfhdr); - /* write out notes program header */ - fill_elf_note_phdr(&phdr, info.notes_size, note_offset); + /* Create elf program headers. */ + fill_elf_note_phdr(hptr, note_size, note_offset); + hptr += sizeof(struct elf_phdr); - if (dump_write(fd, &phdr, sizeof (phdr)) != 0) - goto out; + offset = data_offset; + for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { + struct elf_phdr *phdr = hptr; - /* - * Write program headers for memory regions mapped in - * the target process. - */ - offset = data_offset; - for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { - (void) memset(&phdr, 0, sizeof (phdr)); + phdr->p_type = PT_LOAD; + phdr->p_offset = offset; + phdr->p_vaddr = vma->vma_start; + phdr->p_paddr = 0; + phdr->p_filesz = vma_dump_size(vma); + offset += phdr->p_filesz; + phdr->p_memsz = vma->vma_end - vma->vma_start; + phdr->p_flags = (vma->vma_flags & PROT_READ ? PF_R : 0) + | (vma->vma_flags & PROT_WRITE ? PF_W : 0) + | (vma->vma_flags & PROT_EXEC ? PF_X : 0); + phdr->p_align = ELF_EXEC_PAGESIZE; - phdr.p_type = PT_LOAD; - phdr.p_offset = offset; - phdr.p_vaddr = vma->vma_start; - phdr.p_paddr = 0; - phdr.p_filesz = vma_dump_size(vma); - offset += phdr.p_filesz; - phdr.p_memsz = vma->vma_end - vma->vma_start; - phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0; - if (vma->vma_flags & PROT_WRITE) - phdr.p_flags |= PF_W; - if (vma->vma_flags & PROT_EXEC) - phdr.p_flags |= PF_X; - phdr.p_align = ELF_EXEC_PAGESIZE; + bswap_phdr(phdr, 1); + hptr += sizeof(struct elf_phdr); + } - bswap_phdr(&phdr, 1); - if (dump_write(fd, &phdr, sizeof(phdr)) != 0) { + /* Create the notes. */ + dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); + fill_auxv_note(dptr, ts); + + dptr = fill_note(&hptr, NT_PRPSINFO, "CORE", + sizeof(struct target_elf_prpsinfo)); + fill_prpsinfo_note(dptr, ts); + + CPU_FOREACH(cpu_iter) { + dptr = fill_note(&hptr, NT_PRSTATUS, "CORE", + sizeof(struct target_elf_prstatus)); + fill_prstatus_note(dptr, ts, cpu_iter, + cpu_iter == cpu ? signr : 0); + } + + if (dump_write(fd, header, data_offset) < 0) { goto out; } } - /* - * Next we write notes just after program headers. No - * alignment needed here. - */ - if (write_note_info(&info, fd) < 0) - goto out; - - /* align data to page boundary */ - if (lseek(fd, data_offset, SEEK_SET) != data_offset) - goto out; - /* * Finally we can dump process memory into corefile as well. */ @@ -4768,7 +4585,6 @@ static int elf_core_dump(int signr, const CPUArchState *env) ret = -errno; mmap_unlock(); cpu_list_unlock(); - free_note_info(&info); vma_delete(&mm); close(fd); return ret;