From nobody Tue Nov 26 08:25:51 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1709060125; cv=none; d=zohomail.com; s=zohoarc; b=eBMxWcPlLjyS96DK5Id28WEpLMkioZXa8FyCeEWg5NAU1xLwDbJUSGiDLOPj9GysaGRHTXijJp/fO03lYXqErpUHhMwaKQRHRXYwOX4//a7Rh/NbY/azaC6E97+G+k5O7U9r2vUB38Kf33ZZKnQ20rPvVlHe431g62cZa8q2h+g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1709060125; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WPZgpp/cCi7t1E47Pxo1jS8aeNgwNI8GgVuo9d5SLyc=; b=nZE5GshgIMknA48SZ+foigTEM+a7I3eev/R49xlXlLEdFOxQuT+vRe1/XyM4ujlYDMBr1E0Cda10+I2hLnjYStF8P55+MQt1M5yHm53MqmxamtZGIWVY2YlIlcIF4lC/fgE4nRS3axAta98oT/wEsC3aUuBwBhtCS7l8AgsSgw0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1709060125033140.17155792208086; Tue, 27 Feb 2024 10:55:25 -0800 (PST) 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) 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) 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-Google-Smtp-Source: AGHT+IFZJ5s1qna3W00Ikj4U3dZqN1eIIEqd1xMGOKz3hqlWzcwfzAWLqMhdV9dBKEhgfM64aGxIdw== 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) 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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1709060126342100001 Content-Type: text/plain; charset="utf-8" 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, struc= t image_info *info) * Example for ARM target is provided in this file. */ =20 -/* 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 */ }; =20 -/* 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); =20 -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 da= tasz) return sizeof(struct elf_note) + namesz + datasz; } =20 -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 dat= asz) { - unsigned int namesz; + void *ptr =3D *pptr; + struct elf_note *n =3D ptr; + size_t namesz =3D strlen(name) + 1; =20 - namesz =3D strlen(name) + 1; - note->name =3D name; - note->namesz =3D namesz; - note->namesz_rounded =3D roundup(namesz, sizeof (int32_t)); - note->type =3D type; - note->datasz =3D sz; - note->datasz_rounded =3D roundup(sz, sizeof (int32_t)); + n->n_namesz =3D namesz; + n->n_descsz =3D datasz; + n->n_type =3D type; + bswap_note(n); =20 - note->data =3D data; + ptr +=3D sizeof(*n); + memcpy(ptr, name, namesz); =20 - /* - * We calculate rounded up note size here as specified by - * ELF document. - */ - note->notesz =3D sizeof (struct elf_note) + - note->namesz_rounded + note->datasz_rounded; + namesz =3D ROUND_UP(namesz, 4); + datasz =3D ROUND_UP(datasz, 4); + + *pptr =3D ptr + namesz + datasz; + return ptr + namesz; } =20 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); =20 - (void) memcpy(elf->e_ident, ELFMAG, SELFMAG); elf->e_ident[EI_CLASS] =3D ELF_CLASS; elf->e_ident[EI_DATA] =3D ELF_DATA; elf->e_ident[EI_VERSION] =3D EV_CURRENT; @@ -4326,95 +4261,79 @@ static void fill_elf_header(struct elfhdr *elf, int= segs, uint16_t machine, bswap_ehdr(elf); } =20 -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 off= set) { phdr->p_type =3D PT_NOTE; phdr->p_offset =3D offset; - phdr->p_vaddr =3D 0; - phdr->p_paddr =3D 0; phdr->p_filesz =3D sz; - phdr->p_memsz =3D 0; - phdr->p_flags =3D 0; - phdr->p_align =3D 0; =20 bswap_phdr(phdr, 1); } =20 -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 =3D { + .pr_info.si_signo =3D signr, + .pr_cursig =3D signr, + .pr_pid =3D ts->ts_tid, + .pr_ppid =3D getppid(), + .pr_pgrp =3D getpgrp(), + .pr_sid =3D getsid(0), + }; + + elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu)); + bswap_prstatus(&prstatus); + memcpy(data, &prstatus, sizeof(prstatus)); } =20 -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 =3D prstatus->pr_cursig =3D signr; - prstatus->pr_pid =3D ts->ts_tid; - prstatus->pr_ppid =3D getppid(); - prstatus->pr_pgrp =3D getpgrp(); - prstatus->pr_sid =3D 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; =20 len =3D ts->info->env_strings - ts->info->arg_strings; - if (len >=3D ELF_PRARGSZ) - len =3D ELF_PRARGSZ - 1; - if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) { - return -EFAULT; + len =3D MIN(len, ELF_PRARGSZ); + memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len); + for (size_t i =3D 0; i < len; i++) { + if (psinfo.pr_psargs[i] =3D=3D 0) { + psinfo.pr_psargs[i] =3D ' '; + } } - for (i =3D 0; i < len; i++) - if (psinfo->pr_psargs[i] =3D=3D 0) - psinfo->pr_psargs[i] =3D ' '; - psinfo->pr_psargs[len] =3D 0; =20 - psinfo->pr_pid =3D getpid(); - psinfo->pr_ppid =3D getppid(); - psinfo->pr_pgrp =3D getpgrp(); - psinfo->pr_sid =3D getsid(0); - psinfo->pr_uid =3D getuid(); - psinfo->pr_gid =3D getgid(); + psinfo.pr_pid =3D getpid(); + psinfo.pr_ppid =3D getppid(); + psinfo.pr_pgrp =3D getpgrp(); + psinfo.pr_sid =3D getsid(0); + psinfo.pr_uid =3D getuid(); + psinfo.pr_gid =3D getgid(); =20 base_filename =3D 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)); } =20 -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 =3D (elf_addr_t)ts->info->saved_auxv; - elf_addr_t orig_auxv =3D auxv; - void *ptr; - int len =3D 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 =3D lock_user(VERIFY_READ, orig_auxv, len, 0); - if (ptr !=3D 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); } =20 /* @@ -4462,111 +4381,6 @@ static int dump_write(int fd, const void *ptr, size= _t size) return (0); } =20 -static int write_note(struct memelfnote *men, int fd) -{ - struct elf_note en; - - en.n_namesz =3D men->namesz; - en.n_type =3D men->type; - en.n_descsz =3D men->datasz; - - bswap_note(&en); - - if (dump_write(fd, &en, sizeof(en)) !=3D 0) - return (-1); - if (dump_write(fd, men->name, men->namesz_rounded) !=3D 0) - return (-1); - if (dump_write(fd, men->data, men->datasz_rounded) !=3D 0) - return (-1); - - return (0); -} - -static void fill_thread_info(struct elf_note_info *info, const CPUArchStat= e *env) -{ - CPUState *cpu =3D env_cpu((CPUArchState *)env); - TaskState *ts =3D (TaskState *)cpu->opaque; - struct elf_thread_status *ets; - - ets =3D g_malloc0(sizeof (*ets)); - ets->num_notes =3D 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 +=3D note_size(&ets->notes[0]); -} - -static void fill_note_info(struct elf_note_info *info, - int signr, const CPUArchState *env) -{ - CPUState *cpu =3D env_cpu((CPUArchState *)env); - TaskState *ts =3D (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 =3D 3; - - info->notes_size =3D 0; - for (int i =3D 0; i < info->numnote; i++) { - info->notes_size +=3D note_size(&info->notes[i]); - } - - /* read and fill status of all threads */ - CPU_FOREACH(cpu) { - if (cpu =3D=3D 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 =3D 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 =3D 0; - - /* write prstatus, psinfo and auxv for current thread */ - for (i =3D 0; i < info->numnote; i++) - if ((error =3D write_note(&info->notes[i], fd)) !=3D 0) - return (error); - - /* write prstatus for each thread */ - QTAILQ_FOREACH(ets, &info->thread_list, ets_link) { - if ((error =3D write_note(&ets->notes[0], fd)) !=3D 0) - return (error); - } - - return (0); -} - /* * Write out ELF coredump. * @@ -4615,14 +4429,13 @@ static int elf_core_dump(int signr, const CPUArchSt= ate *env) const CPUState *cpu =3D env_cpu((CPUArchState *)env); const TaskState *ts =3D (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 =3D -1; + CPUState *cpu_iter; =20 if (prctl(PR_GET_DUMPABLE) =3D=3D 0) { return 0; @@ -4644,7 +4457,7 @@ static int elf_core_dump(int signr, const CPUArchStat= e *env) segs =3D vma_get_mapping_count(&mm); =20 cpus =3D 0; - CPU_FOREACH(cpu) { + CPU_FOREACH(cpu_iter) { cpus++; } =20 @@ -4655,6 +4468,7 @@ static int elf_core_dump(int signr, const CPUArchStat= e *env) offset +=3D size_note("CORE", ts->info->auxv_len); offset +=3D size_note("CORE", sizeof(struct target_elf_prpsinfo)); offset +=3D size_note("CORE", sizeof(struct target_elf_prstatus)) * cp= us; + note_size =3D offset - note_offset; offset =3D ROUND_UP(offset, ELF_EXEC_PAGESIZE); data_offset =3D offset; =20 @@ -4678,61 +4492,64 @@ static int elf_core_dump(int signr, const CPUArchSt= ate *env) } =20 /* - * 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)) !=3D 0) - goto out; + { + g_autofree void *header =3D g_malloc0(data_offset); + void *hptr, *dptr; =20 - /* fill in the in-memory version of notes */ - fill_note_info(&info, signr, env); + /* Create elf file header. */ + hptr =3D header; + fill_elf_header(hptr, segs + 1, ELF_MACHINE, 0); + hptr +=3D sizeof(struct elfhdr); =20 - /* 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 +=3D sizeof(struct elf_phdr); =20 - if (dump_write(fd, &phdr, sizeof (phdr)) !=3D 0) - goto out; + offset =3D data_offset; + for (vma =3D vma_first(&mm); vma !=3D NULL; vma =3D vma_next(vma))= { + struct elf_phdr *phdr =3D hptr; =20 - /* - * Write program headers for memory regions mapped in - * the target process. - */ - offset =3D data_offset; - for (vma =3D vma_first(&mm); vma !=3D NULL; vma =3D vma_next(vma)) { - (void) memset(&phdr, 0, sizeof (phdr)); + phdr->p_type =3D PT_LOAD; + phdr->p_offset =3D offset; + phdr->p_vaddr =3D vma->vma_start; + phdr->p_paddr =3D 0; + phdr->p_filesz =3D vma_dump_size(vma); + offset +=3D phdr->p_filesz; + phdr->p_memsz =3D vma->vma_end - vma->vma_start; + phdr->p_flags =3D (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 =3D ELF_EXEC_PAGESIZE; =20 - phdr.p_type =3D PT_LOAD; - phdr.p_offset =3D offset; - phdr.p_vaddr =3D vma->vma_start; - phdr.p_paddr =3D 0; - phdr.p_filesz =3D vma_dump_size(vma); - offset +=3D phdr.p_filesz; - phdr.p_memsz =3D vma->vma_end - vma->vma_start; - phdr.p_flags =3D vma->vma_flags & PROT_READ ? PF_R : 0; - if (vma->vma_flags & PROT_WRITE) - phdr.p_flags |=3D PF_W; - if (vma->vma_flags & PROT_EXEC) - phdr.p_flags |=3D PF_X; - phdr.p_align =3D ELF_EXEC_PAGESIZE; + bswap_phdr(phdr, 1); + hptr +=3D sizeof(struct elf_phdr); + } =20 - bswap_phdr(&phdr, 1); - if (dump_write(fd, &phdr, sizeof(phdr)) !=3D 0) { + /* Create the notes. */ + dptr =3D fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); + fill_auxv_note(dptr, ts); + + dptr =3D fill_note(&hptr, NT_PRPSINFO, "CORE", + sizeof(struct target_elf_prpsinfo)); + fill_prpsinfo_note(dptr, ts); + + CPU_FOREACH(cpu_iter) { + dptr =3D fill_note(&hptr, NT_PRSTATUS, "CORE", + sizeof(struct target_elf_prstatus)); + fill_prstatus_note(dptr, ts, cpu_iter, + cpu_iter =3D=3D cpu ? signr : 0); + } + + if (dump_write(fd, header, data_offset) < 0) { goto out; } } =20 - /* - * 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) !=3D 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 CPUArchStat= e *env) ret =3D -errno; mmap_unlock(); cpu_list_unlock(); - free_note_info(&info); vma_delete(&mm); close(fd); return ret; --=20 2.34.1