Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CParser: Detect packed enums #6780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/C/C.jj
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,13 @@ public class CParser {
}


private DataType allocateEnumDT(Token t, ArrayList<EnumMember> list) {
private DataType allocateEnumDT(Token t, ArrayList<EnumMember> list, Declaration dec) {
String enumName = (t != null ? t.image : ("enum_" + cnt++));

// get the normal enum size, which is an int
// TODO: allow for packing of enum to smallest value with either dataOrganization, or packing flag
int normalEnumLen = (dtMgr != null ? dtMgr.getDataOrganization().getIntegerSize() : 4);

// create an initial enum and add all new members
EnumDataType enumDT= new EnumDataType(getCurrentCategoryPath(), enumName, 8, dtMgr);

// Create an initial enum
EnumDataType enumDT = new EnumDataType(getCurrentCategoryPath(), enumName, 8, dtMgr);

// Add all new enum members
if (list != null) {
for (EnumMember member : list) {
try {
Expand All @@ -533,18 +531,21 @@ public class CParser {
addNearParseMessage("duplicate enum value: " + enumName + " : " + member.name + " : " + member.value);
}
}
// get the minimum length to represent the values and resize if too big
int minLen = enumDT.getMinimumPossibleLength();
if (minLen > normalEnumLen) {
enumDT.setLength(minLen);
} else {
enumDT.setLength(normalEnumLen);
}
}

int defaultEnumLen = 0;
if (dec.getQualifiers().contains(PACKED)) {
// Get the default packed enum length
defaultEnumLen = (dtMgr != null ? dtMgr.getDataOrganization().getCharSize() : 1);
} else {
// length doesn't really matter, forward declaration with no values
enumDT.setLength(normalEnumLen);
// Get the default enum size, which is an int
defaultEnumLen = (dtMgr != null ? dtMgr.getDataOrganization().getIntegerSize() : 4);
}


// If values in the enum require a bigger size, expand the enum...
int enumLen = Math.max(defaultEnumLen, enumDT.getMinimumPossibleLength());
enumDT.setLength(enumLen);

return addDef(enums, enumDT.getName(), enumDT);
}

Expand Down Expand Up @@ -1186,7 +1187,7 @@ TOKEN :
|
<UNALIGNED : "__unaligned" >
|
<PACKED : "__packed" >
<PACKED : ( [ "_" ] )* "packed" >
|
<ATTRIBUTE : "__attribute" (["_"])* >
|
Expand Down Expand Up @@ -1807,7 +1808,7 @@ Declaration TypeQualifier(Declaration dec) : {}
<RESTRICT> |
<EXTENSION> |
<STATIC> |
<PACKED> |
<PACKED> { dec.addQualifier(PACKED); } |
<UNALIGNED> |
( DeclSpec(dec) )
)
Expand Down Expand Up @@ -2235,15 +2236,15 @@ DataType EnumSpecifier() : {
LOOKAHEAD(3)
[AttributeSpecList(dec)] [ t= <IDENTIFIER> ] "{" list= EnumeratorList() "}"
{
dt = allocateEnumDT(t, list);
dt = allocateEnumDT(t, list, dec);
}
|
t= <IDENTIFIER>
{
dt= getEnumDef(t.image);
// not defined yet, define an empty one
if (dt == null) {
dt = allocateEnumDT(t, null);
dt = allocateEnumDT(t, null, dec);
}
}
)
Expand Down