Path: blob/main/contrib/llvm-project/lld/MachO/ConcatOutputSection.h
34889 views
//===- ConcatOutputSection.h ------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLD_MACHO_CONCAT_OUTPUT_SECTION_H9#define LLD_MACHO_CONCAT_OUTPUT_SECTION_H1011#include "InputSection.h"12#include "OutputSection.h"13#include "lld/Common/LLVM.h"14#include "llvm/ADT/DenseMap.h"15#include "llvm/ADT/MapVector.h"1617namespace lld::macho {1819class Defined;2021// Linking multiple files will inevitably mean resolving sections in different22// files that are labeled with the same segment and section name. This class23// contains all such sections and writes the data from each section sequentially24// in the final binary.25class ConcatOutputSection : public OutputSection {26public:27explicit ConcatOutputSection(StringRef name)28: OutputSection(ConcatKind, name) {}2930const ConcatInputSection *firstSection() const { return inputs.front(); }31const ConcatInputSection *lastSection() const { return inputs.back(); }32bool isNeeded() const override { return !inputs.empty(); }3334// These accessors will only be valid after finalizing the section35uint64_t getSize() const override { return size; }36uint64_t getFileSize() const override { return fileSize; }3738// Assign values to InputSection::outSecOff. In contrast to TextOutputSection,39// which does this in its implementation of `finalize()`, we can do this40// without `finalize()`'s sequential guarantees detailed in the block comment41// of `OutputSection::finalize()`.42virtual void finalizeContents();4344void addInput(ConcatInputSection *input);45void writeTo(uint8_t *buf) const override;4647static bool classof(const OutputSection *sec) {48return sec->kind() == ConcatKind;49}5051static ConcatOutputSection *getOrCreateForInput(const InputSection *);5253std::vector<ConcatInputSection *> inputs;5455protected:56size_t size = 0;57uint64_t fileSize = 0;58void finalizeOne(ConcatInputSection *);5960private:61void finalizeFlags(InputSection *input);62};6364// ConcatOutputSections that contain code (text) require special handling to65// support thunk insertion.66class TextOutputSection : public ConcatOutputSection {67public:68explicit TextOutputSection(StringRef name) : ConcatOutputSection(name) {}69void finalizeContents() override {}70void finalize() override;71bool needsThunks() const;72void writeTo(uint8_t *buf) const override;7374private:75uint64_t estimateStubsInRangeVA(size_t callIdx) const;7677std::vector<ConcatInputSection *> thunks;78};7980// We maintain one ThunkInfo per real function.81//82// The "active thunk" is represented by the sym/isec pair that83// turns-over during finalize(): as the call-site address advances,84// the active thunk goes out of branch-range, and we create a new85// thunk to take its place.86//87// The remaining members -- bools and counters -- apply to the88// collection of thunks associated with the real function.8990struct ThunkInfo {91// These denote the active thunk:92Defined *sym = nullptr; // private-extern symbol for active thunk93ConcatInputSection *isec = nullptr; // input section for active thunk9495// The following values are cumulative across all thunks on this function96uint32_t callSiteCount = 0; // how many calls to the real function?97uint32_t callSitesUsed = 0; // how many call sites processed so-far?98uint32_t thunkCallCount = 0; // how many call sites went to thunk?99uint8_t sequence = 0; // how many thunks created so-far?100};101102NamePair maybeRenameSection(NamePair key);103104// Output sections are added to output segments in iteration order105// of ConcatOutputSection, so must have deterministic iteration order.106extern llvm::MapVector<NamePair, ConcatOutputSection *> concatOutputSections;107108extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap;109110} // namespace lld::macho111112#endif113114115