generic.continues.GenericFactory Java Examples

The following examples show how to use generic.continues.GenericFactory. 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: FatHeader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void initFatHeader(GenericFactory factory, ByteProvider provider) throws IOException, UbiException, MachException {
	FactoryBundledWithBinaryReader reader = new FactoryBundledWithBinaryReader(factory, provider, false/*always big endian*/);

	magic = reader.readNextInt();

	if (magic != FAT_MAGIC && magic != FAT_CIGAM) {
		throw new UbiException("Invalid UBI file.");
	}

	nfat_arch = reader.readNextInt();
	if (nfat_arch > 0x1000 || nfat_arch < 0) {
		throw new UbiException("Invalid UBI file.");
	}

	for (int i = 0 ; i < nfat_arch ; ++i) {
		architectures.add(FatArch.createFatArch(reader));
	}

	for (FatArch fatarch : architectures) {
		ByteProviderWrapper wrapper = new ByteProviderWrapper(provider, fatarch.getOffset(), fatarch.getSize());
		MachHeader machHeader = MachHeader.createMachHeader(factory, wrapper);
		machHeaders.add(machHeader);
	}
}
 
Example #2
Source File: ElfRelocation.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * GenericFactory construction and initialization method for a ELF representative 
 * relocation entry 
 * @param reader binary reader positioned at start of relocation entry.
 * @param elfHeader ELF header
 * @param relocationIndex index of entry in relocation table
 * @param withAddend true if if RELA entry with addend, else false
 * @param r_offset The offset for the entry
 * @param r_info The info value for the entry
 * @param r_addend The addend for the entry
 * @return ELF relocation object
 */
static ElfRelocation createElfRelocation(GenericFactory factory, ElfHeader elfHeader,
		int relocationIndex, boolean withAddend, long r_offset, long r_info, long r_addend) {

	Class<? extends ElfRelocation> elfRelocationClass = getElfRelocationClass(elfHeader);
	ElfRelocation elfRelocation = (ElfRelocation) factory.create(elfRelocationClass);
	try {
		elfRelocation.initElfRelocation(elfHeader, relocationIndex, withAddend, r_offset,
			r_info, r_addend);
	}
	catch (IOException e) {
		// absence of reader should prevent any IOException from occurring
		throw new AssertException("unexpected IO error", e);
	}
	return elfRelocation;
}
 
Example #3
Source File: DbgLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
		TaskMonitor monitor, MessageLog log) throws IOException {

	GenericFactory factory = MessageLogContinuesFactory.create(log);

	if (!prog.getExecutableFormat().equals(PeLoader.PE_NAME)) {
		throw new IOException("Loading of DBG file may only be 'added' to existing " +
			PeLoader.PE_NAME + " Program");
	}

	SeparateDebugHeader debug = new SeparateDebugHeader(factory, provider);

	String parentPath = prog.getExecutablePath();
	File parentFile = new File(parentPath);

	RandomAccessByteProvider provider2 = null;
	try {
		provider2 = new RandomAccessByteProvider(parentFile);
		PortableExecutable parentPE =
			PortableExecutable.createPortableExecutable(factory, provider2, SectionLayout.FILE);
		Address imageBase = prog.getImageBase();
		Map<SectionHeader, Address> sectionToAddress = new HashMap<>();
		FileHeader fileHeader = parentPE.getNTHeader().getFileHeader();
		SectionHeader[] sectionHeaders = fileHeader.getSectionHeaders();
		for (SectionHeader sectionHeader : sectionHeaders) {
			sectionToAddress.put(sectionHeader,
				imageBase.add(sectionHeader.getVirtualAddress()));
		}
		processDebug(debug.getParser(), fileHeader, sectionToAddress, prog, monitor);
	}
	finally {
		if (provider2 != null) {
			provider2.close();
		}
	}
}
 
