Skip to content

Commit b8f1894

Browse files
Austen Adlerryanmkurtz
Austen Adler
authored andcommitted
Add clipboard option for byte source offset
Fix docs for byte source offset clipboard Fix variable names for byte source offset clipboard Copy <NO_OFFSET> if address is not backed by a file Change final BYTE_SOURCE_OFFSET_TYPE name to be more consistent
1 parent 17a4f8f commit b8f1894

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-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>%3CNO_OFFSET%3E</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

+31
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,29 @@ 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 = currentMemory.getAddressSourceInfo(addresses.next());
400+
if (addressSourceInfo != null) {
401+
long fileOffset = addressSourceInfo.getFileOffset();
402+
String fileOffsetString;
403+
404+
if (fileOffset == -1) {
405+
fileOffsetString = "<NO_OFFSET>";
406+
} else {
407+
fileOffsetString = String.format("%x", addressSourceInfo.getFileOffset());
408+
}
409+
410+
strings.add(fileOffsetString);
411+
}
412+
}
413+
return createStringTransferable(StringUtils.join(strings, "\n"));
414+
}
415+
385416
protected Transferable copyCode(TaskMonitor monitor) {
386417

387418
AddressSetView addressSet = getSelectedAddresses();

0 commit comments

Comments
 (0)