all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob f31c9a8974414d56d4fb63058aa76a6d948648b1 28964 bytes (raw)
name: exec/exec.c 	 # note: path name is non-authoritative(*)

   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
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
 
/* Program execution for Emacs.

Copyright (C) 2023-2024 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs 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 a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

#include <sys/ptrace.h>
#include <sys/param.h>
#include <sys/mman.h>

#include "exec.h"

#if defined __mips__ && !defined MIPS_NABI
#include "mipsfpu.h"
#endif /* defined __mips__ && !defined MIPS_NABI */

\f


/* Define replacements for required string functions.  */

#if !defined HAVE_STPCPY || !defined HAVE_DECL_STPCPY

/* Copy SRC to DEST, returning the address of the terminating '\0' in
   DEST.  */

static char *
rpl_stpcpy (char *dest, const char *src)
{
  register char *d;
  register const char *s;

  d = dest;
  s = src;

  do
    *d++ = *s;
  while (*s++ != '\0');

  return d - 1;
}

#define stpcpy rpl_stpcpy
#endif /* !defined HAVE_STPCPY || !defined HAVE_DECL_STPCPY */

\f

/* Executable reading functions.
   These functions extract information from an executable that is
   about to be loaded.

   `exec_0' takes the name of the program, determines whether or not
   its format is correct, and if so, returns the list of actions that
   the loader should perform.

   The actions include:

     - Making the stack executable, if PT_GNU_STACK.
     - Mapping PT_LOAD sections into the executable with the correct
       memory protection.
     - On MIPS, setting the floating point register size.
     - Transferring control to the interpreter or executable.  */
\f

/* Check whether or not FD starts with a #!, and return the executable
   to load if it does.  Value is NAME if no interpreter character was
   found, or the interpreter otherwise.  Value is NULL upon an IO
   error.

   If an additional command line argument is specified, place it in
   *EXTRA.  */

static const char *
check_interpreter (const char *name, int fd, const char **extra)
{
  static char buffer[PATH_MAX], *start;
  char first[2], *end, *ws;
  ssize_t rc;

  /* Read the first character.  */
  rc = read (fd, &first, 2);

  if (rc != 2)
    goto fail;

  if (first[0] != '#' || first[1] != '!')
    goto nomatch;

  rc = read (fd, buffer, PATH_MAX);

  if (rc < 0)
    goto fail;

  /* Strip leading whitespace.  */
  start = buffer;
  while (start < buffer + rc && (*start == ' ' || *start == '\t'))
    ++start;

  /* Look for a newline character.  */
  end = memchr (start, '\n', buffer + rc - start);

  if (!end)
    goto fail;

  /* The string containing the interpreter is now in start.  NULL
     terminate it.  */
  *end = '\0';

  /* Now look for any whitespace characters.  */
  for (ws = start; *ws && *ws != ' ' && *ws != '\t'; ws++)
    continue;

  /* If there's no whitespace, return the entire start.  */

  if (!*ws)
    {
      if (lseek (fd, 0, SEEK_SET))
	goto fail;

      return start;
    }

  /* Otherwise, split the string at the whitespace and return the
     additional argument.  */
  *ws = '\0';

  if (lseek (fd, 0, SEEK_SET))
    goto fail;

  *extra = ws + 1;
  return start;

 nomatch:
  /* There's no interpreter.  */
  if (lseek (fd, 0, SEEK_SET))
    goto fail;

  return name;

 fail:
  errno = ENOEXEC;
  return NULL;
}

/* Static area used to store data placed on the loader's stack.  */
static char loader_area[65536];

/* Number of bytes used in that area.  */
static int loader_area_used;

\f

/* Structure definitions for commands placed in the loader area.
   Arrange these so that each member is naturally aligned.  */

struct exec_open_command
{
  /* Word identifying the type of this command.  */
  USER_WORD command;

  /* NULL-terminated file name follows, padded to the size of a user
     word.  */
};

struct exec_map_command
{
  /* Word identifying the type of this command.  */
  USER_WORD command;

