mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-05 02:39:44 +02:00
Merge remote-tracking branch 'origin/GT-3434-dragonmacher-namespace-matches-function-name'
This commit is contained in:
commit
6ae0c1ce23
30 changed files with 439 additions and 240 deletions
|
@ -31,7 +31,7 @@ import ghidra.util.exception.InvalidInputException;
|
|||
* <a id="examples"></a>
|
||||
* Example strings:
|
||||
* <ul>
|
||||
* <li>global{@link Namespace#NAMESPACE_DELIMITER ::}child1{@link Namespace#NAMESPACE_DELIMITER ::}child2
|
||||
* <li>global{@link Namespace#DELIMITER ::}child1{@link Namespace#DELIMITER ::}child2
|
||||
* <li>child1
|
||||
* </ul>
|
||||
* <p>
|
||||
|
|
|
@ -243,14 +243,14 @@ public class ThunkReferenceAddressDialog extends DialogComponentProvider {
|
|||
return null;
|
||||
}
|
||||
|
||||
List<Namespace> namespaces = NamespaceUtils.getNamespaces(parentNs, null, program);
|
||||
List<Namespace> namespaces = NamespaceUtils.getNamespaceByPath(program, null, parentNs);
|
||||
|
||||
if (namespaces.isEmpty()) {
|
||||
SymbolTable symbolTable = program.getSymbolTable();
|
||||
for (String libraryName : program.getExternalManager().getExternalLibraryNames()) {
|
||||
Symbol librarySymbol = symbolTable.getLibrarySymbol(libraryName);
|
||||
namespaces = NamespaceUtils.getNamespaces(parentNs,
|
||||
(Library) librarySymbol.getObject(), program);
|
||||
namespaces = NamespaceUtils.getNamespaceByPath(program,
|
||||
(Library) librarySymbol.getObject(), parentNs);
|
||||
if (!namespaces.isEmpty()) {
|
||||
break; // use first library containing namespace
|
||||
}
|
||||
|
|
|
@ -394,7 +394,7 @@ public class GoToHelper {
|
|||
StringBuilder buf = new StringBuilder();
|
||||
while (!nameStack.isEmpty()) {
|
||||
buf.append(nameStack.pop());
|
||||
buf.append(Namespace.NAMESPACE_DELIMITER);
|
||||
buf.append(Namespace.DELIMITER);
|
||||
}
|
||||
buf.append(externalLoc.getLabel());
|
||||
return buf.toString();
|
||||
|
|
|
@ -117,7 +117,7 @@ public class MatchSymbol {
|
|||
SymbolPath namespacePath = aSymbolPath.getParent();
|
||||
if (!aSymbolIdentifier.isExternalSymbol() && namespacePath != null &&
|
||||
!aSymbolPath.equals(bSymbolIdentifier.symbolPath) &&
|
||||
NamespaceUtils.getNamespace(bProgram, namespacePath, null) != null) {
|
||||
NamespaceUtils.getNonFunctionNamespace(bProgram, namespacePath) != null) {
|
||||
// skip match with namespace mismatch when source namespace exists in destination
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -172,24 +172,39 @@ public class AddEditDialog extends DialogComponentProvider {
|
|||
if (parentPath == null) {
|
||||
return rootNamespace;
|
||||
}
|
||||
String relativeParentPath = parentPath.getPath();
|
||||
|
||||
SymbolPath absoluteParentPath =
|
||||
new SymbolPath(rootNamespace.getSymbol()).append(parentPath);
|
||||
Namespace parentNamespace = NamespaceUtils.getNamespace(program, absoluteParentPath, addr);
|
||||
if (parentNamespace != null) {
|
||||
return parentNamespace;
|
||||
//
|
||||
// Prefer a non-function namespace. This allows us to put a function inside of a namespace
|
||||
// sharing the same name.
|
||||
//
|
||||
SymbolPath fullPath = new SymbolPath(rootNamespace.getSymbol()).append(parentPath);
|
||||
Namespace nonFunctionNs = NamespaceUtils.getNonFunctionNamespace(program, fullPath);
|
||||
if (nonFunctionNs != null) {
|
||||
return nonFunctionNs;
|
||||
}
|
||||
|
||||
// run the create namespaces command
|
||||
CreateNamespacesCmd command =
|
||||
new CreateNamespacesCmd(relativeParentPath, rootNamespace, SourceType.USER_DEFINED);
|
||||
|
||||
if (tool.execute(command, program)) {
|
||||
return command.getNamespace();
|
||||
//
|
||||
// At this point we can either reuse an existing function namespace or we have to create
|
||||
// a new non-function namespaces, depending upon the names being used. Only use an
|
||||
// existing function as a namespace if none of namespace path entries match the function
|
||||
// name.
|
||||
//
|
||||
String name = symbolPath.getName();
|
||||
if (!parentPath.containsPathEntry(name)) {
|
||||
Namespace functionNamespace =
|
||||
NamespaceUtils.getFunctionNamespaceContaining(program, parentPath, addr);
|
||||
if (functionNamespace != null) {
|
||||
return functionNamespace;
|
||||
}
|
||||
}
|
||||
|
||||
setStatusText(command.getStatusMsg());
|
||||
CreateNamespacesCmd cmd =
|
||||
new CreateNamespacesCmd(parentPath.getPath(), rootNamespace, SourceType.USER_DEFINED);
|
||||
if (tool.execute(cmd, program)) {
|
||||
return cmd.getNamespace();
|
||||
}
|
||||
|
||||
setStatusText(cmd.getStatusMsg());
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -192,7 +192,7 @@ public class NamespacePath implements Comparable<NamespacePath> {
|
|||
public String asNamespaceString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
doInOrderTraversal(
|
||||
nsp -> sb.append(sb.length() != 0 ? Namespace.NAMESPACE_DELIMITER : "").append(
|
||||
nsp -> sb.append(sb.length() != 0 ? Namespace.DELIMITER : "").append(
|
||||
nsp.isRoot() ? "ROOT" : nsp.name));
|
||||
return sb.toString();
|
||||
}
|
||||
|
@ -207,7 +207,7 @@ public class NamespacePath implements Comparable<NamespacePath> {
|
|||
|
||||
doInOrderTraversal(nsp -> {
|
||||
if (!nsp.isRoot()) {
|
||||
sb.append(sb.length() != 0 ? Namespace.NAMESPACE_DELIMITER : "").append(nsp.name);
|
||||
sb.append(sb.length() != 0 ? Namespace.DELIMITER : "").append(nsp.name);
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -219,7 +219,7 @@ public class NamespacePath implements Comparable<NamespacePath> {
|
|||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
doInOrderTraversal(
|
||||
nsp -> sb.append(sb.length() != 0 ? Namespace.NAMESPACE_DELIMITER : "").append(
|
||||
nsp -> sb.append(sb.length() != 0 ? Namespace.DELIMITER : "").append(
|
||||
nsp.isRoot() ? "ROOT" : nsp.name).append(
|
||||
"(" + (nsp.getType() != null ? nsp.getType() : "unknown type") + ")"));
|
||||
return sb.toString();
|
||||
|
|
|
@ -39,7 +39,7 @@ public abstract class DemangledObject {
|
|||
protected static final String SPACE = " ";
|
||||
protected static final Pattern SPACE_PATTERN = Pattern.compile(SPACE);
|
||||
|
||||
protected static final String NAMESPACE_SEPARATOR = Namespace.NAMESPACE_DELIMITER;
|
||||
protected static final String NAMESPACE_SEPARATOR = Namespace.DELIMITER;
|
||||
protected static final String EMPTY_STRING = "";
|
||||
|
||||
protected String originalMangled;
|
||||
|
|
|
@ -193,7 +193,7 @@ public class DemangledType {
|
|||
return "";
|
||||
}
|
||||
|
||||
buffer.append(Namespace.NAMESPACE_DELIMITER);
|
||||
buffer.append(Namespace.DELIMITER);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -75,12 +75,12 @@ class FunctionsXmlMgr {
|
|||
* <!ELEMENT FUNCTION (RETURN_TYPE?, ADDRESS_RANGE*, REGULAR_CMT?, REPEATABLE_CMT?, TYPEINFO_CMT?, STACK_FRAME?, REGISTER_VAR*)>
|
||||
* </code></pre>
|
||||
* <p>
|
||||
* @param parser
|
||||
* @param overwriteConflicts
|
||||
* @param ignoreStackFrames
|
||||
* @param monitor
|
||||
* @throws AddressFormatException
|
||||
* @throws CancelledException
|
||||
* @param parser the parser
|
||||
* @param overwriteConflicts true to overwrite any conflicts
|
||||
* @param ignoreStackFrames true to ignore stack frames
|
||||
* @param monitor the task monitor
|
||||
* @throws AddressFormatException if any address is not parsable
|
||||
* @throws CancelledException if the operation is cancelled through the monitor
|
||||
*/
|
||||
void read(XmlPullParser parser, boolean overwriteConflicts, boolean ignoreStackFrames,
|
||||
TaskMonitor monitor) throws AddressFormatException, CancelledException {
|
||||
|
@ -94,9 +94,7 @@ class FunctionsXmlMgr {
|
|||
dtParser = new DtParser(dataManager);
|
||||
|
||||
while (parser.peek().isStart()) {
|
||||
if (monitor.isCancelled()) {
|
||||
throw new CancelledException();
|
||||
}
|
||||
monitor.checkCanceled();
|
||||
|
||||
final XmlElement functionElement = parser.start("FUNCTION");
|
||||
|
||||
|
@ -155,7 +153,8 @@ class FunctionsXmlMgr {
|
|||
try {
|
||||
Symbol symbol = func.getSymbol();
|
||||
Namespace namespace =
|
||||
NamespaceUtils.getNamespace(program, namespacePath, entryPoint);
|
||||
NamespaceUtils.getFunctionNamespaceAt(program, namespacePath,
|
||||
entryPoint);
|
||||
if (namespace == null) {
|
||||
namespace = program.getGlobalNamespace();
|
||||
}
|
||||
|
@ -258,13 +257,6 @@ class FunctionsXmlMgr {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add local vars to a function.
|
||||
*
|
||||
* @param function
|
||||
* @param variables
|
||||
* @throws InvalidInputException
|
||||
*/
|
||||
private void addLocalVars(Function function, List<Variable> variables,
|
||||
boolean overwriteConflicts) throws InvalidInputException {
|
||||
for (Variable v : variables) {
|
||||
|
@ -275,8 +267,9 @@ class FunctionsXmlMgr {
|
|||
try {
|
||||
String name = v.getName();
|
||||
boolean isDefaultVariableName = (name == null) ||
|
||||
SymbolUtilities.getDefaultLocalName(program, v.getStackOffset(), 0).equals(
|
||||
name);
|
||||
SymbolUtilities.getDefaultLocalName(program, v.getStackOffset(), 0)
|
||||
.equals(
|
||||
name);
|
||||
|
||||
SourceType sourceType =
|
||||
isDefaultVariableName ? SourceType.DEFAULT : SourceType.USER_DEFINED;
|
||||
|
@ -305,13 +298,9 @@ class FunctionsXmlMgr {
|
|||
return dtParser.parseDataType(dtName, cp, size);
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Returns the text embedded in an optional xml element. If the next element in the stream is not
|
||||
* the "expectedElementName", the xml parser stream is unchanged.
|
||||
* <p>
|
||||
* @param parser
|
||||
* @param expectedElementName
|
||||
* @return
|
||||
* the "expectedElementName", the xml parser stream is unchanged
|
||||
*/
|
||||
private String getElementText(XmlPullParser parser, String expectedElementName) {
|
||||
String result = null;
|
||||
|
|
|
@ -1065,7 +1065,7 @@ public class ProgramBuilder {
|
|||
ExternalManager extMgr = program.getExternalManager();
|
||||
Namespace namespace = extMgr.addExternalLibraryName(libraryName, sourceType);
|
||||
|
||||
if (externalLabel != null && externalLabel.indexOf(Namespace.NAMESPACE_DELIMITER) > 0) {
|
||||
if (externalLabel != null && externalLabel.indexOf(Namespace.DELIMITER) > 0) {
|
||||
// External manager API does not yet support creation of namespaces within
|
||||
// library so we handle that here
|
||||
SymbolPath symPath = new SymbolPath(externalLabel);
|
||||
|
|
|
@ -1293,8 +1293,8 @@ public class CodeUnitFormat {
|
|||
namespaceName = parentNamespace.getName(true);
|
||||
}
|
||||
}
|
||||
if (namespaceName.length() != 0 && !namespaceName.endsWith(Namespace.NAMESPACE_DELIMITER)) {
|
||||
namespaceName += Namespace.NAMESPACE_DELIMITER;
|
||||
if (namespaceName.length() != 0 && !namespaceName.endsWith(Namespace.DELIMITER)) {
|
||||
namespaceName += Namespace.DELIMITER;
|
||||
}
|
||||
return namespaceName + name;
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import org.junit.*;
|
|||
|
||||
import ghidra.app.cmd.function.CreateFunctionCmd;
|
||||
import ghidra.app.cmd.label.AddLabelCmd;
|
||||
import ghidra.app.cmd.label.CreateNamespacesCmd;
|
||||
import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;
|
||||
import ghidra.app.plugin.core.navigation.GoToAddressLabelPlugin;
|
||||
import ghidra.app.util.AddEditDialog;
|
||||
|
@ -93,6 +94,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
dialog.close();
|
||||
env.dispose();
|
||||
}
|
||||
|
||||
|
@ -107,14 +109,14 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
assertEquals(globalScope, scope);
|
||||
assertTrue(primaryCheckBox.isSelected());
|
||||
assertTrue(!primaryCheckBox.isEnabled());
|
||||
dialogCancel();
|
||||
pressCancel();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLabelChangeOne() {
|
||||
addLabel(addr(0x0100642a));
|
||||
setText("printf");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
Symbol s = getUniqueSymbol(program, "printf", null);
|
||||
assertNotNull(s);
|
||||
|
@ -126,14 +128,14 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
public void testDuplicateLabelForAdd() {
|
||||
addLabel(addr(0x0100642a));
|
||||
setText("printf");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
addLabel(addr(0x0100642c));
|
||||
setText("printf");
|
||||
assertEquals(" ", dialog.getStatusText());
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertTrue(dialog.getStatusText().length() > 0);
|
||||
dialogCancel();
|
||||
pressCancel();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -141,7 +143,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
addLabel(addr(0x0100642a));
|
||||
setText("printf");
|
||||
setCheckbox(entryCheckBox, true);
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertTrue(!dialog.isVisible());
|
||||
Symbol s = getUniqueSymbol(program, "printf", null);
|
||||
assertNotNull(s);
|
||||
|
@ -153,7 +155,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
addLabel(addr(0x0100642a));
|
||||
setText("printf");
|
||||
setCheckbox(pinnedCheckBox, true);
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertTrue(!dialog.isVisible());
|
||||
Symbol s = getUniqueSymbol(program, "printf", null);
|
||||
assertNotNull(s);
|
||||
|
@ -161,7 +163,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
|
||||
editLabel(s);
|
||||
setCheckbox(pinnedCheckBox, false);
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertTrue(!s.isPinned());
|
||||
|
||||
}
|
||||
|
@ -169,7 +171,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
@Test
|
||||
public void testEmptyLabel() {
|
||||
addLabel(addr(0x0100642a));
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertTrue(dialog.isVisible());
|
||||
assertTrue(dialog.getStatusText().length() > 0);
|
||||
|
||||
|
@ -239,7 +241,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
setText("printf");
|
||||
Namespace scope = st.getNamespace(a);
|
||||
setScope(scope);
|
||||
dialogOK();
|
||||
pressOk();
|
||||
s = getUniqueSymbol(program, "printf", scope);
|
||||
assertNotNull(s);
|
||||
assertTrue(!s.isGlobal());
|
||||
|
@ -259,7 +261,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
setText("printf");
|
||||
|
||||
setCheckbox(primaryCheckBox, true);
|
||||
dialogOK();
|
||||
pressOk();
|
||||
s = getUniqueSymbol(program, "entry", null);
|
||||
assertTrue(!s.isPrimary());
|
||||
s = getUniqueSymbol(program, "printf", null);
|
||||
|
@ -286,7 +288,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
addLabel(addr(0x100642a));
|
||||
setText("aaaa");
|
||||
assertEquals(0, recentLabels.size());
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
assertEquals(1, recentLabels.size());
|
||||
assertEquals("aaaa", recentLabels.get(0));
|
||||
|
@ -295,7 +297,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
assertEquals("", getText());
|
||||
//assertEquals("aaaa", getText());
|
||||
setText("bbbb");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
assertEquals(2, recentLabels.size());
|
||||
assertEquals("bbbb", recentLabels.get(0));
|
||||
|
@ -308,7 +310,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
for (int i = 0; i < 12; i++) {
|
||||
addLabel(addr(0x100642a));
|
||||
setText("l" + i);
|
||||
dialogOK();
|
||||
pressOk();
|
||||
}
|
||||
|
||||
assertEquals(10, recentLabels.size());
|
||||
|
@ -328,7 +330,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
assertTrue(entryCheckBox.isSelected());
|
||||
assertTrue(primaryCheckBox.isSelected());
|
||||
assertTrue(!primaryCheckBox.isEnabled());
|
||||
dialogCancel();
|
||||
pressCancel();
|
||||
|
||||
}
|
||||
|
||||
|
@ -352,7 +354,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
editLabel(s);
|
||||
assertEquals("entry", getText());
|
||||
setText("bob");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
program.flushEvents();
|
||||
waitForSwing();
|
||||
assertEquals("bob", function.getName());
|
||||
|
@ -378,7 +380,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
|
||||
editLabel(fredSymbol);
|
||||
setCheckbox(primaryCheckBox, true);
|
||||
dialogOK();
|
||||
pressOk();
|
||||
program.flushEvents();
|
||||
waitForSwing();
|
||||
assertEquals("fred", function.getName());
|
||||
|
@ -400,7 +402,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
|
||||
Object selectedItem = namespacesComboBox.getSelectedItem();
|
||||
assertEquals(ns, ((AddEditDialog.NamespaceWrapper) selectedItem).getNamespace());
|
||||
dialogCancel();
|
||||
pressCancel();
|
||||
|
||||
}
|
||||
|
||||
|
@ -414,7 +416,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
assertTrue(primaryCheckBox.isSelected());
|
||||
assertTrue(!primaryCheckBox.isEnabled());
|
||||
setText("aaaa");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
s = st.getPrimarySymbol(refAddr);
|
||||
assertEquals("aaaa", s.getName());
|
||||
}
|
||||
|
@ -426,7 +428,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
editLabel(s);
|
||||
|
||||
setText("fred");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertEquals("fred", s.getName());
|
||||
|
||||
s = st.getPrimarySymbol(a);
|
||||
|
@ -438,17 +440,17 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
public void testDuplicateLabelForEdit() {
|
||||
addLabel(addr(0x100642a));
|
||||
setText("printf");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
addLabel(addr(0x100642a));
|
||||
setText("fred");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
Symbol s = getUniqueSymbol(program, "printf", null);
|
||||
editLabel(s);
|
||||
|
||||
setText("fred");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertTrue(dialog.isVisible());
|
||||
assertTrue(dialog.getStatusText().length() > 0);
|
||||
|
||||
|
@ -459,18 +461,18 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
Address a = addr(0x100642a);
|
||||
addLabel(a);
|
||||
setText("aaaa");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
addLabel(a);
|
||||
setText("bbbb");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
addLabel(a);
|
||||
setText("cccc");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
Symbol s = getUniqueSymbol(program, "bbbb", null);
|
||||
editLabel(s);
|
||||
setText("zzzz");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
assertNotNull(getUniqueSymbol(program, "aaaa", null));
|
||||
assertNotNull(getUniqueSymbol(program, "zzzz", null));
|
||||
assertNotNull(getUniqueSymbol(program, "cccc", null));
|
||||
|
@ -491,7 +493,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
assertTrue(!primaryCheckBox.isSelected());
|
||||
assertTrue(primaryCheckBox.isEnabled());
|
||||
setText("foo");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
s = st.getPrimarySymbol(a);
|
||||
assertNotNull(s);
|
||||
|
@ -509,16 +511,16 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
|
||||
addLabel(s.getAddress());
|
||||
setText("aaaa");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
s = getUniqueSymbol(program, "aaaa", null);
|
||||
editLabel(s);
|
||||
setText("zzzz");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
editLabel(s);
|
||||
setText("bbbb");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
assertEquals(3, recentLabels.size());
|
||||
assertEquals("bbbb", recentLabels.get(0));
|
||||
|
@ -532,13 +534,13 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
addLabel(s.getAddress());
|
||||
setScope(s.getParentNamespace());
|
||||
setText("foo");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
s = getUniqueSymbol(program, "foo", null);
|
||||
editLabel(s);
|
||||
assertTrue(primaryCheckBox.isEnabled());
|
||||
assertTrue(!primaryCheckBox.isSelected());
|
||||
dialogOK();
|
||||
pressOk();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -579,7 +581,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
Address entryAddress = s.getAddress();
|
||||
addLabel(entryAddress);
|
||||
setText("label_1");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
assertTrue("Encountered a problem adding a label to the Global " + "namespace",
|
||||
!dialog.isVisible());
|
||||
|
@ -588,7 +590,7 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
s = getUniqueSymbol(program, "label_1", null);
|
||||
editLabel(s);
|
||||
setText("namespace_1::label_1");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
// make sure there were no problems
|
||||
assertTrue(
|
||||
|
@ -599,18 +601,128 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
s = st.getSymbols("label_1").next();
|
||||
editLabel(s);
|
||||
setText("Global::label_1");
|
||||
dialogOK();
|
||||
pressOk();
|
||||
|
||||
assertTrue("Encountered a problem changing a symbol's namespace to " +
|
||||
"the Global namespace while editing the symbol", !dialog.isVisible());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntryNamespacePathWithAmbiguousFunctionName() {
|
||||
Symbol origEntry = getUniqueSymbol(program, "entry", null);
|
||||
assertNotNull(origEntry);
|
||||
public void testSetLabelNamespace_InsideFunctionBody_ToFunctionNamespace() {
|
||||
Symbol entry = getUniqueSymbol(program, "entry", null);
|
||||
assertNotNull(entry);
|
||||
|
||||
Symbol dupEntry = st.getPrimarySymbol(addr(0x1002239));
|
||||
Address inBodyAddress = entry.getAddress().add(1);
|
||||
addLabel(inBodyAddress);
|
||||
String newName = "label_1";
|
||||
setText("entry" + Namespace.DELIMITER + newName);
|
||||
pressOk();
|
||||
assertFalse("Encountered a problem adding a label to the Global namespace",
|
||||
dialog.isVisible());
|
||||
|
||||
Symbol label1 = getSymbol(newName);
|
||||
Namespace parentNamespace = label1.getParentNamespace();
|
||||
assertTrue("Namespace is not a function", parentNamespace instanceof Function);
|
||||
Function fun = (Function) parentNamespace;
|
||||
assertEquals(entry.getAddress(), fun.getEntryPoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetLabelNamespace_InsideFunctionBody_ToExistingNonFunctionNamespace() {
|
||||
|
||||
Symbol entry = getUniqueSymbol(program, "entry", null);
|
||||
assertNotNull(entry);
|
||||
|
||||
String namespaceName = "NewNamespace";
|
||||
createNamespace(namespaceName);
|
||||
|
||||
Address inBodyAddress = entry.getAddress().add(1);
|
||||
addLabel(inBodyAddress);
|
||||
String newName = "label_1";
|
||||
setText(namespaceName + Namespace.DELIMITER + newName);
|
||||
pressOk();
|
||||
assertFalse("Encountered a problem adding a label to the Global namespace",
|
||||
dialog.isVisible());
|
||||
|
||||
Symbol label1 = getSymbol(newName);
|
||||
Namespace parentNamespace = label1.getParentNamespace();
|
||||
assertFalse("Namespace is not a function", parentNamespace instanceof Function);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetLabelNamespace_InsideFunctionBody_ToNonExistentNonFunctionNamespace() {
|
||||
|
||||
Symbol entry = getUniqueSymbol(program, "entry", null);
|
||||
assertNotNull(entry);
|
||||
|
||||
String namespaceName = "NewNamespace";
|
||||
Address inBodyAddress = entry.getAddress().add(1);
|
||||
addLabel(inBodyAddress);
|
||||
String newName = "label_1";
|
||||
setText(namespaceName + Namespace.DELIMITER + newName);
|
||||
pressOk();
|
||||
assertFalse("Encountered a problem adding a label to the Global namespace",
|
||||
dialog.isVisible());
|
||||
|
||||
Symbol label1 = getSymbol(newName);
|
||||
Namespace parentNamespace = label1.getParentNamespace();
|
||||
assertFalse("Namespace is not a function", parentNamespace instanceof Function);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntryNamespacePathWithAmbiguousFunctionName() {
|
||||
Symbol entry = getUniqueSymbol(program, "entry", null);
|
||||
assertNotNull(entry);
|
||||
|
||||
Address otherEntryAddress = addr(0x1002239);
|
||||
Symbol dupEntry = createOtherEntry(otherEntryAddress);
|
||||
|
||||
Address inBodyAddress = otherEntryAddress.add(1);
|
||||
addLabel(inBodyAddress);
|
||||
String newName = "label_1";
|
||||
setText("entry" + Namespace.DELIMITER + newName);
|
||||
pressOk();
|
||||
assertFalse("Encountered a problem adding a label to the Global namespace",
|
||||
dialog.isVisible());
|
||||
|
||||
Symbol label1 = getSymbol(newName);
|
||||
Namespace parentNamespace = label1.getParentNamespace();
|
||||
assertTrue("Namespace is not a function", parentNamespace instanceof Function);
|
||||
Function fun = (Function) parentNamespace;
|
||||
assertEquals(dupEntry.getAddress(), fun.getEntryPoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNamespace_NonExistentNamespace_SameNameAsFunction() throws Exception {
|
||||
|
||||
//
|
||||
// Test that we can create a new namespace using the dialog when:
|
||||
// 1) that namespace does not exist
|
||||
// 2) the namespace matches the existing function name
|
||||
// 3) the function name is not default
|
||||
//
|
||||
|
||||
String functionName = "Foo";
|
||||
Symbol function = createFunction(functionName);
|
||||
|
||||
editLabel(function);
|
||||
String nsName = functionName;
|
||||
setText(nsName + Namespace.DELIMITER + functionName);
|
||||
pressOk();
|
||||
assertFalse("Rename unsuccesful", dialog.isShowing());
|
||||
|
||||
Symbol newFunction = getSymbol(functionName);
|
||||
Namespace parentNs = newFunction.getParentNamespace();
|
||||
assertFalse(parentNs instanceof Function);
|
||||
assertEquals(nsName, parentNs.getName());
|
||||
}
|
||||
|
||||
//==================================================================================================
|
||||
// Private Methods
|
||||
//==================================================================================================
|
||||
|
||||
private Symbol createOtherEntry(Address otherAddress) {
|
||||
Symbol dupEntry = st.getPrimarySymbol(otherAddress);
|
||||
assertNotNull(dupEntry);
|
||||
|
||||
int id = program.startTransaction("test");
|
||||
|
@ -618,42 +730,48 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
dupEntry.setName("entry", SourceType.USER_DEFINED);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("Got Exception trying to set the function name to entry at 0x1002239 ");
|
||||
fail("Got Exception trying to set the function name to entry at " + otherAddress);
|
||||
}
|
||||
finally {
|
||||
program.endTransaction(id, true);
|
||||
}
|
||||
|
||||
Address entryAddress = dupEntry.getAddress();
|
||||
addLabel(entryAddress);
|
||||
setText("entry::label_1");
|
||||
dialogOK();
|
||||
|
||||
assertTrue("Encountered a problem adding a label to the Global " + "namespace",
|
||||
!dialog.isVisible());
|
||||
|
||||
SymbolIterator symbolIt = st.getSymbols("label_1");
|
||||
Symbol label1 = symbolIt.next();
|
||||
assertNotNull(label1);
|
||||
Namespace parentNamespace = label1.getParentNamespace();
|
||||
assertTrue(parentNamespace instanceof Function);
|
||||
Function fun = (Function) parentNamespace;
|
||||
assertEquals(dupEntry.getAddress(), fun.getEntryPoint());
|
||||
return dupEntry;
|
||||
}
|
||||
|
||||
private Symbol getSymbol(String functionName) {
|
||||
|
||||
SymbolIterator it = st.getSymbols(functionName);
|
||||
Symbol newLabel = it.next();
|
||||
assertNotNull(newLabel);
|
||||
assertEquals(functionName, newLabel.getName());
|
||||
return newLabel;
|
||||
}
|
||||
|
||||
private Symbol createFunction(String name) throws Exception {
|
||||
Symbol entry = getUniqueSymbol(program, "entry", null);
|
||||
createEntryFunction();
|
||||
editLabel(entry);
|
||||
setText(name);
|
||||
pressOk();
|
||||
|
||||
SymbolIterator it = st.getSymbols(name);
|
||||
Symbol newLabel = it.next();
|
||||
assertNotNull(newLabel);
|
||||
assertEquals(name, newLabel.getName());
|
||||
return newLabel;
|
||||
}
|
||||
//==================================================================================================
|
||||
// Private Methods
|
||||
//==================================================================================================
|
||||
|
||||
private Address addr(long addr) {
|
||||
return program.getAddressFactory().getAddress(Long.toHexString(addr));
|
||||
}
|
||||
|
||||
private void dialogCancel() {
|
||||
private void pressCancel() {
|
||||
runSwing(() -> invokeInstanceMethod("cancelCallback", dialog));
|
||||
}
|
||||
|
||||
private void dialogOK() {
|
||||
runSwing(() -> invokeInstanceMethod("okCallback", dialog));
|
||||
private void pressOk() {
|
||||
runSwing(() -> invokeInstanceMethod("okCallback", dialog), false);
|
||||
waitForSwing();
|
||||
}
|
||||
|
||||
private void setText(final String text) {
|
||||
|
@ -738,4 +856,8 @@ public class AddEditDialoglTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
assertTrue(tool.execute(cmd, program));
|
||||
}
|
||||
}
|
||||
|
||||
private void createNamespace(String name) {
|
||||
applyCmd(program, new CreateNamespacesCmd(name, SourceType.USER_DEFINED));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -651,7 +651,7 @@ public class SymbolTreePlugin1Test extends AbstractGhidraHeadedIntegrationTest {
|
|||
String newNamespace = "bob";
|
||||
String prefix = "MY";
|
||||
String newNameWithoutNamespace = prefix + s.getName();
|
||||
String newName = newNamespace + Namespace.NAMESPACE_DELIMITER + newNameWithoutNamespace;
|
||||
String newName = newNamespace + Namespace.DELIMITER + newNameWithoutNamespace;
|
||||
util.rename(advapi32Node, newName);
|
||||
util.waitForTree();
|
||||
assertEquals(newNameWithoutNamespace, s.getName());
|
||||
|
@ -681,7 +681,7 @@ public class SymbolTreePlugin1Test extends AbstractGhidraHeadedIntegrationTest {
|
|||
GTreeNode nsNode = newNsNode;
|
||||
String newNamespace = "OuterNamespace";
|
||||
String newName = "MyNamespace";
|
||||
String newFullName = newNamespace + Namespace.NAMESPACE_DELIMITER + newName;
|
||||
String newFullName = newNamespace + Namespace.DELIMITER + newName;
|
||||
setEditorText(path, nsNode, newFullName);
|
||||
|
||||
namespacesNode = rootNode.getChild("Namespaces");
|
||||
|
|
|
@ -1379,7 +1379,7 @@ public class SymbolTablePluginTest extends AbstractGhidraHeadedIntegrationTest {
|
|||
Command command = new CreateNamespacesCmd(namespaceName, SourceType.USER_DEFINED);
|
||||
if (tool.execute(command, program)) {
|
||||
List<Namespace> namespaces =
|
||||
NamespaceUtils.getNamespaces(namespaceName, null, program);
|
||||
NamespaceUtils.getNamespaceByPath(program, null, namespaceName);
|
||||
|
||||
if (namespaces.size() != 1) {
|
||||
Assert.fail("Unable to find the newly created parent namespace.");
|
||||
|
|
|
@ -308,7 +308,7 @@ public class CreateNamespacesCmdTest extends AbstractGenericTest {
|
|||
for (int i = 0; i < namespaceNames.length; i++) {
|
||||
buffer.append(namespaceNames[i]);
|
||||
if (i + 1 < namespaceNames.length) {
|
||||
buffer.append(Namespace.NAMESPACE_DELIMITER);
|
||||
buffer.append(Namespace.DELIMITER);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -107,7 +107,7 @@ public class CreateRtti1BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
|
|||
|
||||
// Plate Comment
|
||||
EHDataTypeUtilities.createPlateCommentIfNeeded(program,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.NAMESPACE_DELIMITER,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
|
||||
RTTI_1_NAME, suffix, address, applyOptions);
|
||||
|
||||
monitor.checkCanceled();
|
||||
|
|
|
@ -117,7 +117,7 @@ public class CreateRtti2BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
|
|||
|
||||
// Plate Comment
|
||||
EHDataTypeUtilities.createPlateCommentIfNeeded(program,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.NAMESPACE_DELIMITER,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
|
||||
RTTI_2_NAME, null, address, applyOptions);
|
||||
|
||||
monitor.checkCanceled();
|
||||
|
|
|
@ -104,7 +104,7 @@ public class CreateRtti3BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
|
|||
|
||||
// Plate Comment
|
||||
EHDataTypeUtilities.createPlateCommentIfNeeded(program,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.NAMESPACE_DELIMITER,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
|
||||
RTTI_3_NAME, null, address, applyOptions);
|
||||
|
||||
monitor.checkCanceled();
|
||||
|
|
|
@ -179,7 +179,7 @@ public class CreateRtti4BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
|
|||
|
||||
// Plate Comment
|
||||
EHDataTypeUtilities.createPlateCommentIfNeeded(program, RttiUtil.CONST_PREFIX +
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.NAMESPACE_DELIMITER,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
|
||||
RTTI_4_NAME, null, address, applyOptions);
|
||||
|
||||
monitor.checkCanceled();
|
||||
|
|
|
@ -118,7 +118,7 @@ public class CreateVfTableBackgroundCmd extends AbstractCreateDataBackgroundCmd<
|
|||
monitor.checkCanceled();
|
||||
String demangledTypeDescriptor = rtti0Model.getDemangledTypeDescriptor();
|
||||
String prefixString = ((demangledTypeDescriptor != null)
|
||||
? (demangledTypeDescriptor + Namespace.NAMESPACE_DELIMITER)
|
||||
? (demangledTypeDescriptor + Namespace.DELIMITER)
|
||||
: "");
|
||||
data.setComment(CodeUnit.EOL_COMMENT,
|
||||
"terminator for " + prefixString + VF_TABLE_LABEL);
|
||||
|
@ -174,7 +174,7 @@ public class CreateVfTableBackgroundCmd extends AbstractCreateDataBackgroundCmd<
|
|||
// Plate Comment
|
||||
EHDataTypeUtilities.createPlateCommentIfNeeded(program,
|
||||
RttiUtil.CONST_PREFIX + RttiUtil.getDescriptorTypeNamespace(rtti0Model) +
|
||||
Namespace.NAMESPACE_DELIMITER,
|
||||
Namespace.DELIMITER,
|
||||
VF_TABLE_LABEL, null, vfTableAddress, applyOptions);
|
||||
|
||||
monitor.checkCanceled();
|
||||
|
@ -215,7 +215,7 @@ public class CreateVfTableBackgroundCmd extends AbstractCreateDataBackgroundCmd<
|
|||
// Plate Comment
|
||||
EHDataTypeUtilities.createPlateCommentIfNeeded(
|
||||
program, META_LABEL + " pointer for " +
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.NAMESPACE_DELIMITER,
|
||||
RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
|
||||
VF_TABLE_LABEL, null, metaAddress, applyOptions);
|
||||
|
||||
monitor.checkCanceled();
|
||||
|
|
|
@ -101,7 +101,7 @@ class ApplySymbols {
|
|||
}
|
||||
|
||||
// Place compiler generated symbols (e.g., $LN9) within containing function when possible
|
||||
if (name.startsWith("$") && !name.contains(Namespace.NAMESPACE_DELIMITER)) {
|
||||
if (name.startsWith("$") && !name.contains(Namespace.DELIMITER)) {
|
||||
Function f = functionManager.getFunctionContaining(address);
|
||||
if (f != null && !f.getName().equals(name)) {
|
||||
name = NamespaceUtils.getNamespaceQualifiedName(f, name, true);
|
||||
|
|
|
@ -436,7 +436,7 @@ public class PdbParser {
|
|||
monitor.checkCanceled();
|
||||
boolean isClass = namespaceMap.get(path);
|
||||
Namespace parentNamespace =
|
||||
NamespaceUtils.getNamespace(program, path.getParent(), null);
|
||||
NamespaceUtils.getNonFunctionNamespace(program, path.getParent());
|
||||
if (parentNamespace == null) {
|
||||
String type = isClass ? "class" : "namespace";
|
||||
log.appendMsg("Error: failed to define " + type + ": " + path);
|
||||
|
@ -479,7 +479,7 @@ public class PdbParser {
|
|||
}
|
||||
catch (Exception e) {
|
||||
log.appendMsg("Unable to create class namespace: " + parentNamespace.getName(true) +
|
||||
Namespace.NAMESPACE_DELIMITER + name);
|
||||
Namespace.DELIMITER + name);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -983,11 +983,11 @@ public class PdbParser {
|
|||
* @return name without namespace prefix
|
||||
*/
|
||||
String stripNamespace(String name) {
|
||||
int index = name.lastIndexOf(Namespace.NAMESPACE_DELIMITER);
|
||||
int index = name.lastIndexOf(Namespace.DELIMITER);
|
||||
if (index <= 0) {
|
||||
return name;
|
||||
}
|
||||
return name.substring(index + Namespace.NAMESPACE_DELIMITER.length());
|
||||
return name.substring(index + Namespace.DELIMITER.length());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -997,7 +997,7 @@ public class PdbParser {
|
|||
* @return the category path
|
||||
*/
|
||||
CategoryPath getCategory(String namespaceQualifiedDataTypeName, boolean addPdbRoot) {
|
||||
String[] names = namespaceQualifiedDataTypeName.split(Namespace.NAMESPACE_DELIMITER);
|
||||
String[] names = namespaceQualifiedDataTypeName.split(Namespace.DELIMITER);
|
||||
CategoryPath category = addPdbRoot ? pdbCategory : CategoryPath.ROOT;
|
||||
if (names.length > 1) {
|
||||
String[] categoryNames = new String[names.length - 1];
|
||||
|
|
|
@ -29,7 +29,7 @@ import ghidra.util.exception.*;
|
|||
* <a id="examples"></a>
|
||||
* Example string format:
|
||||
* <ul>
|
||||
* <li>global{@link Namespace#NAMESPACE_DELIMITER ::}child1{@link Namespace#NAMESPACE_DELIMITER ::}child2
|
||||
* <li>global{@link Namespace#DELIMITER ::}child1{@link Namespace#DELIMITER ::}child2
|
||||
* <li>child1
|
||||
* </ul>
|
||||
* <a id="assumptions"></a>
|
||||
|
@ -37,9 +37,6 @@ import ghidra.util.exception.*;
|
|||
* <ul>
|
||||
* <li>All elements of a namespace path should be namespace symbols and not other
|
||||
* symbol types.
|
||||
* <li>If the method takes an address, then the path can contain a function name provided
|
||||
* the address is in the body of the function; otherwise
|
||||
* the names must all be namespaces other than functions.
|
||||
* <li>Absolute paths can optionally start with the global namespace.
|
||||
* <li>You can provide a relative path that will start at the given
|
||||
* parent namespace (or global if there is no parent provided).
|
||||
|
@ -72,7 +69,7 @@ public class NamespaceUtils {
|
|||
String str = new String();
|
||||
while (namespace != null && !(namespace instanceof GlobalNamespace) &&
|
||||
!(namespace instanceof Library)) {
|
||||
str = namespace.getName() + Namespace.NAMESPACE_DELIMITER + str;
|
||||
str = namespace.getName() + Namespace.DELIMITER + str;
|
||||
namespace = namespace.getParentNamespace();
|
||||
}
|
||||
return str;
|
||||
|
@ -93,7 +90,7 @@ public class NamespaceUtils {
|
|||
str = getNamespacePathWithoutLibrary(namespace);
|
||||
}
|
||||
else if (namespace != null && !(namespace instanceof GlobalNamespace)) {
|
||||
str = namespace.getName(true) + Namespace.NAMESPACE_DELIMITER;
|
||||
str = namespace.getName(true) + Namespace.DELIMITER;
|
||||
}
|
||||
str += symbolName;
|
||||
return str;
|
||||
|
@ -110,7 +107,7 @@ public class NamespaceUtils {
|
|||
*/
|
||||
@Deprecated
|
||||
public static List<String> splitNamespacePath(String path) {
|
||||
return Arrays.asList(path.trim().split(Namespace.NAMESPACE_DELIMITER));
|
||||
return Arrays.asList(path.trim().split(Namespace.DELIMITER));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -129,6 +126,28 @@ public class NamespaceUtils {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all namespaces with the given name in the parent namespace
|
||||
*
|
||||
* @param program the program to search
|
||||
* @param parent the parent namespace from which to find all namespaces with the given name;
|
||||
* if null, the global namespace will be used
|
||||
* @param namespaceName the name of the namespaces to retrieve
|
||||
* @return a list of all namespaces that match the given name in the given parent namespace.
|
||||
*/
|
||||
public static List<Namespace> getNamespacesByName(Program program, Namespace parent,
|
||||
String namespaceName) {
|
||||
validate(program, parent);
|
||||
List<Namespace> namespaceList = new ArrayList<>();
|
||||
List<Symbol> symbols = program.getSymbolTable().getSymbols(namespaceName, parent);
|
||||
for (Symbol symbol : symbols) {
|
||||
if (symbol.getSymbolType().isNamespace()) {
|
||||
namespaceList.add((Namespace) symbol.getObject());
|
||||
}
|
||||
}
|
||||
return namespaceList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of namespaces that match the given path. The path can be
|
||||
* relative to the given root namespace or absolute if the path begins with
|
||||
|
@ -136,36 +155,37 @@ public class NamespaceUtils {
|
|||
*
|
||||
* <P>Note: this path must only contain Namespace names and no other symbol types.
|
||||
*
|
||||
* @param namespacePath the path to the desired namespace.
|
||||
* @param rootNamespace the namespace to use as the root for relative paths. If null, the
|
||||
* global namespace will be used.
|
||||
* @param program the program to search.
|
||||
* @return a list of namespaces that match the given path.
|
||||
* @param program the program to search
|
||||
* @param parent the namespace to use as the root for relative paths. If null, the
|
||||
* global namespace will be used
|
||||
* @param pathString the path to the desired namespace
|
||||
* @return a list of namespaces that match the given path
|
||||
*/
|
||||
public static List<Namespace> getNamespaces(String namespacePath, Namespace rootNamespace,
|
||||
Program program) {
|
||||
public static List<Namespace> getNamespaceByPath(Program program, Namespace parent,
|
||||
String pathString) {
|
||||
|
||||
validate(program, rootNamespace);
|
||||
validate(program, parent);
|
||||
|
||||
rootNamespace = adjustForNullRootNamespace(rootNamespace, namespacePath, program);
|
||||
parent = adjustForNullRootNamespace(parent, pathString, program);
|
||||
|
||||
SymbolPath path = new SymbolPath(rootNamespace.getSymbol());
|
||||
if (namespacePath != null) {
|
||||
path = path.append(new SymbolPath(namespacePath));
|
||||
SymbolPath path = new SymbolPath(parent.getSymbol());
|
||||
if (pathString != null) {
|
||||
path = path.append(new SymbolPath(pathString));
|
||||
}
|
||||
|
||||
List<String> namespaceNames = path.asList();
|
||||
|
||||
List<Namespace> namespaces = doGetNamespaces(namespaceNames, rootNamespace, program);
|
||||
List<Namespace> namespaces = doGetNamespaces(namespaceNames, parent, program);
|
||||
return namespaces;
|
||||
}
|
||||
|
||||
private static List<Namespace> doGetNamespaces(List<String> namespaceNames,
|
||||
Namespace rootNamespace, Program program) {
|
||||
if (rootNamespace == null) {
|
||||
rootNamespace = program.getGlobalNamespace();
|
||||
Namespace root, Program program) {
|
||||
|
||||
if (root == null) {
|
||||
root = program.getGlobalNamespace();
|
||||
}
|
||||
List<Namespace> parents = Arrays.asList(rootNamespace);
|
||||
|
||||
List<Namespace> parents = Arrays.asList(root);
|
||||
for (String name : namespaceNames) {
|
||||
List<Namespace> matches = getMatchingNamespaces(name, parents, program);
|
||||
parents = matches;
|
||||
|
@ -174,19 +194,19 @@ public class NamespaceUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a list all namespaces that have the given name in any of the given namespaces.
|
||||
* Returns a list all namespaces that have the given name in any of the given namespaces
|
||||
*
|
||||
* @param childName the name of the namespaces to retrieve.
|
||||
* @param parents a list of all namespaces to search for child namespaces with the given name.
|
||||
* @param program the program to search.
|
||||
* @return a list all namespaces that have the given name in any of the given namespaces.
|
||||
* @param childName the name of the namespaces to retrieve
|
||||
* @param parents a list of all namespaces to search for child namespaces with the given name
|
||||
* @param program the program to search
|
||||
* @return a list all namespaces that have the given name in any of the given namespaces
|
||||
*/
|
||||
public static List<Namespace> getMatchingNamespaces(String childName, List<Namespace> parents,
|
||||
Program program) {
|
||||
validate(program, parents);
|
||||
List<Namespace> list = new ArrayList<>();
|
||||
for (Namespace parent : parents) {
|
||||
list.addAll(getNamespaces(parent, childName, program));
|
||||
list.addAll(getNamespacesByName(program, parent, childName));
|
||||
}
|
||||
|
||||
return list;
|
||||
|
@ -250,27 +270,6 @@ public class NamespaceUtils {
|
|||
return searchForAllSymbolsInAnyOfTheseNamespaces(parents, symbolPath.getName(), program);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all namespaces with the given name in the parent namespace.
|
||||
* @param parent the parent namespace from which to find all namespaces with the given name.
|
||||
* @param namespaceName the name of the namespaces to retrieve.
|
||||
* @param program the program to search.
|
||||
* @return a list of all namespaces that match the given name in
|
||||
* the given parent namespace.
|
||||
*/
|
||||
public static List<Namespace> getNamespaces(Namespace parent, String namespaceName,
|
||||
Program program) {
|
||||
validate(program, parent);
|
||||
List<Namespace> namespaceList = new ArrayList<>();
|
||||
List<Symbol> symbols = program.getSymbolTable().getSymbols(namespaceName, parent);
|
||||
for (Symbol symbol : symbols) {
|
||||
if (symbol.getSymbolType().isNamespace()) {
|
||||
namespaceList.add((Namespace) symbol.getObject());
|
||||
}
|
||||
}
|
||||
return namespaceList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first namespace with the given name and that is NOT a function that
|
||||
* is within the parent namespace. (ie. the first namespace that is not tied to a program
|
||||
|
@ -331,17 +330,22 @@ public class NamespaceUtils {
|
|||
* and uses the given address to resolve functions with duplicate names. When
|
||||
* resolving down the namespace path, a function that matches a name will only
|
||||
* be used if the given address is contained in the body of that function.
|
||||
* <P>
|
||||
* The root namespace can be a function.
|
||||
*
|
||||
* @param namespacePath The namespace name or path string to be parsed.
|
||||
* This value should not include a trailing symbol name, only namespace names.
|
||||
* <p>The root namespace can be a function.
|
||||
*
|
||||
* <p>If an address is passed, then the path can contain a function name provided the
|
||||
* address is in the body of the function; otherwise the names must all be namespaces other
|
||||
* than functions.
|
||||
*
|
||||
* @param namespacePath The namespace name or path string to be parsed
|
||||
* This value should not include a trailing symbol name, only namespace names
|
||||
* @param rootNamespace The parent namespace under which the desired
|
||||
* namespace or path resides. If this value is null, then the
|
||||
* global namespace will be used.
|
||||
* @param program The current program in which the desired namespace
|
||||
* resides.
|
||||
* @param address the address used to resolve possible functions with duplicate names.
|
||||
* resides
|
||||
* @param address the address used to resolve possible functions with duplicate names; may
|
||||
* be null
|
||||
* @param source the source of the namespace
|
||||
* @return The namespace that matches the given path. This can be either an existing
|
||||
* namespace or a newly created one.
|
||||
|
@ -358,8 +362,8 @@ public class NamespaceUtils {
|
|||
if (namespacePath == null) {
|
||||
return rootNamespace;
|
||||
}
|
||||
SymbolPath path = new SymbolPath(namespacePath);
|
||||
|
||||
SymbolPath path = new SymbolPath(namespacePath);
|
||||
List<String> namespacesList = path.asList();
|
||||
|
||||
SymbolTable symbolTable = program.getSymbolTable();
|
||||
|
@ -382,40 +386,76 @@ public class NamespaceUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Finds the namespace for the given symbolPath. Since this method
|
||||
* takes an address, the symbolPath can contain a function name provided the address
|
||||
* lives in the body of that function.
|
||||
* Returns the existing Function at the given address if its {@link SymbolPath} matches the
|
||||
* given path
|
||||
*
|
||||
* @param program the program from which to get the namespace.
|
||||
* @param symbolPath the path of namespace names including the name of the desired namespace.
|
||||
* @param address the address used to determine if any function namespaces can be used
|
||||
* to resolve the path. For a function to be used, it must contain this address in it's body.
|
||||
* @return the namespace represented by the given path, or null if no such namespace exists.
|
||||
* @param program the program
|
||||
* @param symbolPath the path of namespace
|
||||
* @param address the address
|
||||
* @return the namespace represented by the given path, or null if no such namespace exists
|
||||
*/
|
||||
public static Namespace getNamespace(Program program, SymbolPath symbolPath, Address address) {
|
||||
if (address == null) {
|
||||
if (symbolPath == null) {
|
||||
return program.getGlobalNamespace();
|
||||
}
|
||||
List<Symbol> symbols = getSymbols(symbolPath, program);
|
||||
for (Symbol symbol : symbols) {
|
||||
if (symbol.getSymbolType() != SymbolType.FUNCTION &&
|
||||
symbol.getSymbolType().isNamespace()) {
|
||||
return (Namespace) symbol.getObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (symbolPath == null) {
|
||||
public static Namespace getFunctionNamespaceAt(Program program, SymbolPath symbolPath,
|
||||
Address address) {
|
||||
|
||||
if (symbolPath == null || address == null) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
Symbol[] symbols = program.getSymbolTable().getSymbols(address);
|
||||
for (Symbol symbol : symbols) {
|
||||
symbol.getSymbolType();
|
||||
if (symbol.getSymbolType() == SymbolType.FUNCTION &&
|
||||
symbolPath.equals(new SymbolPath(symbol))) {
|
||||
return (Function) symbol.getObject();
|
||||
}
|
||||
|
||||
Symbol[] symbols = program.getSymbolTable().getSymbols(address);
|
||||
for (Symbol symbol : symbols) {
|
||||
if (symbol.getSymbolType() == SymbolType.FUNCTION &&
|
||||
symbolPath.matchesPathOf(symbol)) {
|
||||
return (Function) symbol.getObject();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the existing Function containing the given address if its
|
||||
* {@link SymbolPath} matches the given path
|
||||
*
|
||||
* @param program the program
|
||||
* @param symbolPath the path of namespace
|
||||
* @param address the address
|
||||
* @return the namespace represented by the given path, or null if no such namespace exists
|
||||
*/
|
||||
public static Namespace getFunctionNamespaceContaining(Program program, SymbolPath symbolPath,
|
||||
Address address) {
|
||||
|
||||
if (symbolPath == null || address == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
FunctionManager fm = program.getFunctionManager();
|
||||
Function f = fm.getFunctionContaining(address);
|
||||
if (f != null) {
|
||||
if (symbolPath.matchesPathOf(f.getSymbol())) {
|
||||
return f;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the namespace for the given symbol path <b>that is not a function</b>
|
||||
*
|
||||
* @param program the program from which to get the namespace
|
||||
* @param symbolPath the path of namespace names including the name of the desired namespace
|
||||
* @return the namespace represented by the given path, or null if no such namespace exists or
|
||||
* the namespace is a function
|
||||
*/
|
||||
public static Namespace getNonFunctionNamespace(Program program, SymbolPath symbolPath) {
|
||||
|
||||
if (symbolPath == null) {
|
||||
return program.getGlobalNamespace();
|
||||
}
|
||||
|
||||
List<Symbol> symbols = getSymbols(symbolPath, program);
|
||||
for (Symbol symbol : symbols) {
|
||||
if (symbol.getSymbolType() != SymbolType.FUNCTION &&
|
||||
symbol.getSymbolType().isNamespace()) {
|
||||
return (Namespace) symbol.getObject();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -483,11 +523,12 @@ public class NamespaceUtils {
|
|||
|
||||
/**
|
||||
* Convert a namespace to a class by copying all namespace children into a newly created class
|
||||
* and then removing the old namespace.
|
||||
* and then removing the old namespace
|
||||
*
|
||||
* @param namespace namespace to be converted
|
||||
* @return new class namespace
|
||||
* @throws InvalidInputException if namespace was contained within a function and can not be
|
||||
* converted to a class
|
||||
* converted to a class
|
||||
*/
|
||||
public static GhidraClass convertNamespaceToClass(Namespace namespace)
|
||||
throws InvalidInputException {
|
||||
|
@ -530,7 +571,7 @@ public class NamespaceUtils {
|
|||
"Namespace contained within Function may not be converted to a class: " + name);
|
||||
}
|
||||
|
||||
// move everything from old namespace into new class namespce
|
||||
// move everything from old namespace into new class namespace
|
||||
try {
|
||||
for (Symbol s : symbolTable.getSymbols(namespace)) {
|
||||
s.setNamespace(classNamespace);
|
||||
|
|
|
@ -20,7 +20,6 @@ import java.util.*;
|
|||
import ghidra.program.model.address.GlobalNamespace;
|
||||
import ghidra.program.model.listing.Library;
|
||||
import ghidra.program.model.symbol.*;
|
||||
import ghidra.util.SystemUtilities;
|
||||
|
||||
/**
|
||||
* A convenience object for parsing a namespace path to a symbol.
|
||||
|
@ -173,7 +172,7 @@ public class SymbolPath implements Comparable<SymbolPath> {
|
|||
*/
|
||||
public String getPath() {
|
||||
if (parentPath != null) {
|
||||
return parentPath.getPath() + Namespace.NAMESPACE_DELIMITER + symbolName;
|
||||
return parentPath.getPath() + Namespace.DELIMITER + symbolName;
|
||||
}
|
||||
return symbolName;
|
||||
}
|
||||
|
@ -190,6 +189,16 @@ public class SymbolPath implements Comparable<SymbolPath> {
|
|||
return new SymbolPath(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this path contains any path entry matching the given text
|
||||
*
|
||||
* @param text the text for which to search
|
||||
* @return true if any path entry matches the given text
|
||||
*/
|
||||
public boolean containsPathEntry(String text) {
|
||||
return asList().contains(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
@ -211,15 +220,25 @@ public class SymbolPath implements Comparable<SymbolPath> {
|
|||
return false;
|
||||
}
|
||||
SymbolPath other = (SymbolPath) obj;
|
||||
if (!SystemUtilities.isEqual(parentPath, other.parentPath)) {
|
||||
if (!Objects.equals(parentPath, other.parentPath)) {
|
||||
return false;
|
||||
}
|
||||
if (!SystemUtilities.isEqual(symbolName, other.symbolName)) {
|
||||
if (!Objects.equals(symbolName, other.symbolName)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience method to check if the given symbol's symbol path matches this path
|
||||
*
|
||||
* @param s the symbol to check
|
||||
* @return true if the symbol paths match
|
||||
*/
|
||||
public boolean matchesPathOf(Symbol s) {
|
||||
return equals(new SymbolPath(s));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of names of the symbols in the symbol path, starting with the name just
|
||||
* below the global namespace.
|
||||
|
|
|
@ -45,7 +45,7 @@ public class SymbolPathParser {
|
|||
throw new IllegalArgumentException(
|
||||
"Symbol list must contain at least one symbol name!");
|
||||
}
|
||||
if (name.indexOf(Namespace.NAMESPACE_DELIMITER) == -1) {
|
||||
if (name.indexOf(Namespace.DELIMITER) == -1) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add(name);
|
||||
return list;
|
||||
|
|
|
@ -383,7 +383,7 @@ public class DataTypeUtilities {
|
|||
public static DataType findNamespaceQualifiedDataType(DataTypeManager dataTypeManager,
|
||||
String dtNameWithNamespace, Class<? extends DataType> classConstraint) {
|
||||
|
||||
String[] splitName = dtNameWithNamespace.split(Namespace.NAMESPACE_DELIMITER);
|
||||
String[] splitName = dtNameWithNamespace.split(Namespace.DELIMITER);
|
||||
String dtName = splitName[splitName.length - 1];
|
||||
|
||||
return findDataType(dataTypeManager, dtName, classConstraint,
|
||||
|
|
|
@ -193,7 +193,7 @@ public class ExternalLocationDB implements ExternalLocation {
|
|||
if (label == null) {
|
||||
setName(getLibrary(), null, SourceType.DEFAULT);
|
||||
}
|
||||
else if (label.indexOf(Namespace.NAMESPACE_DELIMITER) < 0) {
|
||||
else if (label.indexOf(Namespace.DELIMITER) < 0) {
|
||||
// if label does not include namespace keep current namespace
|
||||
setName(symbol.getParentNamespace(), label, source);
|
||||
}
|
||||
|
|
|
@ -213,7 +213,7 @@ public abstract class SymbolDB extends DatabaseObject implements Symbol {
|
|||
Namespace ns = getParentNamespace();
|
||||
if (!(ns instanceof GlobalNamespace)) {
|
||||
String nsPath = ns.getName(true);
|
||||
symName = nsPath + Namespace.NAMESPACE_DELIMITER + symName;
|
||||
symName = nsPath + Namespace.DELIMITER + symName;
|
||||
}
|
||||
}
|
||||
return symName;
|
||||
|
|
|
@ -193,7 +193,7 @@ public class PointerDataType extends BuiltIn implements Pointer {
|
|||
|
||||
String symName = symbol.getName();
|
||||
symName = SymbolUtilities.getCleanSymbolName(symName, ref.getToAddress());
|
||||
symName = symName.replace(Namespace.NAMESPACE_DELIMITER, "_");
|
||||
symName = symName.replace(Namespace.DELIMITER, "_");
|
||||
return POINTER_LABEL_PREFIX + "_" + symName;
|
||||
}
|
||||
|
||||
|
|
|
@ -21,39 +21,51 @@ import ghidra.util.exception.DuplicateNameException;
|
|||
import ghidra.util.exception.InvalidInputException;
|
||||
|
||||
/**
|
||||
* The Namespace interface.
|
||||
* The Namespace interface
|
||||
*/
|
||||
|
||||
public interface Namespace {
|
||||
static final long GLOBAL_NAMESPACE_ID = 0;
|
||||
/**
|
||||
* The delimiter that is used to separate namespace nodes in a namespace
|
||||
* string. For example, "Global::child1::symbolName"
|
||||
*/
|
||||
public static final String DELIMITER = "::";
|
||||
|
||||
/**
|
||||
* Replaced by {@link #DELIMITER}
|
||||
* @deprecated use {@link #DELIMITER}
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String NAMESPACE_DELIMITER = "::";
|
||||
|
||||
/**
|
||||
* Get the symbol for this namespace; Note: The global namespace will return null
|
||||
* @return the symbol for this namespace; Note: The global namespace will return null
|
||||
*/
|
||||
public Symbol getSymbol();
|
||||
|
||||
/**
|
||||
* Returns true if this namespace is external (i.e., associated with a Library)
|
||||
* @return true if this namespace is external (i.e., associated with a Library)
|
||||
*/
|
||||
public boolean isExternal();
|
||||
|
||||
/**
|
||||
* Get the name of the symbol for this scope.
|
||||
* Get the name of the symbol for this scope
|
||||
* @return the name of the symbol for this scope
|
||||
*/
|
||||
public String getName();
|
||||
|
||||
/**
|
||||
* Returns the fully qualified name.
|
||||
* Returns the fully qualified name
|
||||
* @param includeNamespacePath true to include the namespace in the returned name
|
||||
* @return the fully qualified name
|
||||
*/
|
||||
public String getName(boolean includeNamespacePath);
|
||||
|
||||
/**
|
||||
* Return the namespace id;
|
||||
* Return the namespace id
|
||||
* @return the namespace id
|
||||
*/
|
||||
public long getID();
|
||||
|
||||
|
@ -66,6 +78,7 @@ public interface Namespace {
|
|||
/**
|
||||
* Get the address set for this namespace. Note: The body of a namespace (currently
|
||||
* only used by the function namespace) is restricted it Integer.MAX_VALUE.
|
||||
* @return the address set for this namespace
|
||||
*/
|
||||
public AddressSetView getBody();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue