xref: /freebsd/contrib/llvm-project/lld/MachO/InputSection.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 target symbol or subsection gives
33   // the destination that this relocation refers to.
34   uint64_t addend;
35   llvm::PointerUnion<Symbol *, InputSection *> target;
36 };
37 
38 inline bool isZeroFill(uint8_t flags) {
39   return (flags & llvm::MachO::SECTION_TYPE) == llvm::MachO::S_ZEROFILL;
40 }
41 
42 class InputSection {
43 public:
44   virtual ~InputSection() = default;
45   virtual uint64_t getSize() const { return data.size(); }
46   virtual uint64_t getFileSize() const {
47     return isZeroFill(flags) ? 0 : getSize();
48   }
49   uint64_t getFileOffset() const;
50   uint64_t getVA() const;
51 
52   virtual void writeTo(uint8_t *buf);
53 
54   InputFile *file = nullptr;
55   StringRef name;
56   StringRef segname;
57 
58   OutputSection *parent = nullptr;
59   uint64_t outSecOff = 0;
60   uint64_t outSecFileOff = 0;
61 
62   uint32_t align = 1;
63   uint32_t flags = 0;
64 
65   ArrayRef<uint8_t> data;
66   std::vector<Reloc> relocs;
67 };
68 
69 extern std::vector<InputSection *> inputSections;
70 
71 } // namespace macho
72 } // namespace lld
73 
74 #endif
75