Ghidra is a software reverse engineering (SRE) framework
Merge remote-tracking branch 'origin/Ghidra_10.1'
Ryan Kurtzad1af902bd7f
diff --cc Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.javaindex f07590bbd3,4a5ed5a2e2..a0bf361b91--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java@@@ -19,11 -19,9 +19,11 @@@ import java.util.*import com.google.common.collect.Range;+import ghidra.app.services.ModuleMapProposal.ModuleMapEntry;+import ghidra.app.services.RegionMapProposal.RegionMapEntry;+import ghidra.app.services.SectionMapProposal.SectionMapEntry;import ghidra.framework.model.DomainFile;- import ghidra.program.model.address.AddressSetView;+ import ghidra.program.model.address.*;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.MemoryBlock;import ghidra.program.util.ProgramLocation;@@@ -50,16 -48,468 +50,30 @@@ import ghidra.util.task.TaskMonitor*/public interface DebuggerStaticMappingService {- /**- * A proposed mapping of module to program- */- public interface ModuleMapProposal {-- /**- * Flatten proposals into a single collection of entries- *- * <p>- * The output is suitable for use in- * {@link DebuggerStaticMappingService#addModuleMappings(Collection, TaskMonitor, boolean)}.- * In some contexts, the user should be permitted to see and optionally adjust the- * collection first.- *- * <p>- * Note, a suitable parameter to this method is derived by invoking {@link Map#values()} on- * the result of- * {@link DebuggerStaticMappingService#proposeModuleMaps(Collection, Collection)}.- *- * <p>- * Note, it is advisable to filter the returned collection using- * {@link DebuggerStaticMappingService#removeOverlappingModuleEntries(Collection)} to avoid- * errors from adding overlapped mappings. Alternatively, you can set- * {@code truncateExisting} to true when calling- * {@link DebuggerStaticMappingService#addModuleMappings(Collection, TaskMonitor, boolean)}.- *- * @param proposals the collection of proposed maps- * @return the flattened, filtered collection- */- static Collection<ModuleMapEntry> flatten(Collection<ModuleMapProposal> proposals) {- Collection<ModuleMapEntry> result = new LinkedHashSet<>();- for (ModuleMapProposal map : proposals) {- result.addAll(map.computeMap().values());- }- return result;- }-- /**- * Remove entries from a collection which overlap existing entries in the trace- *- * @param entries the entries to filter- * @return the filtered entries- */- public static Set<ModuleMapEntry> removeOverlapping(Collection<ModuleMapEntry> entries) {- return entries.stream().filter(e -> {- TraceStaticMappingManager manager = e.module.getTrace().getStaticMappingManager();- return manager.findAllOverlapping(e.moduleRange, e.module.getLifespan()).isEmpty();- }).collect(Collectors.toSet());- }-- /**- * Get the trace module of this proposal- *- * @return the module- */- TraceModule getModule();-- /**- * Get the corresponding program image of this proposal- *- * @return the program- */- Program getProgram();-- /**- * Compute a notional "score" of the proposal- *- * <p>- * This may examine the module and program names, but must consider the likelihood of the- * match based on this proposal. The implementation need not assign meaning to any- * particular score, but a higher score must imply a more likely match.- *- * @implNote some information to consider: length and case of matched image and module- * names, alignment of program memory blocks to trace memory regions, etc.- *- * @return a score of the proposed pair- */- double computeScore();-- /**- * Compute the overall module map given by this proposal- *- * @return the map- */- Map<TraceModule, ModuleMapEntry> computeMap();- }-- /**- * A proposed map of sections to program memory blocks- */- public interface SectionMapProposal {-- /**- * Flatten proposals into a single collection of entries- *- * <p>- * The output is suitable for use in- * {@link DebuggerStaticMappingService#addSectionMappings(Collection, TaskMonitor, boolean)}.- * In some contexts, the user should be permitted to see and optionally adjust the- * collection first.- *- * <p>- * Note, a suitable parameter to this method is derived by invoking {@link Map#values()} on- * the result of- * {@link DebuggerStaticMappingService#proposeSectionMaps(Collection, Collection)}.- *- * <p>- * Note, it is advisable to filter the returned collection using- * {@link DebuggerStaticMappingService#removeOverlappingSectionEntries(Collection)} to avoid- * errors from adding overlapped mappings. Alternatively, you can set- * {@code truncateExisting} to true when calling- * {@link DebuggerStaticMappingService#addSectionMappings(Collection, TaskMonitor, boolean)}.- *- * @param proposals the collection of proposed maps- * @return the flattened, filtered collection- */- static Collection<SectionMapEntry> flatten(Collection<SectionMapProposal> proposals) {- Collection<SectionMapEntry> result = new LinkedHashSet<>();- for (SectionMapProposal map : proposals) {- result.addAll(map.computeMap().values());- }- return result;- }-- /**- * Remove entries from a collection which overlap existing entries in the trace- *- * @param entries the entries to filter- * @return the filtered entries- */- public static Set<SectionMapEntry> removeOverlapping(Collection<SectionMapEntry> entries) {- return entries.stream().filter(e -> {- TraceStaticMappingManager manager = e.section.getTrace().getStaticMappingManager();- Range<Long> moduleLifespan = e.section.getModule().getLifespan();- return manager.findAllOverlapping(e.section.getRange(), moduleLifespan).isEmpty();- }).collect(Collectors.toSet());- }-- /**- * Get the trace module of this proposal- *- * @return the module- */- TraceModule getModule();-- /**- * Get the corresponding program image of this proposal- *- * @return the program- */- Program getProgram();-- /**- * Compute a notional "score" of the proposal- *- * <p>- * This may examine the module and program names, but must consider the likelihood of the- * match based on this proposal. The implementation need not assign meaning to any- * particular score, but a higher score must imply a more likely match.- *- * @implNote some attributes of sections and blocks to consider: matched names vs. total- * names, sizes, addresses (last n hexidecimal digits, to account for relocation),- * consistency of relocation offset, etc.- *- * @return a score of the proposed pair- */- double computeScore();-- /**- * Get the program block proposed for a given trace section- *- * @param section the trace section- * @return the proposed program block- */- MemoryBlock getDestination(TraceSection section);-- /**- * Compute the overall section map given by this proposal- *- * @return the map- */- Map<TraceSection, SectionMapEntry> computeMap();- }-- /**- * A module-program entry in a proposed module map- */- public static class ModuleMapEntry {- /**- * Check if a block should be included in size computations or analyzed for proposals- *- * @param program the program containing the block- * @param block the block- * @return true if included, false otherwise- */- public static boolean includeBlock(Program program, MemoryBlock block) {- if (program.getImageBase().getAddressSpace() != block.getStart().getAddressSpace()) {- return false;- }- if (!block.isLoaded()) {- return false;- }- if (block.isMapped()) {- // TODO: Determine how to handle these.- return false;- }- if (MemoryBlock.EXTERNAL_BLOCK_NAME.equals(block.getName())) {- return false;- }- return true;- }-- /**- * Compute the "size" of an image- *- * <p>- * This is considered the maximum loaded address as mapped in memory, minus the image base.- *- * @param program the program image whose size to compute- * @return the size- */- public static long computeImageSize(Program program) {- Address imageBase = program.getImageBase();- long imageSize = 0;- // TODO: How to handle Harvard architectures?- for (MemoryBlock block : program.getMemory().getBlocks()) {- if (!includeBlock(program, block)) {- continue;- }- imageSize = Math.max(imageSize, block.getEnd().subtract(imageBase) + 1);- }- return imageSize;- }-- private final TraceModule module;- private Program program;- private AddressRange moduleRange;-- /**- * Construct a module map entry- *- * <p>- * Generally, only the service implementation should construct an entry. See- * {@link DebuggerStaticMappingService#proposeModuleMap(TraceModule, Program)} and related- * to obtain these.- *- * @param module the module- * @param program the matched program- * @param moduleRange a range from the module base the size of the program's image- */- public ModuleMapEntry(TraceModule module, Program program, AddressRange moduleRange) {- this.module = module;- this.program = program;- this.moduleRange = moduleRange;- }-- @Override- public boolean equals(Object obj) {- if (!(obj instanceof ModuleMapEntry)) {- return false;- }- ModuleMapEntry that = (ModuleMapEntry) obj;- if (this.module != that.module) {- return false;- }- /*if (this.program != that.program) {- return false;- }*/- // imageSize is derived- return true;- }-- @Override- public int hashCode() {- return Objects.hash(module/*, program*/);- }-- /**- * Get the module for this entry- *- * @return the module- */- public TraceModule getModule() {- return module;- }-- /**- * Get the address range of the module in the trace, as computed from the matched program's- * image size- *- * @return the module range- */- public AddressRange getModuleRange() {- return moduleRange;- }-- /**- * Get the matched program- *- * @return the program- */- public Program getProgram() {- return program;- }-- /**- * Set the matched program- *- * <p>- * This is generally used in UIs to let the user tweak and reassign, if desired. This will- * also re-compute the module range based on the new program's image size.- *- * @param program the program- */- public void setProgram(Program program) {- this.program = program;- try {- this.moduleRange =- new AddressRangeImpl(module.getBase(), computeImageSize(program));- }- catch (AddressOverflowException e) {- // This is terribly unlikely- throw new IllegalArgumentException(- "Specified program is too large for module's memory space");- }- }- }-- /**- * A section-block entry in a proposed section map- */- public static class SectionMapEntry {- private final TraceSection section;- private Program program;- private MemoryBlock block;-- /**- * Construct a section map entry- *- * <p>- * Generally, only the service implementation should construct an entry. See- * {@link DebuggerStaticMappingService#proposeSectionMap(TraceSection, Program, MemoryBlock)}- * and related to obtain these.- *- * @param section the section- * @param program the program containing the matched block- * @param block the matched memory block- */- public SectionMapEntry(TraceSection section, Program program, MemoryBlock block) {- this.section = section;- this.program = program;- this.block = block;- }-- @Override- public boolean equals(Object obj) {- if (!(obj instanceof SectionMapEntry)) {- return false;- }- SectionMapEntry that = (SectionMapEntry) obj;- if (this.section != that.section) {- return false;- }- /*if (this.program != that.program) {- return false;- }- if (this.block != that.block) {- return false;- }*/- return true;- }-- @Override- public int hashCode() {- return Objects.hash(section/*, program, block*/);- }-- /**- * Get the module containing the section- *- * @return the module- */- public TraceModule getModule() {- return section.getModule();- }-- /**- * Get the section- *- * @return the section- */- public TraceSection getSection() {- return section;- }-- /**- * Get the program containing the matched memory block- *- * @return the program- */- public Program getProgram() {- return program;- }-- /**- * Get the matched memory block- *- * @return the block- */- public MemoryBlock getBlock() {- return block;- }-- /**- * Set the matched memory block- *- * @param program the program containing the block- * @param block the block- */- public void setBlock(Program program, MemoryBlock block) {- this.program = program;- this.block = block;- }-- /**- * Get the length of the match- *- * <p>- * Ideally, the section and block have <em>exactly</em> the same length. If they do not, the- * (unsigned) minimum of the two is used.- *- * @return the length- */- public long getLength() {- return MathUtilities.unsignedMin(section.getRange().getLength(), block.getSize());- }- }-/**- * A {@code (shift,view)} pair for describing sets of mapped addresses+ * A pair for describing sets of mapped addresses+ *+ * <p>+ * Note, the natural order is by the <em>destination</em> address.*/- public class ShiftAndAddressSetView {+ public class MappedAddressRange implements Comparable<MappedAddressRange> {++ private final AddressRange srcRange;+ private final AddressRange dstRange;+ private final int hashCode;private final long shift;- private final AddressSetView view;- public ShiftAndAddressSetView(long shift, AddressSetView view) {- this.shift = shift;- this.view = view;+ public MappedAddressRange(AddressRange srcRange, AddressRange dstRange) {+ this.srcRange = srcRange;+ this.dstRange = dstRange;+ this.hashCode = Objects.hash(dstRange, srcRange);+ this.shift = dstRange.getMinAddress().getOffset() -+ srcRange.getMinAddress().getOffset();+ }++ @Override+ public String toString() {+ return "<MappedRange " + srcRange + "::" + dstRange + ">";}/**diff --cc Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssembler.javaindex f251a63610,ac4cb0c714..0eaedd1eb1--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssembler.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/assembler/sleigh/SleighAssembler.java@@@ -114,6 -111,9 +111,10 @@@ public class SleighAssembler implementlisting.clearCodeUnits(at, end, false);memory.setBytes(at, insbytes);AddressSet set = new AddressSet(at, end);+++ // Creating this at construction causes it to assess memory flags too early.+ Disassembler dis = Disassembler.getDisassembler(program, TaskMonitor.DUMMY,+ DisassemblerMessageListener.IGNORE);dis.disassemble(at, set);return listing.getInstructions(set, true);}