OpenCore  1.0.4
OpenCore Bootloader
1.0.4
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Header.c File Reference
#include <Uefi.h>
#include <IndustryStandard/AppleMachoImage.h>
#include <IndustryStandard/AppleFatBinaryImage.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/DebugLib.h>
#include <Library/OcMachoLib.h>
#include "OcMachoLibInternal.h"

Go to the source code of this file.

Functions

BOOLEAN MachoInitializeContext (OUT OC_MACHO_CONTEXT *Context, IN VOID *FileData, IN UINT32 FileSize, IN UINT32 HeaderOffset, IN UINT32 InnerSize, IN BOOLEAN Is32Bit)
 
MACH_HEADER_ANYMachoGetMachHeader (IN OUT OC_MACHO_CONTEXT *Context)
 
UINT32 MachoGetInnerSize (IN OUT OC_MACHO_CONTEXT *Context)
 
VOID * MachoGetFileData (IN OUT OC_MACHO_CONTEXT *Context)
 
UINT32 MachoGetFileSize (IN OUT OC_MACHO_CONTEXT *Context)
 
UINT32 MachoGetVmSize (IN OUT OC_MACHO_CONTEXT *Context)
 
UINT64 MachoGetLastAddress (IN OUT OC_MACHO_CONTEXT *Context)
 
MACH_LOAD_COMMANDMachoGetNextCommand (IN OUT OC_MACHO_CONTEXT *Context, IN MACH_LOAD_COMMAND_TYPE LoadCommandType, IN CONST MACH_LOAD_COMMAND *LoadCommand OPTIONAL)
 
MACH_UUID_COMMANDMachoGetUuid (IN OUT OC_MACHO_CONTEXT *Context)
 
MACH_SEGMENT_COMMAND_ANYMachoGetNextSegment (IN OUT OC_MACHO_CONTEXT *Context, IN CONST MACH_SEGMENT_COMMAND_ANY *Segment OPTIONAL)
 
MACH_SECTION_ANYMachoGetNextSection (IN OUT OC_MACHO_CONTEXT *Context, IN MACH_SEGMENT_COMMAND_ANY *Segment, IN MACH_SECTION_ANY *Section OPTIONAL)
 
MACH_SECTION_ANYMachoGetSectionByIndex (IN OUT OC_MACHO_CONTEXT *Context, IN UINT32 Index)
 
MACH_SEGMENT_COMMAND_ANYMachoGetSegmentByName (IN OUT OC_MACHO_CONTEXT *Context, IN CONST CHAR8 *SegmentName)
 
MACH_SECTION_ANYMachoGetSectionByName (IN OUT OC_MACHO_CONTEXT *Context, IN MACH_SEGMENT_COMMAND_ANY *Segment, IN CONST CHAR8 *SectionName)
 
MACH_SECTION_ANYMachoGetSegmentSectionByName (IN OUT OC_MACHO_CONTEXT *Context, IN CONST CHAR8 *SegmentName, IN CONST CHAR8 *SectionName)
 
STATIC BOOLEAN InternalInitialiseSymtabs (IN OUT OC_MACHO_CONTEXT *Context, IN MACH_SYMTAB_COMMAND *Symtab, IN MACH_DYSYMTAB_COMMAND *DySymtab)
 
BOOLEAN MachoInitialiseSymtabsExternal (IN OUT OC_MACHO_CONTEXT *Context, IN OC_MACHO_CONTEXT *SymsContext)
 
BOOLEAN InternalRetrieveSymtabs (IN OUT OC_MACHO_CONTEXT *Context)
 
UINT32 MachoGetSymbolTable (IN OUT OC_MACHO_CONTEXT *Context, OUT CONST MACH_NLIST_ANY **SymbolTable, OUT CONST CHAR8 **StringTable OPTIONAL, OUT CONST MACH_NLIST_ANY **LocalSymbols OPTIONAL, OUT UINT32 *NumLocalSymbols OPTIONAL, OUT CONST MACH_NLIST_ANY **ExternalSymbols OPTIONAL, OUT UINT32 *NumExternalSymbols OPTIONAL, OUT CONST MACH_NLIST_ANY **UndefinedSymbols OPTIONAL, OUT UINT32 *NumUndefinedSymbols OPTIONAL)
 
