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>
|
|
|
|
|
#include <mach-o/loader.h>
|
|
|
|
|
#include <mach-o/swap.h>
|
|
|
|
|
|
|
|
|
|
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-15 17:43:14 +00:00
|
|
|
|
std::string findDylibName(FILE *obj_file, struct load_command cmd) {
|
|
|
|
|
fpos_t pos;
|
|
|
|
|
fgetpos(obj_file, &pos);
|
|
|
|
|
struct dylib_command dylc;
|
|
|
|
|
dylc.cmd = cmd.cmd;
|
|
|
|
|
dylc.cmdsize = cmd.cmdsize;
|
|
|
|
|
fread(&dylc.dylib, sizeof(struct dylib), 1, obj_file);
|
|
|
|
|
|
|
|
|
|
char *dylib_name = (char*)calloc(cmd.cmdsize, sizeof(char));
|
|
|
|
|
fseek(obj_file,
|
|
|
|
|
// offset is calculated from the beginning of the load command, which is two
|
|
|
|
|
// uint32_t's backwards
|
|
|
|
|
dylc.dylib.name.offset - (sizeof(uint32_t) * 2) + pos,
|
|
|
|
|
SEEK_SET);
|
|
|
|
|
fread(dylib_name, sizeof(char), cmd.cmdsize, obj_file);
|
|
|
|
|
fseek(obj_file, pos, SEEK_SET);
|
|
|
|
|
return std::string(dylib_name);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
bool seekMach64Blob(FILE *obj_file, enum NXByteOrder end) {
|
|
|
|
|
struct fat_header head;
|
|
|
|
|
fread(&head, sizeof(struct fat_header), 1, obj_file);
|
|
|
|
|
swap_fat_header(&head, end);
|
|
|
|
|
for(uint32_t narches = 0; narches < head.nfat_arch; narches++) {
|
|
|
|
|
struct fat_arch arch;
|
|
|
|
|
fread(&arch, sizeof(struct fat_arch), 1, obj_file);
|
|
|
|
|
swap_fat_arch(&arch, 1, end);
|
|
|
|
|
if(arch.cputype == CPU_TYPE_X86_64) {
|
|
|
|
|
fseek(obj_file, arch.offset, SEEK_SET);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
2016-08-15 17:43:14 +00:00
|
|
|
|
return false;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::set<std::string> runResolver(const Path & filename) {
|
2016-08-15 17:43:14 +00:00
|
|
|
|
FILE *obj_file = fopen(filename.c_str(), "rb");
|
|
|
|
|
uint32_t magic;
|
|
|
|
|
fread(&magic, sizeof(uint32_t), 1, obj_file);
|
|
|
|
|
fseek(obj_file, 0, SEEK_SET);
|
|
|
|
|
enum NXByteOrder endianness;
|
|
|
|
|
if(magic == 0xBEBAFECA) {
|
|
|
|
|
endianness = NX_BigEndian;
|
|
|
|
|
if(!seekMach64Blob(obj_file, endianness)) {
|
|
|
|
|
std::cerr << "Could not find any mach64 blobs in file " << filename << ", continuing..." << std::endl;
|
|
|
|
|
return std::set<string>();
|
|
|
|
|
}
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
2016-08-15 17:43:14 +00:00
|
|
|
|
struct mach_header_64 header;
|
|
|
|
|
fread(&header, sizeof(struct mach_header_64), 1, obj_file);
|
|
|
|
|
if(!(header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64)) {
|
|
|
|
|
std::cerr << "Not a mach-o object file: " << filename << std::endl;
|
|
|
|
|
return std::set<string>();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
}
|
2016-08-15 17:43:14 +00:00
|
|
|
|
std::set<string> libs;
|
|
|
|
|
for(uint32_t i = 0; i < header.ncmds; i++) {
|
|
|
|
|
struct load_command cmd;
|
|
|
|
|
fread(&cmd.cmd, sizeof(cmd.cmd), 1, obj_file);
|
|
|
|
|
fread(&cmd.cmdsize, sizeof(cmd.cmdsize), 1, obj_file);
|
|
|
|
|
switch(cmd.cmd) {
|
|
|
|
|
case LC_LOAD_DYLIB:
|
|
|
|
|
case LC_LOAD_UPWARD_DYLIB:
|
|
|
|
|
case LC_REEXPORT_DYLIB:
|
|
|
|
|
libs.insert(findDylibName(obj_file, cmd));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
fseek(obj_file, cmd.cmdsize - (sizeof(uint32_t) * 2), SEEK_CUR);
|
|
|
|
|
}
|
|
|
|
|
fclose(obj_file);
|
|
|
|
|
libs.erase(filename);
|
|
|
|
|
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-15 17:43:14 +00:00
|
|
|
|
initNix();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
struct utsname _uname;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
uname(&_uname);
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:46:21 +00:00
|
|
|
|
auto cacheParentDir = (format("%1%/dependency-maps") % settings.nixStateDir).str();
|
|
|
|
|
|
|
|
|
|
cacheDir = (format("%1%/%2%-%3%-%4%")
|
|
|
|
|
% cacheParentDir
|
2016-08-15 17:43:14 +00:00
|
|
|
|
% _uname.machine
|
|
|
|
|
% _uname.sysname
|
|
|
|
|
% _uname.release).str();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:46:21 +00:00
|
|
|
|
mkdir(cacheParentDir.c_str(), 0755);
|
|
|
|
|
mkdir(cacheDir.c_str(), 0755);
|
|
|
|
|
|
2016-08-15 17:43:14 +00:00
|
|
|
|
auto store = openStore();
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +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-15 17:43:14 +00:00
|
|
|
|
std::set<string> all_paths;
|
2016-08-13 18:36:22 +00:00
|
|
|
|
|
2016-08-15 17:43:14 +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-15 17:43:14 +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
|
|
|
|
});
|
|
|
|
|
}
|