Skip to content

Commit 4f95fdd

Browse files
committed
Merge remote-tracking branch
'origin/GP-4318_ryanmkurtz_PR-6195_austenadler_copy-special' (Closes NationalSecurityAgency#6195)
2 parents 17a4f8f + 73e85ad commit 4f95fdd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/Clipboard.htm

+4
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ <H3>Listing</H3>
182182
current selection. The text is formatted to show the offset from the entry point of the
183183
function, for example: <CODE>main + 0x2</CODE></LI>
184184

185+
<LI><B>Byte Source Offset</B> - Copies the byte source offset from the start of the file for
186+
each address in the current selection. If the address is not backed by a file,
187+
<CODE>&lt;NO_OFFSET&gt;</CODE> is copied.</LI>
188+
185189
<LI><B>GhidraURL</B> <A name="GhidraURL"></A> - Creates a GhidraURL for the address under the
186190
cursor then copies that URL to the clipboard.</LI>
187191
</UL>

Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProvider.java

+26
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
import ghidra.framework.cmd.Command;
4545
import ghidra.framework.model.DomainFile;
4646
import ghidra.framework.plugintool.PluginTool;
47+
import ghidra.program.database.mem.AddressSourceInfo;
4748
import ghidra.program.database.symbol.CodeSymbol;
4849
import ghidra.program.database.symbol.FunctionSymbol;
4950
import ghidra.program.model.address.*;
5051
import ghidra.program.model.listing.*;
52+
import ghidra.program.model.mem.Memory;
5153
import ghidra.program.model.symbol.*;
5254
import ghidra.program.util.*;
5355
import ghidra.util.Msg;
@@ -65,6 +67,8 @@ public class CodeBrowserClipboardProvider extends ByteCopier
6567
new ClipboardType(DataFlavor.stringFlavor, "Address");
6668
public static final ClipboardType ADDRESS_TEXT_WITH_OFFSET_TYPE =
6769
new ClipboardType(DataFlavor.stringFlavor, "Address w/ Offset");
70+
public static final ClipboardType BYTE_SOURCE_OFFSET_TYPE =
71+
new ClipboardType(DataFlavor.stringFlavor, "Byte Source Offset");
6872
public static final ClipboardType CODE_TEXT_TYPE =
6973
new ClipboardType(DataFlavor.stringFlavor, "Formatted Code");
7074
public static final ClipboardType LABELS_COMMENTS_TYPE =
@@ -94,6 +98,7 @@ private static List<ClipboardType> createCopyTypesList() {
9498
list.add(CPP_BYTE_ARRAY_TYPE);
9599
list.add(ADDRESS_TEXT_TYPE);
96100
list.add(ADDRESS_TEXT_WITH_OFFSET_TYPE);
101+
list.add(BYTE_SOURCE_OFFSET_TYPE);
97102

98103
return list;
99104
}
@@ -221,6 +226,9 @@ public Transferable copySpecial(ClipboardType copyType, TaskMonitor monitor) {
221226
else if (copyType == ADDRESS_TEXT_WITH_OFFSET_TYPE) {
222227
return copySymbolString(monitor);
223228
}
229+
else if (copyType == BYTE_SOURCE_OFFSET_TYPE) {
230+
return copyByteSourceOffset(monitor);
231+
}
224232
else if (copyType == CODE_TEXT_TYPE) {
225233
return copyCode(monitor);
226234
}
@@ -382,6 +390,24 @@ else if (delta > 0) {
382390
return createStringTransferable(StringUtils.join(strings, "\n"));
383391
}
384392

393+
private Transferable copyByteSourceOffset(TaskMonitor monitor) {
394+
AddressSetView addrs = getSelectedAddresses();
395+
Memory currentMemory = currentProgram.getMemory();
396+
List<String> strings = new ArrayList<>();
397+
AddressIterator addresses = addrs.getAddresses(true);
398+
while (addresses.hasNext() && !monitor.isCancelled()) {
399+
AddressSourceInfo addressSourceInfo =
400+
currentMemory.getAddressSourceInfo(addresses.next());
401+
if (addressSourceInfo != null) {
402+
long fileOffset = addressSourceInfo.getFileOffset();
403+
String fileOffsetString =
404+
fileOffset != -1 ? "%x".formatted(fileOffset) : "<NO_OFFSET>";
405+
strings.add(fileOffsetString);
406+
}
407+
}
408+
return createStringTransferable(String.join("\n", strings));
409+
}
410+
385411
protected Transferable copyCode(TaskMonitor monitor) {
386412

387413
AddressSetView addressSet = getSelectedAddresses();

0 commit comments

Comments
 (0)