xref: /freebsd/contrib/llvm-project/lld/MachO/ObjC.cpp (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
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 
13 #include "llvm/BinaryFormat/MachO.h"
14 
15 using namespace llvm;
16 using namespace llvm::MachO;
17 using namespace lld;
18 
19 bool macho::hasObjCSection(MemoryBufferRef mb) {
20   auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
21   if (const load_command *cmd = findCommand(hdr, LC_SEGMENT_64)) {
22     auto *c = reinterpret_cast<const segment_command_64 *>(cmd);
23     auto sectionHeaders = ArrayRef<section_64>{
24         reinterpret_cast<const section_64 *>(c + 1), c->nsects};
25     for (const section_64 &sec : sectionHeaders) {
26       StringRef sectname(sec.sectname,
27                          strnlen(sec.sectname, sizeof(sec.sectname)));
28       StringRef segname(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
29       if ((segname == segment_names::data && sectname == "__objc_catlist") ||
30           (segname == segment_names::text && sectname == "__swift")) {
31         return true;
32       }
33     }
34   }
35   return false;
36 }
37