1 //===- ConcatOutputSection.h ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_MACHO_CONCAT_OUTPUT_SECTION_H
10 #define LLD_MACHO_CONCAT_OUTPUT_SECTION_H
12 #include "InputSection.h"
13 #include "OutputSection.h"
14 #include "lld/Common/LLVM.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/MapVector.h"
18 namespace lld::macho
{
22 // Linking multiple files will inevitably mean resolving sections in different
23 // files that are labeled with the same segment and section name. This class
24 // contains all such sections and writes the data from each section sequentially
25 // in the final binary.
26 class ConcatOutputSection
: public OutputSection
{
28 explicit ConcatOutputSection(StringRef name
)
29 : OutputSection(ConcatKind
, name
) {}
31 const ConcatInputSection
*firstSection() const { return inputs
.front(); }
32 const ConcatInputSection
*lastSection() const { return inputs
.back(); }
33 bool isNeeded() const override
{ return !inputs
.empty(); }
35 // These accessors will only be valid after finalizing the section
36 uint64_t getSize() const override
{ return size
; }
37 uint64_t getFileSize() const override
{ return fileSize
; }
39 // Assign values to InputSection::outSecOff. In contrast to TextOutputSection,
40 // which does this in its implementation of `finalize()`, we can do this
41 // without `finalize()`'s sequential guarantees detailed in the block comment
42 // of `OutputSection::finalize()`.
43 virtual void finalizeContents();
45 void addInput(ConcatInputSection
*input
);
46 void writeTo(uint8_t *buf
) const override
;
48 static bool classof(const OutputSection
*sec
) {
49 return sec
->kind() == ConcatKind
;
52 static ConcatOutputSection
*getOrCreateForInput(const InputSection
*);
54 std::vector
<ConcatInputSection
*> inputs
;
58 uint64_t fileSize
= 0;
59 void finalizeOne(ConcatInputSection
*);
62 void finalizeFlags(InputSection
*input
);
65 // ConcatOutputSections that contain code (text) require special handling to
66 // support thunk insertion.
67 class TextOutputSection
: public ConcatOutputSection
{
69 explicit TextOutputSection(StringRef name
) : ConcatOutputSection(name
) {}
70 void finalizeContents() override
{}
71 void finalize() override
;
72 bool needsThunks() const;
73 void writeTo(uint8_t *buf
) const override
;
76 uint64_t estimateStubsInRangeVA(size_t callIdx
) const;
78 std::vector
<ConcatInputSection
*> thunks
;
81 // We maintain one ThunkInfo per real function.
83 // The "active thunk" is represented by the sym/isec pair that
84 // turns-over during finalize(): as the call-site address advances,
85 // the active thunk goes out of branch-range, and we create a new
86 // thunk to take its place.
88 // The remaining members -- bools and counters -- apply to the
89 // collection of thunks associated with the real function.
92 // These denote the active thunk:
93 Defined
*sym
= nullptr; // private-extern symbol for active thunk
94 ConcatInputSection
*isec
= nullptr; // input section for active thunk
96 // The following values are cumulative across all thunks on this function
97 uint32_t callSiteCount
= 0; // how many calls to the real function?
98 uint32_t callSitesUsed
= 0; // how many call sites processed so-far?
99 uint32_t thunkCallCount
= 0; // how many call sites went to thunk?
100 uint8_t sequence
= 0; // how many thunks created so-far?
103 NamePair
maybeRenameSection(NamePair key
);
105 // Output sections are added to output segments in iteration order
106 // of ConcatOutputSection, so must have deterministic iteration order.
107 extern llvm::MapVector
<NamePair
, ConcatOutputSection
*> concatOutputSections
;
109 extern llvm::DenseMap
<Symbol
*, ThunkInfo
> thunkMap
;
111 } // namespace lld::macho