aboutsummaryrefslogtreecommitdiff
path: root/lld/MachO/ObjC.cpp
blob: 7ed800827f3e1aaa6f6ee117e285699831f94476 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//===- ObjC.cpp -----------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ObjC.h"
#include "InputFiles.h"
#include "InputSection.h"
#include "OutputSegment.h"
#include "Target.h"

#include "llvm/BinaryFormat/MachO.h"

using namespace llvm;
using namespace llvm::MachO;
using namespace lld;
using namespace lld::macho;

template <class LP> static bool hasObjCSection(MemoryBufferRef mb) {
  using Section = typename LP::section;

  auto *hdr =
      reinterpret_cast<const typename LP::mach_header *>(mb.getBufferStart());
  if (hdr->magic != LP::magic)
    return false;

  if (const auto *c =
          findCommand<typename LP::segment_command>(hdr, LP::segmentLCType)) {
    auto sectionHeaders =
        ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
    for (const Section &sec : sectionHeaders) {
      StringRef sectname(sec.sectname,
                         strnlen(sec.sectname, sizeof(sec.sectname)));
      StringRef segname(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
      if ((segname == segment_names::data &&
           sectname == section_names::objcCatList) ||
          (segname == segment_names::text &&
           sectname == section_names::swift)) {
        return true;
      }
    }
  }
  return false;
}

bool macho::hasObjCSection(MemoryBufferRef mb) {
  if (target->wordSize == 8)
    return ::hasObjCSection<LP64>(mb);
  else
    return ::hasObjCSection<ILP32>(mb);
}