File: entrypoint-section-run.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (80 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download
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
// RUN: %empty-directory(%t)
// RUN: %target-clang %s -std=c++11 -isysroot %sdk -o %t/main
// RUN: %target-codesign %t/main
// RUN: %target-build-swift %S/Inputs/at-main-struct-simple.swift -O -parse-as-library -emit-library -o %t/libHowdy.dylib -module-name Howdy
// RUN: %target-codesign %t/libHowdy.dylib
// RUN: %target-run %t/main %t/libHowdy.dylib | %FileCheck %s

// REQUIRES: VENDOR=apple
// REQUIRES: executable_test
// UNSUPPORTED: remote_run
// UNSUPPORTED: asan

#include <dlfcn.h>
#include <mach-o/dyld.h>
#include <mach-o/getsect.h>
#include <stdio.h>
#include <string.h>
#include <ptrauth.h>

#if __POINTER_WIDTH__ == 64
using mach_header_platform = mach_header_64;
#else
using mach_header_platform = mach_header;
#endif

int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("no argument!\n");
    return 1;
  }
  auto *dylibPath = argv[1];
  auto *handle = dlopen(dylibPath, RTLD_LAZY);
  if (!handle) {
    printf("no library!\n");
    return 1;
  }

  using MainFunction = void(int, char *[]);
  MainFunction *mainFunction = nullptr;
  for (int index = 0; index < _dyld_image_count(); ++index) {
    auto *imageName = _dyld_get_image_name(index);
    if (strcmp(dylibPath, imageName)) {
      printf("skipping %s\n", imageName);
      continue;
    }
    auto *header =
        reinterpret_cast<const mach_header_platform *>(_dyld_get_image_header(index));
    size_t size;
    auto *data = getsectiondata(header, "__TEXT", "__swift5_entry", &size);
    int32_t offset = *reinterpret_cast<int32_t *>(data);
    if (size >= 8) {
      auto flags = *(reinterpret_cast<int32_t *>(data) + 1);
      enum EntryPointFlags : unsigned {
        HasAtMainTypeFlag = 1 << 0,
      };
      printf("flags: %d\n", flags);
      bool isAtMainEntryPoint = flags & HasAtMainTypeFlag;
      if (!isAtMainEntryPoint) {
        printf("no @main entry point!\n");
        continue;
      }
    }
    mainFunction = reinterpret_cast<MainFunction *>(
      ptrauth_sign_unauthenticated(
          reinterpret_cast<void *>(
              reinterpret_cast<long>(data) + offset
          ),
          ptrauth_key_function_pointer,
          ptrauth_function_pointer_type_discriminator(MainFunction)
      )
    );

    break;
  }
  if (!mainFunction) {
    printf("no function!\n");
    return 1;
  }
  mainFunction(argc, argv); // CHECK: howdy mundo
}