ghidra.app.util.opinion.LoadSpec Java Examples

The following examples show how to use ghidra.app.util.opinion.LoadSpec. 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: SwitchLoader.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
@Override
protected List<Program> loadProgram(ByteProvider provider, String programName,
        DomainFolder programFolder, LoadSpec loadSpec, List<Option> options, MessageLog log,
        Object consumer, TaskMonitor monitor)
                throws IOException, CancelledException 
{
    LanguageCompilerSpecPair pair = loadSpec.getLanguageCompilerSpec();
    Language importerLanguage = getLanguageService().getLanguage(pair.languageID);
    CompilerSpec importerCompilerSpec = importerLanguage.getCompilerSpecByID(pair.compilerSpecID);

    Address baseAddr = importerLanguage.getAddressFactory().getDefaultAddressSpace().getAddress(0);
    Program prog = createProgram(provider, programName, baseAddr, getName(), importerLanguage, importerCompilerSpec, consumer);
    boolean success = false;

    try 
    {
        success = this.loadInto(provider, loadSpec, options, log, prog, monitor);
    }
    finally 
    {
        if (!success) 
        {
            prog.release(consumer);
            prog = null;
        }
    }

    List<Program> results = new ArrayList<Program>();
    if (prog != null) results.add(prog);
    return results;
}
 
Example #2
Source File: GameCubeLoader.java    From Ghidra-GameCube-Loader with Apache License 2.0 5 votes vote down vote up
@Override
public String validateOptions(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program program) {

	// TODO: If this loader has custom options, validate them here.  Not all options require
	// validation.

	return super.validateOptions(provider, loadSpec, options, program);
}
 
Example #3
Source File: GameCubeLoader.java    From Ghidra-GameCube-Loader with Apache License 2.0 5 votes vote down vote up
@Override
public List<Option> getDefaultOptions(ByteProvider provider, LoadSpec loadSpec,
		DomainObject domainObject, boolean isLoadIntoProgram) {
	List<Option> list =
		super.getDefaultOptions(provider, loadSpec, domainObject, isLoadIntoProgram);
	
	list.add(new Option(AUTOLOAD_MAPS_OPTION_NAME, true, Boolean.class, Loader.COMMAND_LINE_ARG_PREFIX + "-autoloadMaps"));
	list.add(new Option(ADD_RELOCATIONS_OPTION_NAME, false, Boolean.class, Loader.COMMAND_LINE_ARG_PREFIX + "-saveRelocations"));
	list.add(new Option(ADD_RESERVED_AND_HARDWAREREGISTERS, true, Boolean.class, Loader.COMMAND_LINE_ARG_PREFIX + "-addSystemMemorySections"));
	list.add(new Option(SPECIFY_BINARY_MEM_ADDRESSES, false, Boolean.class, Loader.COMMAND_LINE_ARG_PREFIX + "-specifyFileMemAddrs"));

	return list;
}
 
Example #4
Source File: GameCubeLoader.java    From Ghidra-GameCube-Loader with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean loadProgramInto(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
        MessageLog messageLog, Program program, TaskMonitor monitor) 
        throws IOException {
	
	if (this.binaryType != BinaryType.UNKNOWN) {
 	boolean autoLoadMaps = OptionUtils.getBooleanOptionValue(AUTOLOAD_MAPS_OPTION_NAME, options, true);
 	boolean saveRelocations = OptionUtils.getBooleanOptionValue(ADD_RELOCATIONS_OPTION_NAME, options, false);
 	boolean createDefaultSections = OptionUtils.getBooleanOptionValue(ADD_RESERVED_AND_HARDWAREREGISTERS, options, true);
 	boolean specifyFileMemAddresses = OptionUtils.getBooleanOptionValue(SPECIFY_BINARY_MEM_ADDRESSES, options, false);
 	
     if (this.binaryType == BinaryType.DOL) {
     	new DOLProgramBuilder(dolHeader, provider, program, monitor, autoLoadMaps, createDefaultSections);
     }
     else if (this.binaryType == BinaryType.REL) {
     	try {
     		// We have to check if the source file is compressed & decompress it again if it is.
     		var file = provider.getFile();
     		Yaz0 yaz0 = new Yaz0();
     		if (yaz0.IsValid(provider)) {
     			provider = yaz0.Decompress(provider);
     		}
     		
	new RELProgramBuilder(relHeader, provider, program, monitor, file,
			autoLoadMaps, saveRelocations, createDefaultSections, specifyFileMemAddresses);
} catch (AddressOverflowException | AddressOutOfBoundsException | MemoryAccessException e ) {
	e.printStackTrace();
}
     }
     else {
     	new ApploaderProgramBuilder(apploaderHeader, provider, program, monitor, createDefaultSections);
     }
    	return true;
	}
	return false;
}
 