  /* Where the file will be mapped.  */
  USER_WORD vm_address;

  /* Offset into the file to map from.  */
  USER_WORD file_offset;

  /* Memory protection for mprotect.  */
  USER_WORD protection;

  /* Number of bytes to be mapped.  */
  USER_WORD length;

  /* Flags for mmap.  */
  USER_WORD flags;

  /* Number of bytes to clear at the end of this mapping.  */
  USER_WORD clear;
};

struct exec_jump_command
{
  /* Word identifying the type of this command.  */
  USER_WORD command;

  /* Address to jump to.  */
  USER_WORD entry;

  /* The value of AT_ENTRY inside the aux vector.  */
  USER_WORD at_entry;

  /* The value of AT_PHENT inside the aux vector.  */
  USER_WORD at_phent;

  /* The value of AT_PHNUM inside the aux vector.  */
  USER_WORD at_phnum;

  /* The value of AT_PHDR inside the aux vector.  */
  USER_WORD at_phdr;

  /* The value of AT_BASE inside the aux vector.  */
  USER_WORD at_base;

#if defined __mips__ && !defined MIPS_NABI
  /* The FPU mode to apply.  */
  USER_WORD fpu_mode;
#endif /* defined __mips__ && !defined MIPS_NABI */
};

\f

/* Write a command to open the file NAME to the loader area.
   If ALTERNATE is true, then use the command code 16 instead
   of 0.  Value is 1 upon failure, else 0.  */

static int
write_open_command (const char *name, bool alternate)
{
  struct exec_open_command command;
  size_t size;

  /* First, write the command to open NAME.  This is followed by NAME
     itself, padded to sizeof (USER_WORD) bytes.  */

  command.command = alternate ? 16 : 0;
  if (sizeof loader_area - loader_area_used < sizeof command)
    return 1;
  memcpy (loader_area + loader_area_used, &command, sizeof command);
  loader_area_used += sizeof command;

  /* Calculate the length of NAME.  */
  size = strlen (name) + 1;

  /* Round it up.  */
  size = ((size + (sizeof (USER_WORD) - 1))
	  & ~(sizeof (USER_WORD) - 1));

  if (sizeof loader_area - loader_area_used < size)
    return 1;

  /* Now copy name to the loader area, filling the padding with NULL
     bytes.  */
  strncpy (loader_area + loader_area_used, name, size);

  /* Increase loader_area_used.  */
  loader_area_used += size;
  return 0;
}

/* Write the commands necessary to map the executable file into memory
   for the given PT_LOAD program HEADER.  Value is 1 upon failure,
   else 0.  If USE_ALTERNATE, use the command code 17 instead of
   1.

   Apply the given OFFSET to virtual addresses that will be mapped.  */

