annotate lld/MachO/ObjC.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- ObjC.cpp -----------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "ObjC.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "InputFiles.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "InputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "OutputSegment.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "llvm/BinaryFormat/MachO.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 using namespace llvm;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 using namespace llvm::MachO;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 using namespace lld::macho;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 template <class LP> static bool hasObjCSection(MemoryBufferRef mb) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 using Section = typename LP::section;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 auto *hdr =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 reinterpret_cast<const typename LP::mach_header *>(mb.getBufferStart());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 if (hdr->magic != LP::magic)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 if (const auto *c =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 findCommand<typename LP::segment_command>(hdr, LP::segmentLCType)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 auto sectionHeaders =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 for (const Section &sec : sectionHeaders) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 StringRef sectname(sec.sectname,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 strnlen(sec.sectname, sizeof(sec.sectname)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 StringRef segname(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 if ((segname == segment_names::data &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 sectname == section_names::objcCatList) ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 (segname == segment_names::text &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 sectname == section_names::swift)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 return true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 bool macho::hasObjCSection(MemoryBufferRef mb) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 if (target->wordSize == 8)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 return ::hasObjCSection<LP64>(mb);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 return ::hasObjCSection<ILP32>(mb);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 }