diff --git a/Library/OcAppleKernelLib/PrelinkedKext.c b/Library/OcAppleKernelLib/PrelinkedKext.c index 97ec2875..6c012295 100644 --- a/Library/OcAppleKernelLib/PrelinkedKext.c +++ b/Library/OcAppleKernelLib/PrelinkedKext.c @@ -412,6 +412,34 @@ InternalScanBuildLinkedVtables ( return RETURN_SUCCESS; } +STATIC +RETURN_STATUS +InternalUpdateLinkBuffer ( + IN OUT PRELINKED_KEXT *Kext, + IN OUT PRELINKED_CONTEXT *Context + ) +{ + ASSERT (Kext != NULL); + ASSERT (Context != NULL); + + if (Context->LinkBuffer == NULL) { + Context->LinkBuffer = AllocatePool (Context->LinkBufferSize); + if (Context->LinkBuffer == NULL) { + return RETURN_OUT_OF_RESOURCES; + } + } else if (Context->LinkBufferSize < Kext->LinkEditSegment->FileSize) { + FreePool (Context->LinkBuffer); + + Context->LinkBufferSize = (UINT32)Kext->LinkEditSegment->FileSize; + Context->LinkBuffer = AllocatePool (Context->LinkBufferSize); + if (Context->LinkBuffer == NULL) { + return RETURN_OUT_OF_RESOURCES; + } + } + + return RETURN_SUCCESS; +} + STATIC RETURN_STATUS InternalInsertPrelinkedKextDependency ( @@ -437,24 +465,13 @@ InternalInsertPrelinkedKextDependency ( if (RETURN_ERROR (Status)) { return Status; } - - if (Context->LinkBuffer == NULL) { - // - // Allocate the LinkBuffer from the size cached during the initial - // function recursion. - // - Context->LinkBuffer = AllocatePool (Context->LinkBufferSize); - if (Context->LinkBuffer == NULL) { - return RETURN_OUT_OF_RESOURCES; - } - } else if (Context->LinkBufferSize < DependencyKext->LinkEditSegment->FileSize) { - FreePool (Context->LinkBuffer); - - Context->LinkBufferSize = (UINT32)DependencyKext->LinkEditSegment->FileSize; - Context->LinkBuffer = AllocatePool (Context->LinkBufferSize); - if (Context->LinkBuffer == NULL) { - return RETURN_OUT_OF_RESOURCES; - } + // + // Allocate the LinkBuffer from the size cached during the initial + // function recursion. + // + Status = InternalUpdateLinkBuffer (DependencyKext, Context); + if (RETURN_ERROR (Status)) { + return Status; } Status = InternalScanBuildLinkedVtables (DependencyKext, Context); @@ -698,14 +715,12 @@ InternalScanPrelinkedKext ( Kext->BundleLibraries = NULL; } - if (Context->LinkBuffer == NULL) { - // - // Allocate the LinkBuffer in case there are no dependencies. - // - Context->LinkBuffer = AllocatePool (Context->LinkBufferSize); - if (Context->LinkBuffer == NULL) { - return RETURN_OUT_OF_RESOURCES; - } + // + // Extend or allocate LinkBuffer in case there are no dependencies (kernel). + // + Status = InternalUpdateLinkBuffer (Kext, Context); + if (RETURN_ERROR (Status)) { + return Status; } return RETURN_SUCCESS;