From ef8fdce8cd7e76bbc9c5490fc6e24f0c76b8bbd1 Mon Sep 17 00:00:00 2001 From: vit9696 Date: Sat, 4 Jul 2020 22:23:02 +0300 Subject: [PATCH] OcAppleKernelLib: Fix linking against injected kexts --- Library/OcAppleKernelLib/KernelCollection.c | 8 +++++++- Library/OcAppleKernelLib/PrelinkedContext.c | 6 ++++-- Library/OcAppleKernelLib/PrelinkedKext.c | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Library/OcAppleKernelLib/KernelCollection.c b/Library/OcAppleKernelLib/KernelCollection.c index 1e7d08e2..ea856baf 100644 --- a/Library/OcAppleKernelLib/KernelCollection.c +++ b/Library/OcAppleKernelLib/KernelCollection.c @@ -688,7 +688,7 @@ KcKextApplyFileDelta ( // // Rebase the segment itself. // - if (Segment->FileOffset != 0) { + if (Segment->FileOffset != 0 || Segment->FileSize != 0) { Segment->FileOffset += Delta; } @@ -741,5 +741,11 @@ KcKextApplyFileDelta ( } } + // + // Update the container offset to make sure we can link against this + // kext later as well. + // + Context->ContainerOffset = Delta; + return EFI_SUCCESS; } diff --git a/Library/OcAppleKernelLib/PrelinkedContext.c b/Library/OcAppleKernelLib/PrelinkedContext.c index 9f357cdb..3479bfa9 100644 --- a/Library/OcAppleKernelLib/PrelinkedContext.c +++ b/Library/OcAppleKernelLib/PrelinkedContext.c @@ -892,9 +892,11 @@ PrelinkedInjectKext ( // // For KC, our KEXTs have their own segment - do not mod __PRELINK_INFO. // Integrate the KEXT into KC by indexing its fixups and rebasing. + // Note, we are no longer using ExecutableContext here, as the context + // ownership was transferred by InternalLinkPrelinkedKext. // - KcKextIndexFixups (Context, &ExecutableContext); - Status = KcKextApplyFileDelta (&ExecutableContext, KextOffset); + KcKextIndexFixups (Context, &PrelinkedKext->Context.MachContext); + Status = KcKextApplyFileDelta (&PrelinkedKext->Context.MachContext, KextOffset); if (EFI_ERROR (Status)) { DEBUG (( DEBUG_WARN, diff --git a/Library/OcAppleKernelLib/PrelinkedKext.c b/Library/OcAppleKernelLib/PrelinkedKext.c index 669c22ec..25afa047 100644 --- a/Library/OcAppleKernelLib/PrelinkedKext.c +++ b/Library/OcAppleKernelLib/PrelinkedKext.c @@ -215,6 +215,7 @@ InternalScanCurrentPrelinkedKextLinkInfo ( } if (Kext->SymbolTable == NULL) { + DEBUG ((DEBUG_VERBOSE, "OCAK: Requesting SymbolTable for %a\n", Kext->Identifier)); Kext->NumberOfSymbols = MachoGetSymbolTable ( &Kext->Context.MachContext, &Kext->SymbolTable,