blob: b91b04414021ace5731f18897a14fbb5dbc51d69 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_MC_MCASSEMBLER_H
11#define LLVM_MC_MCASSEMBLER_H
12
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/iterator.h"
18#include "llvm/ADT/iterator_range.h"
19#include "llvm/BinaryFormat/MachO.h"
20#include "llvm/MC/MCDirectives.h"
21#include "llvm/MC/MCDwarf.h"
22#include "llvm/MC/MCFixup.h"
23#include "llvm/MC/MCFragment.h"
24#include "llvm/MC/MCLinkerOptimizationHint.h"
25#include "llvm/MC/MCSymbol.h"
26#include <cassert>
27#include <cstddef>
28#include <cstdint>
29#include <string>
30#include <utility>
31#include <vector>
32
33namespace llvm {
34
35class MCAsmBackend;
36class MCAsmLayout;
37class MCContext;
38class MCCodeEmitter;
39class MCFragment;
40class MCObjectWriter;
41class MCSection;
42class MCValue;
43
44// FIXME: This really doesn't belong here. See comments below.
45struct IndirectSymbolData {
46 MCSymbol *Symbol;
47 MCSection *Section;
48};
49
50// FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
51// to one another.
52struct DataRegionData {
53 // This enum should be kept in sync w/ the mach-o definition in
54 // llvm/Object/MachOFormat.h.
55 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
56 MCSymbol *Start;
57 MCSymbol *End;
58};
59
60class MCAssembler {
61 friend class MCAsmLayout;
62
63public:
64 using SectionListType = std::vector<MCSection *>;
65 using SymbolDataListType = std::vector<const MCSymbol *>;
66
67 using const_iterator = pointee_iterator<SectionListType::const_iterator>;
68 using iterator = pointee_iterator<SectionListType::iterator>;
69
70 using const_symbol_iterator =
71 pointee_iterator<SymbolDataListType::const_iterator>;
72 using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
73
74 using symbol_range = iterator_range<symbol_iterator>;
75 using const_symbol_range = iterator_range<const_symbol_iterator>;
76
77 using const_indirect_symbol_iterator =
78 std::vector<IndirectSymbolData>::const_iterator;
79 using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
80
81 using const_data_region_iterator =
82 std::vector<DataRegionData>::const_iterator;
83 using data_region_iterator = std::vector<DataRegionData>::iterator;
84
85 /// MachO specific deployment target version info.
86 // A Major version of 0 indicates that no version information was supplied
87 // and so the corresponding load command should not be emitted.
88 using VersionInfoType = struct {
89 bool EmitBuildVersion;
90 union {
91 MCVersionMinType Type; ///< Used when EmitBuildVersion==false.
92 MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true.
93 } TypeOrPlatform;
94 unsigned Major;
95 unsigned Minor;
96 unsigned Update;
97 };
98
99private:
100 MCContext &Context;
101
102 MCAsmBackend &Backend;
103
104 MCCodeEmitter &Emitter;
105
106 MCObjectWriter &Writer;
107
108 SectionListType Sections;
109
110 SymbolDataListType Symbols;
111
112 std::vector<IndirectSymbolData> IndirectSymbols;
113
114 std::vector<DataRegionData> DataRegions;
115
116 /// The list of linker options to propagate into the object file.
117 std::vector<std::vector<std::string>> LinkerOptions;
118
119 /// List of declared file names
120 std::vector<std::string> FileNames;
121
122 MCDwarfLineTableParams LTParams;
123
124 /// The set of function symbols for which a .thumb_func directive has
125 /// been seen.
126 //
127 // FIXME: We really would like this in target specific code rather than
128 // here. Maybe when the relocation stuff moves to target specific,
129 // this can go with it? The streamer would need some target specific
130 // refactoring too.
131 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
132
133 /// \brief The bundle alignment size currently set in the assembler.
134 ///
135 /// By default it's 0, which means bundling is disabled.
136 unsigned BundleAlignSize;
137
138 bool RelaxAll : 1;
139 bool SubsectionsViaSymbols : 1;
140 bool IncrementalLinkerCompatible : 1;
141
142 /// ELF specific e_header flags
143 // It would be good if there were an MCELFAssembler class to hold this.
144 // ELF header flags are used both by the integrated and standalone assemblers.
145 // Access to the flags is necessary in cases where assembler directives affect
146 // which flags to be set.
147 unsigned ELFHeaderEFlags;
148
149 /// Used to communicate Linker Optimization Hint information between
150 /// the Streamer and the .o writer
151 MCLOHContainer LOHContainer;
152
153 VersionInfoType VersionInfo;
154
155 /// Evaluate a fixup to a relocatable expression and the value which should be
156 /// placed into the fixup.
157 ///
158 /// \param Layout The layout to use for evaluation.
159 /// \param Fixup The fixup to evaluate.
160 /// \param DF The fragment the fixup is inside.
161 /// \param Target [out] On return, the relocatable expression the fixup
162 /// evaluates to.
163 /// \param Value [out] On return, the value of the fixup as currently laid
164 /// out.
165 /// \return Whether the fixup value was fully resolved. This is true if the
166 /// \p Value result is fixed, otherwise the value may change due to
167 /// relocation.
168 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
169 const MCFragment *DF, MCValue &Target,
170 uint64_t &Value) const;
171
172 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
173 /// (increased in size, in order to hold its value correctly).
174 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
175 const MCAsmLayout &Layout) const;
176
177 /// Check whether the given fragment needs relaxation.
178 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
179 const MCAsmLayout &Layout) const;
180
181 /// \brief Perform one layout iteration and return true if any offsets
182 /// were adjusted.
183 bool layoutOnce(MCAsmLayout &Layout);
184
185 /// \brief Perform one layout iteration of the given section and return true
186 /// if any offsets were adjusted.
187 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
188
189 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
190
191 bool relaxPaddingFragment(MCAsmLayout &Layout, MCPaddingFragment &PF);
192
193 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
194
195 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
196 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
197 MCDwarfCallFrameFragment &DF);
198 bool relaxCVInlineLineTable(MCAsmLayout &Layout,
199 MCCVInlineLineTableFragment &DF);
200 bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
201
202 /// finishLayout - Finalize a layout, including fragment lowering.
203 void finishLayout(MCAsmLayout &Layout);
204
205 std::tuple<MCValue, uint64_t, bool>
206 handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
207
208public:
209 std::vector<std::pair<StringRef, const MCSymbol *>> Symvers;
210
211 /// Construct a new assembler instance.
212 //
213 // FIXME: How are we going to parameterize this? Two obvious options are stay
214 // concrete and require clients to pass in a target like object. The other
215 // option is to make this abstract, and have targets provide concrete
216 // implementations as we do with AsmParser.
217 MCAssembler(MCContext &Context, MCAsmBackend &Backend,
218 MCCodeEmitter &Emitter, MCObjectWriter &Writer);
219 MCAssembler(const MCAssembler &) = delete;
220 MCAssembler &operator=(const MCAssembler &) = delete;
221 ~MCAssembler();
222
223 /// Compute the effective fragment size assuming it is laid out at the given
224 /// \p SectionAddress and \p FragmentOffset.
225 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
226 const MCFragment &F) const;
227
228 /// Find the symbol which defines the atom containing the given symbol, or
229 /// null if there is no such symbol.
230 const MCSymbol *getAtom(const MCSymbol &S) const;
231
232 /// Check whether a particular symbol is visible to the linker and is required
233 /// in the symbol table, or whether it can be discarded by the assembler. This
234 /// also effects whether the assembler treats the label as potentially
235 /// defining a separate atom.
236 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
237
238 /// Emit the section contents using the given object writer.
239 void writeSectionData(const MCSection *Section,
240 const MCAsmLayout &Layout) const;
241
242 /// Check whether a given symbol has been flagged with .thumb_func.
243 bool isThumbFunc(const MCSymbol *Func) const;
244
245 /// Flag a function symbol as the target of a .thumb_func directive.
246 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
247
248 /// ELF e_header flags
249 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
250 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
251
252 /// MachO deployment target version information.
253 const VersionInfoType &getVersionInfo() const { return VersionInfo; }
254 void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
255 unsigned Update) {
256 VersionInfo.EmitBuildVersion = false;
257 VersionInfo.TypeOrPlatform.Type = Type;
258 VersionInfo.Major = Major;
259 VersionInfo.Minor = Minor;
260 VersionInfo.Update = Update;
261 }
262 void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
263 unsigned Minor, unsigned Update) {
264 VersionInfo.EmitBuildVersion = true;
265 VersionInfo.TypeOrPlatform.Platform = Platform;
266 VersionInfo.Major = Major;
267 VersionInfo.Minor = Minor;
268 VersionInfo.Update = Update;
269 }
270
271 /// Reuse an assembler instance
272 ///
273 void reset();
274
275 MCContext &getContext() const { return Context; }
276
277 MCAsmBackend &getBackend() const { return Backend; }
278
279 MCCodeEmitter &getEmitter() const { return Emitter; }
280
281 MCObjectWriter &getWriter() const { return Writer; }
282
283 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
284 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
285
286 /// Finish - Do final processing and write the object to the output stream.
287 /// \p Writer is used for custom object writer (as the MCJIT does),
288 /// if not specified it is automatically created from backend.
289 void Finish();
290
291 // Layout all section and prepare them for emission.
292 void layout(MCAsmLayout &Layout);
293
294 // FIXME: This does not belong here.
295 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
296 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
297
298 bool isIncrementalLinkerCompatible() const {
299 return IncrementalLinkerCompatible;
300 }
301 void setIncrementalLinkerCompatible(bool Value) {
302 IncrementalLinkerCompatible = Value;
303 }
304
305 bool getRelaxAll() const { return RelaxAll; }
306 void setRelaxAll(bool Value) { RelaxAll = Value; }
307
308 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
309
310 unsigned getBundleAlignSize() const { return BundleAlignSize; }
311
312 void setBundleAlignSize(unsigned Size) {
313 assert((Size == 0 || !(Size & (Size - 1))) &&
314 "Expect a power-of-two bundle align size");
315 BundleAlignSize = Size;
316 }
317
318 /// \name Section List Access
319 /// @{
320
321 iterator begin() { return Sections.begin(); }
322 const_iterator begin() const { return Sections.begin(); }
323
324 iterator end() { return Sections.end(); }
325 const_iterator end() const { return Sections.end(); }
326
327 size_t size() const { return Sections.size(); }
328
329 /// @}
330 /// \name Symbol List Access
331 /// @{
332 symbol_iterator symbol_begin() { return Symbols.begin(); }
333 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
334
335 symbol_iterator symbol_end() { return Symbols.end(); }
336 const_symbol_iterator symbol_end() const { return Symbols.end(); }
337
338 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
339 const_symbol_range symbols() const {
340 return make_range(symbol_begin(), symbol_end());
341 }
342
343 size_t symbol_size() const { return Symbols.size(); }
344
345 /// @}
346 /// \name Indirect Symbol List Access
347 /// @{
348
349 // FIXME: This is a total hack, this should not be here. Once things are
350 // factored so that the streamer has direct access to the .o writer, it can
351 // disappear.
352 std::vector<IndirectSymbolData> &getIndirectSymbols() {
353 return IndirectSymbols;
354 }
355
356 indirect_symbol_iterator indirect_symbol_begin() {
357 return IndirectSymbols.begin();
358 }
359 const_indirect_symbol_iterator indirect_symbol_begin() const {
360 return IndirectSymbols.begin();
361 }
362
363 indirect_symbol_iterator indirect_symbol_end() {
364 return IndirectSymbols.end();
365 }
366 const_indirect_symbol_iterator indirect_symbol_end() const {
367 return IndirectSymbols.end();
368 }
369
370 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
371
372 /// @}
373 /// \name Linker Option List Access
374 /// @{
375
376 std::vector<std::vector<std::string>> &getLinkerOptions() {
377 return LinkerOptions;
378 }
379
380 /// @}
381 /// \name Data Region List Access
382 /// @{
383
384 // FIXME: This is a total hack, this should not be here. Once things are
385 // factored so that the streamer has direct access to the .o writer, it can
386 // disappear.
387 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
388
389 data_region_iterator data_region_begin() { return DataRegions.begin(); }
390 const_data_region_iterator data_region_begin() const {
391 return DataRegions.begin();
392 }
393
394 data_region_iterator data_region_end() { return DataRegions.end(); }
395 const_data_region_iterator data_region_end() const {
396 return DataRegions.end();
397 }
398
399 size_t data_region_size() const { return DataRegions.size(); }
400
401 /// @}
402 /// \name Data Region List Access
403 /// @{
404
405 // FIXME: This is a total hack, this should not be here. Once things are
406 // factored so that the streamer has direct access to the .o writer, it can
407 // disappear.
408 MCLOHContainer &getLOHContainer() { return LOHContainer; }
409 const MCLOHContainer &getLOHContainer() const {
410 return const_cast<MCAssembler *>(this)->getLOHContainer();
411 }
412 /// @}
413 /// \name Backend Data Access
414 /// @{
415
416 bool registerSection(MCSection &Section);
417
418 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
419
420 ArrayRef<std::string> getFileNames() { return FileNames; }
421
422 void addFileName(StringRef FileName) {
423 if (!is_contained(FileNames, FileName))
424 FileNames.push_back(FileName);
425 }
426
427 /// \brief Write the necessary bundle padding to the given object writer.
428 /// Expects a fragment \p F containing instructions and its size \p FSize.
429 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
430 MCObjectWriter *OW) const;
431
432 /// @}
433
434 void dump() const;
435};
436
437/// \brief Compute the amount of padding required before the fragment \p F to
438/// obey bundling restrictions, where \p FOffset is the fragment's offset in
439/// its section and \p FSize is the fragment's size.
440uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
441 uint64_t FOffset, uint64_t FSize);
442
443} // end namespace llvm
444
445#endif // LLVM_MC_MCASSEMBLER_H