Java Code Examples for ghidra.app.util.bin.BinaryReader#length()

The following examples show how to use ghidra.app.util.bin.BinaryReader#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BTreeRootNodeDescriptor.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public BTreeRootNodeDescriptor( BinaryReader reader ) throws IOException {
	super( reader );

	headerRecord   = new BTreeHeaderRecord( reader );
	userDataRecord = new BTreeUserDataRecord( reader );
	mapRecord      = new BTreeMapRecord( reader, headerRecord );

	nodes.add( this );

	int nodeSize = headerRecord.getNodeSize() & 0xffff;

	for ( int i = nodeSize ; i < reader.length() ; i += nodeSize ) {
		reader.setPointerIndex( i );
		BTreeNodeDescriptor node = new BTreeNodeDescriptor( reader );
		nodes.add( node );
		node.readRecordOffsets( reader, i, headerRecord );
		node.readRecords( reader, i );
	}

	this.readRecordOffsets( reader, 0, headerRecord );
}
 
Example 2
Source File: BinaryPropertyListTrailer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public BinaryPropertyListTrailer( BinaryReader reader ) throws IOException {
	trailerIndex = reader.length( ) - BinaryPropertyListConstants.TRAILER_SIZE;

	offsetSize = reader.readByte( trailerIndex + 6 ) & 0xff;
	objectRefSize = reader.readByte( trailerIndex + 7 ) & 0xff;
	objectCount = reader.readInt( trailerIndex + 12 ) & 0xffffffff;
	topObject = reader.readInt( trailerIndex + 20 ) & 0xffffffff;
	offsetTableOffset = reader.readInt( trailerIndex + 28 ) & 0xffffffff;

	// if ( offsetSize != 4 ) {
	// throw new IOException( "Unsupported binary PLIST offset size: " +
	// offsetSize );
	// }

	offsetTable = new int [ objectCount ];

	for ( int i = 0 ; i < objectCount ; ++i ) {
		if ( offsetSize == 4 ) {
			offsetTable[ i ] = reader.readInt( offsetTableOffset + i * offsetSize );
		}
		else if ( offsetSize == 2 ) {
			offsetTable[ i ] = reader.readShort( offsetTableOffset + i * offsetSize ) & 0xffff;
		}
		else if ( offsetSize == 1 ) {
			offsetTable[ i ] = reader.readByte( offsetTableOffset + i * offsetSize ) & 0xff;
		}
		else {
			throw new RuntimeException( "Invalid offset size in binary PList" );
		}
	}
}
 
