xelf.c (21385B)
1 /* see LICENSE file for copyright and license details */ 2 3 /* xelf: records inside an executable, as ELF notes 4 * 5 * a record is one note owned by "xELF": type 1 a manifest (text), 2 the 6 * rules the program serves to a plumber (text), 3 an icon (image(6) 7 * qx) and 4 an icon in svg, the last two after a line "size <px>" 8 * (0 for scalable). type 0 is a record that was taken out. readers walk 9 * the program headers and ignore any type or note they do not know, so 10 * a strip that keeps the segments keeps the records 11 * 12 * adding records to a program that has none (or has them) works as 13 * patchelf does: the notes go in a segment of their own at the end of 14 * the file, with a copy of the program header table that has room for 15 * it, so no section moves and nothing needs relocating. what was there 16 * before is made type 0, and its segment becomes unused */ 17 18 #include "util.h" 19 20 #include <elf.h> 21 #include <fcntl.h> 22 #include <stdint.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <sys/stat.h> 27 #include <unistd.h> 28 29 #define OWNER "xELF" 30 #define OWNER_SZ 5 31 #define NHDR 12 32 #define REC_TEXT (512 * 1024) 33 #define REC_BINARY (1024 * 1024) 34 #define TOTAL_MAX (5 * 1024 * 1024) 35 #define PAGE_MIN 4096 36 37 enum { T_DEAD, T_MANIFEST, T_PLUMB, T_ICON, T_SVG }; 38 39 struct elf { 40 unsigned char *b; 41 size_t n; 42 int wide, big; 43 }; 44 45 struct ph { 46 uint32_t type, flags; 47 uint64_t offset, vaddr, paddr, filesz, memsz, align; 48 }; 49 50 struct rec { 51 uint32_t type; 52 unsigned char *data; 53 size_t len; 54 }; 55 56 static uint64_t 57 get(const struct elf *e, size_t off, int size) 58 { 59 uint64_t v = 0; 60 int i; 61 62 if (off + size > e->n) 63 eprintf("%s: truncated\n", argv0); 64 for (i = 0; i < size; i++) 65 v |= (uint64_t)e->b[off + (e->big ? size - 1 - i : i)] << (8 * i); 66 return v; 67 } 68 69 static void 70 put(const struct elf *e, unsigned char *at, uint64_t v, int size) 71 { 72 int i; 73 74 for (i = 0; i < size; i++) 75 at[e->big ? size - 1 - i : i] = (unsigned char)(v >> (8 * i)); 76 } 77 78 static unsigned char * 79 slurp(const char *path, size_t *len) 80 { 81 FILE *fp = fopen(path, "rb"); 82 unsigned char *b; 83 long n; 84 85 if (!fp) 86 eprintf("open %s:", path); 87 fseek(fp, 0, SEEK_END); 88 n = ftell(fp); 89 rewind(fp); 90 b = emalloc(n ? n : 1); 91 if (fread(b, 1, n, fp) != (size_t)n) 92 eprintf("read %s:", path); 93 fclose(fp); 94 *len = n; 95 return b; 96 } 97 98 static void 99 load(struct elf *e, const char *path) 100 { 101 e->b = slurp(path, &e->n); 102 if (e->n < 52 || memcmp(e->b, ELFMAG, SELFMAG)) 103 eprintf("%s: not an elf file\n", path); 104 if (e->b[EI_CLASS] != ELFCLASS32 && e->b[EI_CLASS] != ELFCLASS64) 105 eprintf("%s: unknown elf class\n", path); 106 e->wide = e->b[EI_CLASS] == ELFCLASS64; 107 e->big = e->b[EI_DATA] == ELFDATA2MSB; 108 if (get(e, 16, 2) != ET_EXEC && get(e, 16, 2) != ET_DYN) 109 eprintf("%s: not an executable or shared object\n", path); 110 } 111 112 /* the header's fields, by where each class keeps them */ 113 static uint64_t 114 phoff(const struct elf *e) 115 { 116 return e->wide ? get(e, 32, 8) : get(e, 28, 4); 117 } 118 119 static int 120 phentsize(const struct elf *e) 121 { 122 return e->wide ? 56 : 32; 123 } 124 125 static int 126 phnum(const struct elf *e) 127 { 128 return (int)get(e, e->wide ? 56 : 44, 2); 129 } 130 131 /* section headers, where a program has them: strip and its kin keep what 132 * a section names, so the notes get one too */ 133 #define SEC_NAME ".note.xelf" 134 135 struct sh { 136 uint32_t name, type; 137 uint64_t flags, addr, offset, size, align; 138 }; 139 140 static uint64_t 141 shoff(const struct elf *e) 142 { 143 return e->wide ? get(e, 40, 8) : get(e, 32, 4); 144 } 145 146 static int 147 shentsize(const struct elf *e) 148 { 149 return e->wide ? 64 : 40; 150 } 151 152 static int 153 shnum(const struct elf *e) 154 { 155 return shoff(e) ? (int)get(e, e->wide ? 60 : 48, 2) : 0; 156 } 157 158 static int 159 shstrndx(const struct elf *e) 160 { 161 return (int)get(e, e->wide ? 62 : 50, 2); 162 } 163 164 static struct sh 165 sh_read(const struct elf *e, const unsigned char *t) 166 { 167 struct elf v = *e; 168 struct sh s; 169 170 v.b = (unsigned char *)t; 171 v.n = shentsize(e); 172 s.name = (uint32_t)get(&v, 0, 4); 173 s.type = (uint32_t)get(&v, 4, 4); 174 if (e->wide) { 175 s.flags = get(&v, 8, 8); 176 s.addr = get(&v, 16, 8); 177 s.offset = get(&v, 24, 8); 178 s.size = get(&v, 32, 8); 179 s.align = get(&v, 48, 8); 180 } else { 181 s.flags = get(&v, 8, 4); 182 s.addr = get(&v, 12, 4); 183 s.offset = get(&v, 16, 4); 184 s.size = get(&v, 20, 4); 185 s.align = get(&v, 32, 4); 186 } 187 return s; 188 } 189 190 static void 191 sh_write(const struct elf *e, unsigned char *at, const struct sh *s) 192 { 193 put(e, at, s->name, 4); 194 put(e, at + 4, s->type, 4); 195 if (e->wide) { 196 put(e, at + 8, s->flags, 8); 197 put(e, at + 16, s->addr, 8); 198 put(e, at + 24, s->offset, 8); 199 put(e, at + 32, s->size, 8); 200 put(e, at + 48, s->align, 8); 201 } else { 202 put(e, at + 8, s->flags, 4); 203 put(e, at + 12, s->addr, 4); 204 put(e, at + 16, s->offset, 4); 205 put(e, at + 20, s->size, 4); 206 put(e, at + 32, s->align, 4); 207 } 208 } 209 210 /* the section table after the segment: a copy with the notes' section 211 * added and the table of names grown to hold its name. an earlier 212 * section of the notes is emptied, so repeated adds do not pile them up. 213 * the bytes go to *tail, from *at on (which they move past) */ 214 static void 215 sections_tail( 216 struct elf *e, 217 uint64_t note_off, 218 uint64_t note_addr, 219 size_t note_len, 220 unsigned char **tail, 221 size_t *tail_len, 222 size_t *at 223 ) 224 { 225 int n = shnum(e), si = shstrndx(e), i, ent = shentsize(e); 226 struct sh names, add_sh; 227 unsigned char *table, *strs; 228 size_t strs_at, table_at, name_off; 229 230 if (!n || shoff(e) + (uint64_t)n * ent > e->n || si >= n) 231 return; 232 names = sh_read(e, e->b + shoff(e) + (size_t)si * ent); 233 if (names.offset + names.size > e->n) 234 return; 235 strs_at = (*at + 7) & ~(size_t)7; 236 name_off = names.size; 237 strs = ecalloc(1, names.size + sizeof SEC_NAME); 238 memcpy(strs, e->b + names.offset, names.size); 239 memcpy(strs + name_off, SEC_NAME, sizeof SEC_NAME); 240 table_at = (strs_at + names.size + sizeof SEC_NAME + 7) & ~(size_t)7; 241 table = ecalloc(n + 1, ent); 242 memcpy(table, e->b + shoff(e), (size_t)n * ent); 243 for (i = 0; i < n; i++) { 244 struct sh x = sh_read(e, table + (size_t)i * ent); 245 246 if (x.name < names.size && !strcmp((char *)strs + x.name, SEC_NAME)) 247 memset(table + (size_t)i * ent, 0, ent); 248 } 249 names.offset = strs_at; 250 names.size += sizeof SEC_NAME; 251 sh_write(e, table + (size_t)si * ent, &names); 252 memset(&add_sh, 0, sizeof add_sh); 253 add_sh.name = (uint32_t)name_off; 254 add_sh.type = SHT_NOTE; 255 add_sh.flags = SHF_ALLOC; 256 add_sh.addr = note_addr; 257 add_sh.offset = note_off; 258 add_sh.size = note_len; 259 add_sh.align = 4; 260 sh_write(e, table + (size_t)n * ent, &add_sh); 261 262 *tail_len = table_at + (size_t)(n + 1) * ent - *at; 263 *tail = ecalloc(1, *tail_len); 264 memcpy(*tail + (strs_at - *at), strs, names.size); 265 memcpy(*tail + (table_at - *at), table, (size_t)(n + 1) * ent); 266 if (e->wide) { 267 put(e, e->b + 40, table_at, 8); 268 put(e, e->b + 60, n + 1, 2); 269 } else { 270 put(e, e->b + 32, table_at, 4); 271 put(e, e->b + 48, n + 1, 2); 272 } 273 free(strs); 274 free(table); 275 } 276 277 static struct ph 278 ph_read(const struct elf *e, int i) 279 { 280 size_t at = phoff(e) + (size_t)i * phentsize(e); 281 struct ph p; 282 283 p.type = (uint32_t)get(e, at, 4); 284 if (e->wide) { 285 p.flags = (uint32_t)get(e, at + 4, 4); 286 p.offset = get(e, at + 8, 8); 287 p.vaddr = get(e, at + 16, 8); 288 p.paddr = get(e, at + 24, 8); 289 p.filesz = get(e, at + 32, 8); 290 p.memsz = get(e, at + 40, 8); 291 p.align = get(e, at + 48, 8); 292 } else { 293 p.offset = get(e, at + 4, 4); 294 p.vaddr = get(e, at + 8, 4); 295 p.paddr = get(e, at + 12, 4); 296 p.filesz = get(e, at + 16, 4); 297 p.memsz = get(e, at + 20, 4); 298 p.flags = (uint32_t)get(e, at + 24, 4); 299 p.align = get(e, at + 28, 4); 300 } 301 return p; 302 } 303 304 static void 305 ph_write(const struct elf *e, unsigned char *at, const struct ph *p) 306 { 307 put(e, at, p->type, 4); 308 if (e->wide) { 309 put(e, at + 4, p->flags, 4); 310 put(e, at + 8, p->offset, 8); 311 put(e, at + 16, p->vaddr, 8); 312 put(e, at + 24, p->paddr, 8); 313 put(e, at + 32, p->filesz, 8); 314 put(e, at + 40, p->memsz, 8); 315 put(e, at + 48, p->align, 8); 316 } else { 317 put(e, at + 4, p->offset, 4); 318 put(e, at + 8, p->vaddr, 4); 319 put(e, at + 12, p->paddr, 4); 320 put(e, at + 16, p->filesz, 4); 321 put(e, at + 20, p->memsz, 4); 322 put(e, at + 24, p->flags, 4); 323 put(e, at + 28, p->align, 4); 324 } 325 } 326 327 /* one note of a segment: where it starts, its type, whether xELF owns 328 * it, and where its desc is. returns the offset of the next, 0 at the end 329 * or on a note that does not fit, which is the end of what can be read */ 330 static size_t 331 note_at( 332 const struct elf *e, size_t at, size_t end, uint32_t *type, int *ours, size_t *desc, size_t *len 333 ) 334 { 335 uint64_t namesz, descsz; 336 size_t next; 337 338 if (at + NHDR > end) 339 return 0; 340 namesz = get(e, at, 4); 341 descsz = get(e, at + 4, 4); 342 *type = (uint32_t)get(e, at + 8, 4); 343 next = at + NHDR + ((namesz + 3) & ~3ull); 344 *desc = next; 345 *len = descsz; 346 next += (descsz + 3) & ~3ull; 347 if (next > end || *desc + descsz > end) 348 return 0; 349 *ours = namesz == OWNER_SZ && !memcmp(e->b + at + NHDR, OWNER, OWNER_SZ); 350 return next; 351 } 352 353 /* calls f on each note of every PT_NOTE segment */ 354 typedef void (*note_fn)(struct elf *, size_t at, uint32_t type, size_t desc, size_t len, void *arg); 355 356 static void 357 each_note(struct elf *e, note_fn f, void *arg) 358 { 359 int i; 360 361 for (i = 0; i < phnum(e); i++) { 362 struct ph p = ph_read(e, i); 363 size_t at, end, next, desc, len; 364 uint32_t type; 365 int ours; 366 367 if (p.type != PT_NOTE) 368 continue; 369 end = p.offset + p.filesz; 370 if (end > e->n) 371 eprintf("%s: a note segment past the end of the file\n", argv0); 372 for (at = p.offset; at < end; at = next) { 373 next = note_at(e, at, end, &type, &ours, &desc, &len); 374 if (!next) 375 break; 376 if (ours) 377 f(e, at, type, desc, len, arg); 378 } 379 } 380 } 381 382 /* taking a record out: its type zero and its bytes too */ 383 static void 384 kill_note(struct elf *e, size_t at, uint32_t type, size_t desc, size_t len, void *arg) 385 { 386 (void)type; 387 (void)arg; 388 put(e, e->b + at + 8, T_DEAD, 4); 389 memset(e->b + desc, 0, len); 390 } 391 392 /* a note segment with no note of anyone else is of no use once its 393 * records are dead: it becomes unused, so the table does not grow */ 394 static void 395 drop_note_segments(struct elf *e) 396 { 397 int i; 398 399 for (i = 0; i < phnum(e); i++) { 400 struct ph p = ph_read(e, i); 401 size_t at, end, next, desc, len; 402 uint32_t type; 403 int ours, others = 0; 404 405 if (p.type != PT_NOTE) 406 continue; 407 end = p.offset + p.filesz; 408 for (at = p.offset; at < end; at = next) { 409 next = note_at(e, at, end, &type, &ours, &desc, &len); 410 if (!next) 411 break; 412 if (!ours) 413 others = 1; 414 } 415 if (!others) 416 put(e, e->b + phoff(e) + (size_t)i * phentsize(e), PT_NULL, 4); 417 } 418 } 419 420 static void 421 dump_note(struct elf *e, size_t at, uint32_t type, size_t desc, size_t len, void *arg) 422 { 423 (void)at; 424 (void)arg; 425 printf("type %u\n", type); 426 if (type == T_MANIFEST || type == T_PLUMB || type == T_SVG) { 427 fwrite(e->b + desc, 1, len, stdout); 428 if (!len || e->b[desc + len - 1] != '\n') 429 putchar('\n'); 430 } else if (type == T_ICON) { 431 size_t n = 0; 432 433 while (n < len && e->b[desc + n] != '\n') 434 n++; 435 printf("%.*s (%zu bytes of qx)\n", (int)n, e->b + desc, len - n - 1); 436 } else { 437 printf("%zu bytes\n", len); 438 } 439 } 440 441 /* the file as it is, then zeros to at, then extra, then more */ 442 static void 443 write_out( 444 const struct elf *e, 445 const char *path, 446 const unsigned char *extra, 447 size_t extra_len, 448 size_t at, 449 const unsigned char *more, 450 size_t more_len 451 ) 452 { 453 char tmp[4096]; 454 struct stat st; 455 FILE *fp; 456 size_t pad; 457 458 if (stat(path, &st) < 0) 459 eprintf("stat %s:", path); 460 snprintf(tmp, sizeof tmp, "%s.xelf", path); 461 fp = fopen(tmp, "wb"); 462 if (!fp) 463 eprintf("create %s:", tmp); 464 fwrite(e->b, 1, e->n, fp); 465 for (pad = e->n; pad < at; pad++) 466 fputc(0, fp); 467 fwrite(extra, 1, extra_len, fp); 468 fwrite(more, 1, more_len, fp); 469 if (fclose(fp) || chmod(tmp, st.st_mode & 07777) < 0 || rename(tmp, path) < 0) { 470 unlink(tmp); 471 eprintf("write %s:", path); 472 } 473 } 474 475 /* the width a qx file says it has, the third field of its first line, 476 * "QX version width height ...", 0 when the bytes are not qx */ 477 static unsigned long 478 qx_width(const unsigned char *b, size_t n) 479 { 480 unsigned long w = 0; 481 size_t i = 3; 482 483 if (n < 4 || memcmp(b, "QX ", 3)) 484 return 0; 485 while (i < n && b[i] >= '0' && b[i] <= '9') 486 i++; 487 if (i == 3 || i >= n || b[i++] != ' ') 488 return 0; 489 for (; i < n && b[i] >= '0' && b[i] <= '9' && w < 1UL << 30; i++) 490 w = w * 10 + (unsigned long)(b[i] - '0'); 491 return i < n && b[i] == ' ' ? w : 0; 492 } 493 494 /* an icon file as a record: svg as it is, anything else must be qx, whose 495 * width is the size the record says */ 496 static struct rec 497 icon_rec(const char *path) 498 { 499 struct rec r; 500 size_t n, len; 501 unsigned char *file = slurp(path, &len), *d; 502 char head[32]; 503 const char *dot = strrchr(path, '.'); 504 505 if (dot && !strcmp(dot, ".svg")) { 506 r.type = T_SVG; 507 snprintf(head, sizeof head, "size 0\n"); 508 } else { 509 unsigned long size = qx_width(file, len); 510 511 if (!size) 512 eprintf("%s: not an svg or a qx file\n", path); 513 r.type = T_ICON; 514 snprintf(head, sizeof head, "size %lu\n", size); 515 } 516 n = strlen(head); 517 d = emalloc(n + len); 518 memcpy(d, head, n); 519 memcpy(d + n, file, len); 520 free(file); 521 r.data = d; 522 r.len = n + len; 523 return r; 524 } 525 526 static struct rec 527 text_rec(uint32_t type, const char *path) 528 { 529 struct rec r; 530 531 r.type = type; 532 r.data = slurp(path, &r.len); 533 if (r.len > REC_TEXT) 534 eprintf("%s: a text record is at most %d bytes\n", path, REC_TEXT); 535 return r; 536 } 537 538 /* the records as notes, each padded to 4 */ 539 static unsigned char * 540 notes_of(const struct elf *e, const struct rec *r, int n, size_t *len) 541 { 542 size_t total = 0, at = 0; 543 unsigned char *buf; 544 int i; 545 546 for (i = 0; i < n; i++) { 547 if ((r[i].type == T_ICON || r[i].type == T_SVG) && r[i].len > REC_BINARY) 548 eprintf("%s: an icon record is at most %d bytes\n", argv0, REC_BINARY); 549 total += NHDR + 8 + ((r[i].len + 3) & ~(size_t)3); 550 } 551 if (total > TOTAL_MAX) 552 eprintf("%s: records of %zu bytes, at most %d\n", argv0, total, TOTAL_MAX); 553 buf = ecalloc(1, total); 554 for (i = 0; i < n; i++) { 555 put(e, buf + at, OWNER_SZ, 4); 556 put(e, buf + at + 4, r[i].len, 4); 557 put(e, buf + at + 8, r[i].type, 4); 558 memcpy(buf + at + NHDR, OWNER, OWNER_SZ); 559 memcpy(buf + at + NHDR + 8, r[i].data, r[i].len); 560 at += NHDR + 8 + ((r[i].len + 3) & ~(size_t)3); 561 } 562 *len = total; 563 return buf; 564 } 565 566 /* the segment the notes go in: a copy of the program header table with 567 * two more entries, then the notes. its address is above every segment 568 * there is, and congruent to its place in the file by the biggest 569 * alignment the file uses, so any page size maps it */ 570 static void 571 add(struct elf *e, const char *path, const struct rec *recs, int nrecs) 572 { 573 int n = phnum(e), i, last_load = -1, m; 574 uint64_t top = 0, align = PAGE_MIN, off, vaddr, ptsz; 575 struct ph *old = emalloc((n + 2) * sizeof *old), load_ph, note_ph; 576 unsigned char *table, *notes, *seg, *tail = NULL; 577 size_t notes_len, seg_len, tail_len = 0, tail_at; 578 579 each_note(e, kill_note, NULL); 580 drop_note_segments(e); 581 for (i = 0; i < n; i++) { 582 old[i] = ph_read(e, i); 583 if (old[i].type != PT_LOAD) 584 continue; 585 last_load = i; 586 if (old[i].vaddr + old[i].memsz > top) 587 top = old[i].vaddr + old[i].memsz; 588 if (old[i].align > align) 589 align = old[i].align; 590 } 591 if (last_load < 0) 592 eprintf("%s: no loadable segment\n", path); 593 m = n + 2; 594 ptsz = (uint64_t)m * phentsize(e); 595 notes = notes_of(e, recs, nrecs, ¬es_len); 596 597 off = (e->n + align - 1) & ~(align - 1); 598 vaddr = ((top + align - 1) & ~(align - 1)) + (off & (align - 1)); 599 /* off is a multiple of align, so the low bits added to vaddr are none */ 600 seg_len = ptsz + notes_len; 601 seg = ecalloc(1, seg_len); 602 table = seg; 603 tail_at = off + seg_len; 604 605 memset(&load_ph, 0, sizeof load_ph); 606 load_ph.type = PT_LOAD; 607 load_ph.flags = PF_R; 608 load_ph.offset = off; 609 load_ph.vaddr = vaddr; 610 load_ph.paddr = vaddr; 611 load_ph.filesz = seg_len; 612 load_ph.memsz = seg_len; 613 load_ph.align = align; 614 note_ph = load_ph; 615 note_ph.type = PT_NOTE; 616 note_ph.offset = off + ptsz; 617 note_ph.vaddr = vaddr + ptsz; 618 note_ph.paddr = vaddr + ptsz; 619 note_ph.filesz = notes_len; 620 note_ph.memsz = notes_len; 621 note_ph.align = 4; 622 note_ph.flags = PF_R; 623 624 /* the new load follows the last old one, so loads stay in address order, 625 * the note segment goes at the end */ 626 for (i = 0; i < m; i++) { 627 struct ph p; 628 629 if (i <= last_load) 630 p = old[i]; 631 else if (i == last_load + 1) 632 p = load_ph; 633 else if (i == m - 1) 634 p = note_ph; 635 else 636 p = old[i - 1]; 637 if (p.type == PT_PHDR) { 638 p.offset = off; 639 p.vaddr = vaddr; 640 p.paddr = vaddr; 641 p.filesz = ptsz; 642 p.memsz = ptsz; 643 } 644 ph_write(e, table + (size_t)i * phentsize(e), &p); 645 } 646 memcpy(seg + ptsz, notes, notes_len); 647 sections_tail(e, off + ptsz, vaddr + ptsz, notes_len, &tail, &tail_len, &tail_at); 648 649 /* the header now points at the copy */ 650 if (e->wide) { 651 put(e, e->b + 32, off, 8); 652 put(e, e->b + 56, m, 2); 653 } else { 654 put(e, e->b + 28, off, 4); 655 put(e, e->b + 44, m, 2); 656 } 657 write_out(e, path, seg, seg_len, off, tail, tail_len); 658 free(tail); 659 free(old); 660 free(notes); 661 free(seg); 662 } 663 664 static void 665 check_load_order(const struct elf *e, const char *path) 666 { 667 int i, n = phnum(e), seen_load = 0, phdr_ok = 1; 668 uint64_t prev = 0, tbl = phoff(e), tblend = phoff(e) + (uint64_t)n * phentsize(e); 669 int in_load = 0; 670 671 for (i = 0; i < n; i++) { 672 struct ph p = ph_read(e, i); 673 674 if (p.type == PT_LOAD) { 675 uint64_t a = p.align > 1 ? p.align : 1; 676 677 if (seen_load && p.vaddr < prev) 678 eprintf("%s: loads are not in address order\n", path); 679 prev = p.vaddr; 680 seen_load = 1; 681 if (p.align > 1 && (p.vaddr % a) != (p.offset % a)) 682 eprintf("%s: a load whose address and offset differ modulo its alignment\n", path); 683 if (p.offset + p.filesz > e->n) 684 eprintf("%s: a load past the end of the file\n", path); 685 if (tbl >= p.offset && tblend <= p.offset + p.filesz) 686 in_load = 1; 687 } else if (p.type == PT_PHDR && seen_load) { 688 phdr_ok = 0; 689 } 690 } 691 if (!phdr_ok) 692 eprintf("%s: the PT_PHDR entry follows a load\n", path); 693 if (!in_load) 694 eprintf("%s: the program header table is in no load segment\n", path); 695 } 696 697 static void 698 check_note(struct elf *e, size_t at, uint32_t type, size_t desc, size_t len, void *arg) 699 { 700 (void)at; 701 if (type == T_ICON) { 702 size_t n = 0; 703 unsigned long said = 0; 704 705 while (n < len && e->b[desc + n] != '\n') 706 n++; 707 if (n < 6 || memcmp(e->b + desc, "size ", 5)) 708 eprintf("%s: an icon record with no size line\n", argv0); 709 said = strtoul((char *)e->b + desc + 5, NULL, 10); 710 if (said != qx_width(e->b + desc + n + 1, len - n - 1)) 711 eprintf("%s: an icon record whose size is not its picture's width\n", argv0); 712 } 713 ++*(int *)arg; 714 } 715 716 static void 717 usage(void) 718 { 719 eprintf( 720 "usage: %s add [-p plumb] bin manifest [icon ...] | dump bin | rm bin | check bin\n", argv0 721 ); 722 } 723 724 // ?man xelf: records inside an executable 725 // ?man arguments: add [-p plumb] bin manifest [icon ...] | dump bin | rm bin | check bin 726 // ?man keeps records in ELF notes owned by xELF: a manifest, the rules the 727 // ?man program serves to a plumber and icons (qx or svg). add 728 // ?man replaces what the program carries, dump prints it, rm takes it 729 // ?man out and check says whether the program is laid out as a loader 730 // ?man needs it 731 int 732 main(int argc, char *argv[]) 733 { 734 struct elf e; 735 const char *plumb = NULL; 736 const char *verb; 737 int count = 0; 738 739 argv0 = argc ? argv[0] : "xelf"; 740 if (argc < 3) 741 usage(); 742 verb = argv[1]; 743 argv += 2; 744 argc -= 2; 745 746 if (!strcmp(verb, "add")) { 747 struct rec *recs; 748 int n = 0; 749 750 if (!strcmp(argv[0], "-p")) { 751 if (argc < 4) 752 usage(); 753 plumb = argv[1]; 754 argv += 2; 755 argc -= 2; 756 } 757 if (argc < 2) 758 usage(); 759 load(&e, argv[0]); 760 recs = ecalloc(argc + 1, sizeof *recs); 761 recs[n++] = text_rec(T_MANIFEST, argv[1]); 762 if (plumb) 763 recs[n++] = text_rec(T_PLUMB, plumb); 764 for (int i = 2; i < argc; i++) 765 recs[n++] = icon_rec(argv[i]); 766 add(&e, argv[0], recs, n); 767 return 0; 768 } 769 load(&e, argv[0]); 770 if (!strcmp(verb, "dump")) { 771 each_note(&e, dump_note, NULL); 772 } else if (!strcmp(verb, "rm")) { 773 each_note(&e, kill_note, NULL); 774 drop_note_segments(&e); 775 write_out(&e, argv[0], NULL, 0, e.n, NULL, 0); 776 } else if (!strcmp(verb, "check")) { 777 check_load_order(&e, argv[0]); 778 each_note(&e, check_note, &count); 779 printf("%s: ok, %d records\n", argv[0], count); 780 } else { 781 usage(); 782 } 783 return 0; 784 }