1 //===- ObjC.cpp -----------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
11 #include "InputSection.h"
12 #include "OutputSegment.h"
15 #include "llvm/BinaryFormat/MachO.h"
16 #include "llvm/Bitcode/BitcodeReader.h"
19 using namespace llvm::MachO
;
21 using namespace lld::macho
;
23 template <class LP
> static bool objectHasObjCSection(MemoryBufferRef mb
) {
24 using SectionHeader
= typename
LP::section
;
27 reinterpret_cast<const typename
LP::mach_header
*>(mb
.getBufferStart());
28 if (hdr
->magic
!= LP::magic
)
32 findCommand
<typename
LP::segment_command
>(hdr
, LP::segmentLCType
)) {
33 auto sectionHeaders
= ArrayRef
<SectionHeader
>{
34 reinterpret_cast<const SectionHeader
*>(c
+ 1), c
->nsects
};
35 for (const SectionHeader
&secHead
: sectionHeaders
) {
36 StringRef
sectname(secHead
.sectname
,
37 strnlen(secHead
.sectname
, sizeof(secHead
.sectname
)));
38 StringRef
segname(secHead
.segname
,
39 strnlen(secHead
.segname
, sizeof(secHead
.segname
)));
40 if ((segname
== segment_names::data
&&
41 sectname
== section_names::objcCatList
) ||
42 (segname
== segment_names::text
&&
43 sectname
.startswith(section_names::swift
))) {
51 static bool objectHasObjCSection(MemoryBufferRef mb
) {
52 if (target
->wordSize
== 8)
53 return ::objectHasObjCSection
<LP64
>(mb
);
55 return ::objectHasObjCSection
<ILP32
>(mb
);
58 bool macho::hasObjCSection(MemoryBufferRef mb
) {
59 switch (identify_magic(mb
.getBuffer())) {
60 case file_magic::macho_object
:
61 return objectHasObjCSection(mb
);
62 case file_magic::bitcode
:
63 return check(isBitcodeContainingObjCCategory(mb
));