Skip to content

Commit

Permalink
feat: option for local symbol visibility if name matches regular expr…
Browse files Browse the repository at this point in the history
…ession
  • Loading branch information
boricj committed Aug 22, 2024
1 parent 6ce0489 commit 648b4e0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import ghidra.app.util.importer.MessageLog;
import ghidra.app.util.visibility.IsSymbolDynamic;
import ghidra.app.util.visibility.IsSymbolInsideFunction;
import ghidra.app.util.visibility.IsSymbolNameMatchingRegex;
import ghidra.framework.model.DomainObject;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSetView;
Expand All @@ -70,6 +71,7 @@ public class CoffRelocatableObjectExporter extends Exporter {
private SymbolPreference symbolNamePreference;
private boolean isDynamicSymbolStatic;
private boolean isSymbolInsideFunctionStatic;
private String patternSymbolNameStatic;

private RelocationTable relocationTable;
private Predicate<Relocation> predicateRelocation;
Expand All @@ -89,6 +91,7 @@ public class CoffRelocatableObjectExporter extends Exporter {
private static final String OPTION_VIS_DYNAMIC = "Give dynamic symbols static visibility";
private static final String OPTION_VIS_INSIDE_FUNCTIONS =
"Give symbols inside functions static visibility";
private static final String OPTION_VIS_PATTERN = "Regular expression for static symbol names";

private static final Map<Short, String> COFF_MACHINES = new TreeMap<>(Map.ofEntries(
Map.entry(CoffMachineType.IMAGE_FILE_MACHINE_UNKNOWN, "(none)"),
Expand Down Expand Up @@ -191,6 +194,8 @@ Short.class, autodetectCoffMachine(program)),
SymbolPreference.class, DEFAULT_SYMBOL_PREFERENCE),
new Option(OPTION_GROUP_SYMBOL_VISIBILITY, OPTION_VIS_DYNAMIC, true),
new Option(OPTION_GROUP_SYMBOL_VISIBILITY, OPTION_VIS_INSIDE_FUNCTIONS, true),
new Option(OPTION_GROUP_SYMBOL_VISIBILITY, OPTION_VIS_PATTERN,
IsSymbolNameMatchingRegex.DEFAULT_PATTERN),
};

return Arrays.asList(options);
Expand All @@ -205,6 +210,8 @@ public void setOptions(List<Option> options) {
isDynamicSymbolStatic = OptionUtils.getOption(OPTION_VIS_DYNAMIC, options, true);
isSymbolInsideFunctionStatic =
OptionUtils.getOption(OPTION_VIS_INSIDE_FUNCTIONS, options, true);
patternSymbolNameStatic = OptionUtils.getOption(OPTION_VIS_PATTERN, options,
IsSymbolNameMatchingRegex.DEFAULT_PATTERN);
}

private class Section {
Expand Down Expand Up @@ -341,6 +348,11 @@ private void initializeSymbolVisibilityPredicate() {
Predicate<Symbol> predicate = new IsSymbolInsideFunction();
predicateVisibility = predicateVisibility.or(predicate);
}

if (!patternSymbolNameStatic.isBlank()) {
Predicate<Symbol> predicate = new IsSymbolNameMatchingRegex(patternSymbolNameStatic);
predicateVisibility = predicateVisibility.or(predicate);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import ghidra.app.util.importer.MessageLog;
import ghidra.app.util.visibility.IsSymbolDynamic;
import ghidra.app.util.visibility.IsSymbolInsideFunction;
import ghidra.app.util.visibility.IsSymbolNameMatchingRegex;
import ghidra.framework.model.DomainObject;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSetView;
Expand Down Expand Up @@ -81,6 +82,7 @@ public class ElfRelocatableObjectExporter extends Exporter {
private SymbolPreference symbolNamePreference;
private boolean isDynamicSymbolLocal;
private boolean isSymbolInsideFunctionLocal;
private String patternSymbolNameLocal;
private boolean generateRelocationTables;
private int relocationTableFormat;

Expand Down Expand Up @@ -117,6 +119,7 @@ public class ElfRelocatableObjectExporter extends Exporter {
private static final String OPTION_VIS_DYNAMIC = "Give dynamic symbols local visibility";
private static final String OPTION_VIS_INSIDE_FUNCTIONS =
"Give symbols inside functions local visibility";
private static final String OPTION_VIS_PATTERN = "Regular expression for local symbol names";
private static final String OPTION_GEN_REL = "Generate relocation tables";
private static final String OPTION_REL_FMT = "Relocation table format";

Expand Down Expand Up @@ -310,6 +313,8 @@ Byte.class, autodetectElfData(program)),
new Option(OPTION_GROUP_SYMBOLS, OPTION_GEN_STRTAB, true),
new Option(OPTION_GROUP_SYMBOL_VISIBILITY, OPTION_VIS_DYNAMIC, true),
new Option(OPTION_GROUP_SYMBOL_VISIBILITY, OPTION_VIS_INSIDE_FUNCTIONS, true),
new Option(OPTION_GROUP_SYMBOL_VISIBILITY, OPTION_VIS_PATTERN,
IsSymbolNameMatchingRegex.DEFAULT_PATTERN),
new EnumDropDownOption<>(OPTION_GROUP_SYMBOLS, OPTION_PREF_SYMNAME,
SymbolPreference.class, DEFAULT_SYMBOL_PREFERENCE),
new Option(OPTION_GROUP_RELOCATIONS, OPTION_GEN_REL, true),
Expand All @@ -334,6 +339,8 @@ public void setOptions(List<Option> options) {
isDynamicSymbolLocal = OptionUtils.getOption(OPTION_VIS_DYNAMIC, options, true);
isSymbolInsideFunctionLocal =
OptionUtils.getOption(OPTION_VIS_INSIDE_FUNCTIONS, options, true);
patternSymbolNameLocal = OptionUtils.getOption(OPTION_VIS_PATTERN, options,
IsSymbolNameMatchingRegex.DEFAULT_PATTERN);
symbolNamePreference =
OptionUtils.getOption(OPTION_PREF_SYMNAME, options, DEFAULT_SYMBOL_PREFERENCE);
generateRelocationTables = OptionUtils.getOption(OPTION_GEN_REL, options, false);
Expand Down Expand Up @@ -491,6 +498,11 @@ private void initializeSymbolVisibilityPredicate() {
Predicate<Symbol> predicate = new IsSymbolInsideFunction();
predicateVisibility = predicateVisibility.or(predicate);
}

if (!patternSymbolNameLocal.isBlank()) {
Predicate<Symbol> predicate = new IsSymbolNameMatchingRegex(patternSymbolNameLocal);
predicateVisibility = predicateVisibility.or(predicate);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.app.util.visibility;

import java.util.function.Predicate;
import java.util.regex.Pattern;

import ghidra.program.model.symbol.Symbol;

public class IsSymbolNameMatchingRegex implements Predicate<Symbol> {
public static final String DEFAULT_PATTERN = "^switchD_.+::switchdataD_.+$";;

private final Pattern pattern;

public IsSymbolNameMatchingRegex(String regex) {
this.pattern = Pattern.compile(regex);
}

@Override
public boolean test(Symbol symbol) {
return pattern.matcher(symbol.getName(true)).matches();
}
}

0 comments on commit 648b4e0

Please sign in to comment.