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

The following examples show how to use ghidra.app.util.bin.BinaryReader#readNextUnsignedInt() . 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: DWARFUtil.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Read a variable-sized unsigned integer and return it as a java signed int.
 * <p>
 * Unsigned 32 bit int values larger than java's signed Integer.MAX_VALUE are not
 * supported and will throw an IOException.
 *
 * @param reader {@link BinaryReader} to read the data from
 * @param size number of bytes the integer value is stored in, must be 1, 2 or 4.
 * @return unsigned integer value.
 * @throws IOException if error
 */
public static int readVarSizedUInt(BinaryReader reader, int size) throws IOException {
	switch (size) {
		case 1:
			return reader.readNextUnsignedByte();
		case 2:
			return reader.readNextUnsignedShort();
		case 4:
			long l = reader.readNextUnsignedInt();
			if (l < 0 || l > Integer.MAX_VALUE) {
				throw new IOException("Unsigned int value too large: " + l);
			}
			return (int) l;
	}
	throw new IOException("Unsupported variable-sized int: " + size);
}
 
Example 2
Source File: DWARFUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Read an offset value who's size depends on the DWARF format: 32 vs 64.
 * <p>
 * @param reader BinaryReader pointing to the value to read
 * @param dwarfFormat - See {@link DWARFCompilationUnit#DWARF_32} and {@link DWARFCompilationUnit#DWARF_64}.
 * @return the offset value
 * @throws IOException if an I/O error occurs or bad dwarfFormat value
 */
public static long readOffsetByDWARFformat(BinaryReader reader, int dwarfFormat)
		throws IOException {
	switch (dwarfFormat) {
		case DWARFCompilationUnit.DWARF_32:
			return reader.readNextUnsignedInt();
		case DWARFCompilationUnit.DWARF_64:
			return reader.readNextLong();
	}
	throw new IOException("Unknown DWARF Format Value: " + dwarfFormat);
}
 
Example 3
Source File: DWARFUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Read a variable-sized unsigned integer and return it as a java signed long.
 * <p>
 * @param reader {@link BinaryReader} to read the data from
 * @param pointerSize number of bytes the value is stored in, must be 1, 2, 4, or 8.
 * @return unsigned long integer value.
 * @throws IOException if error
 */
public static long readVarSizedULong(BinaryReader reader, int pointerSize) throws IOException {
	switch (pointerSize) {
		case 1:
			return reader.readNextUnsignedByte();
		case 2:
			return reader.readNextUnsignedShort();
		case 4:
			return reader.readNextUnsignedInt();
		case 8:
			return reader.readNextLong() /* no unsigned long mask possible */;
	}
	throw new IOException("Unsupported variable-sized int: " + pointerSize);
}
 
Example 4
Source File: DWARFUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a variable-sized unsigned 'address' value from a {@link BinaryReader} and
 * returns it as a 64 bit java long.
 * <p>
 * The valid pointerSizes are 1, 2, 4, and 8.
 * <p>
 * @param reader {@link BinaryReader} to read the data from
 * @param pointerSize number of bytes the value is stored in, must be 1, 2, 4, or 8.
 * @return unsigned long value.
 * @throws IOException if error
 */
public static long readAddressAsLong(BinaryReader reader, byte pointerSize) throws IOException {
	switch (pointerSize) {
		case 1:
			return reader.readNextUnsignedByte();
		case 2:
			return reader.readNextUnsignedShort();
		case 4:
			return reader.readNextUnsignedInt();
		case 8:
			return reader.readNextLong();
	}
	throw new IllegalArgumentException(
		"Unknown pointer size: 0x" + Integer.toHexString(pointerSize));
}
 
Example 5
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 6
Source File: DWARFLine.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public DWARFLine(BinaryReader reader) throws IOException, DWARFException {
	this.unit_length = reader.readNextUnsignedInt();
	// Length of 0xffffffff implies 64-bit DWARF format
	if (this.unit_length == 0xffffffffL) {
		this.unit_length = reader.readNextLong();
		this.format = DWARFCompilationUnit.DWARF_64;
	}
	// Length of 0xfffffff0 or greater is reserved for DWARF
	else if (this.unit_length >= 0xfffffff0L) {
		throw new DWARFException("Reserved DWARF length value: " +
			Long.toHexString(this.unit_length) + ". Unknown extension.");
	}
	else {
		this.format = DWARFCompilationUnit.DWARF_32;
	}

	// A version number for this line number information section
	this.version = reader.readNextUnsignedShort();

	// Get the header length based on the current format
	this.header_length = DWARFUtil.readOffsetByDWARFformat(reader, this.format);

	this.minimum_instruction_length = reader.readNextUnsignedByte();

	// Maximum operations per instruction only exists in DWARF version 4 or higher
	if (this.version >= 4) {
		this.maximum_operations_per_instruction = reader.readNextUnsignedByte();
	}
	else {
		this.maximum_operations_per_instruction = 1;
	}
	this.default_is_stmt = reader.readNextUnsignedByte();
	this.line_base = reader.readNextByte();
	this.line_range = reader.readNextUnsignedByte();
	this.opcode_base = reader.readNextUnsignedByte();
	this.standard_opcode_length = new int[this.opcode_base];
	this.standard_opcode_length[0] = 1; /* Should never be used */
	for (int i = 1; i < this.opcode_base; i++) {
		this.standard_opcode_length[i] = reader.readNextUnsignedByte();
	}

	// Read all include directories
	this.include_directories = new ArrayList<>();
	String include = reader.readNextAsciiString();
	while (include.length() != 0) {
		this.include_directories.add(include);
		include = reader.readNextAsciiString();
	}

	// Read all files
	this.file_names = new ArrayList<>();
	DWARFFile file = new DWARFFile(reader);
	while (file.getName().length() != 0) {
		this.file_names.add(file);
		file = new DWARFFile(reader);
	}
}
 
Example 7
Source File: DWARFCompilationUnit.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new {@link DWARFCompilationUnit} by reading a compilationUnit's header data
 * from the debug_info section and the debug_abbr section and its compileUnit DIE (ie.
 * the first DIE right after the header).
 * <p>
 * Returns NULL if there was an ignorable error while reading the compilation unit (and
 * leaves the input stream at the next compilation unit to read), otherwise throws
 * an IOException if there was an unrecoverable error.
 *
 * @param dwarfProgram the dwarf program.
 * @param debugInfoBR the debug info binary reader.
 * @param debugAbbrBR the debug abbreviation binary reader
 * @param cuNumber the compilation unit number
 * @param monitor the current task monitor
 * @return the read compilation unit.
 * @throws DWARFException if an invalid or unsupported DWARF version is read.
 * @throws IOException if the length of the compilation unit is invalid.
 * @throws CancelledException if the task has been canceled.
 */
public static DWARFCompilationUnit readCompilationUnit(DWARFProgram dwarfProgram,
		BinaryReader debugInfoBR, BinaryReader debugAbbrBR, int cuNumber, TaskMonitor monitor)
		throws DWARFException, IOException, CancelledException {

	long startOffset = debugInfoBR.getPointerIndex();
	long length = debugInfoBR.readNextUnsignedInt();
	int format;

	if (length == 0xffffffffL) {
		// Length of 0xffffffff implies 64-bit DWARF format
		// Mostly untested as there is no easy way to force the compiler
		// to generate this
		length = debugInfoBR.readNextLong();
		format = DWARF_64;
	}
	else if (length >= 0xfffffff0L) {
		// Length of 0xfffffff0 or greater is reserved for DWARF
		throw new DWARFException("Reserved DWARF length value: " + Long.toHexString(length) +
			". Unknown extension.");
	}
	else if (length == 0) {
		throw new DWARFException("Invalid length 0 for DWARF Compilation Unit at 0x" +
			Long.toHexString(startOffset));
	}
	else {
		format = DWARF_32;
	}

	long endOffset = (debugInfoBR.getPointerIndex() + length);
	short version = debugInfoBR.readNextShort();
	long abbreviationOffset = DWARFUtil.readOffsetByDWARFformat(debugInfoBR, format);
	byte pointerSize = debugInfoBR.readNextByte();
	long firstDIEOffset = debugInfoBR.getPointerIndex();

	if (version < 2 || version > 4) {
		throw new DWARFException(
			"Only DWARF version 2, 3, or 4 information is currently supported.");
	}
	if (firstDIEOffset > endOffset) {
		throw new IOException("Invalid length " + (endOffset - startOffset) +
			" for DWARF Compilation Unit at 0x" + Long.toHexString(startOffset));
	}
	else if (firstDIEOffset == endOffset) {
		// silently skip this empty compunit
		return null;
	}

	debugAbbrBR.setPointerIndex(abbreviationOffset);
	Map<Integer, DWARFAbbreviation> abbrMap =
		DWARFAbbreviation.readAbbreviations(debugAbbrBR, dwarfProgram, monitor);

	DWARFCompilationUnit cu =
		new DWARFCompilationUnit(dwarfProgram, startOffset, endOffset, length, format, version,
			abbreviationOffset, pointerSize, cuNumber, firstDIEOffset, abbrMap);

	try {
		DebugInfoEntry compileUnitDIE =
			DebugInfoEntry.read(debugInfoBR, cu, dwarfProgram.getAttributeFactory());

		DWARFCompileUnit compUnit = DWARFCompileUnit.read(
			DIEAggregate.createSingle(compileUnitDIE), dwarfProgram.getDebugLine());
		cu.setCompileUnit(compUnit);
		return cu;
	}
	catch (IOException ioe) {
		Msg.error(null,
			"Failed to parse the DW_TAG_compile_unit DIE at the start of compilation unit " +
				cuNumber + " at offset " + startOffset + " (0x" +
				Long.toHexString(startOffset) + "), skipping entire compilation unit",
			ioe);
		debugInfoBR.setPointerIndex(cu.getEndOffset());
		return null;
	}
}
 
Example 8
Source File: RELHeader.java    From Ghidra-GameCube-Loader with Apache License 2.0 4 votes vote down vote up
private void readHeader(BinaryReader reader) {
	try {
		reader.setPointerIndex(0);
		
		this.moduleId = reader.readNextUnsignedInt();
		this.previousModuleAddress = reader.readNextUnsignedInt();
		this.nextModuleAddress = reader.readNextUnsignedInt();
		this.sectionCount = reader.readNextUnsignedInt();
		this.sectionTableOffset = reader.readNextUnsignedInt();
		this.moduleNameOffset = reader.readNextUnsignedInt();
		this.moduleNameLength = reader.readNextUnsignedInt();
		this.moduleVersion = reader.readNextUnsignedInt();
		this.bssSize = reader.readNextUnsignedInt();
		this.relocationTableOffset = reader.readNextUnsignedInt();
		this.importTableOffset = reader.readNextUnsignedInt();
		this.importTableSize = reader.readNextUnsignedInt();
		this.prologSectionId = reader.readNextUnsignedByte();
		this.epilogSectionId = reader.readNextUnsignedByte();
		this.unresolvedSectionId = reader.readNextUnsignedByte();
		this.bssSectionId = reader.readNextUnsignedByte();
		this.prologSectionOffset = reader.readNextUnsignedInt();
		this.epilogSectionOffset = reader.readNextUnsignedInt();
		this.unresolvedSectionOffset = reader.readNextUnsignedInt();
		
		// Version specific settings
		if (this.moduleVersion > 1) {
			this.sectionAlignment = reader.readNextUnsignedInt();
			this.bssSectionAlignment = reader.readNextUnsignedInt();
		}
		else {
		    // Version 1's default values for alignment
		    this.sectionAlignment = 32;
		    this.bssSectionAlignment = 32;
		}
		
		if (this.moduleVersion > 2) {
			this.fixSize = reader.readNextUnsignedInt();
		}
		
		// Only read the sections if the header is valid.
		if (IsValid(reader)) {
			// Read sections info.
			reader.setPointerIndex(this.sectionTableOffset);
			this.sections = new SectionInfo[(int) this.sectionCount];
			for (var i = 0; i < this.sectionCount; i++) {
				this.sections[i] = new SectionInfo(reader.readNextUnsignedInt(), reader.readNextUnsignedInt());
			}
		}
	}
	catch (IOException e) {
		Msg.error(this,  "Failed to read REL header!");
	}
}
 
Example 9
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;
}
 
