ghidra.program.model.lang.LanguageCompilerSpecPair Java Examples

The following examples show how to use ghidra.program.model.lang.LanguageCompilerSpecPair. 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: ImporterPluginScreenShots.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testLanguagePickerDialog() throws Exception {
	PeLoader peLoader = new PeLoader();
	List<LoadSpec> loadSpecs = new ArrayList<>();
	loadSpecs.add(new LoadSpec(peLoader, 0,
		new LanguageCompilerSpecPair("x86:LE:32:default", "windows"), true));
	loadSpecs.add(new LoadSpec(peLoader, 0,
		new LanguageCompilerSpecPair("x86:LE:32:default", "gcc"), false));
	loadSpecs.add(new LoadSpec(peLoader, 0,
		new LanguageCompilerSpecPair("x86:LE:32:default", "borland"), false));
	loadSpecs.add(new LoadSpec(peLoader, 0,
		new LanguageCompilerSpecPair("x86:LE:32:System Management Mode", "default"), false));
	runSwing(() -> {
		ImporterLanguageDialog dialog = new ImporterLanguageDialog(loadSpecs, tool, null);
		dialog.show(null);
	}, false);
	waitForDialogComponent(ImporterLanguageDialog.class);
	captureDialog();
}
 
Example #2
Source File: JavaLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
	List<LoadSpec> loadSpecs = new ArrayList<>();
	boolean validClass = false;

	if (checkClass(provider)) {
		validClass = true;
	}

	if (validClass) {
		loadSpecs.add(new LoadSpec(this, 0,
			new LanguageCompilerSpecPair("JVM:BE:32:default", "default"), true));
	}

	return loadSpecs;
}
 
Example #3
Source File: PopulateFidDialog.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Component buildLanguageField() {
	JPanel panel = new JPanel(new BorderLayout());
	languageIdField = new JTextField();
	panel.add(languageIdField, BorderLayout.CENTER);
	JButton browseButton = createBrowseButton();
	browseButton.addActionListener(e -> {
		SelectLanguageDialog selectLanguageDialog =
			new SelectLanguageDialog("Select Language", "Ok");
		LanguageCompilerSpecPair selectedLanguage = selectLanguageDialog.getSelectedLanguage();
		if (selectedLanguage != null) {
			languageIdField.setText(selectedLanguage.languageID.toString());
		}
	});
	languageIdField.getDocument().addUndoableEditListener(e -> updateOkEnablement());
	panel.add(browseButton, BorderLayout.EAST);
	return panel;

}
 
