xref: /freebsd-src/contrib/llvm-project/lld/MachO/OutputSection.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
15ffd83dbSDimitry Andric //===- OutputSection.h ------------------------------------------*- C++ -*-===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLD_MACHO_OUTPUT_SECTION_H
105ffd83dbSDimitry Andric #define LLD_MACHO_OUTPUT_SECTION_H
115ffd83dbSDimitry Andric 
12fe6060f1SDimitry Andric #include "Symbols.h"
135ffd83dbSDimitry Andric #include "lld/Common/LLVM.h"
145ffd83dbSDimitry Andric #include "llvm/ADT/DenseMap.h"
15fe6060f1SDimitry Andric #include "llvm/ADT/TinyPtrVector.h"
16fe6060f1SDimitry Andric 
17fe6060f1SDimitry Andric #include <limits>
185ffd83dbSDimitry Andric 
19*bdd1243dSDimitry Andric namespace lld::macho {
205ffd83dbSDimitry Andric 
21fe6060f1SDimitry Andric class Defined;
225ffd83dbSDimitry Andric class InputSection;
235ffd83dbSDimitry Andric class OutputSegment;
245ffd83dbSDimitry Andric 
25fe6060f1SDimitry Andric // The default order value for OutputSections that are not constructed from
26fe6060f1SDimitry Andric // InputSections (i.e. SyntheticSections). We make it less than INT_MAX in order
27fe6060f1SDimitry Andric // not to conflict with the ordering of zerofill sections, which must always be
28fe6060f1SDimitry Andric // placed at the end of their segment.
29fe6060f1SDimitry Andric constexpr int UnspecifiedInputOrder = std::numeric_limits<int>::max() - 1024;
30fe6060f1SDimitry Andric 
315ffd83dbSDimitry Andric // Output sections represent the finalized sections present within the final
325ffd83dbSDimitry Andric // linked executable. They can represent special sections (like the symbol
335ffd83dbSDimitry Andric // table), or represent coalesced sections from the various inputs given to the
345ffd83dbSDimitry Andric // linker with the same segment / section name.
355ffd83dbSDimitry Andric class OutputSection {
365ffd83dbSDimitry Andric public:
375ffd83dbSDimitry Andric   enum Kind {
38fe6060f1SDimitry Andric     ConcatKind,
395ffd83dbSDimitry Andric     SyntheticKind,
405ffd83dbSDimitry Andric   };
415ffd83dbSDimitry Andric 
OutputSection(Kind kind,StringRef name)425ffd83dbSDimitry Andric   OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {}
435ffd83dbSDimitry Andric   virtual ~OutputSection() = default;
kind()445ffd83dbSDimitry Andric   Kind kind() const { return sectionKind; }
455ffd83dbSDimitry Andric 
465ffd83dbSDimitry Andric   // These accessors will only be valid after finalizing the section.
475ffd83dbSDimitry Andric   uint64_t getSegmentOffset() const;
485ffd83dbSDimitry Andric 
495ffd83dbSDimitry Andric   // How much space the section occupies in the address space.
505ffd83dbSDimitry Andric   virtual uint64_t getSize() const = 0;
515ffd83dbSDimitry Andric   // How much space the section occupies in the file. Most sections are copied
525ffd83dbSDimitry Andric   // as-is so their file size is the same as their address space size.
getFileSize()535ffd83dbSDimitry Andric   virtual uint64_t getFileSize() const { return getSize(); }
545ffd83dbSDimitry Andric 
555ffd83dbSDimitry Andric   // Hidden sections omit header content, but body content may still be present.
isHidden()565ffd83dbSDimitry Andric   virtual bool isHidden() const { return false; }
575ffd83dbSDimitry Andric   // Unneeded sections are omitted entirely (header and body).
isNeeded()585ffd83dbSDimitry Andric   virtual bool isNeeded() const { return true; }
595ffd83dbSDimitry Andric 
601fd87a68SDimitry Andric   // The implementations of this method can assume that it is only called right
611fd87a68SDimitry Andric   // before addresses get assigned to this particular OutputSection. In
621fd87a68SDimitry Andric   // particular, this means that it gets called only after addresses have been
631fd87a68SDimitry Andric   // assigned to output sections that occur earlier in the output binary.
641fd87a68SDimitry Andric   // Naturally, this means different sections' finalize() methods cannot execute
651fd87a68SDimitry Andric   // concurrently with each other. As such, avoid using this method for
661fd87a68SDimitry Andric   // operations that do not require this strict sequential guarantee.
671fd87a68SDimitry Andric   //
681fd87a68SDimitry Andric   // Operations that need to occur late in the linking process, but which do not
691fd87a68SDimitry Andric   // need the sequential guarantee, should be named `finalizeContents()`. See
701fd87a68SDimitry Andric   // e.g. LinkEditSection::finalizeContents() and
711fd87a68SDimitry Andric   // CStringSection::finalizeContents().
finalize()721fd87a68SDimitry Andric   virtual void finalize() {}
735ffd83dbSDimitry Andric 
745ffd83dbSDimitry Andric   virtual void writeTo(uint8_t *buf) const = 0;
755ffd83dbSDimitry Andric 
761fd87a68SDimitry Andric   // Handle section$start$ and section$end$ symbols.
77fe6060f1SDimitry Andric   void assignAddressesToStartEndSymbols();
78fe6060f1SDimitry Andric 
795ffd83dbSDimitry Andric   StringRef name;
80fe6060f1SDimitry Andric   llvm::TinyPtrVector<Defined *> sectionStartSymbols;
81fe6060f1SDimitry Andric   llvm::TinyPtrVector<Defined *> sectionEndSymbols;
825ffd83dbSDimitry Andric   OutputSegment *parent = nullptr;
83fe6060f1SDimitry Andric   // For output sections that don't have explicit ordering requirements, their
84fe6060f1SDimitry Andric   // output order should be based on the order of the input sections they
85fe6060f1SDimitry Andric   // contain.
86fe6060f1SDimitry Andric   int inputOrder = UnspecifiedInputOrder;
875ffd83dbSDimitry Andric 
885ffd83dbSDimitry Andric   uint32_t index = 0;
895ffd83dbSDimitry Andric   uint64_t addr = 0;
905ffd83dbSDimitry Andric   uint64_t fileOff = 0;
915ffd83dbSDimitry Andric   uint32_t align = 1;
925ffd83dbSDimitry Andric   uint32_t flags = 0;
93e8d8bef9SDimitry Andric   uint32_t reserved1 = 0;
94e8d8bef9SDimitry Andric   uint32_t reserved2 = 0;
955ffd83dbSDimitry Andric 
965ffd83dbSDimitry Andric private:
975ffd83dbSDimitry Andric   Kind sectionKind;
985ffd83dbSDimitry Andric };
995ffd83dbSDimitry Andric 
100*bdd1243dSDimitry Andric } // namespace lld::macho
1015ffd83dbSDimitry Andric 
1025ffd83dbSDimitry Andric #endif
103