Example 3
Source File: DIEAggregate.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a range list from the debug_ranges section.
 * See DWARF4 Section 2.17.3 (Non-Contiguous Address Ranges).
 * <p>
 * @param attribute attribute ie. {@link DWARFAttribute#DW_AT_ranges}
 * @return list of ranges
 * @throws IOException if an I/O error occurs
 */
public List<DWARFRange> parseDebugRange(int attribute) throws IOException {
	byte pointerSize = getCompilationUnit().getPointerSize();
	BinaryReader reader = getCompilationUnit().getProgram().getDebugRanges();

	long offset = getUnsignedLong(attribute, -1);
	if (offset == -1) {
		throw new IOException("Bad / missing attribute " + attribute);
	}
	reader.setPointerIndex(offset);
	List<DWARFRange> ranges = new ArrayList<>();

	long baseAddress = getCompilationUnit().getCompileUnit() != null &&
		getCompilationUnit().getCompileUnit().getLowPC() != null
				? getCompilationUnit().getCompileUnit().getLowPC().longValue()
				: 0L;

	while (reader.getPointerIndex() < reader.length()) {
		// Read the beginning and ending addresses
		Number beginning = DWARFUtil.readAddress(reader, pointerSize);
		Number ending = DWARFUtil.readAddress(reader, pointerSize);	// dwarf end addrs are exclusive

		// End of the list
		if (beginning.longValue() == 0 && ending.longValue() == 0) {
			break;
		}

		// Check to see if this is a base address entry
		if (NumberUtil.equalsMaxUnsignedValue(beginning)) {
			baseAddress = ending.longValue();
			continue;
		}

		// Add the range to the list
		ranges.add(new DWARFRange(baseAddress + beginning.longValue(),
			baseAddress + ending.longValue()));
	}
	Collections.sort(ranges);
	return ranges;
}
 
Example 4
Source File: ApploaderHeader.java    From Ghidra-GameCube-Loader with Apache License 2.0 5 votes vote down vote up
private void readHeader(BinaryReader reader) {
	try {
		fileSize = reader.length();
		reader.setPointerIndex(0);
		
		revision = reader.readNextAsciiString(16);
		entryPoint = reader.readNextUnsignedInt();
		size = reader.readNextInt();
		trailerSize = reader.readNextInt();
	}
	catch(IOException e) {
		Msg.error(this, "Failed to read Apploader header!");
	}
}
 
Example 5
Source File: DIEAggregate.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Return a list of DWARF locations read from the debug_loc section.
 * @param offset offset into the debug_loc section
 * @return list of DWARF locations (address range and location expression)
 * @throws IOException if an I/O error occurs
 */
private List<DWARFLocation> readDebugLocList(long offset) throws IOException {
	BinaryReader debug_loc = getCompilationUnit().getProgram().getDebugLocation();

	List<DWARFLocation> ranges = new ArrayList<>();
	if (debug_loc == null) {
		return ranges;
	}

	debug_loc.setPointerIndex(offset);
	byte pointerSize = getCompilationUnit().getPointerSize();

	Number baseAddress = getCompilationUnit().getCompileUnit().getLowPC();
	long baseAddressOffset = (baseAddress != null) ? baseAddress.longValue() : 0;

	Number cuLowPC = getCompilationUnit().getCompileUnit().getLowPC();
	long cuBase = (cuLowPC != null) ? cuLowPC.longValue() : Long.MAX_VALUE;

	// Loop through the debug_loc entry
	while (debug_loc.getPointerIndex() < debug_loc.length()) {
		Number beginning = DWARFUtil.readAddress(debug_loc, pointerSize);
		Number ending = DWARFUtil.readAddress(debug_loc, pointerSize);

		// List end
		if (beginning.longValue() == 0 && ending.longValue() == 0) {
			break;
		}

		// Check to see if this is a base address entry
		if (NumberUtil.equalsMaxUnsignedValue(beginning)) {
			baseAddressOffset = ending.longValue();
			continue;
		}

		// Size is 2 bytes
		int size = debug_loc.readNextShort() & NumberUtil.UNSIGNED_SHORT_MASK;

		// Read the location description
		byte[] location = debug_loc.readNextByteArray(size);

		// Test to see if the 'offset' read from the debug_loc data is already
		// greater-than the compunit's lowpc.  This indicates the 'offset' isn't
		// an offset, but already an absolute value.  This occurs in some
		// gcc dwarf compilation flag combinations.
		boolean isBadOffset = (beginning.longValue() > cuBase);

		long absStart = beginning.longValue();
		long absEnd = ending.longValue();
		if (!isBadOffset) {
			absStart += baseAddressOffset;
			absEnd += baseAddressOffset;
		}

		// TODO: verify end addr calc with DWARFstd.pdf, inclusive vs exclusive
		ranges.add(new DWARFLocation(new DWARFRange(absStart, absEnd + 1), location));
	}
	return ranges;
}
 
Example 6
Source File: RELHeader.java    From Ghidra-GameCube-Loader with Apache License 2.0 4 votes vote down vote up
public boolean IsValid(BinaryReader reader) {
	try {
		long fileSize = reader.length();
		
		// Check section info is valid first.
		if (this.sectionTableOffset > fileSize) {
			Msg.error(this, "Unable to load REL file! Reason: Section Info Table address is past file bounds!");
			return false;
		}
		
		// Check that the relocation data offset & import info offset are valid offsets in the file.
		if (this.relocationTableOffset >= fileSize) {
			Msg.error(this, "Unable to load REL file! Reason: Relocation Data offset in header is past the file bounds!");
			return false;
		}
		
		if (this.importTableOffset + this.importTableSize > fileSize) {
			Msg.error(this, "Unable to load REL file! Reason: Import Table offset + Import Table size in header is past the file bounds!");
			return false;
		}
		
		long sectionTableSize = this.sectionCount * RELHeader.SECTION_INFO_SIZE;
		
		// Get the first section address by file address.
		long firstSectionInFileAddress = -1;
		reader.setPointerIndex(this.sectionTableOffset);
		
		for (int i = 0; i < this.sectionCount; i++) {
			long sectionAddress = reader.readNextUnsignedInt() & ~1; // Clear the executable bit-flag.
			long sectionSize = reader.readNextUnsignedInt();
			
			if (sectionAddress != 0 && sectionSize != 0 && sectionSize != this.bssSize) {
				if (firstSectionInFileAddress == -1 || sectionAddress < firstSectionInFileAddress) {
					firstSectionInFileAddress = sectionAddress;
				}
			}
		}
		
		// Ensure that the section table offset doesn't intersect the first section's data.
		if (this.sectionTableOffset + sectionTableSize > firstSectionInFileAddress) {
			Msg.error(this, "Unable to load REL file! Reason: Section Info Table intersects section data!");
			return false;
		}
		
		// TODO: Ensure that no section intersects with another. Should this include the relocation data section & import info section?
	}
	catch (IOException e) {
		return false;
	}
	
	return true;
}