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 "Symbols.h" 13 #include "lld/Common/LLVM.h" 14 #include "llvm/ADT/DenseMap.h" 15 #include "llvm/ADT/TinyPtrVector.h" 16 17 #include <limits> 18 19 namespace lld { 20 namespace macho { 21 22 class Defined; 23 class InputSection; 24 class OutputSegment; 25 26 // The default order value for OutputSections that are not constructed from 27 // InputSections (i.e. SyntheticSections). We make it less than INT_MAX in order 28 // not to conflict with the ordering of zerofill sections, which must always be 29 // placed at the end of their segment. 30 constexpr int UnspecifiedInputOrder = std::numeric_limits<int>::max() - 1024; 31 32 // Output sections represent the finalized sections present within the final 33 // linked executable. They can represent special sections (like the symbol 34 // table), or represent coalesced sections from the various inputs given to the 35 // linker with the same segment / section name. 36 class OutputSection { 37 public: 38 enum Kind { 39 ConcatKind, 40 SyntheticKind, 41 }; 42 43 OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {} 44 virtual ~OutputSection() = default; 45 Kind kind() const { return sectionKind; } 46 47 // These accessors will only be valid after finalizing the section. 48 uint64_t getSegmentOffset() const; 49 50 // How much space the section occupies in the address space. 51 virtual uint64_t getSize() const = 0; 52 // How much space the section occupies in the file. Most sections are copied 53 // as-is so their file size is the same as their address space size. 54 virtual uint64_t getFileSize() const { return getSize(); } 55 56 // Hidden sections omit header content, but body content may still be present. 57 virtual bool isHidden() const { return false; } 58 // Unneeded sections are omitted entirely (header and body). 59 virtual bool isNeeded() const { return true; } 60 61 virtual void finalize() { 62 // TODO investigate refactoring synthetic section finalization logic into 63 // overrides of this function. 64 } 65 66 virtual void writeTo(uint8_t *buf) const = 0; 67 68 void assignAddressesToStartEndSymbols(); 69 70 StringRef name; 71 llvm::TinyPtrVector<Defined *> sectionStartSymbols; 72 llvm::TinyPtrVector<Defined *> sectionEndSymbols; 73 OutputSegment *parent = nullptr; 74 // For output sections that don't have explicit ordering requirements, their 75 // output order should be based on the order of the input sections they 76 // contain. 77 int inputOrder = UnspecifiedInputOrder; 78 79 uint32_t index = 0; 80 uint64_t addr = 0; 81 uint64_t fileOff = 0; 82 uint32_t align = 1; 83 uint32_t flags = 0; 84 uint32_t reserved1 = 0; 85 uint32_t reserved2 = 0; 86 87 private: 88 Kind sectionKind; 89 }; 90 91 } // namespace macho 92 } // namespace lld 93 94 #endif 95