+ /*
+ * Read from the vma segments
+ * a. verify if the *fpos is within a phdr
+ * b. Use access_process_vm() to get data page by page
+ * c. copy_to_user into user buffer
+ */
+
+ while (buflen) {
+ size_t bufsz, offset, bytes;
+ char *readbuf;
+ struct elf_phdr *phdr = get_pos_elfphdr(cp, *fpos);
+
+ if (!phdr)
+ break;
+
+ bufsz = (buflen> PAGE_SIZE) ? PAGE_SIZE : buflen;
+ readbuf = kmalloc(bufsz, GFP_KERNEL);
+ if (!readbuf) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ offset = *fpos - phdr->p_offset;
+ bytes = access_process_vm(cp->task, (phdr->p_vaddr + offset),
+ readbuf, bufsz, 0);
+ if (!bytes) {
+ ret = -EIO;
+ goto out;
+ }
+ if (copy_to_user(buffer, readbuf, bytes)) {
+ ret = -EFAULT;
+ kfree(readbuf);
+ goto out;
+ } else
+ acc += bytes;
+
+ kfree(readbuf);
+ buflen -= bytes;
+ buffer += bytes;
+ *fpos += bytes;
+ }
+
+ /* Fill extnum section header if present */
+ if (buflen&&
+ elf_hdr->e_shoff&&
+ (*fpos>= elf_hdr->e_shoff)&&
+ (*fpos< (elf_hdr->e_shoff + sizeof(struct elf_shdr)))) {
+
+ off_t offset = *fpos - elf_hdr->e_shoff;
+ size_t shdrsz = sizeof(struct elf_shdr) - offset;
+
+ shdrsz = (buflen< shdrsz) ? buflen : shdrsz;
+ if (copy_to_user(buffer, ((char *)cp->shdr) + offset, shdrsz)) {
+ ret = -EFAULT;
+ goto out;
+ } else {
+ acc += shdrsz;
+ buflen -= shdrsz;
+ buffer += shdrsz;
+ }
+ }
+done:
+ ret = acc;
out:
return ret;
}