1 //===- OutputSection.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_OUTPUT_SECTION_H
10 #define LLD_MACHO_OUTPUT_SECTION_H
11 
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/DenseMap.h"
14 
15 namespace lld {
16 namespace macho {
17 
18 class InputSection;
19 class OutputSegment;
20 
21 // Output sections represent the finalized sections present within the final
22 // linked executable. They can represent special sections (like the symbol
23 // table), or represent coalesced sections from the various inputs given to the
24 // linker with the same segment / section name.
25 class OutputSection {
26 public:
27   enum Kind {
28     MergedKind,
29     SyntheticKind,
30   };
31 
OutputSection(Kind kind,StringRef name)32   OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {}
33   virtual ~OutputSection() = default;
kind()34   Kind kind() const { return sectionKind; }
35 
36   // These accessors will only be valid after finalizing the section.
37   uint64_t getSegmentOffset() const;
38 
39   // How much space the section occupies in the address space.
40   virtual uint64_t getSize() const = 0;
41   // How much space the section occupies in the file. Most sections are copied
42   // as-is so their file size is the same as their address space size.
getFileSize()43   virtual uint64_t getFileSize() const { return getSize(); }
44 
45   // Hidden sections omit header content, but body content may still be present.
isHidden()46   virtual bool isHidden() const { return false; }
47   // Unneeded sections are omitted entirely (header and body).
isNeeded()48   virtual bool isNeeded() const { return true; }
49 
50   // Specifically finalizes addresses and section size, not content.
finalize()51   virtual void finalize() {
52     // TODO investigate refactoring synthetic section finalization logic into
53     // overrides of this function.
54   }
55 
56   virtual void writeTo(uint8_t *buf) const = 0;
57 
58   StringRef name;
59   OutputSegment *parent = nullptr;
60 
61   uint32_t index = 0;
62   uint64_t addr = 0;
63   uint64_t fileOff = 0;
64   uint32_t align = 1;
65   uint32_t flags = 0;
66   uint32_t reserved1 = 0;
67   uint32_t reserved2 = 0;
68 
69 private:
70   Kind sectionKind;
71 };
72 
73 } // namespace macho
74 } // namespace lld
75 
76 #endif
77