1bb684c34Spatrick //===- OutputSection.h ------------------------------------------*- C++ -*-===// 2bb684c34Spatrick // 3bb684c34Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4bb684c34Spatrick // See https://llvm.org/LICENSE.txt for license information. 5bb684c34Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bb684c34Spatrick // 7bb684c34Spatrick //===----------------------------------------------------------------------===// 8bb684c34Spatrick 9bb684c34Spatrick #ifndef LLD_MACHO_OUTPUT_SECTION_H 10bb684c34Spatrick #define LLD_MACHO_OUTPUT_SECTION_H 11bb684c34Spatrick 121cf9926bSpatrick #include "Symbols.h" 13bb684c34Spatrick #include "lld/Common/LLVM.h" 14bb684c34Spatrick #include "llvm/ADT/DenseMap.h" 151cf9926bSpatrick #include "llvm/ADT/TinyPtrVector.h" 161cf9926bSpatrick 171cf9926bSpatrick #include <limits> 18bb684c34Spatrick 19*dfe94b16Srobert namespace lld::macho { 20bb684c34Spatrick 211cf9926bSpatrick class Defined; 22bb684c34Spatrick class InputSection; 23bb684c34Spatrick class OutputSegment; 24bb684c34Spatrick 251cf9926bSpatrick // The default order value for OutputSections that are not constructed from 261cf9926bSpatrick // InputSections (i.e. SyntheticSections). We make it less than INT_MAX in order 271cf9926bSpatrick // not to conflict with the ordering of zerofill sections, which must always be 281cf9926bSpatrick // placed at the end of their segment. 291cf9926bSpatrick constexpr int UnspecifiedInputOrder = std::numeric_limits<int>::max() - 1024; 301cf9926bSpatrick 31bb684c34Spatrick // Output sections represent the finalized sections present within the final 32bb684c34Spatrick // linked executable. They can represent special sections (like the symbol 33bb684c34Spatrick // table), or represent coalesced sections from the various inputs given to the 34bb684c34Spatrick // linker with the same segment / section name. 35bb684c34Spatrick class OutputSection { 36bb684c34Spatrick public: 37bb684c34Spatrick enum Kind { 381cf9926bSpatrick ConcatKind, 39bb684c34Spatrick SyntheticKind, 40bb684c34Spatrick }; 41bb684c34Spatrick OutputSection(Kind kind,StringRef name)42bb684c34Spatrick OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {} 43bb684c34Spatrick virtual ~OutputSection() = default; kind()44bb684c34Spatrick Kind kind() const { return sectionKind; } 45bb684c34Spatrick 46bb684c34Spatrick // These accessors will only be valid after finalizing the section. 47bb684c34Spatrick uint64_t getSegmentOffset() const; 48bb684c34Spatrick 49bb684c34Spatrick // How much space the section occupies in the address space. 50bb684c34Spatrick virtual uint64_t getSize() const = 0; 51bb684c34Spatrick // How much space the section occupies in the file. Most sections are copied 52bb684c34Spatrick // as-is so their file size is the same as their address space size. getFileSize()53bb684c34Spatrick virtual uint64_t getFileSize() const { return getSize(); } 54bb684c34Spatrick 55bb684c34Spatrick // Hidden sections omit header content, but body content may still be present. isHidden()56bb684c34Spatrick virtual bool isHidden() const { return false; } 57bb684c34Spatrick // Unneeded sections are omitted entirely (header and body). isNeeded()58bb684c34Spatrick virtual bool isNeeded() const { return true; } 59bb684c34Spatrick 60*dfe94b16Srobert // The implementations of this method can assume that it is only called right 61*dfe94b16Srobert // before addresses get assigned to this particular OutputSection. In 62*dfe94b16Srobert // particular, this means that it gets called only after addresses have been 63*dfe94b16Srobert // assigned to output sections that occur earlier in the output binary. 64*dfe94b16Srobert // Naturally, this means different sections' finalize() methods cannot execute 65*dfe94b16Srobert // concurrently with each other. As such, avoid using this method for 66*dfe94b16Srobert // operations that do not require this strict sequential guarantee. 67*dfe94b16Srobert // 68*dfe94b16Srobert // Operations that need to occur late in the linking process, but which do not 69*dfe94b16Srobert // need the sequential guarantee, should be named `finalizeContents()`. See 70*dfe94b16Srobert // e.g. LinkEditSection::finalizeContents() and 71*dfe94b16Srobert // CStringSection::finalizeContents(). finalize()72*dfe94b16Srobert virtual void finalize() {} 73bb684c34Spatrick 74bb684c34Spatrick virtual void writeTo(uint8_t *buf) const = 0; 75bb684c34Spatrick 76*dfe94b16Srobert // Handle section$start$ and section$end$ symbols. 771cf9926bSpatrick void assignAddressesToStartEndSymbols(); 781cf9926bSpatrick 79bb684c34Spatrick StringRef name; 801cf9926bSpatrick llvm::TinyPtrVector<Defined *> sectionStartSymbols; 811cf9926bSpatrick llvm::TinyPtrVector<Defined *> sectionEndSymbols; 82bb684c34Spatrick OutputSegment *parent = nullptr; 831cf9926bSpatrick // For output sections that don't have explicit ordering requirements, their 841cf9926bSpatrick // output order should be based on the order of the input sections they 851cf9926bSpatrick // contain. 861cf9926bSpatrick int inputOrder = UnspecifiedInputOrder; 87bb684c34Spatrick 88bb684c34Spatrick uint32_t index = 0; 89bb684c34Spatrick uint64_t addr = 0; 90bb684c34Spatrick uint64_t fileOff = 0; 91bb684c34Spatrick uint32_t align = 1; 92bb684c34Spatrick uint32_t flags = 0; 931cf9926bSpatrick uint32_t reserved1 = 0; 941cf9926bSpatrick uint32_t reserved2 = 0; 95bb684c34Spatrick 96bb684c34Spatrick private: 97bb684c34Spatrick Kind sectionKind; 98bb684c34Spatrick }; 99bb684c34Spatrick 100*dfe94b16Srobert } // namespace lld::macho 101bb684c34Spatrick 102bb684c34Spatrick #endif 103