Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5910: IntelHexExporter fixes

Ryan Kurtz8c4bb84489a7
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java
index 5b6e09a72a..1a1e29b67f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -184,7 +184,7 @@ public class ExporterDialog extends DialogComponentProvider implements AddressFa
return "Unexpected exception validating options: " + e.getMessage();
}
};
- AddressFactoryService svc = (domainObject instanceof Program) ? null : this;
+ AddressFactoryService svc = (domainObject instanceof Program) ? this : null;
OptionsDialog optionsDialog = new OptionsDialog(options, validator, svc);
optionsDialog
.setHelpLocation(new HelpLocation("ExporterPlugin", getAnchorForSelectedFormat()));
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/exporter/IntelHexExporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/exporter/IntelHexExporter.java
index 9db2f3fc2d..1076f4f7df 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/exporter/IntelHexExporter.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/exporter/IntelHexExporter.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -150,15 +150,10 @@ public class IntelHexExporter extends Exporter {
log.clear();
- if (!(domainObj instanceof Program)) {
+ if (!(domainObj instanceof Program program)) {
log.appendMsg("Unsupported type: " + domainObj.getClass().getName());
return false;
}
- Program program = (Program) domainObj;
- if (program.getMaxAddress().getSize() > 32) {
- log.appendMsg("Cannot be used for programs larger than 32 bits");
- return false;
- }
if (addressSpaceOption == null || recordSizeOption == null) {
getOptions(() -> program);
@@ -168,12 +163,23 @@ public class IntelHexExporter extends Exporter {
Memory memory = program.getMemory();
- if (addrSet == null) {
- addrSet = memory;
+ AddressSet set = new AddressSet(addrSet != null ? addrSet : memory);
+
+ for (MemoryBlock block : memory.getBlocks()) {
+ if (!block.isInitialized() ||
+ block.getStart().getAddressSpace() != addressSpaceOption.getValue()) {
+ set.delete(new AddressRangeImpl(block.getStart(), block.getEnd()));
+ }
+ }
+
+ Address maxAddr = set.getMaxAddress();
+ if (maxAddr != null && maxAddr.getSize() > 32) {
+ log.appendMsg("Cannot be used for address spaces larger than 32 bits");
+ return false;
}
try {
- List<IntelHexRecord> records = dumpMemory(program, memory, addrSet, monitor);
+ List<IntelHexRecord> records = dumpMemory(program, memory, set, monitor);
for (IntelHexRecord record : records) {
writer.println(record.format());
}
@@ -198,17 +204,7 @@ public class IntelHexExporter extends Exporter {
IntelHexRecordWriter writer = new IntelHexRecordWriter(size, dropBytes);
- AddressSet set = new AddressSet(addrSetView);
-
- MemoryBlock[] blocks = memory.getBlocks();
- for (MemoryBlock block : blocks) {
- if (!block.isInitialized() ||
- block.getStart().getAddressSpace() != addressSpaceOption.getValue()) {
- set.delete(new AddressRangeImpl(block.getStart(), block.getEnd()));
- }
- }
-
- AddressIterator addresses = set.getAddresses(true);
+ AddressIterator addresses = addrSetView.getAddresses(true);
while (addresses.hasNext()) {
Address address = addresses.next();
byte b = memory.getByte(address);
@@ -220,7 +216,7 @@ public class IntelHexExporter extends Exporter {
program.getSymbolTable().getExternalEntryPointIterator();
while (entryPoint == null && entryPointIterator.hasNext()) {
Address address = entryPointIterator.next();
- if (set.contains(address)) {
+ if (addrSetView.contains(address)) {
entryPoint = address;
}
}