xref: /llvm-project-15.0.7/lld/MachO/ObjC.cpp (revision 1206b95e)
1 //===- ObjC.cpp -----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ObjC.h"
10 #include "InputFiles.h"
11 #include "OutputSegment.h"
12 #include "Target.h"
13 
14 #include "llvm/BinaryFormat/MachO.h"
15 
16 using namespace llvm;
17 using namespace llvm::MachO;
18 using namespace lld;
19 using namespace lld::macho;
20 
21 template <class LP> static bool hasObjCSection(MemoryBufferRef mb) {
22   using Section = typename LP::section;
23 
24   auto *hdr =
25       reinterpret_cast<const typename LP::mach_header *>(mb.getBufferStart());
26   if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
27     auto *c = reinterpret_cast<const typename LP::segment_command *>(cmd);
28     auto sectionHeaders =
29         ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
30     for (const Section &sec : sectionHeaders) {
31       StringRef sectname(sec.sectname,
32                          strnlen(sec.sectname, sizeof(sec.sectname)));
33       StringRef segname(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
34       if ((segname == segment_names::data && sectname == "__objc_catlist") ||
35           (segname == segment_names::text && sectname == "__swift")) {
36         return true;
37       }
38     }
39   }
40   return false;
41 }
42 
43 bool macho::hasObjCSection(MemoryBufferRef mb) {
44   if (target->wordSize == 8)
45     return ::hasObjCSection<LP64>(mb);
46   else
47     return ::hasObjCSection<ILP32>(mb);
48 }
49