static int
write_load_command (program_header *header, bool use_alternate,
		    USER_WORD offset)
{
  struct exec_map_command command;
  struct exec_map_command command1;
  USER_WORD start, end;
  bool need_command1;
#ifndef PAGE_MASK
  static long pagesize;
#endif /* !PAGE_MASK */

  /* First, write the commands necessary to map the specified segment
     itself.

     This is the area between header->p_vaddr and header->p_filesz,
     rounded up to the page size.  */

#ifndef PAGE_MASK
  /* This system doesn't define a fixed page size.  */

#ifdef HAVE_GETPAGESIZE
  if (!pagesize)
    pagesize = getpagesize ();
#else /* !HAVE_GETPAGESIZE */
  if (!pagesize)
    pagesize = sysconf (_SC_PAGESIZE);
#endif /* !HAVE_GETPAGESIZE */

#define PAGE_MASK (~(pagesize - 1))
#define PAGE_SIZE (pagesize)
#endif /* !PAGE_MASK */

  start = header->p_vaddr & PAGE_MASK;
  end = ((header->p_vaddr + header->p_filesz
	  + PAGE_SIZE)
	 & PAGE_MASK);

  command.command = use_alternate ? 17 : 1;
  command.vm_address = start;
  command.file_offset = header->p_offset & PAGE_MASK;
  command.protection = 0;
  command.length = end - start;
  command.clear = 0;
  command.flags = MAP_PRIVATE | MAP_FIXED;

  /* Apply the memory protection specified in the header.  */

  if (header->p_flags & 4) /* PF_R */
    command.protection |= PROT_READ;

  if (header->p_flags & 2) /* PF_W */
    command.protection |= PROT_WRITE;

  if (header->p_flags & 1) /* PF_X */
    command.protection |= PROT_EXEC;

  /* Next, write any command necessary to map pages in the area
     between p_filesz and p_memsz.  */
  need_command1 = false;

  if (header->p_memsz > header->p_filesz)
    {
      /* If there are bytes after end which need to be initialized, do
	 that now.  */
      command.clear = end - header->p_vaddr - header->p_filesz;
      start = end;
      end = header->p_vaddr + header->p_memsz + PAGE_SIZE;
      end &= PAGE_MASK;

      if (end > start)
	{
	  command1.command = 4;
	  command1.vm_address = start;
	  command1.file_offset = 0;
	  command1.length = end - start;
	  command1.clear = 0;
	  command1.protection = command.protection;
	  command1.flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;
	  need_command1 = true;
	}
    }

  /* Apply the offset to both commands if necessary.  */

  if (offset)
    {
      if (need_command1)
	command1.vm_address += offset;

      command.vm_address += offset;
    }

  /* Write both commands.  */

  if (sizeof loader_area - loader_area_used < sizeof command)
    return 1;

  memcpy (loader_area + loader_area_used, &command,
	  sizeof command);
  loader_area_used += sizeof command;

  if (!need_command1)
    return 0;

  if (sizeof loader_area - loader_area_used < sizeof command1)
    return 1;

  memcpy (loader_area + loader_area_used, &command1,
	  sizeof command1);
  loader_area_used += sizeof command1;

  return 0;
}

#if defined __mips__ && !defined MIPS_NABI

/* Static storage used for MIPS ABI flags.  */
static struct mips_elf_abi_flags exec_abi, interpreter_abi;

/* Static storage for interpreter headers.  */
static elf_header exec_interpreter_header;

/* Pointer to the ELF header of this executable's interpreter.  */
static elf_header *interpreter_header;

/* Pointer to any PT_MIPS_ABIFLAGS program header found in the
   executable itself.  */
static struct mips_elf_abi_flags *exec_abiflags;

/* Pointer to any PT_MIPS_ABIFLAGS program header found in the
   executable's ELF interpreter.  */
static struct mips_elf_abi_flags *interpreter_abiflags;

#endif /* defined __mips__ && !defined MIPS_NABI */

/* Process the specified program HEADER; HEADER is from the ELF
   interpreter of another executable.  FD is the executable file from
   which it is being read, NAME is its file name, and ELF_HEADER is
   its header.

   If ELF_HEADER->e_type is ET_DYN, add the base address for position
   independent interpreter code to virtual addresses.

   Value is 1 upon failure, else 0.  */

static int
process_interpreter_1 (const char *name, int fd,
		       program_header *header,
		       elf_header *elf_header)
{
  int rc;
#if defined __mips__ && !defined MIPS_NABI
  ssize_t rc1;
#endif /* defined __mips__ && !defined MIPS_NABI */

  switch (header->p_type)
    {
    default: /* PT_NULL, PT_NOTE, PT_DYNAMIC, PT_INTERP, et cetera */
      rc = 0;
      break;

    case 1: /* PT_LOAD */
      /* This describes a segment in the file that must be loaded.
	 Write the appropriate load command.  */

      if (elf_header->e_type == 3) /* ET_DYN */
	rc = write_load_command (header, true,
				 INTERPRETER_BASE);
      else
	rc = write_load_command (header, true, 0);

      break;

#if defined __mips__ && !defined MIPS_NABI
    case 0x70000003: /* PT_MIPS_ABIFLAGS */
      /* Record this header for later use.  */
      rc1 = pread (fd, &interpreter_abi, sizeof interpreter_abi,
		   header->p_offset);

      if (rc1 != sizeof interpreter_abi)
	return 1;

      interpreter_abiflags = &interpreter_abi;
      rc = 0;
#endif /* defined __mips__ && !defined MIPS_NABI */
    }

  return rc;
}

