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

Fixes and improvements to field parser #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import ghidra.app.services.AnalyzerType
import ghidra.app.util.importer.MessageLog
import ghidra.program.model.address.AddressSetView
import ghidra.program.model.address.GenericAddress
import ghidra.program.model.data.DataType
import ghidra.program.model.data.Structure
import ghidra.program.model.data.*
import ghidra.program.model.listing.Data
import ghidra.program.model.listing.Program
import ghidra.program.model.scalar.Scalar
Expand Down Expand Up @@ -59,21 +58,38 @@ class OCClassFieldAnalyzer : AbstractAnalyzer(NAME, DESCRIPTION, AnalyzerType.DA

program.withTransaction<Exception>("Apply fields.") {
for (it in fieldLists) {
monitor.checkCancelled()
val definedClassStruct = typeResolver.tryResolveDefinedStruct(it.classSymbol.name) as Structure?
if (definedClassStruct == null) {
log.appendMsg("Couldn't find defined structure for ${it.classSymbol.name} ivar list.")
continue
}

it.ivars.forEach { field ->
val fieldType = field.type.let {
typeResolver.parseEncoded(it) ?: DataType.DEFAULT
}

println("${field.name}: ${fieldType.name}")

val fieldSize = field.size.toInt()


val fieldType = field.type.let { typeString ->
val resolvedType = typeResolver.parseEncoded(typeString)

if (resolvedType == null ){
Undefined.getUndefinedDataType(fieldSize)
}
// There is some bug where a field is typed as int with size 4, but Ghidra later
// treats an int as size 8, and then fails to decompile functions using this field
// to fix this we change them to short and unsigned short respectively.
else if (resolvedType.name == "int" && fieldSize == 4) {
ShortDataType.dataType
}
else if (resolvedType.name == "uint" && fieldSize == 8) {
UnsignedShortDataType.dataType
}
else {
resolvedType
}
}

definedClassStruct.insertAtOffset(field.offset, fieldType, fieldSize, field.name, null)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ class OCRetypeRecvAnalyzer : AbstractAnalyzer(NAME, DESCRIPTION, AnalyzerType.FU

program.withTransaction<Exception>("Apply receiver types to class methods.") {
classMethods.forEach { (typedef, methods) ->
println("CLASS ${typedef.name}")

for (method in methods) {
println(" METHOD ${method.name}")

if (method.parameterCount == 0) {
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class TypeResolver(val program: Program) {
// this will be more robust in the future.
// reference: https://nshipster.com/type-encodings/

println("input: $encodedType")
if (encodedType.isEmpty())
return null

when (encodedType[0]) {
'@' -> {
if (encodedType.length == 1)
if (encodedType.length == 1 || encodedType == "@?")
return dtm.getDataType("/_objc2_/ID")

// expect a defined type structure
return tryResolveTypedef(encodedType.substring(2, encodedType.length - 1))
}
Expand Down