2021-09-08 13:02:38 +03:00
|
|
|
#include "mold.h"
|
|
|
|
|
2021-09-13 12:15:34 +03:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <fcntl.h>
|
2021-09-14 07:12:43 +03:00
|
|
|
#include <iomanip>
|
2021-09-08 13:15:12 +03:00
|
|
|
#include <iostream>
|
2021-09-13 12:15:34 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2021-09-08 13:15:12 +03:00
|
|
|
|
2021-09-08 13:02:38 +03:00
|
|
|
namespace mold::macho {
|
|
|
|
|
2021-09-13 15:59:20 +03:00
|
|
|
static void print_bytes(u8 *buf, i64 size) {
|
|
|
|
if (size == 0) {
|
|
|
|
std::cout << "[]\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-14 07:12:43 +03:00
|
|
|
std::cout << "[" << std::setw(2) << std::setfill('0') << (u32)buf[0];
|
2021-09-13 15:59:20 +03:00
|
|
|
for (i64 i = 1; i < size; i++)
|
2021-09-14 07:12:43 +03:00
|
|
|
std::cout << " " << std::setw(2) << std::setfill('0') << (u32)buf[i];
|
2021-09-13 15:59:20 +03:00
|
|
|
std::cout << "]\n";
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:15:03 +03:00
|
|
|
int main(int argc, char **argv) {
|
2021-09-15 11:25:06 +03:00
|
|
|
Context ctx;
|
|
|
|
|
2021-09-13 12:15:34 +03:00
|
|
|
if (argc == 1) {
|
2021-09-15 11:25:06 +03:00
|
|
|
SyncOut(ctx) << "mold macho stub\n";
|
2021-09-13 12:15:34 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2021-09-15 11:25:06 +03:00
|
|
|
if (std::string_view(argv[1]) == "-dump") {
|
|
|
|
if (argc != 3)
|
|
|
|
Fatal(ctx) << "usage: ld64.mold -dump <executable-name>\n";
|
|
|
|
dump_file(argv[2]);
|
|
|
|
exit(0);
|
2021-09-13 12:15:34 +03:00
|
|
|
}
|
|
|
|
|
2021-09-15 11:25:06 +03:00
|
|
|
if (argc != 2)
|
|
|
|
Fatal(ctx) << "usage: ld64.mold <output-file>\n";
|
|
|
|
ctx.arg.output = argv[1];
|
2021-09-13 13:07:15 +03:00
|
|
|
|
2021-09-15 11:25:06 +03:00
|
|
|
ctx.output_file = std::make_unique<OutputFile>(ctx, ctx.arg.output, 1024, 0777);
|
|
|
|
ctx.buf = ctx.output_file->buf;
|
2021-09-13 13:07:15 +03:00
|
|
|
|
2021-09-15 11:25:06 +03:00
|
|
|
MachHeader &hdr = *(MachHeader *)ctx.buf;
|
|
|
|
hdr.magic = 0xfeedfacf;
|
|
|
|
hdr.cputype = CPU_TYPE_X86_64;
|
|
|
|
hdr.cpusubtype = CPU_SUBTYPE_X86_64_ALL;
|
|
|
|
hdr.filetype = MH_EXECUTE;
|
|
|
|
hdr.ncmds = 0x10;
|
|
|
|
hdr.sizeofcmds = 0x558;
|
|
|
|
hdr.flags = MH_TWOLEVEL | MH_NOUNDEFS | MH_DYLDLINK | MH_PIE;
|
2021-09-15 06:40:27 +03:00
|
|
|
|
2021-09-15 11:25:06 +03:00
|
|
|
ctx.output_file->close(ctx);
|
2021-09-13 13:07:15 +03:00
|
|
|
|
2021-09-08 13:02:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|