OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
Link.c File Reference
#include <Base.h>
#include <IndustryStandard/AppleKmodInfo.h>
#include <IndustryStandard/AppleMachoImage.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/DebugLib.h>
#include <Library/OcAppleKernelLib.h>
#include <Library/OcMachoLib.h>
#include <Library/OcFileLib.h>
#include "PrelinkedInternal.h"

Go to the source code of this file.

Macros

#define TEXT_SEG_PROT   (MACH_SEGMENT_VM_PROT_READ | MACH_SEGMENT_VM_PROT_EXECUTE)
 
#define DATA_SEG_PROT   (MACH_SEGMENT_VM_PROT_READ | MACH_SEGMENT_VM_PROT_WRITE)
 
#define MATCH(X)
 

Functions

STATIC CONST PRELINKED_KEXT_SYMBOLInternalOcGetSymbolWorkerName (IN PRELINKED_KEXT *Kext, IN CONST CHAR8 *LookupValue, IN UINT32 LookupValueLength, IN OC_GET_SYMBOL_LEVEL SymbolLevel)
 
STATIC CONST PRELINKED_KEXT_SYMBOLInternalOcGetSymbolWorkerValue (IN PRELINKED_KEXT *Kext, IN UINT64 LookupValue, IN OC_GET_SYMBOL_LEVEL SymbolLevel)
 
CONST PRELINKED_KEXT_SYMBOLInternalOcGetSymbolName (IN PRELINKED_CONTEXT *Context, IN PRELINKED_KEXT *Kext, IN CONST CHAR8 *LookupValue, IN OC_GET_SYMBOL_LEVEL SymbolLevel)
 
CONST PRELINKED_KEXT_SYMBOLInternalOcGetSymbolValue (IN PRELINKED_CONTEXT *Context, IN PRELINKED_KEXT *Kext, IN UINT64 LookupValue, IN OC_GET_SYMBOL_LEVEL SymbolLevel)
 
VOID InternalSolveSymbolValue (IN BOOLEAN Is32Bit, IN UINT64 Value, OUT MACH_NLIST_ANY *Symbol)
 
STATIC BOOLEAN InternalSolveSymbolNonWeak (IN PRELINKED_CONTEXT *Context, IN PRELINKED_KEXT *Kext, IN CONST CHAR8 *Name, IN OUT MACH_NLIST_ANY *Symbol)
 
STATIC BOOLEAN InternalSolveSymbol (IN PRELINKED_CONTEXT *Context, IN PRELINKED_KEXT *Kext, IN CONST CHAR8 *Name, IN OUT MACH_NLIST_ANY *Symbol, IN OUT UINT64 *WeakTestValue, IN CONST MACH_NLIST_ANY *UndefinedSymbols, IN UINT32 NumUndefinedSymbols)
 
STATIC BOOLEAN InternalCalculateDisplacementIntel64 (IN UINT64 Target, IN UINT64 Adjustment, IN OUT INT32 *Instruction)
 
STATIC BOOLEAN InternalCalculateTargets (IN PRELINKED_CONTEXT *Context, IN OUT PRELINKED_KEXT *Kext, IN UINT64 LoadAddress, IN CONST MACH_RELOCATION_INFO *Relocation, IN CONST MACH_RELOCATION_INFO *NextRelocation OPTIONAL, OUT UINT64 *Target, OUT UINT64 *PairTarget, OUT CONST PRELINKED_VTABLE **Vtable OPTIONAL)
 
STATIC BOOLEAN InternalIsDirectPureVirtualCall (IN BOOLEAN Is32Bit, IN CONST PRELINKED_VTABLE *Vtable, IN UINT64 Offset)
 
STATIC UINTN InternalRelocateRelocation (IN PRELINKED_CONTEXT *Context, IN PRELINKED_KEXT *Kext, IN UINT64 LoadAddress, IN UINT64 RelocationBase, IN CONST MACH_RELOCATION_INFO *Relocation, IN CONST MACH_RELOCATION_INFO *NextRelocation OPTIONAL)
 
