xref: /llvm-project/lld/MachO/OutputSection.h (revision 162814a7ec00e2c89693f96568b72956d1bf2f0f)
16cb07313SKellie Medlin //===- OutputSection.h ------------------------------------------*- C++ -*-===//
26cb07313SKellie Medlin //
36cb07313SKellie Medlin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46cb07313SKellie Medlin // See https://llvm.org/LICENSE.txt for license information.
56cb07313SKellie Medlin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66cb07313SKellie Medlin //
76cb07313SKellie Medlin //===----------------------------------------------------------------------===//
86cb07313SKellie Medlin 
96cb07313SKellie Medlin #ifndef LLD_MACHO_OUTPUT_SECTION_H
106cb07313SKellie Medlin #define LLD_MACHO_OUTPUT_SECTION_H
116cb07313SKellie Medlin 
1204e8d0b6SNico Weber #include "Symbols.h"
136cb07313SKellie Medlin #include "lld/Common/LLVM.h"
146cb07313SKellie Medlin #include "llvm/ADT/DenseMap.h"
1504e8d0b6SNico Weber #include "llvm/ADT/TinyPtrVector.h"
166cb07313SKellie Medlin 
17fcab06bdSJez Ng #include <limits>
18fcab06bdSJez Ng 
19bf20d43fSNico Weber namespace lld::macho {
206cb07313SKellie Medlin 
2104e8d0b6SNico Weber class Defined;
226cb07313SKellie Medlin class InputSection;
236cb07313SKellie Medlin class OutputSegment;
246cb07313SKellie Medlin 
25f79e7a5aSJez Ng // The default order value for OutputSections that are not constructed from
26f79e7a5aSJez Ng // InputSections (i.e. SyntheticSections). We make it less than INT_MAX in order
27f79e7a5aSJez Ng // not to conflict with the ordering of zerofill sections, which must always be
28f79e7a5aSJez Ng // placed at the end of their segment.
29f79e7a5aSJez Ng constexpr int UnspecifiedInputOrder = std::numeric_limits<int>::max() - 1024;
30f79e7a5aSJez Ng 
316cb07313SKellie Medlin // Output sections represent the finalized sections present within the final
326cb07313SKellie Medlin // linked executable. They can represent special sections (like the symbol
336cb07313SKellie Medlin // table), or represent coalesced sections from the various inputs given to the
346cb07313SKellie Medlin // linker with the same segment / section name.
356cb07313SKellie Medlin class OutputSection {
366cb07313SKellie Medlin public:
3755e9eb41SJez Ng   enum Kind {
3833706191SJez Ng     ConcatKind,
3955e9eb41SJez Ng     SyntheticKind,
40*162814a7Salx32     TextKind,
4155e9eb41SJez Ng   };
4255e9eb41SJez Ng 
4355e9eb41SJez Ng   OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {}
446cb07313SKellie Medlin   virtual ~OutputSection() = default;
4555e9eb41SJez Ng   Kind kind() const { return sectionKind; }
466cb07313SKellie Medlin 
476cb07313SKellie Medlin   // These accessors will only be valid after finalizing the section.
486cb07313SKellie Medlin   uint64_t getSegmentOffset() const;
496cb07313SKellie Medlin 
506cb07313SKellie Medlin   // How much space the section occupies in the address space.
51a2d096dfSJez Ng   virtual uint64_t getSize() const = 0;
526cb07313SKellie Medlin   // How much space the section occupies in the file. Most sections are copied
536cb07313SKellie Medlin   // as-is so their file size is the same as their address space size.
546cb07313SKellie Medlin   virtual uint64_t getFileSize() const { return getSize(); }
556cb07313SKellie Medlin 
56db157d27SJez Ng   // Hidden sections omit header content, but body content may still be present.
57db157d27SJez Ng   virtual bool isHidden() const { return false; }
586cb07313SKellie Medlin   // Unneeded sections are omitted entirely (header and body).
596cb07313SKellie Medlin   virtual bool isNeeded() const { return true; }
606cb07313SKellie Medlin 
613e951808SJez Ng   // The implementations of this method can assume that it is only called right
623e951808SJez Ng   // before addresses get assigned to this particular OutputSection. In
633e951808SJez Ng   // particular, this means that it gets called only after addresses have been
643e951808SJez Ng   // assigned to output sections that occur earlier in the output binary.
653e951808SJez Ng   // Naturally, this means different sections' finalize() methods cannot execute
663e951808SJez Ng   // concurrently with each other. As such, avoid using this method for
673e951808SJez Ng   // operations that do not require this strict sequential guarantee.
683e951808SJez Ng   //
693e951808SJez Ng   // Operations that need to occur late in the linking process, but which do not
703e951808SJez Ng   // need the sequential guarantee, should be named `finalizeContents()`. See
713e951808SJez Ng   // e.g. LinkEditSection::finalizeContents() and
723e951808SJez Ng   // CStringSection::finalizeContents().
733e951808SJez Ng   virtual void finalize() {}
746cb07313SKellie Medlin 
756cb07313SKellie Medlin   virtual void writeTo(uint8_t *buf) const = 0;
766cb07313SKellie Medlin 
773e951808SJez Ng   // Handle section$start$ and section$end$ symbols.
7804e8d0b6SNico Weber   void assignAddressesToStartEndSymbols();
7904e8d0b6SNico Weber 
806cb07313SKellie Medlin   StringRef name;
8104e8d0b6SNico Weber   llvm::TinyPtrVector<Defined *> sectionStartSymbols;
8204e8d0b6SNico Weber   llvm::TinyPtrVector<Defined *> sectionEndSymbols;
836cb07313SKellie Medlin   OutputSegment *parent = nullptr;
84fcab06bdSJez Ng   // For output sections that don't have explicit ordering requirements, their
85fcab06bdSJez Ng   // output order should be based on the order of the input sections they
86fcab06bdSJez Ng   // contain.
87f79e7a5aSJez Ng   int inputOrder = UnspecifiedInputOrder;
886cb07313SKellie Medlin 
896cb07313SKellie Medlin   uint32_t index = 0;
906cb07313SKellie Medlin   uint64_t addr = 0;
916cb07313SKellie Medlin   uint64_t fileOff = 0;
926cb07313SKellie Medlin   uint32_t align = 1;
936cb07313SKellie Medlin   uint32_t flags = 0;
945d26bd3bSJez Ng   uint32_t reserved1 = 0;
955d26bd3bSJez Ng   uint32_t reserved2 = 0;
9655e9eb41SJez Ng 
9755e9eb41SJez Ng private:
9855e9eb41SJez Ng   Kind sectionKind;
996cb07313SKellie Medlin };
1006cb07313SKellie Medlin 
101bf20d43fSNico Weber } // namespace lld::macho
1026cb07313SKellie Medlin 
1036cb07313SKellie Medlin #endif
104