/* Read the ELF interpreter specified in the given program header from
   FD, and append the commands necessary to load it to the load area.
   Then, return the interpreter entry point in *ENTRY.

   Value is 1 upon failure, else 0.  */

static int
process_interpreter (int fd, program_header *prog_header,
		     USER_WORD *entry)
{
  char buffer[PATH_MAX + 1];
  int rc, size, i;
  elf_header header;
  program_header program;

  /* Read the interpreter name.  */
  size = MIN (prog_header->p_filesz, PATH_MAX);
  rc = pread (fd, buffer, size, prog_header->p_offset);
  if (rc < size)
    return 1;

  /* Make sure the name is NULL terminated.  */
  buffer[size] = '\0';

  /* Check if the file is executable.  This is unfortunately not
     atomic.  */

  if (access (buffer, X_OK))
    return 1;

  /* Read the interpreter's header much like exec_0.

     However, use special command codes in `process_program_header' if
     it is position independent.  That way, the loader knows it should
     use the open interpreter instead.  */

  fd = open (buffer, O_RDONLY);

  if (fd < 0)
    return 1;

  rc = read (fd, &header, sizeof header);

  if (rc < sizeof header)
    goto fail;

#if defined __mips__ && !defined MIPS_NABI
  /* Record this interpreter's header for later use determining the
     floating point ABI.  */
  exec_interpreter_header = header;
  interpreter_header = &exec_interpreter_header;
#endif /* defined __mips__ && !defined MIPS_NABI */

  /* Verify that this is indeed an ELF file.  */

  if (header.e_ident[0] != 0x7f
      || header.e_ident[1] != 'E'
      || header.e_ident[2] != 'L'
      || header.e_ident[3] != 'F')
    goto fail;

  /* Now check that the class is correct.  */
#ifdef EXEC_64
  if (header.e_ident[4] != 2)
    goto fail;
#else /* !EXEC_64 */
  if (header.e_ident[4] != 1)
    goto fail;
#endif /* EXEC_64 */

  /* And the endianness.  */
#ifndef WORDS_BIGENDIAN
  if (header.e_ident[5] != 1)
    goto fail;
#else /* WORDS_BIGENDIAN */
  if (header.e_ident[5] != 2)
    goto fail;
#endif /* EXEC_64 */

  /* Check that this is an executable.  */
  if (header.e_type != 2 && header.e_type != 3)
    goto fail;

  /* Now check that the ELF program header makes sense.  */
  if (header.e_phnum > 0xffff
      || (header.e_phentsize
	  != sizeof (program_header)))
    goto fail;

  if (write_open_command (buffer, true))
    goto fail;

  for (i = 0; i < header.e_phnum; ++i)
    {
      rc = read (fd, &program, sizeof program);
      if (rc < sizeof program)
	goto fail;

      if (process_interpreter_1 (buffer, fd, &program,
				 &header))
	goto fail;
    }

  if (header.e_type == 3) /* ET_DYN */
    *entry = header.e_entry + INTERPRETER_BASE;
  else
    *entry = header.e_entry;

  close (fd);
  return 0;

 fail:
  close (fd);
  return 1;
}

/* Process the specified program HEADER.  FD is the executable file
   from which it is being read, NAME is its file name, and ELF_HEADER
   is its header.

   If ELF_HEADER->e_type is ET_DYN, add the base address for position
   independent code to virtual addresses.

   If OFFSET is non-NULL, and *OFFSET is -1, write the virtual address
   of HEADER if it describes a PT_LOAD segment.

   If an interpreter is found, set *ENTRY to its entry point.

   Value is 1 upon failure, else 0.  */