Example #4
Source File: ElfLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
		Program program, TaskMonitor monitor, MessageLog log)
		throws IOException, CancelledException {

	try {
		GenericFactory factory = MessageLogContinuesFactory.create(log);
		ElfHeader elf = ElfHeader.createElfHeader(factory, provider);
		ElfProgramBuilder.loadElf(elf, program, options, log, monitor);
	}
	catch (ElfException e) {
		throw new IOException(e.getMessage());
	}
}
 
Example #5
Source File: MachHeader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void initMachHeader(GenericFactory factory, ByteProvider provider,
		long machHeaderStartIndexInProvider, boolean isRemainingMachoRelativeToStartIndex)
		throws IOException, MachException {
	magic = readMagic(provider, machHeaderStartIndexInProvider);

	if (!MachConstants.isMagic(magic)) {
		throw new MachException("Invalid Mach-O binary.");
	}

	if (isRemainingMachoRelativeToStartIndex) {
		_machHeaderStartIndexInProvider = machHeaderStartIndexInProvider;
	}

	_reader = new FactoryBundledWithBinaryReader(factory, provider, isLittleEndian());
	_reader.setPointerIndex(machHeaderStartIndexInProvider + 4);//skip magic number...

	cpuType = _reader.readNextInt();
	cpuSubType = _reader.readNextInt();
	fileType = _reader.readNextInt();
	nCmds = _reader.readNextInt();
	sizeOfCmds = _reader.readNextInt();
	flags = _reader.readNextInt();

	_is32bit = (cpuType & CpuTypes.CPU_ARCH_ABI64) == 0;

	if (!_is32bit) {
		reserved = _reader.readNextInt();
	}
	_commandIndex = _reader.getPointerIndex();
}
 
Example #6
Source File: NewExecutable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new instance of an new executable.
 * @param factory is the object factory to bundle with the reader
 * @param bp the byte provider
 * @param baseAddr the image base of the executable
 * @throws IOException if an I/O error occurs.
 */
public NewExecutable(GenericFactory factory, ByteProvider bp, SegmentedAddress baseAddr)
		throws IOException {
       reader = new FactoryBundledWithBinaryReader(factory, bp, true);
       dosHeader = DOSHeader.createDOSHeader(reader);

       if (dosHeader.isDosSignature()) {
           try {
			winHeader = new WindowsHeader(reader, baseAddr, (short) dosHeader.e_lfanew());
           }
           catch (InvalidWindowsHeaderException e) {
           }
       }
   }
 
Example #7
Source File: ElfHeader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new ELF header using the specified byte provider.
 * @param provider the byte provider to supply the bytes
 * @throws ElfException if the underlying bytes in the byte provider 
 * do not constitute a valid ELF.
 */
public static ElfHeader createElfHeader(GenericFactory factory, ByteProvider provider)
		throws ElfException {
	ElfHeader elfHeader = (ElfHeader) factory.create(ElfHeader.class);
	elfHeader.initElfHeader(factory, provider);
	return elfHeader;
}
 
Example #8
Source File: PortableExecutable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new Portable Executable using the specified byte provider and layout.
 * @param factory generic factory instance
 * @param bp the byte provider
 * @param layout specifies the layout of the underlying provider and governs RVA resolution
 * @param advancedProcess if true, the data directories are also processed
 * @param parseCliHeaders if true, CLI headers are parsed (if present)
 * @throws IOException if an I/O error occurs.
 */
public static PortableExecutable createPortableExecutable(GenericFactory factory,
		ByteProvider bp, SectionLayout layout, boolean advancedProcess, boolean parseCliHeaders)
		throws IOException {
	PortableExecutable portableExecutable =
		(PortableExecutable) factory.create(PortableExecutable.class);
	portableExecutable.initPortableExecutable(factory, bp, layout, advancedProcess,
		parseCliHeaders);
	return portableExecutable;
}
 
Example #9
Source File: RplHeader.java    From GhidraRPXLoader with GNU General Public License v3.0 4 votes vote down vote up
public static RplHeader createRplHeader(GenericFactory factory, ByteProvider provider)
		throws ElfException {
	RplHeader elfHeader = (RplHeader) factory.create(RplHeader.class);
	elfHeader.initElfHeader(factory, provider);
	return elfHeader;
}
 
