Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions tool/build/apelink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,12 @@ static void OpenLoader(struct Loader *ldr) {
close(fd);
if (IsElf64Binary((elf = ldr->map), ldr->size)) {
ValidateElfImage(ldr->map, ldr->size, ldr->path, true);
ldr->os = _HOSTLINUX | _HOSTFREEBSD | _HOSTNETBSD | _HOSTOPENBSD;
if (ldr->kernel) {
// Don't make any assumptions about the loader's OS
ldr->os = 0;
} else {
ldr->os = _HOSTLINUX | _HOSTFREEBSD | _HOSTNETBSD | _HOSTOPENBSD;
}
ldr->machine = elf->e_machine;
if (ldr->machine == EM_NEXGEN32E && FindLoaderEmbeddedMachoHeader(ldr)) {
ldr->os |= _HOSTXNU;
Expand Down Expand Up @@ -2212,33 +2217,10 @@ int main(int argc, char *argv[]) {
macos_silicon_loader_source_path = 0;
}

// extract the ape loader for open platforms
bool gotsome = false;
if (inputs.n && (support_vector & _HOSTXNU)) {
p = stpcpy(p, "if [ ! -d /Applications ]; then\n");
}
for (i = 0; i < inputs.n; ++i) {
struct Loader *loader;
struct Input *in = inputs.p + i;
if ((loader = GetLoader(in->elf->e_machine, ~_HOSTXNU))) {
loader->used = true;
p = GenerateScriptIfMachine(p, in);
p = stpcpy(p, "mkdir -p \"${t%/*}\" ||exit\n"
"dd if=\"$o\"");
p = stpcpy(p, " skip=");
loader->ddarg_skip2 = p;
p = GenerateDecimalOffsetRelocation(p);
p = stpcpy(p, " count=");
loader->ddarg_size2 = p;
p = GenerateDecimalOffsetRelocation(p);
p = stpcpy(p, " bs=1 2>/dev/null | gzip -dc >\"$t.$$\" ||exit\n"
"chmod 755 \"$t.$$\" ||exit\n"
"mv -f \"$t.$$\" \"$t\" ||exit\n");
p = stpcpy(p, "exec \"$t\" \"$o\" \"$@\"\n"
"fi\n");
gotsome = true;
}
}

// extract the ape loader for non-input architectures
// if the user requested a host kernel check, get the host kernel
Expand All @@ -2250,6 +2232,11 @@ int main(int argc, char *argv[]) {
if (loader->used) {
continue;
}
if (loader->os) {
// if the os field is set, this is a multi-OS ape loader or a
// mach-o loader
continue;
}
loader->used = true;
p = GenerateScriptIfLoaderMachine(p, loader);
p = stpcpy(p, "mkdir -p \"${t%/*}\" ||exit\n"
Expand All @@ -2268,6 +2255,30 @@ int main(int argc, char *argv[]) {
gotsome = true;
}

// extract the ape loader for open platforms
for (i = 0; i < inputs.n; ++i) {
struct Loader *loader;
struct Input *in = inputs.p + i;
if ((loader = GetLoader(in->elf->e_machine, ~_HOSTXNU))) {
loader->used = true;
p = GenerateScriptIfMachine(p, in);
p = stpcpy(p, "mkdir -p \"${t%/*}\" ||exit\n"
"dd if=\"$o\"");
p = stpcpy(p, " skip=");
loader->ddarg_skip2 = p;
p = GenerateDecimalOffsetRelocation(p);
p = stpcpy(p, " count=");
loader->ddarg_size2 = p;
p = GenerateDecimalOffsetRelocation(p);
p = stpcpy(p, " bs=1 2>/dev/null | gzip -dc >\"$t.$$\" ||exit\n"
"chmod 755 \"$t.$$\" ||exit\n"
"mv -f \"$t.$$\" \"$t\" ||exit\n");
p = stpcpy(p, "exec \"$t\" \"$o\" \"$@\"\n"
"fi\n");
gotsome = true;
}
}

// close if-statements
if (inputs.n && (support_vector & _HOSTXNU)) {
if (!gotsome) {
Expand Down