UINT32 MachoGetIndirectSymbolTable (IN OUT OC_MACHO_CONTEXT *Context, OUT CONST MACH_NLIST_ANY **SymbolTable)
 
UINT64 MachoRuntimeGetEntryAddress (IN VOID *Image)
 
VOID * MachoGetFilePointerByAddress (IN OUT OC_MACHO_CONTEXT *Context, IN UINT64 Address, OUT UINT32 *MaxSize OPTIONAL)
 
UINT32 MachoExpandImage (IN OC_MACHO_CONTEXT *Context, OUT UINT8 *Destination, IN UINT32 DestinationSize, IN BOOLEAN Strip, OUT UINT64 *FileOffset OPTIONAL)
 
UINT32 MachoGetExpandedImageSize (IN OC_MACHO_CONTEXT *Context)
 
BOOLEAN MachoMergeSegments (IN OUT OC_MACHO_CONTEXT *Context, IN CONST CHAR8 *Prefix)
 

Function Documentation

◆ InternalInitialiseSymtabs()

STATIC BOOLEAN InternalInitialiseSymtabs ( IN OUT OC_MACHO_CONTEXT * Context,
IN MACH_SYMTAB_COMMAND * Symtab,
IN MACH_DYSYMTAB_COMMAND * DySymtab )

Initialises the symbol information of Context.

Parameters
[in,out]ContextContext of the Mach-O.
[in]SymtabThe SYMTAB command to initialise with.
[in]DySymtabThe DYSYMTAB command to initialise with.
Returns
Whether the operation was successful.

Definition at line 253 of file Header.c.

◆ InternalRetrieveSymtabs()

BOOLEAN InternalRetrieveSymtabs ( IN OUT OC_MACHO_CONTEXT * Context)

Retrieves the SYMTAB command.

Parameters
[in]ContextContext of the Mach-O.
Return values
NULLNULL is returned on failure.

Definition at line 508 of file Header.c.

◆ MachoExpandImage()

UINT32 MachoExpandImage ( IN OC_MACHO_CONTEXT * Context,
OUT UINT8 * Destination,
IN UINT32 DestinationSize,
IN BOOLEAN Strip,
OUT UINT64 *FileOffset OPTIONAL )

Expand Mach-O image to Destination (make segment file sizes equal to vm sizes).

Parameters
[in]ContextContext of the Mach-O.
[out]DestinationOutput buffer.
[in]DestinationSizeOutput buffer maximum size.
[in]StripOutput with stripped prelink commands.
[in]FileOffsetPointer to the file offset of the first segment.
Returns
New image size or 0 on failure.

Definition at line 645 of file Header.c.

◆ MachoGetExpandedImageSize()

UINT32 MachoGetExpandedImageSize ( IN OC_MACHO_CONTEXT * Context)

Calculates size required for expanded Mach-O image using MachoExpandImage().

Parameters
[in]ContextContext of the Mach-O.
Returns
New image size or 0 on failure.

Definition at line 661 of file Header.c.

◆ MachoGetFileData()

VOID * MachoGetFileData ( IN OUT OC_MACHO_CONTEXT * Context)

Returns the file data of the Mach-O.

Parameters
[in,out]ContextContext of the Mach-O.

Definition at line 66 of file Header.c.

◆ MachoGetFilePointerByAddress()

VOID * MachoGetFilePointerByAddress ( IN OUT OC_MACHO_CONTEXT * Context,
IN UINT64 Address,
OUT UINT32 *MaxSize OPTIONAL )

Returns a pointer to the Mach-O file at the specified virtual address.

Parameters
[in,out]ContextContext of the Mach-O.
[in]AddressVirtual address to look up.
[out]MaxSizeMaximum data safely available from FileOffset. If NULL is returned, the output is undefined.

Definition at line 627 of file Header.c.

◆ MachoGetFileSize()

UINT32 MachoGetFileSize ( IN OUT OC_MACHO_CONTEXT * Context)

Returns the Mach-O's file size.

Parameters
[in,out]ContextContext of the Mach-O.

Definition at line 77 of file Header.c.

◆ MachoGetIndirectSymbolTable()

UINT32 MachoGetIndirectSymbolTable ( IN OUT OC_MACHO_CONTEXT * Context,
OUT CONST MACH_NLIST_ANY ** SymbolTable )