STATIC BOOLEAN InternalRelocateAndCopyRelocations (IN PRELINKED_CONTEXT *Context, IN PRELINKED_KEXT *Kext, IN UINT64 LoadAddress, IN UINT64 RelocationBase, IN CONST MACH_RELOCATION_INFO *SourceRelocations, IN OUT UINT32 *NumRelocations, OUT MACH_RELOCATION_INFO *TargetRelocations)
 
STATIC BOOLEAN InternalRelocateSymbol (IN OC_MACHO_CONTEXT *MachoContext, IN UINT64 LoadAddress, IN OUT MACH_NLIST_ANY *Symbol, IN OUT UINT32 *KmodInfoOffset)
 
STATIC BOOLEAN InternalRelocateSymbols (IN OC_MACHO_CONTEXT *MachoContext, IN UINT64 LoadAddress, IN UINT32 NumSymbols, IN OUT MACH_NLIST_ANY *Symbols, OUT UINT32 *KmodInfoOffset)
 
STATIC BOOLEAN InternalProcessSymbolPointers (IN OC_MACHO_CONTEXT *MachoContext, IN CONST MACH_DYSYMTAB_COMMAND *DySymtab, IN UINT64 LoadAddress)
 
EFI_STATUS InternalPrelinkKext (IN OUT PRELINKED_CONTEXT *Context, IN PRELINKED_KEXT *Kext, IN UINT64 LoadAddress, IN UINT64 FileOffset)
 

Detailed Description

Library handling KEXT prelinking.

Copyright (c) 2018, Download-Fritz. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php.

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

Definition in file Link.c.

Macro Definition Documentation

◆ DATA_SEG_PROT

#define DATA_SEG_PROT   (MACH_SEGMENT_VM_PROT_READ | MACH_SEGMENT_VM_PROT_WRITE)

Definition at line 32 of file Link.c.

◆ MATCH

#define MATCH ( X)
Value:
if (Symbols[X].Value == LookupValue) { return &Symbols[X]; }

◆ TEXT_SEG_PROT

Definition at line 31 of file Link.c.

Function Documentation

◆ InternalCalculateDisplacementIntel64()

STATIC BOOLEAN InternalCalculateDisplacementIntel64 ( IN UINT64 Target,
IN UINT64 Adjustment,
IN OUT INT32 * Instruction )

Calculate the target address' displacement for the Intel 64 platform. Instruction will be patched with the resulting address. Logically matches XNU's calculate_displacement_x86_64.

Parameters
[in]TargetThe target address.
[in]AdjustmentAdjustment to be subtracted from the displacement.
[in,out]InstructionPointer to the instruction to be patched.
Return values
Returnedis whether the target instruction has been patched.

Definition at line 554 of file Link.c.

◆ InternalCalculateTargets()

STATIC BOOLEAN InternalCalculateTargets ( IN PRELINKED_CONTEXT * Context,
IN OUT PRELINKED_KEXT * Kext,
IN UINT64 LoadAddress,
IN CONST MACH_RELOCATION_INFO * Relocation,
IN CONST MACH_RELOCATION_INFO *NextRelocation OPTIONAL,
OUT UINT64 * Target,
OUT UINT64 * PairTarget,
OUT CONST PRELINKED_VTABLE **Vtable OPTIONAL )

Calculate the target addresses for Relocation and NextRelocation. Logically matches XNU's calculate_targets.

Parameters
[in,out]ContextPrelinking context.
[in]KextKEXT prelinking context.
[in]LoadAddressThe address to be linked against.
[in]RelocationThe Relocation to be resolved.
[in]NextRelocationThe Relocation following Relocation.
[out]TargetRelocation's target address.
[out]PairTargetNextRelocation's target address.
[out]VtableThe VTable described by the symbol referenced by Relocation. NULL, if there is none.
Returns
Whether the operation was completed successfully.

Definition at line 596 of file Link.c.

◆ InternalIsDirectPureVirtualCall()

STATIC BOOLEAN InternalIsDirectPureVirtualCall ( IN BOOLEAN Is32Bit,
IN CONST PRELINKED_VTABLE * Vtable,
IN UINT64 Offset )

Returns whether the VTable entry at Offset is a direct pure virtual call. Logically macthes XNU's check_for_direct_pure_virtual_call.

