public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Janani Venkataraman <jananive@linux.vnet.ibm.com>
To: linux-kernel@vger.kernel.org
Cc: amwang@redhat.com, procps@freelists.org, rdunlap@xenotime.net,
	james.hogan@imgtec.com, aravinda@linux.vnet.ibm.com, hch@lst.de,
	mhiramat@redhat.com, jeremy.fitzhardinge@citrix.com,
	xemul@parallels.com, d.hatayama@jp.fujitsu.com,
	coreutils@gnu.org, kosaki.motohiro@jp.fujitsu.com,
	adobriyan@gmail.com, util-linux@vger.kernel.org,
	tarundsk@linux.vnet.ibm.com, vapier@gentoo.org,
	roland@hack.frob.com, ananth@linux.vnet.ibm.com,
	gorcunov@openvz.org, avagin@openvz.org, oleg@redhat.com,
	eparis@redhat.com, suzuki@linux.vnet.ibm.com,
	andi@firstfloor.org, tj@kernel.org, akpm@linux-foundation.org,
	torvalds@linux-foundation.org
Subject: [PATCH 14/33] Populating Program Headers
Date: Thu, 20 Mar 2014 15:11:05 +0530	[thread overview]
Message-ID: <20140320094105.14878.82749.stgit@localhost.localdomain> (raw)
In-Reply-To: <20140320093040.14878.903.stgit@localhost.localdomain>

Populates the program headers using memory maps linked list which was
populated in patch 5. The source, size and permissions of the maps are
found out and filled respectively.

Signed-off-by: Janani Venkataraman <jananive@linux.vnet.ibm.com>
---
 src/coredump.c |    6 +++
 src/coredump.h |    2 +
 src/elf.c      |  108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/elf32.c    |    2 +
 src/elf64.c    |    1 +
 5 files changed, 119 insertions(+)

diff --git a/src/coredump.c b/src/coredump.c
index 0fd6343e..5cccb7a 100644
--- a/src/coredump.c
+++ b/src/coredump.c
@@ -231,6 +231,12 @@ cleanup:
 	if (cp.notes)
 		free_notes(cp.notes);
 
+	if (cp.phdrs)
+		free(cp.phdrs);
+
+	if (cp.phdrs_count)
+		free(cp.shdrs);
+
 	errno = status;
 
 	return ret;
diff --git a/src/coredump.h b/src/coredump.h
index dce6292..a729f16 100644
--- a/src/coredump.h
+++ b/src/coredump.h
@@ -68,4 +68,6 @@ struct core_proc {
 	int elf_class;			/* Elf class of the process */
 	void *elf_hdr;			/* Stores the ELF_header */
 	struct mem_note *notes;		/* Head of Notes */
+	void *shdrs;			/* Extra Program Headers */
+	void *phdrs;			/* Program Headers */
 };
diff --git a/src/elf.c b/src/elf.c
index 62dc5a9..a5a1feb 100644
--- a/src/elf.c
+++ b/src/elf.c
@@ -39,6 +39,11 @@
 
 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
 