static int
process_program_header (const char *name, int fd,
			program_header *header,
			elf_header *elf_header,
			USER_WORD *entry,
			USER_WORD *offset)
{
  int rc;
#if defined __mips__ && !defined MIPS_NABI
  ssize_t rc1;
#endif /* defined __mips__ && !defined MIPS_NABI */

  switch (header->p_type)
    {
    default: /* PT_NULL, PT_NOTE, PT_DYNAMIC, et cetera */
      rc = 0;
      break;

    case 1: /* PT_LOAD */
      /* This describes a segment in the file that must be loaded.
	 Write the appropriate load command.  */

      if (elf_header->e_type == 3) /* ET_DYN */
	{
	  rc = write_load_command (header, false,
				   EXECUTABLE_BASE);

	  if (!rc && offset && *offset == (USER_WORD) -1)
	    *offset = EXECUTABLE_BASE + header->p_vaddr;
	}
      else
	{
	  rc = write_load_command (header, false, 0);

	  if (!rc && offset && *offset == (USER_WORD) -1)
	    *offset = header->p_vaddr;
	}

      break;

    case 3: /* PT_INTERP */
      /* This describes another executable that must be loaded.  Open
	 the interpreter and process each of its headers as well.  */
      rc = process_interpreter (fd, header, entry);
      break;

    case 1685382481: /* PT_GNU_STACK */
      /* TODO */
      rc = 0;
      break;

#if defined __mips__ && !defined MIPS_NABI
    case 0x70000003: /* PT_MIPS_ABIFLAGS */
      /* Record this header for later use.  */
      rc1 = pread (fd, &exec_abi, sizeof exec_abi,
		   header->p_offset);

      if (rc1 != sizeof exec_abi)
	return 1;

      exec_abiflags = &exec_abi;
      rc = 0;
#endif /* defined __mips__ && !defined MIPS_NABI */
    }

  return rc;
}

/* Prepend one or two extra arguments ARG1 and ARG2 to a pending
   execve system call.  Replace the argument immediately after
   with ARG3.

   TRACEE is the tracee performing the system call, and REGS are its
   current user registers.  Value is 1 upon failure, else 0.  */

