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 "lld/Common/LLVM.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/PointerUnion.h"
15 #include "llvm/BinaryFormat/MachO.h"
16 
17 namespace lld {
18 namespace macho {
19 
20 class InputFile;
21 class InputSection;
22 class OutputSection;
23 class Symbol;
24 
25 struct Reloc {
26   uint8_t type;
27   bool pcrel;
28   uint8_t length;
29   // The offset from the start of the subsection that this relocation belongs
30   // to.
31   uint32_t offset;
32   // Adding this offset to the address of the referent symbol or subsection
33   // gives the destination that this relocation refers to.
34   uint64_t addend;
35   llvm::PointerUnion<Symbol *, InputSection *> referent;
36 };
37 
38 class InputSection {
39 public:
40   virtual ~InputSection() = default;
41   virtual uint64_t getSize() const { return data.size(); }
42   virtual uint64_t getFileSize() const;
43   uint64_t getFileOffset() const;
44   uint64_t getVA() const;
45 
46   virtual void writeTo(uint8_t *buf);
47 
48   InputFile *file = nullptr;
49   StringRef name;
50   StringRef segname;
51 
52   OutputSection *parent = nullptr;
53   uint64_t outSecOff = 0;
54   uint64_t outSecFileOff = 0;
55 
56   uint32_t align = 1;
57   uint32_t flags = 0;
58 
59   ArrayRef<uint8_t> data;
60   std::vector<Reloc> relocs;
61 };
62 
63 inline uint8_t sectionType(uint32_t flags) {
64   return flags & llvm::MachO::SECTION_TYPE;
65 }
66 
67 inline bool isZeroFill(uint32_t flags) {
68   return llvm::MachO::isVirtualSection(sectionType(flags));
69 }
70 
71 inline bool isThreadLocalVariables(uint32_t flags) {
72   return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_VARIABLES;
73 }
74 
75 // These sections contain the data for initializing thread-local variables.
76 inline bool isThreadLocalData(uint32_t flags) {
77   return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_REGULAR ||
78          sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_ZEROFILL;
79 }
80 
81 inline bool isDebugSection(uint32_t flags) {
82   return (flags & llvm::MachO::SECTION_ATTRIBUTES_USR) ==
83          llvm::MachO::S_ATTR_DEBUG;
84 }
85 
86 bool isCodeSection(InputSection *);
87 
88 extern std::vector<InputSection *> inputSections;
89 
90 } // namespace macho
91 
92 std::string toString(const macho::InputSection *);
93 
94 } // namespace lld
95 
96 #endif
97