Example #10
Source File: LegacyFactoryBundledWithBinaryReader.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
public LegacyFactoryBundledWithBinaryReader(GenericFactory factory, ByteProvider provider, boolean isLittleEndian) 
{
    super(factory, provider, isLittleEndian);
}
 
Example #11
Source File: SeparateDebugHeader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a new separate debug header using the specified byte provider.
 * @param bp the byte provider
 * @throws IOException if an I/O error occurs.
 */
public SeparateDebugHeader(GenericFactory factory, ByteProvider bp) throws IOException {
	FactoryBundledWithBinaryReader reader =
		new FactoryBundledWithBinaryReader(factory, bp, true);

	reader.setPointerIndex(0);

	signature = reader.readNextShort();

	if (signature != IMAGE_SEPARATE_DEBUG_SIGNATURE) {
		return;
	}

	flags = reader.readNextShort();
	machine = reader.readNextShort();
	characteristics = reader.readNextShort();
	timeDateStamp = reader.readNextInt();
	checkSum = reader.readNextInt();
	imageBase = reader.readNextInt();
	sizeOfImage = reader.readNextInt();
	numberOfSections = reader.readNextInt();
	exportedNamesSize = reader.readNextInt();
	debugDirectorySize = reader.readNextInt();
	sectionAlignment = reader.readNextInt();
	reserved = reader.readNextIntArray(2);

	if (numberOfSections > NTHeader.MAX_SANE_COUNT) {
		Msg.error(this, "Number of sections " + numberOfSections);
		return;
	}

	long ptr = reader.getPointerIndex();

	sections = new SectionHeader[numberOfSections];
	for (int i = 0; i < numberOfSections; ++i) {
		sections[i] = SectionHeader.createSectionHeader(reader, ptr);
		ptr += SectionHeader.IMAGE_SIZEOF_SECTION_HEADER;
	}

	long tmp = ptr;
	List<String> exportedNameslist = new ArrayList<String>();
	while (true) {
		String str = reader.readAsciiString(tmp);
		if (str == null || str.length() == 0) {
			break;
		}
		tmp += str.length() + 1;
		exportedNameslist.add(str);
	}
	exportedNames = new String[exportedNameslist.size()];
	exportedNameslist.toArray(exportedNames);

	ptr += exportedNamesSize;

	parser =
		DebugDirectoryParser.createDebugDirectoryParser(reader, ptr, debugDirectorySize, this);
}
 
Example #12
Source File: ElfHeader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected void initElfHeader(GenericFactory factory, ByteProvider provider)
		throws ElfException {
	try {

		determineHeaderEndianess(provider);

		reader = new FactoryBundledWithBinaryReader(factory, provider, hasLittleEndianHeaders);

		e_ident_magic_num = reader.readNextByte();
		e_ident_magic_str = reader.readNextAsciiString(ElfConstants.MAGIC_STR_LEN);

		boolean magicMatch = ElfConstants.MAGIC_NUM == e_ident_magic_num &&
			ElfConstants.MAGIC_STR.equalsIgnoreCase(e_ident_magic_str);

		if (!magicMatch) {
			throw new ElfException("Not a valid ELF executable.");
		}

		e_ident_class = reader.readNextByte();
		e_ident_data = reader.readNextByte();
		e_ident_version = reader.readNextByte();
		e_ident_osabi = reader.readNextByte();
		e_ident_abiversion = reader.readNextByte();
		e_ident_pad = reader.readNextByteArray(PAD_LENGTH);
		e_type = reader.readNextShort();
		e_machine = reader.readNextShort();
		e_version = reader.readNextInt();

		if (is32Bit()) {
			e_entry = reader.readNextInt() & 0xffffffffL;
			e_phoff = reader.readNextInt() & 0xffffffffL;
			e_shoff = reader.readNextInt() & 0xffffffffL;
		}
		else if (is64Bit()) {
			e_entry = reader.readNextLong();
			e_phoff = reader.readNextLong();
			e_shoff = reader.readNextLong();
		}
		else {
			throw new ElfException("Only 32-bit and 64-bit ELF headers are supported.");
		}

		e_flags = reader.readNextInt();
		e_ehsize = reader.readNextShort();
		e_phentsize = reader.readNextShort();
		e_phnum = reader.readNextShort();
		if (e_phnum < 0) {
			e_phnum = 0; // protect against stripped program headers
		}
		e_shentsize = reader.readNextShort();
		e_shnum = reader.readNextShort();
		if (e_shnum < 0) {
			e_shnum = 0; // protect against stripped section headers (have seen -1)
		}
		e_shstrndx = reader.readNextShort();
	}
	catch (IOException e) {
		throw new ElfException(e);
	}
}
 
