diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java index 670bfb8816..888447425b 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/MemoryBlockUtils.java @@ -28,6 +28,7 @@ import ghidra.program.model.listing.*; import ghidra.program.model.mem.*; import ghidra.util.Msg; import ghidra.util.exception.DuplicateNameException; +import ghidra.util.task.TaskMonitor; /** * Convenience methods for creating memory blocks. @@ -57,7 +58,44 @@ public class MemoryBlockUtils { try { MemoryBlock block = memory.createUninitializedBlock(name, start, length, isOverlay); setBlockAttributes(block, comment, source, r, w, x); - adjustFragment(program, start, name); + adjustFragment(program, block.getStart(), name); + return block; + } + catch (LockException e) { + log.appendMsg("Failed to create memory block: exclusive lock/checkout required"); + } + catch (Exception e) { + log.appendMsg("Failed to create '" + name + "' memory block: " + e.getMessage()); + } + return null; + } + + /** + * Create a new initialized memory block. Initialized to all zeros. + * @param program the program in which to create the block. + * @param isOverlay if true, the block will be created in a new overlay space for that block + * @param name the name of the new block. + * @param start the starting address of the new block. + * @param is source of the data used to fill the block or null for zero initialization. + * @param length the length of the new block + * @param comment the comment text to associate with the new block. + * @param source the source of the block (This field is not well defined - currently another comment) + * @param r the read permission for the new block. + * @param w the write permission for the new block. + * @param x the execute permission for the new block. + * @param log a {@link MessageLog} for appending error messages + * @return the newly created block or null if the operation failed. + */ + public static MemoryBlock createInitializedBlock(Program program, boolean isOverlay, + String name, Address start, long length, String comment, String source, boolean r, + boolean w, boolean x, MessageLog log) { + + Memory memory = program.getMemory(); + try { + MemoryBlock block = memory.createInitializedBlock(name, start, null, length, + TaskMonitor.DUMMY, isOverlay); + setBlockAttributes(block, comment, source, r, w, x); + adjustFragment(program, block.getStart(), name); return block; } catch (LockException e) { @@ -86,9 +124,9 @@ public class MemoryBlockUtils { * @param log a {@link StringBuffer} for appending error messages * @return the new created block */ - public static MemoryBlock createBitMappedBlock(Program program, String name, - Address start, Address base, int length, String comment, String source, boolean r, - boolean w, boolean x, MessageLog log) { + public static MemoryBlock createBitMappedBlock(Program program, String name, Address start, + Address base, int length, String comment, String source, boolean r, boolean w, + boolean x, MessageLog log) { Memory memory = program.getMemory(); try { @@ -197,7 +235,7 @@ public class MemoryBlockUtils { } setBlockAttributes(block, comment, source, r, w, x); - adjustFragment(program, start, name); + adjustFragment(program, block.getStart(), name); return block; } @@ -252,8 +290,7 @@ public class MemoryBlockUtils { } private static void setBlockAttributes(MemoryBlock block, String comment, String source, - boolean r, - boolean w, boolean x) { + boolean r, boolean w, boolean x) { block.setComment(comment); block.setSourceName(source); block.setRead(r); diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.java index da7e896637..3a5751e856 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.java @@ -15,7 +15,6 @@ */ package ghidra.app.util.bin.format.elf; -import ghidra.app.util.MemoryBlockUtil; import ghidra.app.util.bin.format.MemoryLoadable; import ghidra.app.util.importer.MessageLog; import ghidra.program.model.address.Address; @@ -44,12 +43,6 @@ public interface ElfLoadHelper { */ ElfHeader getElfHeader(); - /** - * Memory block utility (program memory may be accessed directly if preferred) - * @return memory block utility associated with program load - */ - MemoryBlockUtil getMemoryBlockUtil(); - /** * Get the message log * @return message log diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java index 4b0f60c406..e14c7eb8bf 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfLoader.java @@ -28,12 +28,10 @@ import ghidra.app.util.bin.format.elf.ElfHeader; import ghidra.app.util.importer.*; import ghidra.framework.model.DomainFolder; import ghidra.framework.model.DomainObject; -import ghidra.program.model.address.AddressSpace; import ghidra.program.model.lang.Endian; import ghidra.program.model.listing.Program; import ghidra.program.util.ELFExternalSymbolResolver; import ghidra.util.Msg; -import ghidra.util.StringUtilities; import ghidra.util.exception.CancelledException; import ghidra.util.task.TaskMonitor; @@ -76,22 +74,6 @@ public class ElfLoader extends AbstractLibrarySupportLoader { return options; } - private String getBaseOffsetString(long imageBase, AddressSpace defaultSpace) { - int maxNibbles = defaultSpace.getSize() / 4; - int minNibbles = Math.min(8, maxNibbles); - String baseOffsetStr = Long.toHexString(imageBase); - int baseOffsetStrLen = baseOffsetStr.length(); - if (imageBase < 0) { - if (baseOffsetStrLen > maxNibbles) { - baseOffsetStr = baseOffsetStr.substring(baseOffsetStrLen - maxNibbles); - } - } - else if (baseOffsetStrLen < minNibbles) { - baseOffsetStr = StringUtilities.pad(baseOffsetStr, '0', minNibbles - baseOffsetStrLen); - } - return baseOffsetStr; - } - @Override public String validateOptions(ByteProvider provider, LoadSpec loadSpec, List