2016-08-13 18:36:22 +00:00
|
|
|
|
#include "derivations.hh"
|
|
|
|
|
#include "globals.hh"
|
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
2016-08-16 19:45:55 +00:00
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <fcntl.h>
|
2016-08-13 18:36:22 +00:00
|
|
|
|
#include <mach-o/loader.h>
|
|
|
|
|
#include <mach-o/swap.h>
|
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
#define DO_SWAP(x, y) ((x) ? OSSwapInt32(y) : (y))
|
|
|
|
|
|
2016-08-13 18:36:22 +00:00
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
|
static auto cacheDir = Path{};
|
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
Path resolveCacheFile(Path lib) {
|
|
|
|
|
std::replace(lib.begin(), lib.end(), '/', '%');
|
|
|
|
|
return cacheDir + "/" + lib;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
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)));
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::set<std::string> runResolver(const Path & filename) {
|
2016-08-16 19:45:55 +00:00
|
|
|
|
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++) {
|
2016-08-17 17:24:11 +00:00
|
|
|
|
ptrdiff_t header_offset = (ptrdiff_t)obj + sizeof(struct fat_header) * (iter + 1);
|
2016-08-16 19:45:55 +00:00
|
|
|
|
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) {
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printError(format("Could not find any mach64 blobs in file ‘%1%’, continuing...") % filename);
|
2016-08-15 17:43:14 +00:00
|
|
|
|
return std::set<string>();
|
|
|
|
|
}
|
2016-08-16 19:45:55 +00:00
|
|
|
|
} else if (magic == MH_MAGIC_64 || magic == MH_CIGAM_64) {
|
|
|
|
|
mach64_offset = 0;
|
2016-08-16 19:52:49 +00:00
|
|
|
|
} else {
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printError(format("Object file has unknown magic number ‘%1%’, skipping it...") % magic);
|
2016-08-16 19:52:49 +00:00
|
|
|
|
return std::set<string>();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
2016-08-16 21:09:57 +00:00
|
|
|
|
ptrdiff_t mach_header_offset = (ptrdiff_t)obj + mach64_offset;
|
|
|
|
|
struct mach_header_64 *m_header = (struct mach_header_64 *)mach_header_offset;
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
|
|
|
|
bool should_swap = magic == MH_CIGAM_64;
|
2016-08-16 21:09:57 +00:00
|
|
|
|
ptrdiff_t cmd_offset = mach_header_offset + sizeof(struct mach_header_64);
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
std::set<string> libs;
|
2016-08-16 19:45:55 +00:00
|
|
|
|
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)) {
|
2016-08-15 17:43:14 +00:00
|
|
|
|
case LC_LOAD_UPWARD_DYLIB:
|
2016-08-16 19:45:55 +00:00
|
|
|
|
case LC_LOAD_DYLIB:
|
2016-08-15 17:43:14 +00:00
|
|
|
|
case LC_REEXPORT_DYLIB:
|
2016-08-16 19:45:55 +00:00
|
|
|
|
libs.insert(findDylibName(should_swap, cmd_offset));
|
2016-08-15 17:43:14 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2016-08-16 19:45:55 +00:00
|
|
|
|
cmd_offset += DO_SWAP(should_swap, cmd->cmdsize);
|
2016-08-15 17:43:14 +00:00
|
|
|
|
}
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
return libs;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isSymlink(const Path & path) {
|
2016-08-15 17:43:14 +00:00
|
|
|
|
struct stat st;
|
|
|
|
|
if(lstat(path.c_str(), &st))
|
|
|
|
|
throw SysError(format("getting attributes of path ‘%1%’") % path);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
return S_ISLNK(st.st_mode);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Path resolveSymlink(const Path & path) {
|
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
|
ssize_t len = readlink(path.c_str(), buf, sizeof(buf) - 1);
|
|
|
|
|
if(len != -1) {
|
2016-08-15 17:43:14 +00:00
|
|
|
|
buf[len] = 0;
|
|
|
|
|
return Path(buf);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
} else {
|
2016-08-15 17:43:14 +00:00
|
|
|
|
throw SysError(format("readlink('%1%')") % path);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
2016-08-15 17:43:14 +00:00
|
|
|
|
return results;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
std::set<string> getPath(const Path & path) {
|
|
|
|
|
Path cacheFile = resolveCacheFile(path);
|
|
|
|
|
if(pathExists(cacheFile)) {
|
|
|
|
|
return readCacheFile(cacheFile);
|
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
std::set<string> deps;
|
|
|
|
|
std::set<string> paths;
|
|
|
|
|
paths.insert(path);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
Path next_path = Path(path);
|
|
|
|
|
while(isSymlink(next_path)) {
|
|
|
|
|
next_path = resolveSymlink(next_path);
|
|
|
|
|
paths.insert(next_path);
|
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
for(auto & t : resolveTree(next_path, deps)) {
|
|
|
|
|
paths.insert(t);
|
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
writeCacheFile(cacheFile, paths);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
return paths;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
2016-08-16 19:45:55 +00:00
|
|
|
|
initNix();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
struct utsname _uname;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
uname(&_uname);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
auto cacheParentDir = (format("%1%/dependency-maps") % settings.nixStateDir).str();
|
2016-08-15 17:46:21 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
cacheDir = (format("%1%/%2%-%3%-%4%")
|
|
|
|
|
% cacheParentDir
|
|
|
|
|
% _uname.machine
|
|
|
|
|
% _uname.sysname
|
|
|
|
|
% _uname.release).str();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
mkdir(cacheParentDir.c_str(), 0755);
|
|
|
|
|
mkdir(cacheDir.c_str(), 0755);
|
2016-08-15 17:46:21 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
auto store = openStore();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
auto drv = store->derivationFromPath(Path(argv[1]));
|
|
|
|
|
Strings impurePaths = tokenizeString<Strings>(get(drv.env, "__impureHostDeps"));
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
std::set<string> all_paths;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
for (auto & path : impurePaths) {
|
|
|
|
|
for(auto & p : getPath(path)) {
|
|
|
|
|
all_paths.insert(p);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
2016-08-16 19:45:55 +00:00
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
std::cout << "extra-chroot-dirs" << std::endl;
|
|
|
|
|
for(auto & path : all_paths) {
|
|
|
|
|
std::cout << path << std::endl;
|
|
|
|
|
}
|
|
|
|
|
std::cout << std::endl;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
});
|
|
|
|
|
}
|