Example #5
Source File: GameCubeLoader.java    From Ghidra-GameCube-Loader with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Program> loadProgram(ByteProvider provider, String programName,
		DomainFolder programFolder, LoadSpec loadSpec, List<Option> options, MessageLog log,
		Object consumer, TaskMonitor monitor)
		throws IOException, CancelledException {
	LanguageCompilerSpecPair pair = loadSpec.getLanguageCompilerSpec();
	Language importerLanguage = getLanguageService().getLanguage(pair.languageID);
	CompilerSpec importerCompilerSpec = importerLanguage.getCompilerSpecByID(pair.compilerSpecID);
	
	Address baseAddress = importerLanguage.getAddressFactory().getDefaultAddressSpace().getAddress(0);
	Program program = createProgram(provider, programName, baseAddress, getName(),
			importerLanguage, importerCompilerSpec, consumer);
	
	boolean success = false;
	try {
		success = this.loadInto(provider, loadSpec, options, log, program, monitor);
	}
	finally {
		if (!success) {
			program.release(consumer);
			program = null;
		}
	}
	
	List<Program> results = new ArrayList<Program>();
	if (program != null) {
		results.add(program);
	}
	
	return results;
}
 
Example #6
Source File: SkeletonLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public String validateOptions(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program program) {

	// TODO: If this loader has custom options, validate them here.  Not all options require
	// validation.

	return super.validateOptions(provider, loadSpec, options, program);
}
 
Example #7
Source File: SkeletonLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public List<Option> getDefaultOptions(ByteProvider provider, LoadSpec loadSpec,
		DomainObject domainObject, boolean isLoadIntoProgram) {
	List<Option> list =
		super.getDefaultOptions(provider, loadSpec, domainObject, isLoadIntoProgram);

	// TODO: If this loader has custom options, add them to 'list'
	list.add(new Option("Option name goes here", "Default option value goes here"));

	return list;
}
 
Example #8
Source File: SkeletonLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
		Program program, TaskMonitor monitor, MessageLog log)
		throws CancelledException, IOException {

	// TODO: Load the bytes from 'provider' into the 'program'.
}
 
Example #9
Source File: SkeletonLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
	List<LoadSpec> loadSpecs = new ArrayList<>();

	// TODO: Examine the bytes in 'provider' to determine if this loader can load it.  If it 
	// can load it, return the appropriate load specifications.

	return loadSpecs;
}
 
Example #10
Source File: BatchGroup.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public LoadSpec getLoadSpec(BatchGroupLoadSpec batchGroupLoadSpec) {
	for (LoadSpec loadSpec : loadSpecs) {
		if (batchGroupLoadSpec.matches(loadSpec)) {
			return loadSpec;
		}
	}
	return null;
}
 
Example #11
Source File: BatchGroup.java    From ghidra with Apache License 2.0 5 votes vote down vote up
BatchLoadConfig(ByteProvider provider, Collection<LoadSpec> loadSpecs, FSRL fsrl,
		UserAddedSourceInfo uasi) {
	this.loadSpecs = loadSpecs;
	this.fsrl = fsrl;
	this.uasi = uasi;

	this.loader = loadSpecs.iterator().next().getLoader();
	this.preferredFileName = loader.getPreferredFileName(provider);
}
 
Example #12
Source File: BatchSegregatingCriteria.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public BatchSegregatingCriteria(Loader loader, Collection<LoadSpec> loadSpecs,
		ByteProvider provider) {
	for (LoadSpec loadSpec : loadSpecs) {
		groupLoadSpecs.add(new BatchGroupLoadSpec(loadSpec));
	}
	this.loader = loader.getName();
	fileExt = FilenameUtils.getExtension(loader.getPreferredFileName(provider));
}
 
Example #13
Source File: ImporterLanguageDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void selectPreferredLanguage() {
	List<LoadSpec> preferredLoadSpecs = new ArrayList<>();
	for (LoadSpec loadSpec : loadSpecs) {
		if (loadSpec.isPreferred()) {
			preferredLoadSpecs.add(loadSpec);
		}
	}
	if (preferredLoadSpecs.size() == 1) {
		languagePanel.setRecommendedLcsPair(
			preferredLoadSpecs.get(0).getLanguageCompilerSpec());
	}
	else {
		languagePanel.setRecommendedLcsPair(null);
	}
}
 
Example #14
Source File: ImporterLanguageDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ImporterLanguageDialog(Collection<LoadSpec> loadSpecs, PluginTool tool,
		LanguageCompilerSpecPair defaultSelectedLanguage) {
	super("Language", true);
	this.loadSpecs = loadSpecs;
	this.tool = tool;
	this.defaultSelectedLanguage = defaultSelectedLanguage;
	setHelpLocation(new HelpLocation("ImporterPlugin", "language_picker_dialog"));
}
 
