Skip to content

Commit 14126c2

Browse files
committed
Testing miscellaneous Objective-C fixes
* #7077 * #7083 * #7084
3 parents a900f01 + 88b70de + b226f2a commit 14126c2

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

objectivec/objc.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ namespace {
106106
return component;
107107
}
108108

109+
Ref<Type> NamedType(const std::string& name)
110+
{
111+
NamedTypeReferenceBuilder builder;
112+
builder.SetName(QualifiedName(name));
113+
return Type::NamedType(builder.Finalize());
114+
}
115+
109116
} // namespace
110117

111118
Ref<Metadata> ObjCProcessor::SerializeMethod(uint64_t loc, const Method& method)
@@ -320,12 +327,12 @@ std::vector<QualifiedNameOrType> ObjCProcessor::ParseEncodedType(const std::stri
320327
nameOrType.type = Type::PointerType(m_data->GetAddressSize(), Type::IntegerType(1, true));
321328
break;
322329
case '@':
323-
qualifiedName = "id";
330+
nameOrType.type = m_types.id;
324331
// There can be a type after this, like @"NSString", that overrides this
325332
// The handler for " will catch it and drop this "id" entry.
326333
break;
327334
case ':':
328-
qualifiedName = "SEL";
335+
nameOrType.type = m_types.sel;
329336
break;
330337
case '#':
331338
qualifiedName = "objc_class_t";
@@ -1214,11 +1221,11 @@ bool ObjCProcessor::ApplyMethodType(Class& cls, Method& method, bool isInstanceM
12141221

12151222
params.push_back({"self",
12161223
cls.associatedName.IsEmpty() ?
1217-
Type::NamedType(m_data, {"id"}) :
1224+
m_types.id :
12181225
Type::PointerType(m_data->GetAddressSize(), Type::NamedType(m_data, cls.associatedName)),
12191226
true, BinaryNinja::Variable()});
12201227

1221-
params.push_back({"sel", Type::NamedType(m_data, {"SEL"}), true, BinaryNinja::Variable()});
1228+
params.push_back({"sel", m_types.sel, true, BinaryNinja::Variable()});
12221229

12231230
for (size_t i = 3; i < typeTokens.size(); i++)
12241231
{
@@ -1342,10 +1349,13 @@ void ObjCProcessor::PostProcessObjCSections(ObjCReader* reader)
13421349
{
13431350
auto start = ivars->GetStart();
13441351
auto end = ivars->GetEnd();
1345-
auto ivarSectionEntryTypeBuilder = new TypeBuilder(Type::IntegerType(8, false));
1352+
// The ivar section contains entries of type `long` for for all architectures
1353+
// except arm64, which uses `int` for the ivar offset.
1354+
size_t ivarOffsetSize = m_data->GetDefaultArchitecture()->GetName() == "aarch64" ? 4 : ptrSize;
1355+
auto ivarSectionEntryTypeBuilder = new TypeBuilder(Type::IntegerType(ivarOffsetSize, false));
13461356
ivarSectionEntryTypeBuilder->SetConst(true);
13471357
auto type = ivarSectionEntryTypeBuilder->Finalize();
1348-
for (view_ptr_t i = start; i < end; i += ptrSize)
1358+
for (view_ptr_t i = start; i < end; i += ivarOffsetSize)
13491359
{
13501360
m_data->DefineDataVariable(i, type);
13511361
}
@@ -1367,6 +1377,10 @@ ObjCProcessor::ObjCProcessor(BinaryView* data, const char* loggerName, bool skip
13671377
m_skipClassBaseProtocols(skipClassBaseProtocols), m_data(data)
13681378
{
13691379
m_logger = m_data->CreateLogger(loggerName);
1380+
1381+
m_types.id = NamedType("id");
1382+
m_types.sel = NamedType("SEL");
1383+
m_types.BOOL = NamedType("BOOL");
13701384
}
13711385

13721386
uint64_t ObjCProcessor::GetObjCRelativeMethodBaseAddress(ObjCReader* reader)
@@ -1384,11 +1398,6 @@ void ObjCProcessor::ProcessObjCData()
13841398
auto guard = ScopedSymbolQueue::Make();
13851399

13861400
auto addrSize = m_data->GetAddressSize();
1387-
1388-
m_typeNames.id = defineTypedef(m_data, {"id"}, Type::PointerType(addrSize, Type::VoidType()));
1389-
m_typeNames.sel = defineTypedef(m_data, {"SEL"}, Type::PointerType(addrSize, Type::IntegerType(1, false)));
1390-
1391-
m_typeNames.BOOL = defineTypedef(m_data, {"BOOL"}, Type::IntegerType(1, false));
13921401
m_typeNames.nsInteger = defineTypedef(m_data, {"NSInteger"}, Type::IntegerType(addrSize, true));
13931402
m_typeNames.nsuInteger = defineTypedef(m_data, {"NSUInteger"}, Type::IntegerType(addrSize, false));
13941403
m_typeNames.cgFloat = defineTypedef(m_data, {"CGFloat"}, Type::FloatType(addrSize));
@@ -1716,11 +1725,10 @@ void ObjCProcessor::ProcessNSConstantArrays()
17161725
auto guard = ScopedSymbolQueue::Make();
17171726
uint64_t ptrSize = m_data->GetAddressSize();
17181727

1719-
auto idType = Type::NamedType(m_data, m_typeNames.id);
17201728
StructureBuilder nsConstantArrayBuilder;
17211729
nsConstantArrayBuilder.AddMember(Type::PointerType(ptrSize, Type::VoidType()), "isa");
17221730
nsConstantArrayBuilder.AddMember(Type::IntegerType(ptrSize, false), "count");
1723-
nsConstantArrayBuilder.AddMember(Type::PointerType(ptrSize, idType), "objects");
1731+
nsConstantArrayBuilder.AddMember(Type::PointerType(ptrSize, m_types.id), "objects");
17241732
auto type = finalizeStructureBuilder(m_data, nsConstantArrayBuilder, "__NSConstantArray");
17251733
m_typeNames.nsConstantArray = type.first;
17261734

@@ -1737,7 +1745,7 @@ void ObjCProcessor::ProcessNSConstantArrays()
17371745
uint64_t count = reader->ReadPointer();
17381746
auto dataLoc = ReadPointerAccountingForRelocations(reader.get());
17391747
DefineObjCSymbol(
1740-
DataSymbol, Type::ArrayType(idType, count), fmt::format("nsarray_{:x}_data", i), dataLoc, true);
1748+
DataSymbol, Type::ArrayType(m_types.id, count), fmt::format("nsarray_{:x}_data", i), dataLoc, true);
17411749
DefineObjCSymbol(DataSymbol, Type::NamedType(m_data, m_typeNames.nsConstantArray),
17421750
fmt::format("nsarray_{:x}", i), i, true);
17431751
}
@@ -1754,13 +1762,12 @@ void ObjCProcessor::ProcessNSConstantDictionaries()
17541762
auto guard = ScopedSymbolQueue::Make();
17551763
uint64_t ptrSize = m_data->GetAddressSize();
17561764

1757-
auto idType = Type::NamedType(m_data, m_typeNames.id);
17581765
StructureBuilder nsConstantDictionaryBuilder;
17591766
nsConstantDictionaryBuilder.AddMember(Type::PointerType(ptrSize, Type::VoidType()), "isa");
17601767
nsConstantDictionaryBuilder.AddMember(Type::IntegerType(ptrSize, false), "options");
17611768
nsConstantDictionaryBuilder.AddMember(Type::IntegerType(ptrSize, false), "count");
1762-
nsConstantDictionaryBuilder.AddMember(Type::PointerType(ptrSize, idType), "keys");
1763-
nsConstantDictionaryBuilder.AddMember(Type::PointerType(ptrSize, idType), "objects");
1769+
nsConstantDictionaryBuilder.AddMember(Type::PointerType(ptrSize, m_types.id), "keys");
1770+
nsConstantDictionaryBuilder.AddMember(Type::PointerType(ptrSize, m_types.id), "objects");
17641771
auto type = finalizeStructureBuilder(m_data, nsConstantDictionaryBuilder, "__NSConstantDictionary");
17651772
m_typeNames.nsConstantDictionary = type.first;
17661773

@@ -1779,9 +1786,9 @@ void ObjCProcessor::ProcessNSConstantDictionaries()
17791786
auto keysLoc = ReadPointerAccountingForRelocations(reader.get());
17801787
auto objectsLoc = ReadPointerAccountingForRelocations(reader.get());
17811788
DefineObjCSymbol(
1782-
DataSymbol, Type::ArrayType(idType, count), fmt::format("nsdict_{:x}_keys", i), keysLoc, true);
1783-
DefineObjCSymbol(
1784-
DataSymbol, Type::ArrayType(idType, count), fmt::format("nsdict_{:x}_objects", i), objectsLoc, true);
1789+
DataSymbol, Type::ArrayType(m_types.id, count), fmt::format("nsdict_{:x}_keys", i), keysLoc, true);
1790+
DefineObjCSymbol(DataSymbol, Type::ArrayType(m_types.id, count), fmt::format("nsdict_{:x}_objects", i),
1791+
objectsLoc, true);
17851792
DefineObjCSymbol(DataSymbol, Type::NamedType(m_data, m_typeNames.nsConstantDictionary),
17861793
fmt::format("nsdict_{:x}", i), i, true);
17871794
}

objectivec/objc.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,12 @@ namespace BinaryNinja {
252252

253253
class ObjCProcessor {
254254
struct Types {
255-
QualifiedName id;
256-
QualifiedName sel;
257-
QualifiedName BOOL;
255+
Ref<Type> id;
256+
Ref<Type> sel;
257+
Ref<Type> BOOL;
258+
} m_types;
259+
260+
struct TypeNames {
258261
QualifiedName nsInteger;
259262
QualifiedName nsuInteger;
260263
QualifiedName cgFloat;

0 commit comments

Comments
 (0)