MCAsmLayout.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===- MCAsmLayout.h - Assembly Layout Object -------------------*- 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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_MC_MCASMLAYOUT_H
  9. #define LLVM_MC_MCASMLAYOUT_H
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. namespace llvm {
  13. class MCAssembler;
  14. class MCFragment;
  15. class MCSection;
  16. class MCSymbol;
  17. /// Encapsulates the layout of an assembly file at a particular point in time.
  18. ///
  19. /// Assembly may require computing multiple layouts for a particular assembly
  20. /// file as part of the relaxation process. This class encapsulates the layout
  21. /// at a single point in time in such a way that it is always possible to
  22. /// efficiently compute the exact address of any symbol in the assembly file,
  23. /// even during the relaxation process.
  24. class MCAsmLayout {
  25. MCAssembler &Assembler;
  26. /// List of sections in layout order.
  27. llvm::SmallVector<MCSection *, 16> SectionOrder;
  28. /// The last fragment which was laid out, or 0 if nothing has been laid
  29. /// out. Fragments are always laid out in order, so all fragments with a
  30. /// lower ordinal will be valid.
  31. mutable DenseMap<const MCSection *, MCFragment *> LastValidFragment;
  32. /// Make sure that the layout for the given fragment is valid, lazily
  33. /// computing it if necessary.
  34. void ensureValid(const MCFragment *F) const;
  35. /// Is the layout for this fragment valid?
  36. bool isFragmentValid(const MCFragment *F) const;
  37. public:
  38. MCAsmLayout(MCAssembler &Assembler);
  39. /// Get the assembler object this is a layout for.
  40. MCAssembler &getAssembler() const { return Assembler; }
  41. /// \returns whether the offset of fragment \p F can be obtained via
  42. /// getFragmentOffset.
  43. bool canGetFragmentOffset(const MCFragment *F) const;
  44. /// Invalidate the fragments starting with F because it has been
  45. /// resized. The fragment's size should have already been updated, but
  46. /// its bundle padding will be recomputed.
  47. void invalidateFragmentsFrom(MCFragment *F);
  48. /// Perform layout for a single fragment, assuming that the previous
  49. /// fragment has already been laid out correctly, and the parent section has
  50. /// been initialized.
  51. void layoutFragment(MCFragment *Fragment);
  52. /// \name Section Access (in layout order)
  53. /// @{
  54. llvm::SmallVectorImpl<MCSection *> &getSectionOrder() { return SectionOrder; }
  55. const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
  56. return SectionOrder;
  57. }
  58. /// @}
  59. /// \name Fragment Layout Data
  60. /// @{
  61. /// Get the offset of the given fragment inside its containing section.
  62. uint64_t getFragmentOffset(const MCFragment *F) const;
  63. /// @}
  64. /// \name Utility Functions
  65. /// @{
  66. /// Get the address space size of the given section, as it effects
  67. /// layout. This may differ from the size reported by \see getSectionSize() by
  68. /// not including section tail padding.
  69. uint64_t getSectionAddressSize(const MCSection *Sec) const;
  70. /// Get the data size of the given section, as emitted to the object
  71. /// file. This may include additional padding, or be 0 for virtual sections.
  72. uint64_t getSectionFileSize(const MCSection *Sec) const;
  73. /// Get the offset of the given symbol, as computed in the current
  74. /// layout.
  75. /// \return True on success.
  76. bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
  77. /// Variant that reports a fatal error if the offset is not computable.
  78. uint64_t getSymbolOffset(const MCSymbol &S) const;
  79. /// If this symbol is equivalent to A + Constant, return A.
  80. const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
  81. /// @}
  82. };
  83. } // end namespace llvm
  84. #endif