1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
/* ELF format test suite.
Copyright (C) 2016 Petr Tesarik <ptesarik@suse.com>
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
libkdumpfile is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <endian.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <elf.h>
#include "config.h"
#include "testutil.h"
#include "diskdump.h"
typedef int write_fn(FILE *);
struct page_data_elf {
FILE *f;
unsigned shnum;
unsigned phnum;
union {
struct {
unsigned long long sh_name;
unsigned long long sh_type;
unsigned long long sh_flags;
unsigned long long sh_addr;
unsigned long long sh_link;
unsigned long long sh_info;
unsigned long long sh_addralign;
unsigned long long sh_entsize;
} shdr;
struct {
unsigned long long p_type;
unsigned long long p_flags;
unsigned long long p_vaddr;
unsigned long long p_paddr;
unsigned long long p_memsz;
unsigned long long p_align;
} phdr;
};
off_t filepos;
off_t filesz;
int (*parsehdr)(struct page_data *pg, char *p);
int (*finshdr)(struct page_data *pg);
int (*finphdr)(struct page_data *pg);
int (*finhdr)(struct page_data *pg);
};
static endian_t be;
static bool flattened;
static unsigned long long flattened_type = MDF_TYPE_FLAT_HEADER;
static unsigned long long flattened_version = MDF_VERSION_FLAT_HEADER;
static char *ei_mag;
static unsigned long long ei_class = ELFCLASS64;
static unsigned long long ei_data = ELFDATA2LSB;
static unsigned long long ei_version = EV_CURRENT;
static unsigned long long ei_osabi = ELFOSABI_NONE;
static unsigned long long ei_abiversion = 0;
static unsigned long long e_type = ET_CORE;
static unsigned long long e_machine = EM_X86_64;
static unsigned long long e_version = EV_CURRENT;
static unsigned long long e_entry;
static unsigned long long e_phoff;
static unsigned long long e_shoff;
static unsigned long long e_flags;
static unsigned long long e_ehsize = sizeof(Elf64_Ehdr);
static unsigned long long e_phentsize = sizeof(Elf64_Phdr);
static unsigned long long e_phnum;
static unsigned long long e_shentsize = sizeof(Elf64_Shdr);
static unsigned long long e_shnum;
static unsigned long long e_shstrndx;
static char *data_file;
static const struct param param_array[] = {
/* meta-data */
PARAM_YESNO("flattened", flattened),
PARAM_NUMBER("flattened.type", flattened_type),
PARAM_NUMBER("flattened.version", flattened_version),
/* ELF file header */
PARAM_STRING("ei_mag", ei_mag),
PARAM_NUMBER("ei_class", ei_class),
PARAM_NUMBER("ei_data", ei_data),
PARAM_NUMBER("ei_version", ei_version),
PARAM_NUMBER("ei_osabi", ei_osabi),
PARAM_NUMBER("ei_abiversion", ei_abiversion),
PARAM_NUMBER("e_type", e_type),
PARAM_NUMBER("e_machine", e_machine),
PARAM_NUMBER("e_version", e_version),
PARAM_NUMBER("e_entry", e_entry),
PARAM_NUMBER("e_phoff", e_phoff),
PARAM_NUMBER("e_shoff", e_shoff),
PARAM_NUMBER("e_flags", e_flags),
PARAM_NUMBER("e_ehsize", e_ehsize),
PARAM_NUMBER("e_phentsize", e_phentsize),
PARAM_NUMBER("e_phnum", e_phnum),
PARAM_NUMBER("e_shentsize", e_shentsize),
PARAM_NUMBER("e_shnum", e_shnum),
PARAM_NUMBER("e_shstrndx", e_shstrndx),
PARAM_STRING("DATA", data_file)
};
static const struct params params = {
ARRAY_SIZE(param_array),
param_array
};
static int
write_chunk(FILE *f, off_t off, const void *ptr, size_t sz, const char *what)
{
if (flattened) {
struct makedumpfile_data_header hdr = {
.offset = htobe64(off),
.buf_size = htobe64(sz),
};
if (fwrite(&hdr, sizeof hdr, 1, f) != 1) {
perror("flattened segment header");
return -1;
}
} else if (fseek(f, off, SEEK_SET) != 0) {
fprintf(stderr, "seek %s: %s\n", what, strerror(errno));
return -1;
}
if (fwrite(ptr, 1, sz, f) != sz) {
fprintf(stderr, "write %s: %s\n", what, strerror(errno));
return -1;
}
return 0;
}
static int
writeheader32(FILE *f)
{
Elf32_Ehdr ehdr;
/* initialize dump header to zero */
memset(&ehdr, 0, sizeof ehdr);
strncpy((char*)ehdr.e_ident, ei_mag, SELFMAG);
ehdr.e_ident[EI_CLASS] = ei_class;
ehdr.e_ident[EI_DATA] = ei_data;
ehdr.e_ident[EI_VERSION] = ei_version;
ehdr.e_ident[EI_OSABI] = ei_osabi;
ehdr.e_ident[EI_ABIVERSION] = ei_abiversion;
ehdr.e_type = htodump16(be, e_type);
ehdr.e_machine = htodump16(be, e_machine);
ehdr.e_version = htodump32(be, e_version);
ehdr.e_entry = htodump32(be, e_entry);
ehdr.e_phoff = htodump32(be, e_phoff);
ehdr.e_shoff = htodump32(be, e_shoff);
ehdr.e_flags = htodump32(be, e_flags);
ehdr.e_ehsize = htodump16(be, e_ehsize);
ehdr.e_phentsize = htodump16(be, e_phentsize);
ehdr.e_phnum = htodump16(be, e_phnum);
ehdr.e_shentsize = htodump16(be, e_shentsize);
ehdr.e_shnum = htodump16(be, e_shnum);
ehdr.e_shstrndx = htodump16(be, e_shstrndx);
if (write_chunk(f, 0, &ehdr, sizeof ehdr, "header"))
return TEST_ERR;
return TEST_OK;
}
static int
writeheader64(FILE *f)
{
Elf64_Ehdr ehdr;
/* initialize dump header to zero */
memset(&ehdr, 0, sizeof ehdr);
strncpy((char*)ehdr.e_ident, ei_mag, SELFMAG);
ehdr.e_ident[EI_CLASS] = ei_class;
ehdr.e_ident[EI_DATA] = ei_data;
ehdr.e_ident[EI_VERSION] = ei_version;
ehdr.e_ident[EI_OSABI] = ei_osabi;
ehdr.e_ident[EI_ABIVERSION] = ei_abiversion;
ehdr.e_type = htodump16(be, e_type);
ehdr.e_machine = htodump16(be, e_machine);
ehdr.e_version = htodump32(be, e_version);
ehdr.e_entry = htodump64(be, e_entry);
ehdr.e_phoff = htodump64(be, e_phoff);
ehdr.e_shoff = htodump64(be, e_shoff);
ehdr.e_flags = htodump32(be, e_flags);
ehdr.e_ehsize = htodump16(be, e_ehsize);
ehdr.e_phentsize = htodump16(be, e_phentsize);
ehdr.e_phnum = htodump16(be, e_phnum);
ehdr.e_shentsize = htodump16(be, e_shentsize);
ehdr.e_shnum = htodump16(be, e_shnum);
ehdr.e_shstrndx = htodump16(be, e_shstrndx);
if (write_chunk(f, 0, &ehdr, sizeof ehdr, "header"))
return TEST_ERR;
return TEST_OK;
}
static int
writeheader(FILE *f)
{
if (ei_class == ELFCLASS32)
return writeheader32(f);
else if (ei_class == ELFCLASS64)
return writeheader64(f);
fprintf(stderr, "Unsupported class: %llu\n", ei_class);
return TEST_ERR;
}
static unsigned long long
numarg(const char *s)
{
unsigned long long val;
char *end;
val = strtoull(s, &end, 0);
if (*end)
val = ULLONG_MAX;
return val;
}
static unsigned long long
stype(const char *s)
{
if (!strcmp(s, "NULL"))
return SHT_NULL;
else if (!strcmp(s, "PROGBITS"))
return SHT_PROGBITS;
else if (!strcmp(s, "STRTAB"))
return SHT_STRTAB;
else if (!strcmp(s, "NOTE"))
return SHT_NOTE;
return numarg(s);
}
static int
parseshdr(struct page_data *pg, char *p)
{
struct page_data_elf *pgelf = pg->priv;
char *endp, *v;
unsigned long long num;
memset(&pgelf->shdr, 0, sizeof pgelf->shdr);
while (*p) {
while (*p && isspace(*p))
++p;
endp = p;
while (*endp && !isspace(*endp))
++endp;
if (*endp)
*endp++ = '\0';
v = strchr(p, '=');
if (!v) {
fprintf(stderr, "Missing value: %s\n", p);
return TEST_ERR;
}
*v = '\0';
++v;
if (!strcmp(p, "name"))
num = pgelf->shdr.sh_name = numarg(v);
else if (!strcmp(p, "type"))
num = pgelf->shdr.sh_type = stype(v);
else if (!strcmp(p, "flags"))
num = pgelf->shdr.sh_flags = numarg(v);
else if (!strcmp(p, "addr"))
num = pgelf->shdr.sh_addr = numarg(v);
else if (!strcmp(p, "offset"))
num = pgelf->filepos = numarg(v);
else if (!strcmp(p, "link"))
num = pgelf->shdr.sh_link = numarg(v);
else if (!strcmp(p, "info"))
num = pgelf->shdr.sh_info = numarg(v);
else if (!strcmp(p, "align"))
num = pgelf->shdr.sh_addralign = numarg(v);
else if (!strcmp(p, "entsize"))
num = pgelf->shdr.sh_entsize = numarg(v);
else {
fprintf(stderr, "Invalid argument: %s\n", p);
return TEST_ERR;
}
if (num == ULLONG_MAX) {
fprintf(stderr, "Invalid %s value: %s\n", p, v);
return TEST_ERR;
}
p = endp;
}
return TEST_OK;
}
static unsigned long long
ptype(const char *s)
{
if (!strcmp(s, "NULL"))
return PT_NULL;
else if (!strcmp(s, "LOAD"))
return PT_LOAD;
else if (!strcmp(s, "NOTE"))
return PT_NOTE;
return numarg(s);
}
static int
parsephdr(struct page_data *pg, char *p)
{
struct page_data_elf *pgelf = pg->priv;
char *endp, *v;
unsigned long long num;
memset(&pgelf->phdr, 0, sizeof pgelf->phdr);
while (*p) {
while (*p && isspace(*p))
++p;
endp = p;
while (*endp && !isspace(*endp))
++endp;
if (*endp)
*endp++ = '\0';
v = strchr(p, '=');
if (!v) {
fprintf(stderr, "Missing value: %s\n", p);
return TEST_ERR;
}
*v = '\0';
++v;
if (!strcmp(p, "type"))
num = pgelf->phdr.p_type = ptype(v);
else if (!strcmp(p, "flags"))
num = pgelf->phdr.p_flags = numarg(v);
else if (!strcmp(p, "offset"))
num = pgelf->filepos = numarg(v);
else if (!strcmp(p, "vaddr"))
num = pgelf->phdr.p_vaddr = numarg(v);
else if (!strcmp(p, "paddr"))
num = pgelf->phdr.p_paddr = numarg(v);
else if (!strcmp(p, "memsz"))
num = pgelf->phdr.p_memsz = numarg(v);
else if (!strcmp(p, "align"))
num = pgelf->phdr.p_align = numarg(v);
else {
fprintf(stderr, "Invalid argument: %s\n", p);
return TEST_ERR;
}
if (num == ULLONG_MAX) {
fprintf(stderr, "Invalid %s value: %s\n", p, v);
return TEST_ERR;
}
p = endp;
}
return TEST_OK;
}
static int
parseheader(struct page_data *pg, char *p)
{
struct page_data_elf *pgelf = pg->priv;
char *endp, endc;
int rc;
if (pgelf->finhdr) {
rc = pgelf->finhdr(pg);
if (rc != TEST_OK)
return rc;
}
pgelf->filepos += pgelf->filesz;
pgelf->filesz = 0;
while (*p && isspace(*p))
++p;
endp = p;
while (*endp && !isspace(*endp))
++endp;
endc = *endp;
if (*endp)
*endp++ = '\0';
if (!strcmp(p, "shdr")) {
pgelf->parsehdr = parseshdr;
pgelf->finhdr = pgelf->finshdr;
} else if (!strcmp(p, "phdr")) {
pgelf->parsehdr = parsephdr;
pgelf->finhdr = pgelf->finphdr;
} else {
/* recover for type-specific parsing */
*endp = endc;
endp = p;
}
return pgelf->parsehdr(pg, endp);
}
static int
finshdr32(struct page_data *pg)
{
struct page_data_elf *pgelf = pg->priv;
Elf32_Shdr shdr;
off_t off;
shdr.sh_name = htodump32(be, pgelf->shdr.sh_name);
shdr.sh_type = htodump32(be, pgelf->shdr.sh_type);
shdr.sh_flags = htodump32(be, pgelf->shdr.sh_flags);
shdr.sh_addr = htodump32(be, pgelf->shdr.sh_addr);
shdr.sh_offset = htodump32(be, pgelf->filepos);
shdr.sh_size = htodump32(be, pgelf->filesz);
shdr.sh_link = htodump32(be, pgelf->shdr.sh_link);
shdr.sh_info = htodump32(be, pgelf->shdr.sh_info);
shdr.sh_addralign = htodump32(be, pgelf->shdr.sh_addralign);
shdr.sh_entsize = htodump32(be, pgelf->shdr.sh_entsize);
off = e_shoff + pgelf->shnum * sizeof shdr;
if (write_chunk(pgelf->f, off, &shdr, sizeof shdr, "section header"))
return TEST_ERR;
++pgelf->shnum;
return TEST_OK;
}
static int
finshdr64(struct page_data *pg)
{
struct page_data_elf *pgelf = pg->priv;
Elf64_Shdr shdr;
off_t off;
shdr.sh_name = htodump32(be, pgelf->shdr.sh_name);
shdr.sh_type = htodump32(be, pgelf->shdr.sh_type);
shdr.sh_flags = htodump64(be, pgelf->shdr.sh_flags);
shdr.sh_addr = htodump64(be, pgelf->shdr.sh_addr);
shdr.sh_offset = htodump64(be, pgelf->filepos);
shdr.sh_size = htodump64(be, pgelf->filesz);
shdr.sh_link = htodump32(be, pgelf->shdr.sh_link);
shdr.sh_info = htodump32(be, pgelf->shdr.sh_info);
shdr.sh_addralign = htodump64(be, pgelf->shdr.sh_addralign);
shdr.sh_entsize = htodump64(be, pgelf->shdr.sh_entsize);
off = e_shoff + pgelf->shnum * sizeof shdr;
if (write_chunk(pgelf->f, off, &shdr, sizeof shdr, "section header"))
return TEST_ERR;
++pgelf->shnum;
return TEST_OK;
}
static int
finphdr32(struct page_data *pg)
{
struct page_data_elf *pgelf = pg->priv;
Elf32_Phdr phdr;
off_t off;
if (!pgelf->phdr.p_memsz)
pgelf->phdr.p_memsz = pgelf->filesz;
phdr.p_type = htodump32(be, pgelf->phdr.p_type);
phdr.p_offset = htodump32(be, pgelf->filepos);
phdr.p_vaddr = htodump32(be, pgelf->phdr.p_vaddr);
phdr.p_paddr = htodump32(be, pgelf->phdr.p_paddr);
phdr.p_filesz = htodump32(be, pgelf->filesz);
phdr.p_memsz = htodump32(be, pgelf->phdr.p_memsz);
phdr.p_flags = htodump32(be, pgelf->phdr.p_flags);
phdr.p_align = htodump32(be, pgelf->phdr.p_align);
off = e_phoff + pgelf->phnum * sizeof phdr;
if (write_chunk(pgelf->f, off, &phdr, sizeof phdr, "program header"))
return TEST_ERR;
++pgelf->phnum;
return TEST_OK;
}
static int
finphdr64(struct page_data *pg)
{
struct page_data_elf *pgelf = pg->priv;
Elf64_Phdr phdr;
off_t off;
if (!pgelf->phdr.p_memsz)
pgelf->phdr.p_memsz = pgelf->filesz;
phdr.p_type = htodump32(be, pgelf->phdr.p_type);
phdr.p_flags = htodump64(be, pgelf->phdr.p_flags);
phdr.p_offset = htodump64(be, pgelf->filepos);
phdr.p_vaddr = htodump64(be, pgelf->phdr.p_vaddr);
phdr.p_paddr = htodump64(be, pgelf->phdr.p_paddr);
phdr.p_filesz = htodump64(be, pgelf->filesz);
phdr.p_memsz = htodump64(be, pgelf->phdr.p_memsz);
phdr.p_align = htodump64(be, pgelf->phdr.p_align);
off = e_phoff + pgelf->phnum * sizeof phdr;
if (write_chunk(pgelf->f, off, &phdr, sizeof phdr, "program header"))
return TEST_ERR;
++pgelf->phnum;
return TEST_OK;
}
static int
writepage(struct page_data *pg)
{
struct page_data_elf *pgelf = pg->priv;
pgelf->filesz += pg->len;
if (write_chunk(pgelf->f, pgelf->filepos, pg->buf, pg->len, "data"))
return TEST_ERR;
return TEST_OK;
}
static int
writedata(FILE *f)
{
struct page_data_elf pgelf;
struct page_data pg;
int rc;
if (!data_file)
return TEST_OK;
printf("Creating segments and/or sections\n");
memset(&pgelf, 0, sizeof pgelf);
pgelf.f = f;
pgelf.shnum = 0;
pgelf.phnum = 0;
if (ei_class == ELFCLASS32) {
pgelf.finshdr = finshdr32;
pgelf.finphdr = finphdr32;
} else if (ei_class == ELFCLASS64) {
pgelf.finshdr = finshdr64;
pgelf.finphdr = finphdr64;
} else {
fprintf(stderr, "Unsupported class: %llu\n", ei_class);
return TEST_ERR;
}
pg.endian = be;
pg.priv = &pgelf;
pg.parse_hdr = parseheader;
pg.write_page = writepage;
rc = process_data(&pg, data_file);
if (rc == TEST_OK && pgelf.finhdr)
rc = pgelf.finhdr(&pg);
if (!e_shnum)
e_shnum = pgelf.shnum;
if (!e_phnum)
e_phnum = pgelf.phnum;
return rc;
}
static int
writedump(FILE *f)
{
int rc;
if (flattened) {
struct makedumpfile_header hdr = {
.signature = MDF_SIGNATURE,
.type = htobe64(flattened_type),
.version = htobe64(flattened_version),
};
size_t remain;
if (fwrite(&hdr, sizeof hdr, 1, f) != 1) {
perror("flattened header");
return TEST_ERR;
}
remain = MDF_HEADER_SIZE - sizeof hdr;
while (remain--) {
if (putc(0, f) != 0) {
perror("flattened header padding");
return TEST_ERR;
}
}
}
if (ei_data == ELFDATA2LSB)
be = data_le;
else if (ei_data == ELFDATA2MSB)
be = data_be;
else {
fprintf(stderr, "Unsupported data format: %llu\n", ei_data);
return TEST_ERR;
}
rc = writedata(f);
if (rc != TEST_OK)
return rc;
rc = writeheader(f);
if (rc != TEST_OK)
return rc;
if (flattened) {
struct makedumpfile_data_header hdr = {
.offset = htobe64(MDF_OFFSET_END_FLAG),
.buf_size = htobe64(MDF_OFFSET_END_FLAG),
};
if (fwrite(&hdr, sizeof hdr, 1, f) != 1) {
perror("end segment header");
return TEST_ERR;
}
}
return TEST_OK;
}
static int
create_file(const char *name)
{
FILE *f;
int rc;
f = fopen(name, "w");
if (!f) {
perror("Cannot create output");
return TEST_ERR;
}
rc = writedump(f);
if (fclose(f) != 0) {
perror("Error closing output");
rc = TEST_ERR;
}
return rc;
}
int
main(int argc, char **argv)
{
int rc;
if (argc != 2) {
fprintf(stderr, "Usage: %s <dump>\n", argv[0]);
return TEST_ERR;
}
ei_mag = strdup(ELFMAG);
if (!ei_mag) {
perror("Cannot set default ELF identification bytes");
return TEST_ERR;
}
rc = parse_params_file(¶ms, stdin);
if (rc != TEST_OK)
return rc;
rc = create_file(argv[1]);
if (rc != TEST_OK)
return rc;
return TEST_OK;
}
|