Parameters
[in]VtableThe current VTable to be inspected.
[in]OffsetThe VTable's entry offset.

Definition at line 741 of file Link.c.

◆ InternalOcGetSymbolName()

CONST PRELINKED_KEXT_SYMBOL * InternalOcGetSymbolName ( IN PRELINKED_CONTEXT * Context,
IN PRELINKED_KEXT * Kext,
IN CONST CHAR8 * LookupValue,
IN OC_GET_SYMBOL_LEVEL SymbolLevel )

Definition at line 191 of file Link.c.

◆ InternalOcGetSymbolValue()

CONST PRELINKED_KEXT_SYMBOL * InternalOcGetSymbolValue ( IN PRELINKED_CONTEXT * Context,
IN PRELINKED_KEXT * Kext,
IN UINT64 LookupValue,
IN OC_GET_SYMBOL_LEVEL SymbolLevel )

Definition at line 246 of file Link.c.

◆ InternalOcGetSymbolWorkerName()

STATIC CONST PRELINKED_KEXT_SYMBOL * InternalOcGetSymbolWorkerName ( IN PRELINKED_KEXT * Kext,
IN CONST CHAR8 * LookupValue,
IN UINT32 LookupValueLength,
IN OC_GET_SYMBOL_LEVEL SymbolLevel )

Definition at line 40 of file Link.c.

◆ InternalOcGetSymbolWorkerValue()

STATIC CONST PRELINKED_KEXT_SYMBOL * InternalOcGetSymbolWorkerValue ( IN PRELINKED_KEXT * Kext,
IN UINT64 LookupValue,
IN OC_GET_SYMBOL_LEVEL SymbolLevel )

Definition at line 122 of file Link.c.

◆ InternalPrelinkKext()

EFI_STATUS InternalPrelinkKext ( IN OUT PRELINKED_CONTEXT * Context,
IN PRELINKED_KEXT * Kext,
IN UINT64 LoadAddress,
IN UINT64 FileOffset )

Prelinks the specified KEXT against the specified LoadAddress and the data of its dependencies.

Parameters
[in,out]ContextPrelinking context.
[in]KextKEXT prelinking context.
[in]LoadAddressThe address this KEXT shall be linked against.
[in]FileOffsetThe file offset of the first segment.
Return values
Returnedis whether the prelinking process has been successful. The state of the KEXT is undefined in case this routine fails.

Definition at line 1503 of file Link.c.

◆ InternalProcessSymbolPointers()

STATIC BOOLEAN InternalProcessSymbolPointers ( IN OC_MACHO_CONTEXT * MachoContext,
IN CONST MACH_DYSYMTAB_COMMAND * DySymtab,
IN UINT64 LoadAddress )

process_symbol_pointers

Definition at line 1384 of file Link.c.

◆ InternalRelocateAndCopyRelocations()

STATIC BOOLEAN InternalRelocateAndCopyRelocations ( IN PRELINKED_CONTEXT * Context,
IN PRELINKED_KEXT * Kext,
IN UINT64 LoadAddress,
IN UINT64 RelocationBase,
IN CONST MACH_RELOCATION_INFO * SourceRelocations,
IN OUT UINT32 * NumRelocations,
OUT MACH_RELOCATION_INFO * TargetRelocations )

Relocates all Mach-O Relocations and copies the ones to be preserved after prelinking to TargetRelocations.

Parameters
[in,out]ContextPrelinking context.
[in]KextKEXT prelinking context.
[in]LoadAddressThe address to be linked against. dependencies.
[in]RelocationBaseThe Relocations base address.
[in]SourceRelocationsThe Relocations source buffer.
[in]NumRelocationsOn input, the number of source Relocations. On output, the number of Relocations to preserve.
[out]TargetRelocationsThe Relocations destination buffer.
Return values
Returnedis the number of preserved Relocations.

Definition at line 1106 of file Link.c.

◆ InternalRelocateRelocation()

STATIC UINTN InternalRelocateRelocation ( IN PRELINKED_CONTEXT * Context,
IN PRELINKED_KEXT * Kext,
IN UINT64 LoadAddress,
IN UINT64 RelocationBase,
IN CONST MACH_RELOCATION_INFO * Relocation,
IN CONST MACH_RELOCATION_INFO *NextRelocation OPTIONAL )