Example 10
Source File: DOLHeader.java    From Ghidra-GameCube-Loader with Apache License 2.0 4 votes vote down vote up
private void readHeader(BinaryReader reader) {
	try {
		reader.setPointerIndex(0);
		
		textSectionOffsets = new long[7];
		for (int i = 0; i < 7; i++) {
			textSectionOffsets[i] = reader.readNextUnsignedInt();
		}
		
		dataSectionOffsets = new long[11];
		for (int i = 0; i < 11; i++) {
			dataSectionOffsets[i] = reader.readNextUnsignedInt();
		}
		
		textSectionMemoryAddresses = new long[7];
		for (int i = 0; i < 7; i++) {
			textSectionMemoryAddresses[i] = reader.readNextUnsignedInt();
		}
		
		dataSectionMemoryAddresses = new long[11];
		for (int i = 0; i < 11; i++) {
			dataSectionMemoryAddresses[i] = reader.readNextUnsignedInt();
		}
		
		textSectionSizes = new long[7];
		for (int i = 0; i < 7; i++) {
			textSectionSizes[i] = reader.readNextUnsignedInt();
		}
		
		dataSectionSizes = new long[11];
		for (int i = 0; i < 11; i++) {
			dataSectionSizes[i] = reader.readNextUnsignedInt();
		}
		
		bssMemoryAddress = reader.readNextUnsignedInt();
		bssSize = reader.readNextUnsignedInt();
		entryPoint = reader.readNextUnsignedInt();
	}
	catch (IOException e) {
		Msg.error(this,  "DOL Header failed to read!");
	}
}