forked from lix-project/lix
switch to mmap strategy
This commit is contained in:
parent
7b006122ae
commit
90516c5a7b
|
@ -6,9 +6,13 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <mach-o/loader.h>
|
#include <mach-o/loader.h>
|
||||||
#include <mach-o/swap.h>
|
#include <mach-o/swap.h>
|
||||||
|
|
||||||
|
#define DO_SWAP(x, y) ((x) ? OSSwapInt32(y) : (y))
|
||||||
|
|
||||||
using namespace nix;
|
using namespace nix;
|
||||||
|
|
||||||
static auto cacheDir = Path{};
|
static auto cacheDir = Path{};
|
||||||
|
@ -31,76 +35,59 @@ void writeCacheFile(const Path & file, std::set<string> & deps) {
|
||||||
fp.close();
|
fp.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string findDylibName(FILE *obj_file, struct load_command cmd) {
|
std::string findDylibName(bool should_swap, ptrdiff_t dylib_command_start) {
|
||||||
fpos_t pos;
|
struct dylib_command *dylc = (struct dylib_command*)dylib_command_start;
|
||||||
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));
|
return std::string((char*)(dylib_command_start + DO_SWAP(should_swap, dylc->dylib.name.offset)));
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<std::string> runResolver(const Path & filename) {
|
std::set<std::string> runResolver(const Path & filename) {
|
||||||
FILE *obj_file = fopen(filename.c_str(), "rb");
|
int fd = open(filename.c_str(), O_RDONLY);
|
||||||
uint32_t magic;
|
struct stat s;
|
||||||
fread(&magic, sizeof(uint32_t), 1, obj_file);
|
fstat(fd, &s);
|
||||||
fseek(obj_file, 0, SEEK_SET);
|
void *obj = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
enum NXByteOrder endianness;
|
|
||||||
if(magic == 0xBEBAFECA) {
|
ptrdiff_t mach64_offset = 0;
|
||||||
endianness = NX_BigEndian;
|
|
||||||
if(!seekMach64Blob(obj_file, endianness)) {
|
uint32_t magic = ((struct mach_header_64*)obj)->magic;
|
||||||
std::cerr << "Could not find any mach64 blobs in file " << filename << ", continuing..." << std::endl;
|
if(magic == FAT_CIGAM || magic == FAT_MAGIC) {
|
||||||
return std::set<string>();
|
bool should_swap = magic == FAT_CIGAM;
|
||||||
}
|
uint32_t narches = DO_SWAP(should_swap, ((struct fat_header*)obj)->nfat_arch);
|
||||||
}
|
|
||||||
struct mach_header_64 header;
|
for(uint32_t iter = 0; iter < narches; iter++) {
|
||||||
fread(&header, sizeof(struct mach_header_64), 1, obj_file);
|
ptrdiff_t header_offset = (ptrdiff_t)obj + sizeof(struct fat_header);
|
||||||
if(!(header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64)) {
|
struct fat_arch* arch = (struct fat_arch*)header_offset;
|
||||||
std::cerr << "Not a mach-o object file: " << filename << std::endl;
|
if(DO_SWAP(should_swap, arch->cputype) == CPU_TYPE_X86_64) {
|
||||||
return std::set<string>();
|
mach64_offset = (ptrdiff_t)DO_SWAP(should_swap, arch->offset);
|
||||||
}
|
|
||||||
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;
|
break;
|
||||||
}
|
}
|
||||||
fseek(obj_file, cmd.cmdsize - (sizeof(uint32_t) * 2), SEEK_CUR);
|
|
||||||
}
|
}
|
||||||
fclose(obj_file);
|
if (mach64_offset == 0) {
|
||||||
libs.erase(filename);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
return libs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue