about summary refs log tree commit diff
path: root/src/resolve-system-dependencies/resolve-system-dependencies.cc
blob: 7aad8e1c580e4b93c86a57d98dbe0d5a7c14f479 (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
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
#include "derivations.hh"
#include "globals.hh"
#include "shared.hh"
#include "store-api.hh"
#include <sys/utsname.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sys/mman.h>
#include <fcntl.h>
#include <mach-o/loader.h>
#include <mach-o/swap.h>

#define DO_SWAP(x, y) ((x) ? OSSwapInt32(y) : (y))

using namespace nix;

static auto cacheDir = Path{};

Path resolveCacheFile(Path lib) {
    std::replace(lib.begin(), lib.end(), '/', '%');
    return cacheDir + "/" + lib;
}

std::set<string> readCacheFile(const Path & file) {
    return tokenizeString<set<string>>(readFile(file), "\n");
}

void writeCacheFile(const Path & file, std::set<string> & deps) {
    std::ofstream fp;
    fp.open(file);
    for (auto & d : deps) {
        fp << d << "\n";
    }
    fp.close();
}

std::string findDylibName(bool should_swap, ptrdiff_t dylib_command_start) {
    struct dylib_command *dylc = (struct dylib_command*)dylib_command_start;

    return std::string((char*)(dylib_command_start + DO_SWAP(should_swap, dylc->dylib.name.offset)));
}

std::set<std::string> runResolver(const Path & filename) {
    int fd = open(filename.c_str(), O_RDONLY);
    struct stat s;
    fstat(fd, &s);
    void *obj = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);

    ptrdiff_t mach64_offset = 0;

    uint32_t magic = ((struct mach_header_64*)obj)->magic;
    if(magic == FAT_CIGAM || magic == FAT_MAGIC) {
        bool should_swap = magic == FAT_CIGAM;
        uint32_t narches = DO_SWAP(should_swap, ((struct fat_header*)obj)->nfat_arch);

        for(uint32_t iter = 0; iter < narches; iter++) {
            ptrdiff_t header_offset = (ptrdiff_t)obj + sizeof(struct fat_header);
            struct fat_arch* arch = (struct fat_arch*)header_offset;
            if(DO_SWAP(should_swap, arch->cputype) == CPU_TYPE_X86_64) {
                mach64_offset = (ptrdiff_t)DO_SWAP(should_swap, arch->offset);
                break;
            }
        }
        if (mach64_offset == 0) {
            printMsg(lvlError, format("Could not find any mach64 blobs in file ‘%1%’, continuing...") % filename);
            return std::set<string>();
        }
    } else if (magic == MH_MAGIC_64 || magic == MH_CIGAM_64) {
        mach64_offset = 0;
    } else {
        printMsg(lvlError, format("Object file has unknown magic number ‘%1%’, skipping it...") % magic);
        return std::set<string>();
    }

    struct mach_header_64 *m_header = (struct mach_header_64 *)((ptrdiff_t)obj + mach64_offset);

    bool should_swap = magic == MH_CIGAM_64;
    ptrdiff_t cmd_offset = (ptrdiff_t)m_header + sizeof(struct mach_header_64);

    std::set<string> libs;
    for(uint32_t i = 0; i < DO_SWAP(should_swap, m_header->ncmds); i++) {
        struct load_command *cmd = (struct load_command*)cmd_offset;
        switch(DO_SWAP(should_swap, cmd->cmd)) {
            case LC_LOAD_UPWARD_DYLIB:
            case LC_LOAD_DYLIB:
            case LC_REEXPORT_DYLIB:
                libs.insert(findDylibName(should_swap, cmd_offset));
                break;
        }
        cmd_offset += DO_SWAP(should_swap, cmd->cmdsize);
    }

    return libs;
}

bool isSymlink(const Path & path) {
    struct stat st;
    if(lstat(path.c_str(), &st))
        throw SysError(format("getting attributes of path ‘%1%’") % path);

    return S_ISLNK(st.st_mode);
}

Path resolveSymlink(const Path & path) {
    char buf[PATH_MAX];
    ssize_t len = readlink(path.c_str(), buf, sizeof(buf) - 1);
    if(len != -1) {
        buf[len] = 0;
        return Path(buf);
    } else {
        throw SysError(format("readlink('%1%')") % path);
    }
}

std::set<string> resolveTree(const Path & path, PathSet & deps) {
    std::set<string> results;
    if(deps.find(path) != deps.end()) {
        return std::set<string>();
    }
    deps.insert(path);
    for (auto & lib : runResolver(path)) {
        results.insert(lib);
        for (auto & p : resolveTree(lib, deps)) {
            results.insert(p);
        }
    }
    return results;
}

std::set<string> getPath(const Path & path) {
    Path cacheFile = resolveCacheFile(path);
    if(pathExists(cacheFile)) {
        return readCacheFile(cacheFile);
    }

    std::set<string> deps;
    std::set<string> paths;
    paths.insert(path);

    Path next_path = Path(path);
    while(isSymlink(next_path)) {
        next_path = resolveSymlink(next_path);
        paths.insert(next_path);
    }

    for(auto & t : resolveTree(next_path, deps)) {
        paths.insert(t);
    }

    writeCacheFile(cacheFile, paths);

    return paths;
}

int main(int argc, char ** argv) {
    return handleExceptions(argv[0], [&]() {
        initNix();

        struct utsname _uname;

        uname(&_uname);

        auto cacheParentDir = (format("%1%/dependency-maps") % settings.nixStateDir).str();

        cacheDir = (format("%1%/%2%-%3%-%4%")
                % cacheParentDir
                % _uname.machine
                % _uname.sysname
                % _uname.release).str();

        mkdir(cacheParentDir.c_str(), 0755);
        mkdir(cacheDir.c_str(), 0755);

        auto store = openStore();

        auto drv = store->derivationFromPath(Path(argv[1]));
        Strings impurePaths = tokenizeString<Strings>(get(drv.env, "__impureHostDeps"));

        std::set<string> all_paths;

        for (auto & path : impurePaths) {
            for(auto & p : getPath(path)) {
                all_paths.insert(p);
            }
        }

        std::cout << "extra-chroot-dirs" << std::endl;
        for(auto & path : all_paths) {
            std::cout << path << std::endl;
        }
        std::cout << std::endl;
    });
}