Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6314 Reset label counter for every named section

caheckman4830a7ab9954
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.cc
index d5ca7302c0..66f81e6ecd 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.cc
@@ -3345,6 +3345,18 @@ vector<OpTpl *> *SleighCompile::createCrossBuild(VarnodeTpl *addr,SectionSymbol
return res;
}
+/// \brief Prepare for a new section of p-code templates
+///
+/// Create the ConstructTpl to hold the templates and reset counters.
+/// \return the new ConstructTpl
+ConstructTpl *SleighCompile::enterSection(void)
+
+{
+ ConstructTpl *tpl = new ConstructTpl();
+ pcode.resetLabelCount(); // Macros have their own labels
+ return tpl;
+}
+
/// \brief Create a new Constructor under the given subtable
///
/// Create the object and initialize parsing for the new definition
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.hh
index 4f55bea0b8..d59f9829eb 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/slgh_compile.hh
@@ -466,6 +466,7 @@ public:
SectionVector *nextNamedSection(SectionVector *vec,ConstructTpl *section,SectionSymbol *sym);
SectionVector *finalNamedSection(SectionVector *vec,ConstructTpl *section);
vector<OpTpl *> *createCrossBuild(VarnodeTpl *addr,SectionSymbol *sym);
+ ConstructTpl *enterSection(void);
Constructor *createConstructor(SubtableSymbol *sym);
bool isInRoot(Constructor *ct) const { return (root == ct->getParent()); } ///< Is the Constructor in the root table?
void resetConstructors(void);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.cc
index ec1f76a0e4..741525a324 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.cc
@@ -4,9 +4,9 @@
* 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.
@@ -2856,7 +2856,7 @@ yyreduce:
break;
case 145:
- { (yyval.sem) = new ConstructTpl(); }
+ { (yyval.sem) = slgh->enterSection(); }
break;
case 146:
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.y b/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.y
index fd7f293e7b..cddc09f5cb 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.y
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/slghparse.y
@@ -4,9 +4,9 @@
* 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.
@@ -350,7 +350,7 @@ rtl: rtlmid { $$ = $1; if ($$->getOpvec().empty() && ($$->getResult() == (Handle
| rtlmid EXPORT_KEY STRING { string errmsg="Unknown export varnode: "+*$3; delete $3; slgh->reportError(errmsg); YYERROR; }
| rtlmid EXPORT_KEY sizedstar STRING { string errmsg="Unknown pointer varnode: "+*$4; delete $3; delete $4; slgh->reportError(errmsg); YYERROR; }
;
-rtlmid: /* EMPTY */ { $$ = new ConstructTpl(); }
+rtlmid: /* EMPTY */ { $$ = slgh->enterSection(); }
| rtlmid statement { $$ = $1; if (!$$->addOpList(*$2)) { delete $2; slgh->reportError("Multiple delayslot declarations"); YYERROR; } delete $2; }
| rtlmid LOCAL_KEY STRING ';' { $$ = $1; slgh->pcode.newLocalDefinition($3); }
| rtlmid LOCAL_KEY STRING ':' INTEGER ';' { $$ = $1; slgh->pcode.newLocalDefinition($3,*$5); delete $5; }
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g b/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g
index d254d2bd92..39effb8a3d 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g
+++ b/Ghidra/Framework/SoftwareModeling/src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g
@@ -1023,7 +1023,7 @@ code_block[Location startingPoint] returns [ConstructTpl rtl]
}
scope Block;
@init {
- $Block::ct = new ConstructTpl(startingPoint);
+ $Block::ct = pcode.enterSection(startingPoint);
$code_block::stmtLocation = new Location("<internal error populating statement location>", 0);
}
@after {
@@ -1089,7 +1089,7 @@ statement
pcode.recordNop(s.first);
}
$semantic::containsMultipleSections = true;
- $Block::ct = new ConstructTpl(s.first);
+ $Block::ct = pcode.enterSection(s.first);
}
;
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/PcodeCompile.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/PcodeCompile.java
index 26bc1c172b..17447b9155 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/PcodeCompile.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/PcodeCompile.java
@@ -68,6 +68,8 @@ public abstract class PcodeCompile {
public abstract VectorSTL<OpTpl> createCrossBuild(Location find, VarnodeTpl v,
SectionSymbol second);
+ public abstract ConstructTpl enterSection(Location where);
+
public abstract SectionVector standaloneSection(ConstructTpl c);
public abstract SectionVector firstNamedSection(ConstructTpl main, SectionSymbol sym);
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/SleighCompile.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/SleighCompile.java
index 332f70e6ca..e1ee52bef4 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/SleighCompile.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcodeCPort/slgh_compile/SleighCompile.java
@@ -167,6 +167,11 @@ public class SleighCompile extends SleighBase {
return SleighCompile.this.createCrossBuild(find, v, section);
}
+ @Override
+ public ConstructTpl enterSection(Location where) {
+ return SleighCompile.this.enterSection(where);
+ }
+
@Override
public SectionVector standaloneSection(ConstructTpl c) {
return SleighCompile.this.standaloneSection(c);
@@ -333,6 +338,12 @@ public class SleighCompile extends SleighBase {
return res;
}
+ protected ConstructTpl enterSection(Location where) {
+ entry("enterSection", where);
+ pcode.resetLabelCount();
+ return new ConstructTpl(where);
+ }
+
protected SectionVector standaloneSection(ConstructTpl main) {
entry("standaloneSection", main);
// Create SectionVector for just the main rtl section with no named sections
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PcodeParser.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PcodeParser.java
index 8166b87ef8..0d7c85a56f 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PcodeParser.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PcodeParser.java
@@ -4,9 +4,9 @@
* 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.
@@ -479,6 +479,12 @@ public class PcodeParser extends PcodeCompile {
throw new SleighError("Pcode snippet parsing does not support use of sections", where);
}
+ @Override
+ public ghidra.pcodeCPort.semantics.ConstructTpl enterSection(Location where) {
+ resetLabelCount();
+ return new ghidra.pcodeCPort.semantics.ConstructTpl(where);
+ }
+
@Override
public SectionVector standaloneSection(ghidra.pcodeCPort.semantics.ConstructTpl main) {
// Create SectionVector for just the main rtl section with no named sections