+/* Default alignment for program headers */
+#ifndef ELF_EXEC_PAGESIZE
+#define ELF_EXEC_PAGESIZE PAGESIZE
+#endif
+
 /* Appending the note to the list */
 static void append_note(struct mem_note *new_note, struct core_proc *cp)
 {
@@ -110,6 +115,26 @@ static int add_note(const char *name, int type, unsigned int data_sz, void *data
 	return 0;
 }
 
+/*
+ * Reads first few bytes of the address specified and checks if it is
+ * an ELF by checking the magic number.
+ */
+static int get_elf_hdr_vaddr(int pid, Elf_Ehdr *elf, Elf_Addr addr)
+{
+	int ret;
+	struct iovec local, remote;
+
+	local.iov_base = elf;
+	local.iov_len = sizeof(Elf_Ehdr);
+	remote.iov_base = (void *)addr;
+	remote.iov_len = sizeof(Elf_Ehdr);
+	ret = process_vm_readv(pid, &local, 1, &remote, 1, 0);
+	if (ret == -1)
+		return -1;
+
+	return check_elf_hdr(elf->e_ident);
+}
+
 /* Fetchs ELF header of the executable */
 static int get_elf_hdr_exe_file(int pid, Elf_Ehdr *elf)
 {
@@ -568,6 +593,84 @@ static int fetch_thread_notes(struct core_proc *cp)
 	return 0;
 }
 
+/* Populate Program headers */
+static int get_phdrs(int pid, struct core_proc *cp)
+{
+	int n;
+	struct maps *map = cp->vmas;
+	Elf_Ehdr elf;
+	Elf_Phdr *cp_phdrs;
+	Elf_Shdr *cp_shdrs;
+	Elf_Ehdr *cp_elf;
+	cp_elf = (Elf_Ehdr *)cp->elf_hdr;
+
+	cp->phdrs = calloc(cp->phdrs_count, sizeof(Elf_Phdr));
+	if (!cp->phdrs) {
+		status = errno;
+		gencore_log("Could not allocate memory for Program headers.\n");
+		return -1;
+	}
+
+	cp_phdrs = (Elf_Phdr *)cp->phdrs;
+
+	cp_phdrs[0].p_type = PT_NOTE;
+	cp_phdrs[0].p_offset = 0;
+	cp_phdrs[0].p_vaddr = 0;
+	cp_phdrs[0].p_paddr = 0;
+	/* Will fill the size after filling notes */
+	cp_phdrs[0].p_filesz = 0;
+	cp_phdrs[0].p_memsz = 0;
+
+	n = 1;
+
+	while (map) {
+
+		/* Filling the Program Header Values */
+		cp_phdrs[n].p_type = PT_LOAD;
+		cp_phdrs[n].p_offset = 0;
+		cp_phdrs[n].p_vaddr = map->src;
+		cp_phdrs[n].p_paddr = 0;
+		cp_phdrs[n].p_flags = 0;
+		if (map->r == 'r')
+			cp_phdrs[n].p_flags |= PF_R;
+		if (map->w == 'w')
+			cp_phdrs[n].p_flags |= PF_W;
+		if (map->x == 'x')
+			cp_phdrs[n].p_flags |= PF_X;
+
+		cp_phdrs[n].p_memsz = map->dst - map->src;
+
+		if (!(cp_phdrs[n].p_flags & PF_R))
+			cp_phdrs[n].p_filesz = 0;
+		else if (map->inode &&
+			get_elf_hdr_vaddr(pid, &elf, cp_phdrs[n].p_vaddr) == 0)
+			cp_phdrs[n].p_filesz = ELF_EXEC_PAGESIZE;
+		else
+			cp_phdrs[n].p_filesz = cp_phdrs[n].p_memsz;
+		
+		cp_phdrs[n].p_align = ELF_EXEC_PAGESIZE;
+
+		n++;
+		map = map->next;
+	}
+
+	if (cp->phdrs_count > PN_XNUM) {
+		cp->shdrs = malloc(sizeof(Elf_Shdr));
+		if (!cp->shdrs) {
+			status = errno;
+			gencore_log("Could not allocate memory for Extra Program headers.\n");
+			return -1;
+		}
+		cp_shdrs = (Elf_Shdr *)cp->shdrs;
+		cp_shdrs->sh_type = SHT_NULL;
+		cp_shdrs->sh_size = cp_elf->e_shnum;
+		cp_shdrs->sh_link = cp_elf->e_shstrndx;
+		cp_shdrs->sh_info = cp->phdrs_count;
+	}
+
+	return 0;
+}
+
 int do_elf_coredump(int pid, struct core_proc *cp)
 {
 	int ret, i;
@@ -597,5 +700,10 @@ int do_elf_coredump(int pid, struct core_proc *cp)
 	if (ret)
 		return -1;
 
+	/* Get Program headers */
+	ret = get_phdrs(pid, cp);
+	if (ret)
+		return -1;
+
 	return 0;
 }
diff --git a/src/elf32.c b/src/elf32.c
index 8ec287b..daf9940 100644
--- a/src/elf32.c
+++ b/src/elf32.c
@@ -38,4 +38,6 @@
 #define Elf_Long int
 #define Elf_prstatus compat_elf_prstatus
 
+#define Elf_Addr Elf32_Addr
+
 #include "elf.c"
diff --git a/src/elf64.c b/src/elf64.c
index 780e2d4..651f218 100644
--- a/src/elf64.c
+++ b/src/elf64.c
@@ -37,5 +37,6 @@
 #define Elf_prpsinfo elf_prpsinfo
 #define Elf_Long long
 #define Elf_prstatus elf_prstatus
+#define Elf_Addr Elf64_Addr
 
 #include "elf.c"


  parent reply	other threads:[~2014-03-20  9:41 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-20  9:39 [PATCH 00/33] [RFC] Non disruptive application core dump infrastructure Janani Venkataraman
2014-03-20  9:39 ` [PATCH 01/33] Configure and Make files Janani Venkataraman
2014-03-20  9:39 ` [PATCH 02/33] Validity of arguments Janani Venkataraman
2014-03-20  9:39 ` [PATCH 03/33] Process Status Janani Venkataraman
2014-03-20  9:39 ` [PATCH 04/33] Hold threads Janani Venkataraman
2014-03-20 19:01   ` Pavel Emelyanov
2014-03-25  6:58     ` Janani Venkataraman
2014-03-20  9:39 ` [PATCH 05/33] Fetching Memory maps Janani Venkataraman
2014-03-20  9:39 ` [PATCH 06/33] Check ELF class Janani Venkataraman
2014-03-20  9:39 ` [PATCH 07/33] Do elf_coredump Janani Venkataraman
2014-03-20  9:40 ` [PATCH 08/33] Fills elf header Janani Venkataraman
2014-03-20  9:40 ` [PATCH 09/33] Adding notes infrastructure Janani Venkataraman
2014-03-20  9:40 ` [PATCH 10/33] Populates PRPS info Janani Venkataraman
2014-03-20  9:40 ` [PATCH 11/33] Populate AUXV Janani Venkataraman
2014-03-20  9:40 ` [PATCH 12/33] Fetch File maps Janani Venkataraman
2014-03-20  9:41 ` [PATCH 13/33] Fetching thread specific Notes Janani Venkataraman
2014-03-20  9:41 ` Janani Venkataraman [this message]
2014-03-20  9:41 ` [PATCH 15/33] Updating Offset Janani Venkataraman
2014-03-20  9:41 ` [PATCH 16/33] Writing to core file Janani Venkataraman
2014-03-20  9:41 ` [PATCH 17/33] Daemonizing the Process Janani Venkataraman
2014-03-20  9:41 ` [PATCH 18/33] Socket operations Janani Venkataraman
2014-03-20  9:41 ` [PATCH 19/33] Block till request Janani Venkataraman
2014-03-20  9:41 ` [PATCH 20/33] Handling Requests Janani Venkataraman
2014-03-20  9:41 ` [PATCH 21/33] Get Clients PID Janani Venkataraman
2014-03-20  9:41 ` [PATCH 22/33] Dump the task Janani Venkataraman
2014-03-20  9:42 ` [PATCH 23/33] Handling SIG TERM of the daemon Janani Venkataraman
2014-03-20  9:42 ` [PATCH 24/33] Handling SIG TERM of the child Janani Venkataraman
2014-03-20  9:42 ` [PATCH 25/33] Systemd Socket ID retrieval Janani Venkataraman
2014-03-20  9:42 ` [PATCH 26/33] [libgencore] Setting up Connection Janani Venkataraman
2014-03-20  9:42 ` [PATCH 27/33] [libgencore] Request for dump Janani Venkataraman
2014-03-20  9:43 ` [PATCH 28/33] Man pages Janani Venkataraman
2014-03-20  9:43 ` [PATCH 29/33] Automake files for the doc folder Janani Venkataraman
2014-03-20  9:43 ` [PATCH 30/33] README, COPYING, Changelog Janani Venkataraman
2014-03-20  9:43 ` [PATCH 31/33] Spec file Janani Venkataraman
2014-03-20  9:43 ` [PATCH 32/33] Socket and Service files Janani Venkataraman
2014-03-20  9:44 ` [PATCH 33/33] Support check Janani Venkataraman
2014-03-20 10:24 ` [PATCH 00/33] [RFC] Non disruptive application core dump infrastructure Pádraig Brady
2014-03-21  8:17 ` Karel Zak
2014-03-21 15:02   ` Phillip Susi
2014-03-24  9:43     ` Janani Venkataraman
2014-03-24 13:54       ` Phillip Susi
2014-07-03 12:59         ` Suzuki K. Poulose
2014-03-24  9:38   ` Janani Venkataraman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140320094105.14878.82749.stgit@localhost.localdomain \
    --to=jananive@linux.vnet.ibm.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=amwang@redhat.com \
    --cc=ananth@linux.vnet.ibm.com \
    --cc=andi@firstfloor.org \
    --cc=aravinda@linux.vnet.ibm.com \
    --cc=avagin@openvz.org \
    --cc=coreutils@gnu.org \
    --cc=d.hatayama@jp.fujitsu.com \
    --cc=eparis@redhat.com \
    --cc=gorcunov@openvz.org \
    --cc=hch@lst.de \
    --cc=james.hogan@imgtec.com \
    --cc=jeremy.fitzhardinge@citrix.com \
    --cc=kosaki.motohiro@jp.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@redhat.com \
    --cc=oleg@redhat.com \
    --cc=procps@freelists.org \
    --cc=rdunlap@xenotime.net \
    --cc=roland@hack.frob.com \
    --cc=suzuki@linux.vnet.ibm.com \
    --cc=tarundsk@linux.vnet.ibm.com \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=util-linux@vger.kernel.org \
    --cc=vapier@gentoo.org \
    --cc=xemul@parallels.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox