1 //===- InputSection.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_INPUT_SECTION_H
10 #define LLD_MACHO_INPUT_SECTION_H
13 #include "Relocations.h"
16 #include "lld/Common/LLVM.h"
17 #include "lld/Common/Memory.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/BitVector.h"
20 #include "llvm/ADT/CachedHashString.h"
21 #include "llvm/ADT/TinyPtrVector.h"
22 #include "llvm/BinaryFormat/MachO.h"
38 Kind
kind() const { return sectionKind
; }
39 virtual ~InputSection() = default;
40 virtual uint64_t getSize() const { return data
.size(); }
41 virtual bool empty() const { return data
.empty(); }
42 InputFile
*getFile() const { return section
.file
; }
43 StringRef
getName() const { return section
.name
; }
44 StringRef
getSegName() const { return section
.segname
; }
45 uint32_t getFlags() const { return section
.flags
; }
46 uint64_t getFileSize() const;
47 // Translates \p off -- an offset relative to this InputSection -- into an
48 // offset from the beginning of its parent OutputSection.
49 virtual uint64_t getOffset(uint64_t off
) const = 0;
50 // The offset from the beginning of the file.
51 uint64_t getVA(uint64_t off
) const;
52 // Return a user-friendly string for use in diagnostics.
53 // Format: /path/to/object.o:(symbol _func+0x123)
54 std::string
getLocation(uint64_t off
) const;
55 // Return the source line corresponding to an address, or the empty string.
56 // Format: Source.cpp:123 (/path/to/Source.cpp:123)
57 std::string
getSourceLocation(uint64_t off
) const;
58 // Whether the data at \p off in this InputSection is live.
59 virtual bool isLive(uint64_t off
) const = 0;
60 virtual void markLive(uint64_t off
) = 0;
61 virtual InputSection
*canonical() { return this; }
62 virtual const InputSection
*canonical() const { return this; }
65 InputSection(Kind kind
, const Section
§ion
, ArrayRef
<uint8_t> data
,
67 : sectionKind(kind
), align(align
), data(data
), section(section
) {}
69 InputSection(const InputSection
&rhs
)
70 : sectionKind(rhs
.sectionKind
), align(rhs
.align
), data(rhs
.data
),
71 section(rhs
.section
) {}
76 // is address assigned?
78 // keep the address of the symbol(s) in this section unique in the final
80 bool keepUnique
= false;
83 OutputSection
*parent
= nullptr;
84 ArrayRef
<uint8_t> data
;
85 std::vector
<Reloc
> relocs
;
86 // The symbols that belong to this InputSection, sorted by value. With
87 // .subsections_via_symbols, there is typically only one element here.
88 llvm::TinyPtrVector
<Defined
*> symbols
;
91 const Section
§ion
;
93 const Defined
*getContainingSymbol(uint64_t off
) const;
96 // ConcatInputSections are combined into (Concat)OutputSections through simple
97 // concatenation, in contrast with literal sections which may have their
98 // contents merged before output.
99 class ConcatInputSection final
: public InputSection
{
101 ConcatInputSection(const Section
§ion
, ArrayRef
<uint8_t> data
,
103 : InputSection(ConcatKind
, section
, data
, align
) {}
105 uint64_t getOffset(uint64_t off
) const override
{ return outSecOff
+ off
; }
106 uint64_t getVA() const { return InputSection::getVA(0); }
107 // ConcatInputSections are entirely live or dead, so the offset is irrelevant.
108 bool isLive(uint64_t off
) const override
{ return live
; }
109 void markLive(uint64_t off
) override
{ live
= true; }
110 bool isCoalescedWeak() const { return wasCoalesced
&& symbols
.empty(); }
111 bool shouldOmitFromOutput() const { return !live
|| isCoalescedWeak(); }
112 void writeTo(uint8_t *buf
);
114 void foldIdentical(ConcatInputSection
*redundant
);
115 ConcatInputSection
*canonical() override
{
116 return replacement
? replacement
: this;
118 const InputSection
*canonical() const override
{
119 return replacement
? replacement
: this;
122 static bool classof(const InputSection
*isec
) {
123 return isec
->kind() == ConcatKind
;
126 // Points to the surviving section after this one is folded by ICF
127 ConcatInputSection
*replacement
= nullptr;
128 // Equivalence-class ID for ICF
129 uint32_t icfEqClass
[2] = {0, 0};
131 // With subsections_via_symbols, most symbols have their own InputSection,
132 // and for weak symbols (e.g. from inline functions), only the
133 // InputSection from one translation unit will make it to the output,
134 // while all copies in other translation units are coalesced into the
135 // first and not copied to the output.
136 bool wasCoalesced
= false;
137 bool live
= !config
->deadStrip
;
138 bool hasCallSites
= false;
139 // This variable has two usages. Initially, it represents the input order.
140 // After assignAddresses is called, it represents the offset from the
141 // beginning of the output section this section was assigned to.
142 uint64_t outSecOff
= 0;
145 // Initialize a fake InputSection that does not belong to any InputFile.
146 ConcatInputSection
*makeSyntheticInputSection(StringRef segName
,
149 ArrayRef
<uint8_t> data
= {},
152 // Helper functions to make it easy to sprinkle asserts.
154 inline bool shouldOmitFromOutput(InputSection
*isec
) {
155 return isa
<ConcatInputSection
>(isec
) &&
156 cast
<ConcatInputSection
>(isec
)->shouldOmitFromOutput();
159 inline bool isCoalescedWeak(InputSection
*isec
) {
160 return isa
<ConcatInputSection
>(isec
) &&
161 cast
<ConcatInputSection
>(isec
)->isCoalescedWeak();
164 // We allocate a lot of these and binary search on them, so they should be as
165 // compact as possible. Hence the use of 31 rather than 64 bits for the hash.
167 // Offset from the start of the containing input section.
170 // Only set if deduplicating literals
172 // Offset from the start of the containing output section.
173 uint64_t outSecOff
= 0;
175 StringPiece(uint64_t off
, uint32_t hash
)
176 : inSecOff(off
), live(!config
->deadStrip
), hash(hash
) {}
179 static_assert(sizeof(StringPiece
) == 16, "StringPiece is too big!");
181 // CStringInputSections are composed of multiple null-terminated string
182 // literals, which we represent using StringPieces. These literals can be
183 // deduplicated and tail-merged, so translating offsets between the input and
184 // outputs sections is more complicated.
186 // NOTE: One significant difference between LLD and ld64 is that we merge all
187 // cstring literals, even those referenced directly by non-private symbols.
188 // ld64 is more conservative and does not do that. This was mostly done for
189 // implementation simplicity; if we find programs that need the more
190 // conservative behavior we can certainly implement that.
191 class CStringInputSection final
: public InputSection
{
193 CStringInputSection(const Section
§ion
, ArrayRef
<uint8_t> data
,
195 : InputSection(CStringLiteralKind
, section
, data
, align
) {}
196 uint64_t getOffset(uint64_t off
) const override
;
197 bool isLive(uint64_t off
) const override
{ return getStringPiece(off
).live
; }
198 void markLive(uint64_t off
) override
{ getStringPiece(off
).live
= true; }
199 // Find the StringPiece that contains this offset.
200 StringPiece
&getStringPiece(uint64_t off
);
201 const StringPiece
&getStringPiece(uint64_t off
) const;
202 // Split at each null byte.
203 void splitIntoPieces();
205 LLVM_ATTRIBUTE_ALWAYS_INLINE
206 StringRef
getStringRef(size_t i
) const {
207 size_t begin
= pieces
[i
].inSecOff
;
209 (pieces
.size() - 1 == i
) ? data
.size() : pieces
[i
+ 1].inSecOff
;
210 return toStringRef(data
.slice(begin
, end
- begin
));
213 // Returns i'th piece as a CachedHashStringRef. This function is very hot when
214 // string merging is enabled, so we want to inline.
215 LLVM_ATTRIBUTE_ALWAYS_INLINE
216 llvm::CachedHashStringRef
getCachedHashStringRef(size_t i
) const {
217 assert(config
->dedupLiterals
);
218 return {getStringRef(i
), pieces
[i
].hash
};
221 static bool classof(const InputSection
*isec
) {
222 return isec
->kind() == CStringLiteralKind
;
225 std::vector
<StringPiece
> pieces
;
228 class WordLiteralInputSection final
: public InputSection
{
230 WordLiteralInputSection(const Section
§ion
, ArrayRef
<uint8_t> data
,
232 uint64_t getOffset(uint64_t off
) const override
;
233 bool isLive(uint64_t off
) const override
{
234 return live
[off
>> power2LiteralSize
];
236 void markLive(uint64_t off
) override
{
237 live
[off
>> power2LiteralSize
] = true;
240 static bool classof(const InputSection
*isec
) {
241 return isec
->kind() == WordLiteralKind
;
245 unsigned power2LiteralSize
;
246 // The liveness of data[off] is tracked by live[off >> power2LiteralSize].
247 llvm::BitVector live
;
250 inline uint8_t sectionType(uint32_t flags
) {
251 return flags
& llvm::MachO::SECTION_TYPE
;
254 inline bool isZeroFill(uint32_t flags
) {
255 return llvm::MachO::isVirtualSection(sectionType(flags
));
258 inline bool isThreadLocalVariables(uint32_t flags
) {
259 return sectionType(flags
) == llvm::MachO::S_THREAD_LOCAL_VARIABLES
;
262 // These sections contain the data for initializing thread-local variables.
263 inline bool isThreadLocalData(uint32_t flags
) {
264 return sectionType(flags
) == llvm::MachO::S_THREAD_LOCAL_REGULAR
||
265 sectionType(flags
) == llvm::MachO::S_THREAD_LOCAL_ZEROFILL
;
268 inline bool isDebugSection(uint32_t flags
) {
269 return (flags
& llvm::MachO::SECTION_ATTRIBUTES_USR
) ==
270 llvm::MachO::S_ATTR_DEBUG
;
273 inline bool isWordLiteralSection(uint32_t flags
) {
274 return sectionType(flags
) == llvm::MachO::S_4BYTE_LITERALS
||
275 sectionType(flags
) == llvm::MachO::S_8BYTE_LITERALS
||
276 sectionType(flags
) == llvm::MachO::S_16BYTE_LITERALS
;
279 bool isCodeSection(const InputSection
*);
280 bool isCfStringSection(const InputSection
*);
281 bool isClassRefsSection(const InputSection
*);
282 bool isEhFrameSection(const InputSection
*);
284 extern std::vector
<ConcatInputSection
*> inputSections
;
286 namespace section_names
{
288 constexpr const char authGot
[] = "__auth_got";
289 constexpr const char authPtr
[] = "__auth_ptr";
290 constexpr const char binding
[] = "__binding";
291 constexpr const char bitcodeBundle
[] = "__bundle";
292 constexpr const char cString
[] = "__cstring";
293 constexpr const char cfString
[] = "__cfstring";
294 constexpr const char cgProfile
[] = "__cg_profile";
295 constexpr const char codeSignature
[] = "__code_signature";
296 constexpr const char common
[] = "__common";
297 constexpr const char compactUnwind
[] = "__compact_unwind";
298 constexpr const char data
[] = "__data";
299 constexpr const char debugAbbrev
[] = "__debug_abbrev";
300 constexpr const char debugInfo
[] = "__debug_info";
301 constexpr const char debugLine
[] = "__debug_line";
302 constexpr const char debugStr
[] = "__debug_str";
303 constexpr const char ehFrame
[] = "__eh_frame";
304 constexpr const char gccExceptTab
[] = "__gcc_except_tab";
305 constexpr const char export_
[] = "__export";
306 constexpr const char dataInCode
[] = "__data_in_code";
307 constexpr const char functionStarts
[] = "__func_starts";
308 constexpr const char got
[] = "__got";
309 constexpr const char header
[] = "__mach_header";
310 constexpr const char indirectSymbolTable
[] = "__ind_sym_tab";
311 constexpr const char const_
[] = "__const";
312 constexpr const char lazySymbolPtr
[] = "__la_symbol_ptr";
313 constexpr const char lazyBinding
[] = "__lazy_binding";
314 constexpr const char literals
[] = "__literals";
315 constexpr const char moduleInitFunc
[] = "__mod_init_func";
316 constexpr const char moduleTermFunc
[] = "__mod_term_func";
317 constexpr const char nonLazySymbolPtr
[] = "__nl_symbol_ptr";
318 constexpr const char objcCatList
[] = "__objc_catlist";
319 constexpr const char objcClassList
[] = "__objc_classlist";
320 constexpr const char objcClassRefs
[] = "__objc_classrefs";
321 constexpr const char objcConst
[] = "__objc_const";
322 constexpr const char objcImageInfo
[] = "__objc_imageinfo";
323 constexpr const char objcNonLazyCatList
[] = "__objc_nlcatlist";
324 constexpr const char objcNonLazyClassList
[] = "__objc_nlclslist";
325 constexpr const char objcProtoList
[] = "__objc_protolist";
326 constexpr const char pageZero
[] = "__pagezero";
327 constexpr const char pointers
[] = "__pointers";
328 constexpr const char rebase
[] = "__rebase";
329 constexpr const char staticInit
[] = "__StaticInit";
330 constexpr const char stringTable
[] = "__string_table";
331 constexpr const char stubHelper
[] = "__stub_helper";
332 constexpr const char stubs
[] = "__stubs";
333 constexpr const char swift
[] = "__swift";
334 constexpr const char symbolTable
[] = "__symbol_table";
335 constexpr const char textCoalNt
[] = "__textcoal_nt";
336 constexpr const char text
[] = "__text";
337 constexpr const char threadPtrs
[] = "__thread_ptrs";
338 constexpr const char threadVars
[] = "__thread_vars";
339 constexpr const char unwindInfo
[] = "__unwind_info";
340 constexpr const char weakBinding
[] = "__weak_binding";
341 constexpr const char zeroFill
[] = "__zerofill";
342 constexpr const char addrSig
[] = "__llvm_addrsig";
344 } // namespace section_names
348 std::string
toString(const macho::InputSection
*);