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 19namespace lld::macho { 20 21class Defined; 22class InputSection; 23class OutputSegment; 24 25// The default order value for OutputSections that are not constructed from 26// InputSections (i.e. SyntheticSections). We make it less than INT_MAX in order 27// not to conflict with the ordering of zerofill sections, which must always be 28// placed at the end of their segment. 29constexpr int UnspecifiedInputOrder = std::numeric_limits<int>::max() - 1024; 30 31// Output sections represent the finalized sections present within the final 32// linked executable. They can represent special sections (like the symbol 33// table), or represent coalesced sections from the various inputs given to the 34// linker with the same segment / section name. 35class OutputSection { 36public: 37 enum Kind { 38 ConcatKind, 39 SyntheticKind, 40 }; 41 42 OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {} 43 virtual ~OutputSection() = default; 44 Kind kind() const { return sectionKind; } 45 46 // These accessors will only be valid after finalizing the section. 47 uint64_t getSegmentOffset() const; 48 49 // How much space the section occupies in the address space. 50 virtual uint64_t getSize() const = 0; 51 // How much space the section occupies in the file. Most sections are copied 52 // as-is so their file size is the same as their address space size. 53 virtual uint64_t getFileSize() const { return getSize(); } 54 55 // Hidden sections omit header content, but body content may still be present. 56 virtual bool isHidden() const { return false; } 57 // Unneeded sections are omitted entirely (header and body). 58 virtual bool isNeeded() const { return true; } 59 60 // The implementations of this method can assume that it is only called right 61 // before addresses get assigned to this particular OutputSection. In 62 // particular, this means that it gets called only after addresses have been 63 // assigned to output sections that occur earlier in the output binary. 64 // Naturally, this means different sections' finalize() methods cannot execute 65 // concurrently with each other. As such, avoid using this method for 66 // operations that do not require this strict sequential guarantee. 67 // 68 // Operations that need to occur late in the linking process, but which do not 69 // need the sequential guarantee, should be named `finalizeContents()`. See 70 // e.g. LinkEditSection::finalizeContents() and 71 // CStringSection::finalizeContents(). 72 virtual void finalize() {} 73 74 virtual void writeTo(uint8_t *buf) const = 0; 75 76 // Handle section$start$ and section$end$ symbols. 77 void assignAddressesToStartEndSymbols(); 78 79 StringRef name; 80 llvm::TinyPtrVector<Defined *> sectionStartSymbols; 81 llvm::TinyPtrVector<Defined *> sectionEndSymbols; 82 OutputSegment *parent = nullptr; 83 // For output sections that don't have explicit ordering requirements, their 84 // output order should be based on the order of the input sections they 85 // contain. 86 int inputOrder = UnspecifiedInputOrder; 87 88 uint32_t index = 0; 89 uint64_t addr = 0; 90 uint64_t fileOff = 0; 91 uint32_t align = 1; 92 uint32_t flags = 0; 93 uint32_t reserved1 = 0; 94 uint32_t reserved2 = 0; 95 96private: 97 Kind sectionKind; 98}; 99 100} // namespace lld::macho 101 102#endif 103