Example #4
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 #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: LoadSpec.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a {@link LoadSpec} from a manually supplied {@link LanguageCompilerSpecPair}.
 * 
 * @param loader This {@link LoadSpec}'s {@link Loader}.
 * @param imageBase The desired image base address for the load.
 * @param languageCompilerSpec The language/compiler spec ID.  If this is not needed or not 
 *   known, use {@link #LoadSpec(Loader, long, boolean)}.
 * @param isPreferred true if this {@link LoadSpec} is preferred; otherwise, false.
 */
public LoadSpec(Loader loader, long imageBase, LanguageCompilerSpecPair languageCompilerSpec,
		boolean isPreferred) {
	this.loader = loader;
	this.imageBase = imageBase;
	this.lcs = languageCompilerSpec;
	this.isPreferred = isPreferred;

	// We internally define a "preferred" language/compiler being null to mean that the 
	// associated Loader doesn't use a language/compiler, and we define a "non-preferred" 
	// language/compiler being null to mean that the Loader does indeed use a language/compiler,
	// but the Loader wasn't able to figure it out on its own.
	this.requiresLanguageCompilerSpec = lcs != null || !isPreferred;
}
 
Example #7
Source File: ImporterLanguageDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void initialize() {
	List<LanguageCompilerSpecPair> pairs = ImporterUtilities.getPairs(loadSpecs);

	languagePanel.setRecommendedLcsPairsList(pairs);
	languagePanel.setShowAllLcsPairs(pairs.isEmpty());
	languagePanel.setEnabled(true);

	selectPreferredLanguage();
}
 
Example #8
Source File: ImporterLanguageDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void build() {
	languagePanel = new NewLanguagePanel();
	languagePanel.setRecommendedLcsPairsList(new ArrayList<LanguageCompilerSpecPair>());
	languagePanel.setShowAllLcsPairs(false);
	languagePanel.setBorder(
		BorderFactory.createTitledBorder(" Select Language and Compiler Specification "));
	languagePanel.addSelectionListener(new LcsSelectionListener() {
		@Override
		public void valueChanged(LcsSelectionEvent e) {
			validateFormInput();
		}
	});

	initialize();

	addWorkPanel(languagePanel);

	addOKButton();
	addCancelButton();

	setDefaultButton(okButton);// set default button

	setOkEnabled(false);

	if (defaultSelectedLanguage != null) {
		languagePanel.setSelectedLcsPair(defaultSelectedLanguage);
	}

	validateFormInput();
}
 
Example #9
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 #10
Source File: ImporterDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private LanguageCompilerSpecPair getPreferredLanguage(Loader loader) {
	LanguageCompilerSpecPair preferredSpecPair = null;
	for (LoadSpec loadSpec : loaderMap.get(loader)) {
		if (loadSpec.isPreferred()) {
			if (preferredSpecPair != null) {
				return null;
			}
			preferredSpecPair = loadSpec.getLanguageCompilerSpec();
		}
	}
	return preferredSpecPair;
}
 
Example #11
Source File: ImporterDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected void setSelectedLanguage(LanguageCompilerSpecPair lcsPair) {
	this.selectedLanguage = lcsPair;
	if (selectedLanguage == null) {
		languageTextField.setText("");
	}
	else {
		languageTextField.setText(selectedLanguage.toString());
	}
}
 
Example #12
Source File: ImporterDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private JComponent buildLanguagePanel() {
	languageTextField = new JTextField();
	languageTextField.setEditable(false);
	languageTextField.setFocusable(false);

	languageButton = ButtonPanelFactory.createButton(ButtonPanelFactory.BROWSE_TYPE);
	languageButton.addActionListener(e -> {
		Object selectedItem = loaderComboBox.getSelectedItem();
		if (selectedItem instanceof Loader) {
			Loader loader = (Loader) selectedItem;
			ImporterLanguageDialog dialog =
				new ImporterLanguageDialog(loaderMap.get(loader), tool, selectedLanguage);
			dialog.show(getComponent());
			LanguageCompilerSpecPair dialogResult = dialog.getSelectedLanguage();
			if (dialogResult != null) {
				setSelectedLanguage(dialogResult);
			}
		}
		validateFormInput();
	});

	Font font = languageButton.getFont();
	languageButton.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));

	JPanel panel = new JPanel(new BorderLayout());
	panel.add(languageTextField, BorderLayout.CENTER);
	panel.add(languageButton, BorderLayout.EAST);
	return panel;
}
 
Example #13
Source File: ImporterUtilities.java    From ghidra with Apache License 2.0 5 votes vote down vote up
static List<LanguageCompilerSpecPair> getPairs(Collection<LoadSpec> loadSpecs) {
	Set<LanguageCompilerSpecPair> pairs = new HashSet<>();
	for (LoadSpec loadSpec : loadSpecs) {
		LanguageCompilerSpecPair pair = loadSpec.getLanguageCompilerSpec();
		if (pair != null) {
			pairs.add(pair);
		}
	}
	return CollectionUtils.asList(pairs);
}
 