Relocates Relocation against the specified Symtab's Symbol Table and LoadAddress. This logically matches KXLD's generic_process_reloc (for 32-bit) and x86_64_process_reloc (for 64-bit).

Parameters
[in,out]ContextPrelinking context.
[in]KextKEXT prelinking context.
[in]LoadAddressThe address to be linked against.
[in]RelocationBaseThe Relocations base address.
[in]RelocationThe Relocation to be processed.
[in]NextRelocationThe Relocation following Relocation.
Return values
0The Relocation does not need to be preseved.
1The Relocation must be preseved.
0| BIT31 The Relocation does not need to be preseved. The next Relocation shall be skipped.
1| BIT31 The Relocation must be preseved. The next Relocation shall be skipped.
MAX_UINTNThe Relocation's target cannot be determined or it is a direct pure virtual call.

Definition at line 793 of file Link.c.

◆ InternalRelocateSymbol()

STATIC BOOLEAN InternalRelocateSymbol ( IN OC_MACHO_CONTEXT * MachoContext,
IN UINT64 LoadAddress,
IN OUT MACH_NLIST_ANY * Symbol,
IN OUT UINT32 * KmodInfoOffset )

Definition at line 1264 of file Link.c.

◆ InternalRelocateSymbols()

STATIC BOOLEAN InternalRelocateSymbols ( IN OC_MACHO_CONTEXT * MachoContext,
IN UINT64 LoadAddress,
IN UINT32 NumSymbols,
IN OUT MACH_NLIST_ANY * Symbols,
OUT UINT32 * KmodInfoOffset )

Definition at line 1309 of file Link.c.

◆ InternalSolveSymbol()

STATIC BOOLEAN InternalSolveSymbol ( IN PRELINKED_CONTEXT * Context,
IN PRELINKED_KEXT * Kext,
IN CONST CHAR8 * Name,
IN OUT MACH_NLIST_ANY * Symbol,
IN OUT UINT64 * WeakTestValue,
IN CONST MACH_NLIST_ANY * UndefinedSymbols,
IN UINT32 NumUndefinedSymbols )

Solves Symbol against the specified DefinedSymbols.

Parameters
[in,out]ContextPrelinking context.
[in]KextKEXT prelinking context.
[in]NameThe name of the symbol to resolve against.
[in,out]SymbolThe symbol to be resolved.
[in,out]WeakTestValuePoints to the value of the Weak test symbol. If it is 0 on input, it will get assigned the value on the first resolution of a weak symbol.
[in]UndefinedSymbolsArray of the Mach-O'S undefined symbols.
[in]NumUndefinedSymbolsNumber of symbols in UndefinedSymbols.
Return values
Returnedis whether the symbol was solved successfully. For weak symbols, this includes solving with _gOSKextUnresolved.

Definition at line 404 of file Link.c.

◆ InternalSolveSymbolNonWeak()

STATIC BOOLEAN InternalSolveSymbolNonWeak ( IN PRELINKED_CONTEXT * Context,
IN PRELINKED_KEXT * Kext,
IN CONST CHAR8 * Name,
IN OUT MACH_NLIST_ANY * Symbol )

Worker function to solve Symbol against the specified DefinedSymbols. It does not consider Symbol might be a weak reference.

Parameters
[in,out]ContextPrelinking context.
[in]KextKEXT prelinking context.
[in]NameThe name of the symbol to resolve against.
[in,out]SymbolThe symbol to be resolved.
Return values
Returnedis whether the symbol was solved successfully.

Definition at line 325 of file Link.c.

◆ InternalSolveSymbolValue()

VOID InternalSolveSymbolValue ( IN BOOLEAN Is32Bit,
IN UINT64 Value,
OUT MACH_NLIST_ANY * Symbol )

Patches Symbol with Value and marks it as solved.

Parameters
[in]Is32BitSymbol is 32-bit.
[in]ValueThe value to solve Symbol with.
[out]SymbolThe symbol to solve.

Definition at line 294 of file Link.c.