blob: 45ac96f0b81ec26995e9a0c7fc102c22fe0bc40d [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MCAsmLayout.h - Assembly Layout Object -------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_MC_MCASMLAYOUT_H
10#define LLVM_MC_MCASMLAYOUT_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/SmallVector.h"
14
15namespace llvm {
16class MCAssembler;
17class MCFragment;
18class MCSection;
19class MCSymbol;
20
21/// Encapsulates the layout of an assembly file at a particular point in time.
22///
23/// Assembly may require computing multiple layouts for a particular assembly
24/// file as part of the relaxation process. This class encapsulates the layout
25/// at a single point in time in such a way that it is always possible to
26/// efficiently compute the exact address of any symbol in the assembly file,
27/// even during the relaxation process.
28class MCAsmLayout {
29 MCAssembler &Assembler;
30
31 /// List of sections in layout order.
32 llvm::SmallVector<MCSection *, 16> SectionOrder;
33
34 /// The last fragment which was laid out, or 0 if nothing has been laid
35 /// out. Fragments are always laid out in order, so all fragments with a
36 /// lower ordinal will be valid.
37 mutable DenseMap<const MCSection *, MCFragment *> LastValidFragment;
38
Andrew Scullcdfcccc2018-10-05 20:58:37 +010039 /// Make sure that the layout for the given fragment is valid, lazily
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040 /// computing it if necessary.
41 void ensureValid(const MCFragment *F) const;
42
Andrew Scullcdfcccc2018-10-05 20:58:37 +010043 /// Is the layout for this fragment valid?
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044 bool isFragmentValid(const MCFragment *F) const;
45
46public:
47 MCAsmLayout(MCAssembler &Assembler);
48
49 /// Get the assembler object this is a layout for.
50 MCAssembler &getAssembler() const { return Assembler; }
51
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052 /// Invalidate the fragments starting with F because it has been
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053 /// resized. The fragment's size should have already been updated, but
54 /// its bundle padding will be recomputed.
55 void invalidateFragmentsFrom(MCFragment *F);
56
Andrew Scullcdfcccc2018-10-05 20:58:37 +010057 /// Perform layout for a single fragment, assuming that the previous
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058 /// fragment has already been laid out correctly, and the parent section has
59 /// been initialized.
60 void layoutFragment(MCFragment *Fragment);
61
62 /// \name Section Access (in layout order)
63 /// @{
64
65 llvm::SmallVectorImpl<MCSection *> &getSectionOrder() { return SectionOrder; }
66 const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
67 return SectionOrder;
68 }
69
70 /// @}
71 /// \name Fragment Layout Data
72 /// @{
73
Andrew Scullcdfcccc2018-10-05 20:58:37 +010074 /// Get the offset of the given fragment inside its containing section.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 uint64_t getFragmentOffset(const MCFragment *F) const;
76
77 /// @}
78 /// \name Utility Functions
79 /// @{
80
Andrew Scullcdfcccc2018-10-05 20:58:37 +010081 /// Get the address space size of the given section, as it effects
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010082 /// layout. This may differ from the size reported by \see getSectionSize() by
83 /// not including section tail padding.
84 uint64_t getSectionAddressSize(const MCSection *Sec) const;
85
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 /// Get the data size of the given section, as emitted to the object
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087 /// file. This may include additional padding, or be 0 for virtual sections.
88 uint64_t getSectionFileSize(const MCSection *Sec) const;
89
Andrew Scullcdfcccc2018-10-05 20:58:37 +010090 /// Get the offset of the given symbol, as computed in the current
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091 /// layout.
92 /// \return True on success.
93 bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
94
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095 /// Variant that reports a fatal error if the offset is not computable.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 uint64_t getSymbolOffset(const MCSymbol &S) const;
97
Andrew Scullcdfcccc2018-10-05 20:58:37 +010098 /// If this symbol is equivalent to A + Constant, return A.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099 const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
100
101 /// @}
102};
103
104} // end namespace llvm
105
106#endif