xref: /llvm-project-15.0.7/lld/MachO/ObjC.cpp (revision 2d28100b)
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 "InputSection.h"
12 #include "OutputSegment.h"
13 #include "Target.h"
14 
15 #include "llvm/BinaryFormat/MachO.h"
16 
17 using namespace llvm;
18 using namespace llvm::MachO;
19 using namespace lld;
20 using namespace lld::macho;
21 
22 template <class LP> static bool hasObjCSection(MemoryBufferRef mb) {
23   using Section = typename LP::section;
24 
25   auto *hdr =
26       reinterpret_cast<const typename LP::mach_header *>(mb.getBufferStart());
27   if (const auto *c =
28           findCommand<typename LP::segment_command>(hdr, LP::segmentLCType)) {
29     auto sectionHeaders =
30         ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
31     for (const Section &sec : sectionHeaders) {
32       StringRef sectname(sec.sectname,
33                          strnlen(sec.sectname, sizeof(sec.sectname)));
34       StringRef segname(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
35       if ((segname == segment_names::data &&
36            sectname == section_names::objcCatList) ||
37           (segname == segment_names::text &&
38            sectname == section_names::swift)) {
39         return true;
40       }
41     }
42   }
43   return false;
44 }
45 
46 bool macho::hasObjCSection(MemoryBufferRef mb) {
47   if (target->wordSize == 8)
48     return ::hasObjCSection<LP64>(mb);
49   else
50     return ::hasObjCSection<ILP32>(mb);
51 }
52