static int
insert_args (struct exec_tracee *tracee, USER_REGS_STRUCT *regs,
	     const char *arg1, const char *arg2, const char *arg3)
{
  USER_WORD argv, argc, word, new;
  USER_WORD new1, new2, new3, i;
  size_t text_size, effective_size;
  USER_REGS_STRUCT original;

  /* First, get a pointer to the current argument vector.  */
  argv = regs->SYSCALL_ARG1_REG;

  /* Now figure out how many arguments there are.  */
  argc = 0;
  while (true)
    {
      /* Clear errno.  PTRACE_PEEKDATA returns the word read the same
	 way failure indications are returned, so the only way to
	 catch IO errors is by clearing errno before the call to
	 ptrace and checking it afterwards.  */

      errno = 0;
      word = ptrace (PTRACE_PEEKDATA, tracee->pid,
		     (void *) argv, NULL);
      argv += sizeof (USER_WORD);

      if (errno)
	return 1;

      if (!word)
	break;

      ++argc;
    };

  /* Allocate enough to hold that many arguments, alongside the argc
     text.  */

  text_size = (strlen (arg1) + 1
	       + (arg2 ? strlen (arg2) + 1 : 0)
	       + strlen (arg3) + 1);

  /* Round it up to the user word size.  */
  text_size += sizeof (USER_WORD) - 1;
  text_size &= ~(sizeof (USER_WORD) - 1);

  /* Now allocate the new argv.  Make sure argc is at least 1; it
     needs to hold ARG3.  */

  effective_size = sizeof word * (MAX (1, argc) + 2) + text_size;

  if (arg2)
    effective_size += sizeof word;

  /* Copy regs to original so that user_alloca knows it should append
     the ABI red zone.  */

  memcpy (&original, regs, sizeof *regs);
  new = user_alloca (tracee, &original, regs,
		     effective_size);

  if (!new)
    goto fail;

  /* Figure out where argv starts.  */

  new3 = new + text_size;

  /* Now write the first two strings.  */

  new1 = new + strlen (arg1) + 1;
  new2 = new1 + (arg2 ? strlen (arg2) + 1 : 0);

  if (user_copy (tracee, (const unsigned char *) arg1,
		 new, new1 - new))
    goto fail;

  if (arg2 && user_copy (tracee, (const unsigned char *) arg2,
			 new1, new2 - new1))
    goto fail;

  /* Write the replacement arg3, the file name of the executable.  */

  if (user_copy (tracee, (const unsigned char *) arg3,
		 new2, new3 - new2))
    goto fail;

  /* Start copying argv back to new2.  First, write the one or two new
     arguments.  */

  if (ptrace (PTRACE_POKETEXT, tracee->pid,
	      (void *) new3, (void *) new))
    goto fail;

  new3 += sizeof new3;

  if (arg2 && ptrace (PTRACE_POKETEXT, tracee->pid,
		      (void *) new3, (void *) new1))
    goto fail;
  else if (arg2)
    new3 += sizeof new3;

  /* Next, write the third argument.  */

  if (ptrace (PTRACE_POKETEXT, tracee->pid, (void *) new3,
	      (void *) new2))
    goto fail;

  new3 += sizeof new3;

  /* Copy the remaining arguments back.  */

  argv = regs->SYSCALL_ARG1_REG;

  if (argc)
    {
      /* Make sure the trailing NULL is included.  */
      argc += 1;

      /* Now copy each argument in argv, starting from argv[1].  */

      for (i = 1; i < argc; ++i)
	{
	  /* Read one argument.  */
	  word = ptrace (PTRACE_PEEKDATA, tracee->pid,
			 (void *) (argv + i * sizeof argv), NULL);

	  /* Write one argument, then increment new3.  */

	  if (ptrace (PTRACE_POKETEXT, tracee->pid,
		      (void *) new3, (void *) word))
	    goto fail;

	  new3 += sizeof new3;
	}
    }
  else
    {
      /* Just write the trailing NULL.  */

      if (ptrace (PTRACE_POKETEXT, tracee->pid,
		  (void *) new3, (void *) 0))
	goto fail;

      new3 += sizeof new3;
    }

  /* Assert that new3 is not out of bounds.  */
  assert (new3 == new + effective_size);

  /* And that it is properly aligned.  */
  assert (!(new3 & (sizeof new3 - 2)));

  /* Now modify the system call argument to point to new +
     text_size.  */

  regs->SYSCALL_ARG1_REG = new + text_size;

#ifdef __aarch64__
  if (aarch64_set_regs (tracee->pid, regs, false))
    goto fail;
#else /* !__aarch64__ */
  if (ptrace (PTRACE_SETREGS, tracee->pid, NULL, regs))
    goto fail;
#endif /* __aarch64__ */

  /* Success.  */

  return 0;

 fail:
  /* Restore the original stack pointer.  */
#ifdef __aarch64__
  aarch64_set_regs (tracee->pid, &original, false);
#else /* !__aarch64__ */
  ptrace (PTRACE_SETREGS, tracee->pid, NULL, &original);
#endif /* __aarch64__ */
  errno = ENOMEM;
  return 1;
}

\f

/* Format PID, an unsigned process identifier, in base 10.  Place the
   result in *IN, and return a pointer to the byte after the
   result.  REM should be NULL.  */

char *
format_pid (char *in, unsigned int pid)
{
  unsigned int digits[32], *fill;

  fill = digits;

  for (; pid != 0; pid = pid / 10)
    *fill++ = pid % 10;

  /* Insert 0 if the number would otherwise be empty.  */

  if (fill == digits)
    *fill++ = 0;

  while (fill != digits)
    {
      --fill;
      *in++ = '0' + *fill;
    }

  *in = '\0';
  return in;
}