Obtain indirect symbol table.

Parameters
[in]ContextContext of the Mach-O.
[in,out]SymbolTableIndirect symbol table.
Returns
number of symbols in indirect symbol table or 0.

Definition at line 559 of file Header.c.

◆ MachoGetInnerSize()

UINT32 MachoGetInnerSize ( IN OUT OC_MACHO_CONTEXT * Context)

Returns the size of the inner Mach-O file (otherwise, the file size).

Parameters
[in,out]ContextContext of the Mach-O.

Definition at line 55 of file Header.c.

◆ MachoGetLastAddress()

UINT64 MachoGetLastAddress ( IN OUT OC_MACHO_CONTEXT * Context)

Returns the last virtual address of a Mach-O.

Parameters
[in]ContextContext of the Mach-O.
Return values
0The binary is malformed.

Definition at line 99 of file Header.c.

◆ MachoGetMachHeader()

MACH_HEADER_ANY * MachoGetMachHeader ( IN OUT OC_MACHO_CONTEXT * Context)

Returns the universal Mach-O Header structure.

Parameters
[in,out]ContextContext of the Mach-O.

Definition at line 44 of file Header.c.

◆ MachoGetNextCommand()

MACH_LOAD_COMMAND * MachoGetNextCommand ( IN OUT OC_MACHO_CONTEXT * Context,
IN MACH_LOAD_COMMAND_TYPE LoadCommandType,
IN CONST MACH_LOAD_COMMAND *LoadCommand OPTIONAL )

Retrieves the next Load Command of type LoadCommandType.

Parameters
[in,out]ContextContext of the Mach-O.
[in]LoadCommandTypeType of the Load Command to retrieve.
[in]LoadCommandPrevious Load Command. If NULL, the first match is returned.
Return values
NULLNULL is returned on failure.

Definition at line 110 of file Header.c.

◆ MachoGetNextSection()

MACH_SECTION_ANY * MachoGetNextSection ( IN OUT OC_MACHO_CONTEXT * Context,
IN MACH_SEGMENT_COMMAND_ANY * Segment,
IN MACH_SECTION_ANY *Section OPTIONAL )

Retrieves the next section of a segment.

Parameters
[in,out]ContextContext of the Mach-O.
[in]SegmentThe segment to get the section of.
[in]SectionThe section to get the successor of.
Return values
NULLNULL is returned on failure.

Definition at line 173 of file Header.c.

◆ MachoGetNextSegment()

MACH_SEGMENT_COMMAND_ANY * MachoGetNextSegment ( IN OUT OC_MACHO_CONTEXT * Context,
IN CONST MACH_SEGMENT_COMMAND_ANY *Segment OPTIONAL )

Retrieves the next segment.

Parameters
[in,out]ContextContext of the Mach-O.
[in]SegmentSegment to retrieve the successor of. if NULL, the first segment is returned.
Return values
NULLNULL is returned on failure.

Definition at line 160 of file Header.c.

◆ MachoGetSectionByIndex()

MACH_SECTION_ANY * MachoGetSectionByIndex ( IN OUT OC_MACHO_CONTEXT * Context,
IN UINT32 Index )

Retrieves a section by its index.

Parameters
[in,out]ContextContext of the Mach-O.
[in]IndexIndex of the section to retrieve.
Return values
NULLNULL is returned on failure.

Definition at line 188 of file Header.c.

◆ MachoGetSectionByName()

MACH_SECTION_ANY * MachoGetSectionByName ( IN OUT OC_MACHO_CONTEXT * Context,
IN MACH_SEGMENT_COMMAND_ANY * Segment,
IN CONST CHAR8 * SectionName )

Retrieves the first section by the name of SectionName.

Parameters
[in,out]ContextContext of the Mach-O.
[in]SegmentSegment to search in.
[in]SectionNameSection name to search for.
Return values
NULLNULL is returned on failure.

Definition at line 214 of file Header.c.

◆ MachoGetSegmentByName()

MACH_SEGMENT_COMMAND_ANY * MachoGetSegmentByName ( IN OUT OC_MACHO_CONTEXT * Context,
IN CONST CHAR8 * SegmentName )

Retrieves the first segment by the name of SegmentName.

