Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

Fix: Make audio hook features work in LiveContainer (#59)

anna7768b53daa67
diff --git a/tweak/Sources/Core/SGRebind.h b/tweak/Sources/Core/SGRebind.h
index 4539a4a1..5f5d838e 100644
--- a/tweak/Sources/Core/SGRebind.h
+++ b/tweak/Sources/Core/SGRebind.h
@@ -1,8 +1,11 @@
-// Hooks a C function Spotify's executable imports, by pointing the executable's own import slot for it
+// Hooks a C function Spotify's binary imports, by pointing the binary's own import slot for it
// (its __got, or __la_symbol_ptr in an older build) at a replacement. Only calls Spotify's own code
// makes go through the new function; the system's frameworks keep calling the original. This is the
-// fishhook technique, kept to the main executable, for the few places no Objective-C method stands
-// between Spotify and a system call (the audio output unit Music Haptics listens to).
+// fishhook technique, kept to Spotify's binary, for the few places no Objective-C method stands
+// between Spotify and a system call (the audio output unit Music Haptics listens to). The binary is
+// the image named Spotify (or named as the bundle's executable): the main executable, or under
+// LiveContainer a dylib it dlopens. There it is expected to be loaded and bound before any %ctor runs
+// (not yet confirmed by a LiveContainer log); if it is not, the log says so and nothing is rebound.
//
// Threading: call from a %ctor, before Spotify has run; the slots are written without a lock.
#import <Foundation/Foundation.h>
diff --git a/tweak/Sources/Core/SGRebind.m b/tweak/Sources/Core/SGRebind.m
index 05c87279..fba3294b 100644
--- a/tweak/Sources/Core/SGRebind.m
+++ b/tweak/Sources/Core/SGRebind.m
@@ -6,13 +6,39 @@
#import "SGRebind.h"
#import "SGLog.h"
-static const struct mach_header_64 *mainExecutable(intptr_t *slide) {
- for (uint32_t i = 0; i < _dyld_image_count(); i++) {
- const struct mach_header *header = _dyld_get_image_header(i);
- if (header->magic != MH_MAGIC_64 || header->filetype != MH_EXECUTE) continue;
- *slide = _dyld_get_image_vmaddr_slide(i);
- return (const struct mach_header_64 *)header;
+// The slide from the image's own __TEXT, so it cannot come from a different dyld index than the header.
+static BOOL imageSlide(const struct mach_header_64 *header, intptr_t *slide) {
+ const struct load_command *command = (const struct load_command *)(header + 1);
+ for (uint32_t i = 0; i < header->ncmds; i++, command = (const struct load_command *)((const char *)command + command->cmdsize)) {
+ if (command->cmd != LC_SEGMENT_64 || strcmp(((const struct segment_command_64 *)command)->segname, SEG_TEXT) != 0) continue;
+ *slide = (intptr_t)header - (intptr_t)((const struct segment_command_64 *)command)->vmaddr;
+ return YES;
}
+ return NO;
+}
+
+// Spotify's binary: the image named Spotify (a dylib under LiveContainer), else the one named as the
+// bundle's executable (a normal install whose executable was renamed).
+// Logs the first lookup, so a missing image is not read as a missing import.
+static const struct mach_header_64 *spotifyImage(intptr_t *slide) {
+ static BOOL logged;
+ NSString *executable = NSBundle.mainBundle.infoDictionary[@"CFBundleExecutable"];
+ const char *names[] = {"Spotify", executable.UTF8String};
+ for (int n = 0; n < 2; n++) {
+ if (!names[n]) continue;
+ for (uint32_t i = 0; i < _dyld_image_count(); i++) {
+ const struct mach_header *header = _dyld_get_image_header(i);
+ const char *name = _dyld_get_image_name(i);
+ const char *slash = name ? strrchr(name, '/') : NULL;
+ if (!header || header->magic != MH_MAGIC_64 || !slash || strcmp(slash + 1, names[n]) != 0) continue;
+ if (!imageSlide((const struct mach_header_64 *)header, slide)) continue;
+ if (!logged) SGLog(@"rebind: Spotify's binary is %s", name);
+ logged = YES;
+ return (const struct mach_header_64 *)header;
+ }
+ }
+ if (!logged) SGLog(@"rebind: no image named Spotify or %@ is loaded, nothing is rebound", executable);
+ logged = YES;
return NULL;
}
@@ -22,6 +48,11 @@ static BOOL writeSlot(void **slot, void *value, BOOL readOnly) {
vm_size_t page = vm_page_size;
vm_address_t start = (vm_address_t)slot & ~(page - 1);
kern_return_t result = vm_protect(mach_task_self(), start, page, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
+ // A page an earlier slot already copied refuses a second copy; it only needs to be writable again.
+ if (result == KERN_PROTECTION_FAILURE) {
+ kern_return_t retry = vm_protect(mach_task_self(), start, page, FALSE, VM_PROT_READ | VM_PROT_WRITE);
+ if (retry == KERN_SUCCESS) result = KERN_SUCCESS;
+ }
if (result != KERN_SUCCESS) {
SGLog(@"rebind: vm_protect failed (%d)", result);
return NO;
@@ -33,7 +64,7 @@ static BOOL writeSlot(void **slot, void *value, BOOL readOnly) {
BOOL SGRebindImport(const char *symbol, void *replacement, void **original) {
intptr_t slide = 0;
- const struct mach_header_64 *header = mainExecutable(&slide);
+ const struct mach_header_64 *header = spotifyImage(&slide);
if (!header) return NO;
const struct segment_command_64 *linkedit = NULL;
@@ -69,7 +100,7 @@ BOOL SGRebindImport(const char *symbol, void *replacement, void **original) {
}
if (wanted == UINT32_MAX) return NO;
- BOOL rebound = NO;
+ BOOL rebound = NO, unbound = NO;
command = (const struct load_command *)(header + 1);
for (uint32_t i = 0; i < header->ncmds; i++, command = (const struct load_command *)((const char *)command + command->cmdsize)) {
if (command->cmd != LC_SEGMENT_64) continue;
@@ -86,11 +117,17 @@ BOOL SGRebindImport(const char *symbol, void *replacement, void **original) {
if (entry >= dysymtab->nindirectsyms || indirect[entry] != wanted) continue;
if (slots[k] == replacement) continue;
void *held = slots[k];
+ // Bit 63 marks a chained-fixup bind dyld has not applied; it would overwrite the slot later.
+ if ((uintptr_t)held >> 63) {
+ unbound = YES;
+ continue;
+ }
if (!writeSlot(&slots[k], replacement, readOnly)) continue;
if (original && !*original) *original = held;
rebound = YES;
}
}
}
+ if (unbound) SGLog(@"rebind: %s is not bound yet, left as it is", symbol);
return rebound;
}