/* Return a sequence of actions required to load the executable under
   the file NAME for the given TRACEE.  First, see if the file starts
   with #!; in that case, find the program to open and use that
   instead.

   Next, read the executable header, and add the necessary memory
   mappings for each file.  Finally, return the action data and its
   size in *SIZE.

   Finally, use REGS to add the required interpreter arguments to the
   caller's argv.

   Value is NULL upon failure, with errno set accordingly.  */

char *
exec_0 (char *name, struct exec_tracee *tracee,
	size_t *size, USER_REGS_STRUCT *regs)
{
  int fd, rc, i;
  elf_header header;
  const char *interpreter_name, *extra;
  program_header program;
  USER_WORD entry, program_entry, offset;
  USER_WORD header_offset;
  struct exec_jump_command jump;
#if defined __mips__ && !defined MIPS_NABI
  int fpu_mode;
#endif /* defined __mips__ && !defined MIPS_NABI */
  char buffer[80], buffer1[PATH_MAX + 80], *rewrite;
  ssize_t link_size;
  size_t remaining;

  /* If the process is trying to run /proc/self/exe, make it run
     itself instead.  */

  if (!strcmp (name, "/proc/self/exe") && tracee->exec_file)
    {
      strncpy (name, tracee->exec_file, PATH_MAX - 1);
      name[PATH_MAX] = '\0';
    }
  else
    {
      /* If name is not absolute, then make it relative to TRACEE's
	 cwd.  Do not use sprintf at it is not reentrant and it
	 mishandles results longer than INT_MAX.  */

      if (name[0] && name[0] != '/')
	{
	  /* Clear both buffers.  */
	  memset (buffer, 0, sizeof buffer);
	  memset (buffer1, 0, sizeof buffer1);

	  /* Copy over /proc, the PID, and /cwd/.  */
	  rewrite = stpcpy (buffer, "/proc/");
	  rewrite = format_pid (rewrite, tracee->pid);
	  strcpy (rewrite, "/cwd");

	  /* Resolve this symbolic link.  */

	  link_size = readlink (buffer, buffer1,
				PATH_MAX + 1);

	  if (link_size < 0)
	    return NULL;

	  /* Check that the name is a reasonable size.  */

	  if (link_size > PATH_MAX)
	    {
	      /* The name is too long.  */
	      errno = ENAMETOOLONG;
	      return NULL;
	    }

	  /* Add a directory separator if necessary.  */

	  if (!link_size || buffer1[link_size - 1] != '/')
	    buffer1[link_size] = '/', link_size++;

	  rewrite = buffer1 + link_size;
	  remaining = buffer1 + sizeof buffer1 - rewrite - 1;
	  memcpy (rewrite, name, strnlen (name, remaining));

	  /* Replace name with buffer1.  */
	  strcpy (name, buffer1);
	}
    }

  /* Check that the file is accessible and executable.  */

  if (access (name, X_OK))
    return NULL;

  fd = open (name, O_RDONLY);
  if (fd < 0)
    return NULL;

  /* Now read the header.  */

  extra = NULL;
  interpreter_name = check_interpreter (name, fd, &extra);
  if (!interpreter_name)
    goto fail;

  /* Open the interpreter instead, if necessary.  */
  if (interpreter_name != name)
    {
      close (fd);
      fd = open (interpreter_name, O_RDONLY);
      if (fd < 0)
	return NULL;

      /* Now, rewrite the argument list to include `interpreter_name'
	 and perhaps `extra'.  */

      if (insert_args (tracee, regs, interpreter_name,
		       extra, name))
	goto fail1;
    }

  rc = read (fd, &header, sizeof header);

  if (rc < sizeof header)
    goto fail1;

  /* Verify that this is indeed an ELF file.  */

  if (header.e_ident[0] != 0x7f
      || header.e_ident[1] != 'E'
      || header.e_ident[2] != 'L'
      || header.e_ident[3] != 'F')
    goto fail1;

  /* Now check that the class is correct.  */
#ifdef EXEC_64
  if (header.e_ident[4] != 2)
    goto fail1;
#else /* !EXEC_64 */
  if (header.e_ident[4] != 1)
    goto fail1;
#endif /* EXEC_64 */

  /* And the endianness.  */
#ifndef WORDS_BIGENDIAN
  if (header.e_ident[5] != 1)
    goto fail1;
#else /* WORDS_BIGENDIAN */
  if (header.e_ident[5] != 2)
    goto fail1;
#endif /* EXEC_64 */

  /* Check that this is an executable.  */
  if (header.e_type != 2 && header.e_type != 3)
    goto fail1;

  /* Now check that the ELF program header makes sense.  */
  if (header.e_phnum > 0xffff
      || (header.e_phentsize
	  != sizeof (program_header)))
    goto fail1;

  /* Seek to the first program header and read each one.  */
  rc = lseek (fd, header.e_phoff, SEEK_SET);
  if (rc < 0)
    goto fail1;
  loader_area_used = 0;

  /* Write the command used to open the executable.  */
  if (write_open_command (interpreter_name, false))
    goto fail1;

  /* Apply base addresses for PIC code.  */

  if (header.e_type == 3) /* ET_DYN */
    offset = EXECUTABLE_BASE;
  else
    offset = 0;

  /* entry and program_entry are initially the same, but entry may be
     set to that of the interpreter if one is present.  */

  entry = header.e_entry + offset;
  program_entry = header.e_entry;

#if defined __mips__ && !defined MIPS_NABI
  /* Clear MIPS ABI flags.  */
  exec_abiflags = NULL;
  interpreter_abiflags = NULL;
  interpreter_header = NULL;
#endif /* defined __mips__ && !defined MIPS_NABI */

  /* Set header_offset to -1; `process_program_header' then updates it
     to that of the first mapping.  */
  header_offset = -1;

  for (i = 0; i < header.e_phnum; ++i)
    {
      rc = read (fd, &program, sizeof program);
      if (rc < sizeof program)
	goto fail1;

      if (process_program_header (interpreter_name, fd,
				  &program, &header,
				  &entry, &header_offset))
	goto fail1;
    }

  /* Write the entry point and program entry.  */

  jump.command = 3;
  jump.entry = entry;

  /* Now calculate values for the aux vector.  */

  jump.at_entry = program_entry + offset;
  jump.at_phent = header.e_phentsize;
  jump.at_phnum = header.e_phnum;
  jump.at_base = (entry == header.e_entry + offset
		  ? EXECUTABLE_BASE
		  : INTERPRETER_BASE);

#if defined __mips__ && !defined MIPS_NABI
  /* Finally, calculate the FPU mode wanted by the executable.  */

  if (determine_fpu_mode (&header, interpreter_header,
			  &fpu_mode, exec_abiflags,
			  interpreter_abiflags))
    /* N.B. that `determine_fpu_mode' sets errno.  */
    goto fail;

  /* If the processor is too new to support FR0 operation, place the
     executable in floating point emulation mode.  */

  if (fpu_mode == FP_FR0 && !cpu_supports_fr0_p ())
    fpu_mode = FP_FRE;

  jump.fpu_mode = fpu_mode;
#endif /* defined __mips__ && !defined MIPS_NABI */

  /* The offset used for at_phdr should be that of the first
     mapping.  */

  if (header_offset == (USER_WORD) -1)
    header_offset = 0;

  jump.at_phdr = header.e_phoff + header_offset;

  if (sizeof loader_area - loader_area_used < sizeof jump)
    goto fail1;

  memcpy (loader_area + loader_area_used, &jump,
	  sizeof jump);
  loader_area_used += sizeof jump;

  /* Close the file descriptor and return the number of bytes
     used.  */

  close (fd);
  *size = loader_area_used;

  /* Make sure the loader area is properly aligned.  */
  assert (!(loader_area_used & (sizeof (USER_WORD) - 1)));
  return loader_area;

 fail1:
  errno = ENOEXEC;
 fail:
  close (fd);
  return NULL;
}

debug log:

solving f31c9a89744 ...
found f31c9a89744 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.