brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.2 KiB · 1c68018 Raw
121 lines · c
1//===- ConcatOutputSection.h ------------------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLD_MACHO_CONCAT_OUTPUT_SECTION_H10#define LLD_MACHO_CONCAT_OUTPUT_SECTION_H11 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"17 18namespace lld::macho {19 20class Defined;21 22// Linking multiple files will inevitably mean resolving sections in different23// files that are labeled with the same segment and section name. This class24// contains all such sections and writes the data from each section sequentially25// in the final binary.26class ConcatOutputSection : public OutputSection {27public:28  explicit ConcatOutputSection(StringRef name,29                               OutputSection::Kind kind = ConcatKind)30      : OutputSection(kind, name) {}31 32  const ConcatInputSection *firstSection() const { return inputs.front(); }33  const ConcatInputSection *lastSection() const { return inputs.back(); }34  bool isNeeded() const override { return !inputs.empty(); }35 36  // These accessors will only be valid after finalizing the section37  uint64_t getSize() const override { return size; }38  uint64_t getFileSize() const override { return fileSize; }39 40  // Assign values to InputSection::outSecOff. In contrast to TextOutputSection,41  // which does this in its implementation of `finalize()`, we can do this42  // without `finalize()`'s sequential guarantees detailed in the block comment43  // of `OutputSection::finalize()`.44  virtual void finalizeContents();45 46  void addInput(ConcatInputSection *input);47  void writeTo(uint8_t *buf) const override;48 49  static bool classof(const OutputSection *sec) {50    return sec->kind() == ConcatKind || sec->kind() == TextKind;51  }52 53  static ConcatOutputSection *getOrCreateForInput(const InputSection *);54 55  std::vector<ConcatInputSection *> inputs;56 57protected:58  size_t size = 0;59  uint64_t fileSize = 0;60  void finalizeOne(ConcatInputSection *);61 62private:63  void finalizeFlags(InputSection *input);64};65 66// ConcatOutputSections that contain code (text) require special handling to67// support thunk insertion.68class TextOutputSection : public ConcatOutputSection {69public:70  explicit TextOutputSection(StringRef name)71      : ConcatOutputSection(name, TextKind) {}72  void finalizeContents() override {}73  void finalize() override;74  bool needsThunks() const;75  ArrayRef<ConcatInputSection *> getThunks() const { return thunks; }76  void writeTo(uint8_t *buf) const override;77 78  static bool classof(const OutputSection *sec) {79    return sec->kind() == TextKind;80  }81 82private:83  uint64_t estimateBranchTargetThresholdVA(size_t callIdx) const;84 85  std::vector<ConcatInputSection *> thunks;86};87 88// We maintain one ThunkInfo per real function.89//90// The "active thunk" is represented by the sym/isec pair that91// turns-over during finalize(): as the call-site address advances,92// the active thunk goes out of branch-range, and we create a new93// thunk to take its place.94//95// The remaining members -- bools and counters -- apply to the96// collection of thunks associated with the real function.97 98struct ThunkInfo {99  // These denote the active thunk:100  Defined *sym = nullptr;             // private-extern symbol for active thunk101  ConcatInputSection *isec = nullptr; // input section for active thunk102 103  // The following values are cumulative across all thunks on this function104  uint32_t callSiteCount = 0;  // how many calls to the real function?105  uint32_t callSitesUsed = 0;  // how many call sites processed so-far?106  uint32_t thunkCallCount = 0; // how many call sites went to thunk?107  uint8_t sequence = 0;        // how many thunks created so-far?108};109 110NamePair maybeRenameSection(NamePair key);111 112// Output sections are added to output segments in iteration order113// of ConcatOutputSection, so must have deterministic iteration order.114extern llvm::MapVector<NamePair, ConcatOutputSection *> concatOutputSections;115 116extern llvm::DenseMap<Symbol *, ThunkInfo> thunkMap;117 118} // namespace lld::macho119 120#endif121