Example #13
Source File: FatHeader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static FatHeader createFatHeader(GenericFactory factory, ByteProvider provider)
        throws IOException, UbiException, MachException {
    FatHeader fatHeader = (FatHeader) factory.create(FatHeader.class);
    fatHeader.initFatHeader(factory, provider);
    return fatHeader;
}
 
Example #14
Source File: MachHeader.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Assumes the MachHeader starts at index <i>machHeaderStartIndexInProvider</i> in the ByteProvider.
 * @param provider the ByteProvider
 * @param machHeaderStartIndexInProvider the index into the ByteProvider where the MachHeader begins.
 * @param isRemainingMachoRelativeToStartIndex TRUE if the rest of the macho uses relative indexing. This is common in UBI and kernel cache files.
 *                                             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 ByteProvider
 * @throws MachException if an invalid MachHeader is detected
 */
public static MachHeader createMachHeader(GenericFactory factory, ByteProvider provider,
		long machHeaderStartIndexInProvider, boolean isRemainingMachoRelativeToStartIndex)
		throws IOException, MachException {
	MachHeader machHeader = (MachHeader) factory.create(MachHeader.class);
	machHeader.initMachHeader(factory, provider, machHeaderStartIndexInProvider,
		isRemainingMachoRelativeToStartIndex);
	return machHeader;
}
 
Example #15
Source File: MachHeader.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Assumes the MachHeader starts at index <i>machHeaderStartIndexInProvider</i> in the ByteProvider.
 * @param provider the ByteProvider
 * @param machHeaderStartIndexInProvider the index into the ByteProvider where the MachHeader begins.
 * @throws IOException if an I/O error occurs while reading from the ByteProvider
 * @throws MachException if an invalid MachHeader is detected
 */
public static MachHeader createMachHeader(GenericFactory factory, ByteProvider provider,
		long machHeaderStartIndexInProvider) throws IOException, MachException {
	MachHeader machHeader = (MachHeader) factory.create(MachHeader.class);
	machHeader.initMachHeader(factory, provider, machHeaderStartIndexInProvider, true);
	return machHeader;
}
 
Example #16
Source File: PortableExecutable.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a new Portable Executable using the specified byte provider and layout.
 *  <p>
 * Same as calling <code>createFileAlignedPortableExecutable(factory, bp, layout, true, false)</code>
 * @param factory generic factory instance
 * @param bp the byte provider
 * @param layout specifies the layout of the underlying provider and governs RVA resolution
 * @throws IOException if an I/O error occurs.
 * @see #createPortableExecutable(GenericFactory, ByteProvider, SectionLayout, boolean, boolean)
 **/
public static PortableExecutable createPortableExecutable(GenericFactory factory,
		ByteProvider bp, SectionLayout layout) throws IOException {
	return createPortableExecutable(factory, bp, layout, true, false);
}
 
Example #17
Source File: MachHeader.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Assumes the MachHeader starts at index 0 in the ByteProvider.
 * @param provider the ByteProvider
 * @throws IOException if an I/O error occurs while reading from the ByteProvider
 * @throws MachException if an invalid MachHeader is detected
 */
public static MachHeader createMachHeader(GenericFactory factory, ByteProvider provider)
		throws IOException, MachException {
	return createMachHeader(factory, provider, 0);
}