Example #14
Source File: LoadDataScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run( ) throws Exception {

	LanguageService languageService = DefaultLanguageService.getLanguageService( );

	LanguageCompilerSpecPair dataBE16 = DataLanguageHelper.getLanguage( languageService, 16, true );
	LanguageCompilerSpecPair dataBE32 = DataLanguageHelper.getLanguage( languageService, 32, true );
	LanguageCompilerSpecPair dataBE64 = DataLanguageHelper.getLanguage( languageService, 64, true );

	LanguageCompilerSpecPair dataLE16 = DataLanguageHelper.getLanguage( languageService, 16, false );
	LanguageCompilerSpecPair dataLE32 = DataLanguageHelper.getLanguage( languageService, 32, false );
	LanguageCompilerSpecPair dataLE64 = DataLanguageHelper.getLanguage( languageService, 64, false );

	File file = askFile( "Select DATA File", "OK" );
	if ( file == null ) {
		printerr( "No file selected, ending script." );
		return;
	}

	openProgram( importFileAsBinary( file, dataBE16.getLanguage( ), dataBE16.getCompilerSpec( ) ) );
	openProgram( importFileAsBinary( file, dataLE16.getLanguage( ), dataLE16.getCompilerSpec( ) ) );

	openProgram( importFileAsBinary( file, dataBE32.getLanguage( ), dataBE32.getCompilerSpec( ) ) );
	openProgram( importFileAsBinary( file, dataLE32.getLanguage( ), dataLE32.getCompilerSpec( ) ) );

	openProgram( importFileAsBinary( file, dataBE64.getLanguage( ), dataBE64.getCompilerSpec( ) ) );
	openProgram( importFileAsBinary( file, dataLE64.getLanguage( ), dataLE64.getCompilerSpec( ) ) );
}
 
Example #15
Source File: LcsSelectionEvent.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public LcsSelectionEvent(LanguageCompilerSpecPair selection) {
    this.selection = selection;
}
 
Example #16
Source File: PrelinkFileSystem.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Program getProgram(GFile file, LanguageService languageService, TaskMonitor monitor,
		Object consumer) throws Exception {
	Long offset = fileToMachoOffsetMap.get(file);
	if (offset == null) {
		return null;
	}
	MachHeader machHeader =
		MachHeader.createMachHeader(RethrowContinuesFactory.INSTANCE, provider, offset, true);
	LanguageCompilerSpecPair lcs = MacosxLanguageHelper.getLanguageCompilerSpecPair(
		languageService, machHeader.getCpuType(), machHeader.getCpuSubType());
	Program program =
		new ProgramDB(file.getName(), lcs.getLanguage(), lcs.getCompilerSpec(), consumer);
	int id = program.startTransaction(getName());
	boolean success = false;
	try {
		FileBytes fileBytes = MemoryBlockUtils.createFileBytes(program, provider, offset,
			provider.length() - offset, monitor);
		ByteProvider providerWrapper =
			new ByteProviderWrapper(provider, offset, provider.length() - offset);
		MachoProgramBuilder.buildProgram(program, providerWrapper, fileBytes, new MessageLog(),
			monitor);
		program.setExecutableFormat(MachoLoader.MACH_O_NAME);
		program.setExecutablePath(file.getPath());

		if (file.equals(systemKextFile)) {
			processSystemKext(languageService, program, monitor);
		}

		success = true;
	}
	catch (Exception e) {
		throw e;
	}
	finally {
		program.endTransaction(id, success);
		if (!success) {
			program.release(consumer);
		}
	}
	return program;
}
 
Example #17
Source File: ImporterLanguageDialog.java    From ghidra with Apache License 2.0 4 votes vote down vote up
LanguageCompilerSpecPair getSelectedLanguage() {
	if (wasDialogCancelled) {
		return null;
	}
	return languagePanel.getSelectedLcsPair();
}
 
Example #18
Source File: SelectLanguageDialog.java    From ghidra with Apache License 2.0 4 votes vote down vote up
void setSelectedLanguage(LanguageCompilerSpecPair language) {
	SystemUtilities.runSwingNow(() -> languagePanel.setSelectedLcsPair(language));
}
 
Example #19
Source File: SelectLanguageDialog.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public LanguageCompilerSpecPair getSelectedLanguage() {

		SystemUtilities.runSwingNow(() -> doSelect());
		return selectedLcsPair;
	}
 
Example #20
Source File: QueryResult.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public QueryResult(LanguageCompilerSpecPair pair, boolean preferred) {
	this.pair = pair;
	this.preferred = preferred;
}
 
Example #21
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 #22
Source File: LoadSpec.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Gets this {@link LoadSpec}'s {@link LanguageCompilerSpecPair}.
 *   
 * @return This {@link LoadSpec}'s {@link LanguageCompilerSpecPair}.  Could be null if this
 *   {@link LoadSpec} doesn't need or know the language/compiler.
 */
public LanguageCompilerSpecPair getLanguageCompilerSpec() {
	return lcs;
}