| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup taskdump
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Write ELF core files.
|
|---|
| 33 | *
|
|---|
| 34 | * Creates ELF core files. Core files do not seem to be specified by some
|
|---|
| 35 | * standard (the System V ABI explicitely states that it does not specify
|
|---|
| 36 | * them).
|
|---|
| 37 | *
|
|---|
| 38 | * Looking at core files produced by Linux, these don't have section headers,
|
|---|
| 39 | * only program headers, although objdump shows them as having sections.
|
|---|
| 40 | * Basically at the beginning there should be a note segment followed
|
|---|
| 41 | * by one loadable segment per memory area.
|
|---|
| 42 | *
|
|---|
| 43 | * The note segment contains a series of records with register state,
|
|---|
| 44 | * process info etc. We only write one record NT_PRSTATUS which contains
|
|---|
| 45 | * process/register state (anything which is not register state we fill
|
|---|
| 46 | * with zeroes).
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | #include <align.h>
|
|---|
| 50 | #include <elf/elf.h>
|
|---|
| 51 | #include <elf/elf_linux.h>
|
|---|
| 52 | #include <stdio.h>
|
|---|
| 53 | #include <stdlib.h>
|
|---|
| 54 | #include <errno.h>
|
|---|
| 55 | #include <sys/types.h>
|
|---|
| 56 | #include <sys/stat.h>
|
|---|
| 57 | #include <unistd.h>
|
|---|
| 58 | #include <fcntl.h>
|
|---|
| 59 | #include <mem.h>
|
|---|
| 60 | #include <stdint.h>
|
|---|
| 61 | #include <as.h>
|
|---|
| 62 | #include <udebug.h>
|
|---|
| 63 | #include <macros.h>
|
|---|
| 64 | #include <libarch/istate.h>
|
|---|
| 65 |
|
|---|
| 66 | #include "elf_core.h"
|
|---|
| 67 |
|
|---|
| 68 | static off64_t align_foff_up(off64_t, uintptr_t, size_t);
|
|---|
| 69 | static int align_pos(int, size_t);
|
|---|
| 70 | static int write_mem_area(int, as_area_info_t *, async_sess_t *);
|
|---|
| 71 |
|
|---|
| 72 | #define BUFFER_SIZE 0x1000
|
|---|
| 73 | static uint8_t buffer[BUFFER_SIZE];
|
|---|
| 74 |
|
|---|
| 75 | /** Save ELF core file.
|
|---|
| 76 | *
|
|---|
| 77 | * @param file_name Name of file to save to.
|
|---|
| 78 | * @param ainfo Array of @a n memory area info structures.
|
|---|
| 79 | * @param n Number of memory areas.
|
|---|
| 80 | * @param sess Debugging session.
|
|---|
| 81 | *
|
|---|
| 82 | * @return EOK on sucess.
|
|---|
| 83 | * @return ENOENT if file cannot be created.
|
|---|
| 84 | * @return ENOMEM on out of memory.
|
|---|
| 85 | * @return EIO on write error.
|
|---|
| 86 | *
|
|---|
| 87 | */
|
|---|
| 88 | int elf_core_save(const char *file_name, as_area_info_t *ainfo, unsigned int n,
|
|---|
| 89 | async_sess_t *sess, istate_t *istate)
|
|---|
| 90 | {
|
|---|
| 91 | elf_header_t elf_hdr;
|
|---|
| 92 | off64_t foff;
|
|---|
| 93 | size_t n_ph;
|
|---|
| 94 | elf_word flags;
|
|---|
| 95 | elf_segment_header_t *p_hdr;
|
|---|
| 96 | elf_prstatus_t pr_status;
|
|---|
| 97 | elf_note_t note;
|
|---|
| 98 | size_t word_size;
|
|---|
| 99 |
|
|---|
| 100 | int fd;
|
|---|
| 101 | ssize_t rc;
|
|---|
| 102 | unsigned int i;
|
|---|
| 103 |
|
|---|
| 104 | #ifdef __32_BITS__
|
|---|
| 105 | word_size = 4;
|
|---|
| 106 | #endif
|
|---|
| 107 | #ifdef __64_BITS__
|
|---|
| 108 | /*
|
|---|
| 109 | * This should be 8 per the 64-bit ELF spec, but the Linux kernel
|
|---|
| 110 | * screws up and uses 4 anyway (and screws up elf_note_t as well)
|
|---|
| 111 | * and we are trying to be compatible with Linux GDB target. Sigh.
|
|---|
| 112 | */
|
|---|
| 113 | word_size = 4;
|
|---|
| 114 | #endif
|
|---|
| 115 | memset(&pr_status, 0, sizeof(pr_status));
|
|---|
| 116 | istate_to_elf_regs(istate, &pr_status.regs);
|
|---|
| 117 |
|
|---|
| 118 | n_ph = n + 1;
|
|---|
| 119 |
|
|---|
| 120 | p_hdr = malloc(sizeof(elf_segment_header_t) * n_ph);
|
|---|
| 121 | if (p_hdr == NULL) {
|
|---|
| 122 | printf("Failed allocating memory.\n");
|
|---|
| 123 | return ENOMEM;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | fd = open(file_name, O_CREAT | O_WRONLY, 0644);
|
|---|
| 127 | if (fd < 0) {
|
|---|
| 128 | printf("Failed opening file.\n");
|
|---|
| 129 | free(p_hdr);
|
|---|
| 130 | return ENOENT;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /*
|
|---|
| 134 | * File layout:
|
|---|
| 135 | *
|
|---|
| 136 | * ELF header
|
|---|
| 137 | * program headers
|
|---|
| 138 | * note segment
|
|---|
| 139 | * repeat:
|
|---|
| 140 | * (pad for alignment)
|
|---|
| 141 | * core segment
|
|---|
| 142 | * end repeat
|
|---|
| 143 | */
|
|---|
| 144 |
|
|---|
| 145 | memset(&elf_hdr, 0, sizeof(elf_hdr));
|
|---|
| 146 | elf_hdr.e_ident[EI_MAG0] = ELFMAG0;
|
|---|
| 147 | elf_hdr.e_ident[EI_MAG1] = ELFMAG1;
|
|---|
| 148 | elf_hdr.e_ident[EI_MAG2] = ELFMAG2;
|
|---|
| 149 | elf_hdr.e_ident[EI_MAG3] = ELFMAG3;
|
|---|
| 150 | elf_hdr.e_ident[EI_CLASS] = ELF_CLASS;
|
|---|
| 151 | elf_hdr.e_ident[EI_DATA] = ELF_DATA_ENCODING;
|
|---|
| 152 | elf_hdr.e_ident[EI_VERSION] = EV_CURRENT;
|
|---|
| 153 |
|
|---|
| 154 | elf_hdr.e_type = ET_CORE;
|
|---|
| 155 | elf_hdr.e_machine = ELF_MACHINE;
|
|---|
| 156 | elf_hdr.e_version = EV_CURRENT;
|
|---|
| 157 | elf_hdr.e_entry = 0;
|
|---|
| 158 | elf_hdr.e_phoff = sizeof(elf_header_t);
|
|---|
| 159 | elf_hdr.e_shoff = 0;
|
|---|
| 160 | elf_hdr.e_flags = 0;
|
|---|
| 161 | elf_hdr.e_ehsize = sizeof(elf_hdr);
|
|---|
| 162 | elf_hdr.e_phentsize = sizeof(elf_segment_header_t);
|
|---|
| 163 | elf_hdr.e_phnum = n_ph;
|
|---|
| 164 | elf_hdr.e_shentsize = 0;
|
|---|
| 165 | elf_hdr.e_shnum = 0;
|
|---|
| 166 | elf_hdr.e_shstrndx = 0;
|
|---|
| 167 |
|
|---|
| 168 | /* foff is used for allocation of file space for segment data. */
|
|---|
| 169 | foff = elf_hdr.e_phoff + n_ph * sizeof(elf_segment_header_t);
|
|---|
| 170 |
|
|---|
| 171 | memset(&p_hdr[0], 0, sizeof(p_hdr[0]));
|
|---|
| 172 | p_hdr[0].p_type = PT_NOTE;
|
|---|
| 173 | p_hdr[0].p_offset = foff;
|
|---|
| 174 | p_hdr[0].p_vaddr = 0;
|
|---|
| 175 | p_hdr[0].p_paddr = 0;
|
|---|
| 176 | p_hdr[0].p_filesz = sizeof(elf_note_t)
|
|---|
| 177 | + ALIGN_UP((str_size("CORE") + 1), word_size)
|
|---|
| 178 | + ALIGN_UP(sizeof(elf_prstatus_t), word_size);
|
|---|
| 179 | p_hdr[0].p_memsz = 0;
|
|---|
| 180 | p_hdr[0].p_flags = 0;
|
|---|
| 181 | p_hdr[0].p_align = 1;
|
|---|
| 182 |
|
|---|
| 183 | foff += p_hdr[0].p_filesz;
|
|---|
| 184 |
|
|---|
| 185 | for (i = 0; i < n; ++i) {
|
|---|
| 186 | foff = align_foff_up(foff, ainfo[i].start_addr, PAGE_SIZE);
|
|---|
| 187 |
|
|---|
| 188 | flags = 0;
|
|---|
| 189 | if (ainfo[i].flags & AS_AREA_READ)
|
|---|
| 190 | flags |= PF_R;
|
|---|
| 191 | if (ainfo[i].flags & AS_AREA_WRITE)
|
|---|
| 192 | flags |= PF_W;
|
|---|
| 193 | if (ainfo[i].flags & AS_AREA_EXEC)
|
|---|
| 194 | flags |= PF_X;
|
|---|
| 195 |
|
|---|
| 196 | memset(&p_hdr[i + 1], 0, sizeof(p_hdr[i + 1]));
|
|---|
| 197 | p_hdr[i + 1].p_type = PT_LOAD;
|
|---|
| 198 | p_hdr[i + 1].p_offset = foff;
|
|---|
| 199 | p_hdr[i + 1].p_vaddr = ainfo[i].start_addr;
|
|---|
| 200 | p_hdr[i + 1].p_paddr = 0;
|
|---|
| 201 | p_hdr[i + 1].p_filesz = ainfo[i].size;
|
|---|
| 202 | p_hdr[i + 1].p_memsz = ainfo[i].size;
|
|---|
| 203 | p_hdr[i + 1].p_flags = flags;
|
|---|
| 204 | p_hdr[i + 1].p_align = PAGE_SIZE;
|
|---|
| 205 |
|
|---|
| 206 | foff += ainfo[i].size;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | rc = write_all(fd, &elf_hdr, sizeof(elf_hdr));
|
|---|
| 210 | if (rc != sizeof(elf_hdr)) {
|
|---|
| 211 | printf("Failed writing ELF header.\n");
|
|---|
| 212 | free(p_hdr);
|
|---|
| 213 | return EIO;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | for (i = 0; i < n_ph; ++i) {
|
|---|
| 217 | rc = write_all(fd, &p_hdr[i], sizeof(p_hdr[i]));
|
|---|
| 218 | if (rc != sizeof(p_hdr[i])) {
|
|---|
| 219 | printf("Failed writing program header.\n");
|
|---|
| 220 | free(p_hdr);
|
|---|
| 221 | return EIO;
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (lseek(fd, p_hdr[0].p_offset, SEEK_SET) == (off64_t) -1) {
|
|---|
| 226 | printf("Failed writing memory data.\n");
|
|---|
| 227 | free(p_hdr);
|
|---|
| 228 | return EIO;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /*
|
|---|
| 232 | * Write note header
|
|---|
| 233 | */
|
|---|
| 234 | note.namesz = str_size("CORE") + 1;
|
|---|
| 235 | note.descsz = sizeof(elf_prstatus_t);
|
|---|
| 236 | note.type = NT_PRSTATUS;
|
|---|
| 237 |
|
|---|
| 238 | rc = write_all(fd, ¬e, sizeof(elf_note_t));
|
|---|
| 239 | if (rc != sizeof(elf_note_t)) {
|
|---|
| 240 | printf("Failed writing note header.\n");
|
|---|
| 241 | free(p_hdr);
|
|---|
| 242 | return EIO;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | rc = write_all(fd, "CORE", note.namesz);
|
|---|
| 246 | if (rc != (ssize_t) note.namesz) {
|
|---|
| 247 | printf("Failed writing note header.\n");
|
|---|
| 248 | free(p_hdr);
|
|---|
| 249 | return EIO;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | rc = align_pos(fd, word_size);
|
|---|
| 253 | if (rc != EOK) {
|
|---|
| 254 | printf("Failed writing note header.\n");
|
|---|
| 255 | free(p_hdr);
|
|---|
| 256 | return EIO;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | rc = write_all(fd, &pr_status, sizeof(elf_prstatus_t));
|
|---|
| 260 | if (rc != sizeof(elf_prstatus_t)) {
|
|---|
| 261 | printf("Failed writing register data.\n");
|
|---|
| 262 | free(p_hdr);
|
|---|
| 263 | return EIO;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | for (i = 1; i < n_ph; ++i) {
|
|---|
| 267 | if (lseek(fd, p_hdr[i].p_offset, SEEK_SET) == (off64_t) -1) {
|
|---|
| 268 | printf("Failed writing memory data.\n");
|
|---|
| 269 | free(p_hdr);
|
|---|
| 270 | return EIO;
|
|---|
| 271 | }
|
|---|
| 272 | if (write_mem_area(fd, &ainfo[i - 1], sess) != EOK) {
|
|---|
| 273 | printf("Failed writing memory data.\n");
|
|---|
| 274 | free(p_hdr);
|
|---|
| 275 | return EIO;
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | free(p_hdr);
|
|---|
| 280 |
|
|---|
| 281 | return EOK;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** Align file offset up to be congruent with vaddr modulo page size. */
|
|---|
| 285 | static off64_t align_foff_up(off64_t foff, uintptr_t vaddr, size_t page_size)
|
|---|
| 286 | {
|
|---|
| 287 | off64_t rva = vaddr % page_size;
|
|---|
| 288 | off64_t rfo = foff % page_size;
|
|---|
| 289 |
|
|---|
| 290 | if (rva >= rfo)
|
|---|
| 291 | return (foff + (rva - rfo));
|
|---|
| 292 |
|
|---|
| 293 | return (foff + (page_size + (rva - rfo)));
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /** Write memory area from application to core file.
|
|---|
| 297 | *
|
|---|
| 298 | * @param fd File to write to.
|
|---|
| 299 | * @param area Memory area info structure.
|
|---|
| 300 | * @param sess Debugging session.
|
|---|
| 301 | *
|
|---|
| 302 | * @return EOK on success, EIO on failure.
|
|---|
| 303 | *
|
|---|
| 304 | */
|
|---|
| 305 | static int write_mem_area(int fd, as_area_info_t *area, async_sess_t *sess)
|
|---|
| 306 | {
|
|---|
| 307 | size_t to_copy;
|
|---|
| 308 | size_t total;
|
|---|
| 309 | uintptr_t addr;
|
|---|
| 310 | ssize_t rc;
|
|---|
| 311 |
|
|---|
| 312 | addr = area->start_addr;
|
|---|
| 313 | total = 0;
|
|---|
| 314 |
|
|---|
| 315 | while (total < area->size) {
|
|---|
| 316 | to_copy = min(area->size - total, BUFFER_SIZE);
|
|---|
| 317 | rc = udebug_mem_read(sess, buffer, addr, to_copy);
|
|---|
| 318 | if (rc < 0) {
|
|---|
| 319 | printf("Failed reading task memory.\n");
|
|---|
| 320 | return EIO;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | rc = write_all(fd, buffer, to_copy);
|
|---|
| 324 | if (rc != (ssize_t) to_copy) {
|
|---|
| 325 | printf("Failed writing memory contents.\n");
|
|---|
| 326 | return EIO;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | addr += to_copy;
|
|---|
| 330 | total += to_copy;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | return EOK;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | static int align_pos(int fd, size_t align)
|
|---|
| 337 | {
|
|---|
| 338 | off64_t cur_pos;
|
|---|
| 339 | size_t rem, adv;
|
|---|
| 340 |
|
|---|
| 341 | cur_pos = lseek(fd, 0, SEEK_CUR);
|
|---|
| 342 | if (cur_pos < 0)
|
|---|
| 343 | return -1;
|
|---|
| 344 |
|
|---|
| 345 | rem = cur_pos % align;
|
|---|
| 346 | adv = align - rem;
|
|---|
| 347 |
|
|---|
| 348 | cur_pos = lseek(fd, adv, SEEK_CUR);
|
|---|
| 349 | if (cur_pos < 0)
|
|---|
| 350 | return -1;
|
|---|
| 351 |
|
|---|
| 352 | return EOK;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | /** @}
|
|---|
| 356 | */
|
|---|