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{};
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
Path resolveCacheFile(Path lib)
|
|
|
|
|
{
|
2016-08-15 17:43:14 +00:00
|
|
|
|
std::replace(lib.begin(), lib.end(), '/', '%');
|
|
|
|
|
return cacheDir + "/" + lib;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
std::set<string> readCacheFile(const Path & file)
|
|
|
|
|
{
|
2016-08-13 18:36:22 +00:00
|
|
|
|
return tokenizeString<set<string>>(readFile(file), "\n");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
std::set<std::string> runResolver(const Path & filename)
|
|
|
|
|
{
|
|
|
|
|
AutoCloseFD fd = open(filename.c_str(), O_RDONLY);
|
|
|
|
|
if (!fd)
|
|
|
|
|
throw SysError("opening ‘%s’", filename);
|
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (fstat(fd.get(), &st))
|
|
|
|
|
throw SysError("statting ‘%s’", filename);
|
|
|
|
|
|
2017-05-30 18:39:40 +00:00
|
|
|
|
if (!S_ISREG(st.st_mode)) {
|
|
|
|
|
printError("file ‘%s’ is not a regular file", filename);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
if (st.st_size < sizeof(mach_header_64)) {
|
|
|
|
|
printError("file ‘%s’ is too short for a MACH binary", filename);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-31 14:10:10 +00:00
|
|
|
|
char* obj = (char*) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd.get(), 0);
|
2017-05-30 13:56:40 +00:00
|
|
|
|
if (!obj)
|
|
|
|
|
throw SysError("mmapping ‘%s’", filename);
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
|
|
|
|
ptrdiff_t mach64_offset = 0;
|
|
|
|
|
|
2017-05-31 14:10:10 +00:00
|
|
|
|
uint32_t magic = ((mach_header_64*) obj)->magic;
|
2017-05-30 13:56:40 +00:00
|
|
|
|
if (magic == FAT_CIGAM || magic == FAT_MAGIC) {
|
2016-08-16 19:45:55 +00:00
|
|
|
|
bool should_swap = magic == FAT_CIGAM;
|
2017-05-31 14:10:10 +00:00
|
|
|
|
uint32_t narches = DO_SWAP(should_swap, ((fat_header *) obj)->nfat_arch);
|
|
|
|
|
for (uint32_t i = 0; i < narches; i++) {
|
|
|
|
|
fat_arch* arch = (fat_arch*) (obj + sizeof(fat_header) + sizeof(fat_arch) * i);
|
2017-05-30 13:56:40 +00:00
|
|
|
|
if (DO_SWAP(should_swap, arch->cputype) == CPU_TYPE_X86_64) {
|
2017-05-31 14:10:10 +00:00
|
|
|
|
mach64_offset = (ptrdiff_t) DO_SWAP(should_swap, arch->offset);
|
2016-08-16 19:45:55 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mach64_offset == 0) {
|
2016-11-25 23:37:43 +00:00
|
|
|
|
printError(format("Could not find any mach64 blobs in file ‘%1%’, continuing...") % filename);
|
2017-05-30 13:56:40 +00:00
|
|
|
|
return {};
|
2016-08-15 17:43:14 +00:00
|
|
|
|
}
|
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-11-25 23:37:43 +00:00
|
|
|
|
printError(format("Object file has unknown magic number ‘%1%’, skipping it...") % magic);
|
2017-05-30 13:56:40 +00:00
|
|
|
|
return {};
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
2017-05-31 14:10:10 +00:00
|
|
|
|
mach_header_64 * m_header = (mach_header_64 *) (obj + mach64_offset);
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
|
|
|
|
bool should_swap = magic == MH_CIGAM_64;
|
2017-05-31 14:10:10 +00:00
|
|
|
|
ptrdiff_t cmd_offset = mach64_offset + sizeof(mach_header_64);
|
2016-08-16 19:45:55 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
std::set<string> libs;
|
2017-05-31 14:10:10 +00:00
|
|
|
|
for (uint32_t i = 0; i < DO_SWAP(should_swap, m_header->ncmds); i++) {
|
|
|
|
|
load_command * cmd = (load_command *) (obj + cmd_offset);
|
2016-08-16 19:45:55 +00:00
|
|
|
|
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:
|
2017-05-31 14:10:10 +00:00
|
|
|
|
libs.insert(std::string((char *) cmd + ((dylib_command*) cmd)->dylib.name.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
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
bool isSymlink(const Path & path)
|
|
|
|
|
{
|
2016-08-15 17:43:14 +00:00
|
|
|
|
struct stat st;
|
2017-05-30 13:56:40 +00:00
|
|
|
|
if (lstat(path.c_str(), &st) == -1)
|
|
|
|
|
throw SysError("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
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
Path resolveSymlink(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
auto target = readLink(path);
|
|
|
|
|
return hasPrefix(target, "/")
|
|
|
|
|
? target
|
|
|
|
|
: dirOf(path) + "/" + target;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
std::set<string> resolveTree(const Path & path, PathSet & deps)
|
|
|
|
|
{
|
2016-08-15 17:43:14 +00:00
|
|
|
|
std::set<string> results;
|
2017-05-30 13:56:40 +00:00
|
|
|
|
if (deps.count(path))
|
|
|
|
|
return {};
|
2016-08-15 17:43:14 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
std::set<string> getPath(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
if (hasPrefix(path, "/dev")) return {};
|
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
Path cacheFile = resolveCacheFile(path);
|
2017-05-30 13:56:40 +00:00
|
|
|
|
if (pathExists(cacheFile))
|
2016-08-15 17:43:14 +00:00
|
|
|
|
return readCacheFile(cacheFile);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
std::set<string> deps, paths;
|
2016-08-15 17:43:14 +00:00
|
|
|
|
paths.insert(path);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
Path nextPath(path);
|
|
|
|
|
while (isSymlink(nextPath)) {
|
|
|
|
|
nextPath = resolveSymlink(nextPath);
|
|
|
|
|
paths.insert(nextPath);
|
2016-08-15 17:43:14 +00:00
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
for (auto & t : resolveTree(nextPath, deps))
|
2016-08-15 17:43:14 +00:00
|
|
|
|
paths.insert(t);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2017-05-31 13:34:03 +00:00
|
|
|
|
writeFile(cacheFile, concatStringsSep("\n", 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
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
|
{
|
2016-08-13 18:36:22 +00:00
|
|
|
|
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
|
|
|
|
|
2017-05-31 14:10:10 +00:00
|
|
|
|
StringSet impurePaths;
|
|
|
|
|
|
|
|
|
|
if (std::string(argv[1]) == "--test")
|
|
|
|
|
impurePaths.insert(argv[2]);
|
|
|
|
|
else {
|
|
|
|
|
auto drv = store->derivationFromPath(Path(argv[1]));
|
|
|
|
|
impurePaths = tokenizeString<StringSet>(get(drv.env, "__impureHostDeps"));
|
|
|
|
|
impurePaths.insert("/usr/lib/libSystem.dylib");
|
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
std::set<string> allPaths;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2017-05-30 13:56:40 +00:00
|
|
|
|
for (auto & path : impurePaths)
|
|
|
|
|
for (auto & p : getPath(path))
|
|
|
|
|
allPaths.insert(p);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-16 19:45:55 +00:00
|
|
|
|
std::cout << "extra-chroot-dirs" << std::endl;
|
2017-05-30 13:56:40 +00:00
|
|
|
|
for (auto & path : allPaths)
|
2016-08-16 19:45:55 +00:00
|
|
|
|
std::cout << path << std::endl;
|
|
|
|
|
std::cout << std::endl;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
});
|
|
|
|
|
}
|