Example #15
Source File: BatchGroupLoadSpec.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public BatchGroupLoadSpec(LoadSpec loadSpec) {
	lcsPair = loadSpec.getLanguageCompilerSpec();
	preferred = loadSpec.isPreferred();
}
 
Example #16
Source File: BatchGroupLoadSpec.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public boolean matches(LoadSpec loadSpec) {
	return (loadSpec.getLanguageCompilerSpec() == lcsPair) ||
		(loadSpec.getLanguageCompilerSpec() != null &&
			loadSpec.getLanguageCompilerSpec().equals(lcsPair));
}
 
Example #17
Source File: BatchGroup.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public Collection<LoadSpec> getLoadSpecs() {
	return loadSpecs;
}
 
Example #18
Source File: ImportBatchTask.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private List<Option> getOptionsFor(BatchLoadConfig batchLoadConfig, LoadSpec loadSpec,
		ByteProvider byteProvider) {
	List<Option> options =
		batchLoadConfig.getLoader().getDefaultOptions(byteProvider, loadSpec, null, false);
	return options;
}
 
Example #19
Source File: SwitchLoader.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
@Override
protected boolean loadProgramInto(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
        MessageLog messageLog, Program program, TaskMonitor monitor) 
                throws IOException
{
    var space = program.getAddressFactory().getDefaultAddressSpace();
    
    if (this.binaryType == BinaryType.SX_KIP1)
    {
        provider = new ByteProviderWrapper(provider, 0x10, provider.length() - 0x10);
    }

    var adapter = this.binaryType.createAdapter(program, provider);
    
    // Set the base address
    try 
    {
        long baseAddress = adapter.isAarch32() ? 0x60000000L : 0x7100000000L;
        
        if (this.binaryType == BinaryType.KERNEL_800)
        {
            baseAddress = 0x80060000L;
        }

        program.setImageBase(space.getAddress(baseAddress), true);
    } 
    catch (AddressOverflowException | LockException | IllegalStateException | AddressOutOfBoundsException e) 
    {
        Msg.error(this, "Failed to set image base", e);
    }

    var loader = new NXProgramBuilder(program, provider, adapter);
    loader.load(monitor);
    
    if (this.binaryType == BinaryType.KIP1)
    {
        // KIP1s always start with a branch instruction at the start of their text
        loader.createEntryFunction("entry", program.getImageBase().getOffset(), monitor);
    }
    
    return true;
}
 
Example #20
Source File: SwitchLoader.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
@Override
public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException 
{
    List<LoadSpec> loadSpecs = new ArrayList<>();
    BinaryReader reader = new BinaryReader(provider, true);
    String magic_0x0 = reader.readAsciiString(0, 4);
    String magic_0x10 = reader.readAsciiString(0x10, 4);
    
    reader.setPointerIndex(0);

    if (magic_0x0.equals("KIP1")) 
    {
        this.binaryType = BinaryType.KIP1;
    }
    else if (magic_0x0.equals("NSO0"))
    {
        this.binaryType = BinaryType.NSO0;
    }
    else if (magic_0x0.equals("\u00DF\u004F\u0003\u00D5"))
    {
        this.binaryType = BinaryType.KERNEL_800;
    }
    else if (magic_0x10.equals("NRO0"))
    {
        this.binaryType = BinaryType.NRO0;
    }
    else if (magic_0x10.equals("KIP1"))
    {
        // Note: This is kinda a bad way of determining this, but for now it gets the job done
        // and I don't believe there are any clashes.
        this.binaryType = BinaryType.SX_KIP1; 
    }
    else
        return loadSpecs;

    var adapter = this.binaryType.createAdapter(null, provider);
    
    if (adapter.isAarch32())
    {
        loadSpecs.add(new LoadSpec(this, 0, new LanguageCompilerSpecPair(AARCH32_LANGUAGE_ID, new CompilerSpecID("default")), true));
    }
    else
    {
        loadSpecs.add(new LoadSpec(this, 0, new LanguageCompilerSpecPair(AARCH64_LANGUAGE_ID, new CompilerSpecID("default")), true));
    }

    return loadSpecs;
}
 
Example #21
Source File: BatchGroup.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Adds {@link LoadSpec}s to this group.
 *
 * @param provider The {@link ByteProvider}.
 * @param loadSpecs {@link LoadSpec}s to add to this group.
 * @param fsrl {@link FSRL} of the application's import source file.
 * @param uasi {@link UserAddedSourceInfo}
 */
public void add(ByteProvider provider, Collection<LoadSpec> loadSpecs, FSRL fsrl,
		UserAddedSourceInfo uasi) {
	batchLoadConfigs.add(new BatchLoadConfig(provider, loadSpecs, fsrl, uasi));
}