Parameters
[in,out]ContextContext of the Mach-O.
[in]SegmentNameSegment name to search for.
Return values
NULLNULL is returned on failure.

Definition at line 201 of file Header.c.

◆ MachoGetSegmentSectionByName()

MACH_SECTION_ANY * MachoGetSegmentSectionByName ( IN OUT OC_MACHO_CONTEXT * Context,
IN CONST CHAR8 * SegmentName,
IN CONST CHAR8 * SectionName )

Retrieves a section within a segment by the name of SegmentName.

Parameters
[in,out]ContextContext of the Mach-O.
[in]SegmentNameThe name of the segment to search in.
[in]SectionNameThe name of the section to search for.
Return values
NULLNULL is returned on failure.

Definition at line 228 of file Header.c.

◆ MachoGetSymbolTable()

UINT32 MachoGetSymbolTable ( IN OUT OC_MACHO_CONTEXT * Context,
OUT CONST MACH_NLIST_ANY ** SymbolTable,
OUT CONST CHAR8 **StringTable OPTIONAL,
OUT CONST MACH_NLIST_ANY **LocalSymbols OPTIONAL,
OUT UINT32 *NumLocalSymbols OPTIONAL,
OUT CONST MACH_NLIST_ANY **ExternalSymbols OPTIONAL,
OUT UINT32 *NumExternalSymbols OPTIONAL,
OUT CONST MACH_NLIST_ANY **UndefinedSymbols OPTIONAL,
OUT UINT32 *NumUndefinedSymbols OPTIONAL )

Obtain symbol tables.

Parameters
[in]ContextContext of the Mach-O.
[out]SymbolTableSymbol table.
[out]StringTableString table for that symbol table.
[out]LocalSymbolsLocal symbol table.
[out]NumLocalSymbolsNumber of symbols in local symbol table.
[out]ExternalSymbolsExternal symbol table.
[out]NumExternalSymbolsNumber of symbols in external symbol table.
[out]UndefinedSymbolsUndefined symbol table.
[out]NumUndefinedSymbolsNumber of symbols in undefined symbol table.
Returns
number of symbols in symbol table or 0.

Definition at line 519 of file Header.c.

◆ MachoGetUuid()

MACH_UUID_COMMAND * MachoGetUuid ( IN OUT OC_MACHO_CONTEXT * Context)

Retrieves the first UUID Load Command.

Parameters
[in,out]ContextContext of the Mach-O.
Return values
NULLNULL is returned on failure.

Definition at line 124 of file Header.c.

◆ MachoGetVmSize()

UINT32 MachoGetVmSize ( IN OUT OC_MACHO_CONTEXT * Context)

Returns the Mach-O's virtual address space size.

Parameters
[out]ContextContext of the Mach-O.

Definition at line 88 of file Header.c.

◆ MachoInitialiseSymtabsExternal()

BOOLEAN MachoInitialiseSymtabsExternal ( IN OUT OC_MACHO_CONTEXT * Context,
IN OC_MACHO_CONTEXT * SymsContext )

Definition at line 423 of file Header.c.

◆ MachoInitializeContext()

BOOLEAN MachoInitializeContext ( OUT OC_MACHO_CONTEXT * Context,
IN VOID * FileData,
IN UINT32 FileSize,
IN UINT32 HeaderOffset,
IN UINT32 InnerSize,
IN BOOLEAN Is32Bit )

Provides services for Mach-O headers.

Copyright (C) 2016 - 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 at line 29 of file Header.c.

◆ MachoMergeSegments()

BOOLEAN MachoMergeSegments ( IN OUT OC_MACHO_CONTEXT * Context,
IN CONST CHAR8 * Prefix )

Merge Mach-O segments into one with lowest protection.

Parameters
[in,out]ContextContext of the Mach-O.
[in]PrefixSegment prefix to merge.
Return values
TRUEon success

Definition at line 673 of file Header.c.

◆ MachoRuntimeGetEntryAddress()

UINT64 MachoRuntimeGetEntryAddress ( IN VOID * Image)

Find Mach-O entry point from LC_UNIXTHREAD loader command. This command does not verify Mach-O and assumes it is valid.

Parameters
[in]ImageLoaded Mach-O image.
Returns
Entry point or 0.

Definition at line 572 of file Header.c.