Package ghidra.app.util.bin.format.macho
Class MachHeader
java.lang.Object
ghidra.app.util.bin.format.macho.MachHeader
- All Implemented Interfaces:
StructConverter
Represents a mach_header structure.
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionMachHeader
(ByteProvider provider) Creates a newMachHeader
.MachHeader
(ByteProvider provider, long machHeaderStartIndexInProvider) Creates a newMachHeader
.MachHeader
(ByteProvider provider, long machHeaderStartIndexInProvider, boolean isRemainingMachoRelativeToStartIndex) Creatse a newMachHeader
. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
create
(int magic, int cpuType, int cpuSubType, int fileType, int nCmds, int sizeOfCmds, int flags, int reserved) Creates a new Mach Header byte arrayint
int
int
int
<T> T
getFirstLoadCommand
(Class<T> classType) int
getFlags()
long
<T> List
<T> getLoadCommands
(Class<T> classType) int
getMagic()
int
int
getSection
(String segmentName, String sectionName) getSegment
(String segmentName) long
getSize()
Gets the size of thisMachHeader
in bytesint
long
Returns the start index that should be used for calculating offsets.long
Returns the offset of the MachHeader in the ByteProviderboolean
is32bit()
boolean
static boolean
isMachHeader
(ByteProvider provider) Returns true if the specified ByteProvider starts with a Mach header magic signature.parse()
Parses thisMachHeader
'sload commands
parse
(DyldCacheUtils.SplitDyldCache splitDyldCache) Parses thisMachHeader
'sload commands
boolean
parseAndCheck
(int loadCommandType) Parses only thisMachHeader
'sLoadCommand
s to check to see if one of the given type existsParses only thisMachHeader
'ssegments
Returns a structure datatype representing the contents of the implementor of this interface.toString()
-
Constructor Details
-
MachHeader
Creates a newMachHeader
. Assumes the MachHeader starts at index 0 in the ByteProvider.- Parameters:
provider
- the ByteProvider- Throws:
IOException
- if an I/O error occurs while reading from the ByteProviderMachException
- if an invalid MachHeader is detected
-
MachHeader
public MachHeader(ByteProvider provider, long machHeaderStartIndexInProvider) throws IOException, MachException Creates a newMachHeader
. Assumes the MachHeader starts at index machHeaderStartIndexInProvider in the ByteProvider.- Parameters:
provider
- the ByteProvidermachHeaderStartIndexInProvider
- the index into the ByteProvider where the MachHeader begins- Throws:
IOException
- if an I/O error occurs while reading from the ByteProviderMachException
- if an invalid MachHeader is detected
-
MachHeader
public MachHeader(ByteProvider provider, long machHeaderStartIndexInProvider, boolean isRemainingMachoRelativeToStartIndex) throws IOException, MachException Creatse a newMachHeader
. Assumes the MachHeader starts at index machHeaderStartIndexInProvider in the ByteProvider.- Parameters:
provider
- the ByteProvidermachHeaderStartIndexInProvider
- the index into the ByteProvider where the MachHeader begins.isRemainingMachoRelativeToStartIndex
- true if the rest of the macho uses relative indexin (this is common in UBI and kernel cache files); otherwise, false if the rest of the file uses absolute indexing from 0 (this is common in DYLD cache files)- Throws:
IOException
- if an I/O error occurs while reading from the ByteProviderMachException
- if an invalid MachHeader is detected
-
-
Method Details
-
isMachHeader
Returns true if the specified ByteProvider starts with a Mach header magic signature.- Parameters:
provider
-ByteProvider
to check- Returns:
- boolean true if byte provider starts with a MachHeader
-
parse
Parses thisMachHeader
'sload commands
- Returns:
- This
MachHeader
, for convenience - Throws:
IOException
- If there was an IO-related errorMachException
- if the load command is invalid
-
parse
public MachHeader parse(DyldCacheUtils.SplitDyldCache splitDyldCache) throws IOException, MachException Parses thisMachHeader
'sload commands
- Parameters:
splitDyldCache
- TheDyldCacheUtils.SplitDyldCache
that this header resides in. Could be null if a split DYLD cache is not being used.- Returns:
- This
MachHeader
, for convenience - Throws:
IOException
- If there was an IO-related errorMachException
- if the load command is invalid
-
parseSegments
Parses only thisMachHeader
'ssegments
- Returns:
- A
List
of thisMachHeader
'ssegments
- Throws:
IOException
- If there was an IO-related error
-
parseAndCheck
Parses only thisMachHeader
'sLoadCommand
s to check to see if one of the given type exists- Parameters:
loadCommandType
- The type ofLoadCommand
to check for- Returns:
- True if this
MachHeader
contains the givenLoadCommand
type - Throws:
IOException
- If there was an IO-related error- See Also:
-
getMagic
public int getMagic() -
getCpuType
public int getCpuType() -
getImageBase
public long getImageBase() -
getCpuSubType
public int getCpuSubType() -
getFileType
public int getFileType() -
getNumberOfCommands
public int getNumberOfCommands() -
getSizeOfCommands
public int getSizeOfCommands() -
getFlags
public int getFlags() -
getReserved
- Throws:
MachException
-
toDataType
Description copied from interface:StructConverter
Returns a structure datatype representing the contents of the implementor of this interface.For example, given:
class A { int foo; double bar; }
The return value should be a structure data type with two data type components; an INT and a DOUBLE. The structure should contain field names and, if possible, field comments.
- Specified by:
toDataType
in interfaceStructConverter
- Returns:
- returns a structure datatype representing the implementor of this interface
- Throws:
DuplicateNameException
- when a datatype of the same name already existsIOException
- if an IO-related error occurs- See Also:
-
getStartIndex
public long getStartIndex()Returns the start index that should be used for calculating offsets. This will be 0 for things such as the dyld shared cache where offsets are based off the beginning of the file.- Returns:
- the start index that should be used for calculating offsets
-
getStartIndexInProvider
public long getStartIndexInProvider()Returns the offset of the MachHeader in the ByteProvider- Returns:
- the offset of the MachHeader in the ByteProvider
-
is32bit
public boolean is32bit() -
getAddressSize
public int getAddressSize() -
getAllSegments
-
getSegment
-
getSection
-
getAllSections
-
getLoadCommands
-
getLoadCommands
-
getFirstLoadCommand
-
isLittleEndian
public boolean isLittleEndian() -
getSize
public long getSize()Gets the size of thisMachHeader
in bytes- Returns:
- The size of this
MachHeader
in bytes
-
getDescription
-
toString
-
create
public static byte[] create(int magic, int cpuType, int cpuSubType, int fileType, int nCmds, int sizeOfCmds, int flags, int reserved) throws MachException Creates a new Mach Header byte array- Parameters:
magic
- The magiccpuType
- The cpu typecpuSubType
- The cpu subtypefileType
- The file typenCmds
- The number of commandssizeOfCmds
- The size of the commandsflags
- The flagsreserved
- A reserved value (ignored for 32-bit magic)- Returns:
- The new header in byte array form
- Throws:
MachException
- if an invalid magic value was passed in (seeMachConstants
)
-