aboutsummaryrefslogtreecommitdiff
path: root/lib/substitute-internal.h
blob: b378989417c891fc61ab226973bd63fb24fc839b (plain) (blame)
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
#pragma once

#include <stdio.h>
#define substitute_panic(...) do { \
    fprintf(stderr, __VA_ARGS__); \
    abort(); \
    __builtin_unreachable(); \
} while(0)

#define EXPORT __attribute__ ((visibility("default")))
#define UNUSED __attribute__((unused))

#ifdef __APPLE__
#include <mach-o/loader.h>
#include <mach-o/dyld.h>
#include <mach-o/dyld_images.h>
#ifdef __LP64__
typedef struct mach_header_64 mach_header_x;
typedef struct segment_command_64 segment_command_x;
typedef struct section_64 section_x;
#define LC_SEGMENT_X LC_SEGMENT_64
#else
typedef struct mach_header mach_header_x;
typedef struct segment_command segment_command_x;
typedef struct section section_x;
#define LC_SEGMENT_X LC_SEGMENT
#endif
#endif

/* FORCE_* are for tests */
#if defined(FORCE_TARGET_x86_64)
    #define TARGET_x86_64
#elif defined(FORCE_TARGET_i386)
    #define TARGET_i386
#elif defined(FORCE_TARGET_arm)
    #define TARGET_arm
#elif defined(FORCE_TARGET_arm64)
    #define TARGET_arm64
#elif defined(__x86_64__)
    #define TARGET_x86_64
#elif defined(__i386__)
    #define TARGET_i386
#elif defined(__arm__)
    #define TARGET_arm
#elif defined(__arm64__)
    #define TARGET_arm64
#else
    #error target?
#endif

#if defined(TARGET_arm)
    #include "arm/misc.h"
#elif defined(TARGET_arm64)
    #include "arm64/misc.h"
#endif

#ifdef __APPLE__
/* This could graduate to a public API but is not yet.  Needs more
 * functionality. */

enum {
    /* substitute_dlopen_in_pid: task_for_pid failed; on OS X the reasons this
     * can happen are really complicated and dumb, but generally one solution
     * is to be root */
    SUBSTITUTE_ERR_TASK_FOR_PID = 1000,
    SUBSTITUTE_ERR_TIMEOUT = 1000,
    SUBSTITUTE_ERR_MISC,
};

enum shuttle_type {
    SUBSTITUTE_SHUTTLE_MACH_PORT,
    /* ... */
};

struct shuttle {
    int type;
    union {
        struct {
            mach_port_t port;
            mach_msg_type_name_t right_type;
        } mach;
    } u;
};

int substitute_dlopen_in_pid(int pid, const char *filename, int options,
                             const struct shuttle *shuttle, size_t nshuttle,
                             char **error);

int substitute_ios_unrestrict(pid_t pid, bool should_resume);
#endif