1 //===- InputSection.h -------------------------------------------*- C++ -*-===//
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 #ifndef LLD_MACHO_INPUT_SECTION_H
10 #define LLD_MACHO_INPUT_SECTION_H
11 
12 #include "Relocations.h"
13 
14 #include "lld/Common/LLVM.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 
18 namespace lld {
19 namespace macho {
20 
21 class InputFile;
22 class InputSection;
23 class OutputSection;
24 class Symbol;
25 class Defined;
26 
27 class InputSection {
28 public:
29   virtual ~InputSection() = default;
30   virtual uint64_t getSize() const { return data.size(); }
31   virtual uint64_t getFileSize() const;
32   uint64_t getFileOffset() const;
33   uint64_t getVA() const;
34 
35   virtual void writeTo(uint8_t *buf);
36 
37   InputFile *file = nullptr;
38   StringRef name;
39   StringRef segname;
40 
41   OutputSection *parent = nullptr;
42   uint64_t outSecOff = 0;
43   uint64_t outSecFileOff = 0;
44 
45   uint32_t align = 1;
46   uint32_t flags = 0;
47 
48   ArrayRef<uint8_t> data;
49   std::vector<Reloc> relocs;
50 };
51 
52 inline uint8_t sectionType(uint32_t flags) {
53   return flags & llvm::MachO::SECTION_TYPE;
54 }
55 
56 inline bool isZeroFill(uint32_t flags) {
57   return llvm::MachO::isVirtualSection(sectionType(flags));
58 }
59 
60 inline bool isThreadLocalVariables(uint32_t flags) {
61   return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_VARIABLES;
62 }
63 
64 // These sections contain the data for initializing thread-local variables.
65 inline bool isThreadLocalData(uint32_t flags) {
66   return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_REGULAR ||
67          sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_ZEROFILL;
68 }
69 
70 inline bool isDebugSection(uint32_t flags) {
71   return (flags & llvm::MachO::SECTION_ATTRIBUTES_USR) ==
72          llvm::MachO::S_ATTR_DEBUG;
73 }
74 
75 bool isCodeSection(InputSection *);
76 
77 extern std::vector<InputSection *> inputSections;
78 
79 namespace section_names {
80 
81 constexpr const char pageZero[] = "__pagezero";
82 constexpr const char common[] = "__common";
83 constexpr const char header[] = "__mach_header";
84 constexpr const char rebase[] = "__rebase";
85 constexpr const char binding[] = "__binding";
86 constexpr const char weakBinding[] = "__weak_binding";
87 constexpr const char lazyBinding[] = "__lazy_binding";
88 constexpr const char export_[] = "__export";
89 constexpr const char functionStarts[] = "__func_starts";
90 constexpr const char symbolTable[] = "__symbol_table";
91 constexpr const char indirectSymbolTable[] = "__ind_sym_tab";
92 constexpr const char stringTable[] = "__string_table";
93 constexpr const char codeSignature[] = "__code_signature";
94 constexpr const char got[] = "__got";
95 constexpr const char threadPtrs[] = "__thread_ptrs";
96 constexpr const char unwindInfo[] = "__unwind_info";
97 constexpr const char compactUnwind[] = "__compact_unwind";
98 constexpr const char ehFrame[] = "__eh_frame";
99 constexpr const char text[] = "__text";
100 constexpr const char stubs[] = "__stubs";
101 constexpr const char stubHelper[] = "__stub_helper";
102 constexpr const char laSymbolPtr[] = "__la_symbol_ptr";
103 constexpr const char data[] = "__data";
104 
105 } // namespace section_names
106 
107 } // namespace macho
108 
109 std::string toString(const macho::InputSection *);
110 
111 } // namespace lld
112 
113 #endif
114