blob: 1b76559d33b337fbe8d76f2796618e8af6e6f82f [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MCAssembler.h - Object File Generation -------------------*- 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_MCASSEMBLER_H
10#define LLVM_MC_MCASSEMBLER_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SmallPtrSet.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/iterator.h"
17#include "llvm/ADT/iterator_range.h"
18#include "llvm/BinaryFormat/MachO.h"
19#include "llvm/MC/MCDirectives.h"
20#include "llvm/MC/MCDwarf.h"
21#include "llvm/MC/MCFixup.h"
22#include "llvm/MC/MCFragment.h"
23#include "llvm/MC/MCLinkerOptimizationHint.h"
24#include "llvm/MC/MCSymbol.h"
Andrew Walbran16937d02019-10-22 13:54:20 +010025#include "llvm/Support/VersionTuple.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026#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;
Andrew Walbran16937d02019-10-22 13:54:20 +010097 /// An optional version of the SDK that was used to build the source.
98 VersionTuple SDKVersion;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099 };
100
101private:
102 MCContext &Context;
103
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100104 std::unique_ptr<MCAsmBackend> Backend;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100105
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100106 std::unique_ptr<MCCodeEmitter> Emitter;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100108 std::unique_ptr<MCObjectWriter> Writer;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109
110 SectionListType Sections;
111
112 SymbolDataListType Symbols;
113
114 std::vector<IndirectSymbolData> IndirectSymbols;
115
116 std::vector<DataRegionData> DataRegions;
117
118 /// The list of linker options to propagate into the object file.
119 std::vector<std::vector<std::string>> LinkerOptions;
120
121 /// List of declared file names
122 std::vector<std::string> FileNames;
123
124 MCDwarfLineTableParams LTParams;
125
126 /// The set of function symbols for which a .thumb_func directive has
127 /// been seen.
128 //
129 // FIXME: We really would like this in target specific code rather than
130 // here. Maybe when the relocation stuff moves to target specific,
131 // this can go with it? The streamer would need some target specific
132 // refactoring too.
133 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
134
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100135 /// The bundle alignment size currently set in the assembler.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100136 ///
137 /// By default it's 0, which means bundling is disabled.
138 unsigned BundleAlignSize;
139
140 bool RelaxAll : 1;
141 bool SubsectionsViaSymbols : 1;
142 bool IncrementalLinkerCompatible : 1;
143
144 /// ELF specific e_header flags
145 // It would be good if there were an MCELFAssembler class to hold this.
146 // ELF header flags are used both by the integrated and standalone assemblers.
147 // Access to the flags is necessary in cases where assembler directives affect
148 // which flags to be set.
149 unsigned ELFHeaderEFlags;
150
151 /// Used to communicate Linker Optimization Hint information between
152 /// the Streamer and the .o writer
153 MCLOHContainer LOHContainer;
154
155 VersionInfoType VersionInfo;
156
157 /// Evaluate a fixup to a relocatable expression and the value which should be
158 /// placed into the fixup.
159 ///
160 /// \param Layout The layout to use for evaluation.
161 /// \param Fixup The fixup to evaluate.
162 /// \param DF The fragment the fixup is inside.
163 /// \param Target [out] On return, the relocatable expression the fixup
164 /// evaluates to.
165 /// \param Value [out] On return, the value of the fixup as currently laid
166 /// out.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100167 /// \param WasForced [out] On return, the value in the fixup is set to the
168 /// correct value if WasForced is true, even if evaluateFixup returns false.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100169 /// \return Whether the fixup value was fully resolved. This is true if the
170 /// \p Value result is fixed, otherwise the value may change due to
171 /// relocation.
172 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
173 const MCFragment *DF, MCValue &Target,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100174 uint64_t &Value, bool &WasForced) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100175
176 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
177 /// (increased in size, in order to hold its value correctly).
178 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
179 const MCAsmLayout &Layout) const;
180
181 /// Check whether the given fragment needs relaxation.
182 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
183 const MCAsmLayout &Layout) const;
184
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100185 /// Perform one layout iteration and return true if any offsets
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186 /// were adjusted.
187 bool layoutOnce(MCAsmLayout &Layout);
188
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100189 /// Perform one layout iteration of the given section and return true
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100190 /// if any offsets were adjusted.
191 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
192
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200193 /// Perform relaxation on a single fragment - returns true if the fragment
194 /// changes as a result of relaxation.
195 bool relaxFragment(MCAsmLayout &Layout, MCFragment &F);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100196 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100197 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200198 bool relaxBoundaryAlign(MCAsmLayout &Layout, MCBoundaryAlignFragment &BF);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100199 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
200 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
201 MCDwarfCallFrameFragment &DF);
202 bool relaxCVInlineLineTable(MCAsmLayout &Layout,
203 MCCVInlineLineTableFragment &DF);
204 bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200205 bool relaxPseudoProbeAddr(MCAsmLayout &Layout, MCPseudoProbeAddrFragment &DF);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100206
207 /// finishLayout - Finalize a layout, including fragment lowering.
208 void finishLayout(MCAsmLayout &Layout);
209
210 std::tuple<MCValue, uint64_t, bool>
211 handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
212
213public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200214 struct Symver {
215 StringRef Name;
216 const MCSymbol *Sym;
217 SMLoc Loc;
218 };
219 std::vector<Symver> Symvers;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100220
221 /// Construct a new assembler instance.
222 //
223 // FIXME: How are we going to parameterize this? Two obvious options are stay
224 // concrete and require clients to pass in a target like object. The other
225 // option is to make this abstract, and have targets provide concrete
226 // implementations as we do with AsmParser.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100227 MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
228 std::unique_ptr<MCCodeEmitter> Emitter,
229 std::unique_ptr<MCObjectWriter> Writer);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100230 MCAssembler(const MCAssembler &) = delete;
231 MCAssembler &operator=(const MCAssembler &) = delete;
232 ~MCAssembler();
233
234 /// Compute the effective fragment size assuming it is laid out at the given
235 /// \p SectionAddress and \p FragmentOffset.
236 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
237 const MCFragment &F) const;
238
239 /// Find the symbol which defines the atom containing the given symbol, or
240 /// null if there is no such symbol.
241 const MCSymbol *getAtom(const MCSymbol &S) const;
242
243 /// Check whether a particular symbol is visible to the linker and is required
244 /// in the symbol table, or whether it can be discarded by the assembler. This
245 /// also effects whether the assembler treats the label as potentially
246 /// defining a separate atom.
247 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
248
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100249 /// Emit the section contents to \p OS.
250 void writeSectionData(raw_ostream &OS, const MCSection *Section,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100251 const MCAsmLayout &Layout) const;
252
253 /// Check whether a given symbol has been flagged with .thumb_func.
254 bool isThumbFunc(const MCSymbol *Func) const;
255
256 /// Flag a function symbol as the target of a .thumb_func directive.
257 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
258
259 /// ELF e_header flags
260 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
261 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
262
263 /// MachO deployment target version information.
264 const VersionInfoType &getVersionInfo() const { return VersionInfo; }
265 void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
Andrew Walbran16937d02019-10-22 13:54:20 +0100266 unsigned Update,
267 VersionTuple SDKVersion = VersionTuple()) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100268 VersionInfo.EmitBuildVersion = false;
269 VersionInfo.TypeOrPlatform.Type = Type;
270 VersionInfo.Major = Major;
271 VersionInfo.Minor = Minor;
272 VersionInfo.Update = Update;
Andrew Walbran16937d02019-10-22 13:54:20 +0100273 VersionInfo.SDKVersion = SDKVersion;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100274 }
275 void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
Andrew Walbran16937d02019-10-22 13:54:20 +0100276 unsigned Minor, unsigned Update,
277 VersionTuple SDKVersion = VersionTuple()) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100278 VersionInfo.EmitBuildVersion = true;
279 VersionInfo.TypeOrPlatform.Platform = Platform;
280 VersionInfo.Major = Major;
281 VersionInfo.Minor = Minor;
282 VersionInfo.Update = Update;
Andrew Walbran16937d02019-10-22 13:54:20 +0100283 VersionInfo.SDKVersion = SDKVersion;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100284 }
285
286 /// Reuse an assembler instance
287 ///
288 void reset();
289
290 MCContext &getContext() const { return Context; }
291
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100292 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100293
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100294 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100295
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100296 MCObjectWriter *getWriterPtr() const { return Writer.get(); }
297
298 MCAsmBackend &getBackend() const { return *Backend; }
299
300 MCCodeEmitter &getEmitter() const { return *Emitter; }
301
302 MCObjectWriter &getWriter() const { return *Writer; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100303
304 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
305 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
306
307 /// Finish - Do final processing and write the object to the output stream.
308 /// \p Writer is used for custom object writer (as the MCJIT does),
309 /// if not specified it is automatically created from backend.
310 void Finish();
311
312 // Layout all section and prepare them for emission.
313 void layout(MCAsmLayout &Layout);
314
315 // FIXME: This does not belong here.
316 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
317 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
318
319 bool isIncrementalLinkerCompatible() const {
320 return IncrementalLinkerCompatible;
321 }
322 void setIncrementalLinkerCompatible(bool Value) {
323 IncrementalLinkerCompatible = Value;
324 }
325
326 bool getRelaxAll() const { return RelaxAll; }
327 void setRelaxAll(bool Value) { RelaxAll = Value; }
328
329 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
330
331 unsigned getBundleAlignSize() const { return BundleAlignSize; }
332
333 void setBundleAlignSize(unsigned Size) {
334 assert((Size == 0 || !(Size & (Size - 1))) &&
335 "Expect a power-of-two bundle align size");
336 BundleAlignSize = Size;
337 }
338
339 /// \name Section List Access
340 /// @{
341
342 iterator begin() { return Sections.begin(); }
343 const_iterator begin() const { return Sections.begin(); }
344
345 iterator end() { return Sections.end(); }
346 const_iterator end() const { return Sections.end(); }
347
348 size_t size() const { return Sections.size(); }
349
350 /// @}
351 /// \name Symbol List Access
352 /// @{
353 symbol_iterator symbol_begin() { return Symbols.begin(); }
354 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
355
356 symbol_iterator symbol_end() { return Symbols.end(); }
357 const_symbol_iterator symbol_end() const { return Symbols.end(); }
358
359 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
360 const_symbol_range symbols() const {
361 return make_range(symbol_begin(), symbol_end());
362 }
363
364 size_t symbol_size() const { return Symbols.size(); }
365
366 /// @}
367 /// \name Indirect Symbol List Access
368 /// @{
369
370 // FIXME: This is a total hack, this should not be here. Once things are
371 // factored so that the streamer has direct access to the .o writer, it can
372 // disappear.
373 std::vector<IndirectSymbolData> &getIndirectSymbols() {
374 return IndirectSymbols;
375 }
376
377 indirect_symbol_iterator indirect_symbol_begin() {
378 return IndirectSymbols.begin();
379 }
380 const_indirect_symbol_iterator indirect_symbol_begin() const {
381 return IndirectSymbols.begin();
382 }
383
384 indirect_symbol_iterator indirect_symbol_end() {
385 return IndirectSymbols.end();
386 }
387 const_indirect_symbol_iterator indirect_symbol_end() const {
388 return IndirectSymbols.end();
389 }
390
391 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
392
393 /// @}
394 /// \name Linker Option List Access
395 /// @{
396
397 std::vector<std::vector<std::string>> &getLinkerOptions() {
398 return LinkerOptions;
399 }
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 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
409
410 data_region_iterator data_region_begin() { return DataRegions.begin(); }
411 const_data_region_iterator data_region_begin() const {
412 return DataRegions.begin();
413 }
414
415 data_region_iterator data_region_end() { return DataRegions.end(); }
416 const_data_region_iterator data_region_end() const {
417 return DataRegions.end();
418 }
419
420 size_t data_region_size() const { return DataRegions.size(); }
421
422 /// @}
423 /// \name Data Region List Access
424 /// @{
425
426 // FIXME: This is a total hack, this should not be here. Once things are
427 // factored so that the streamer has direct access to the .o writer, it can
428 // disappear.
429 MCLOHContainer &getLOHContainer() { return LOHContainer; }
430 const MCLOHContainer &getLOHContainer() const {
431 return const_cast<MCAssembler *>(this)->getLOHContainer();
432 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100433
434 struct CGProfileEntry {
435 const MCSymbolRefExpr *From;
436 const MCSymbolRefExpr *To;
437 uint64_t Count;
438 };
439 std::vector<CGProfileEntry> CGProfile;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100440 /// @}
441 /// \name Backend Data Access
442 /// @{
443
444 bool registerSection(MCSection &Section);
445
446 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
447
448 ArrayRef<std::string> getFileNames() { return FileNames; }
449
450 void addFileName(StringRef FileName) {
451 if (!is_contained(FileNames, FileName))
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200452 FileNames.push_back(std::string(FileName));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100453 }
454
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100455 /// Write the necessary bundle padding to \p OS.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100456 /// Expects a fragment \p F containing instructions and its size \p FSize.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100457 void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,
458 uint64_t FSize) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100459
460 /// @}
461
462 void dump() const;
463};
464
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100465/// Compute the amount of padding required before the fragment \p F to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100466/// obey bundling restrictions, where \p FOffset is the fragment's offset in
467/// its section and \p FSize is the fragment's size.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100468uint64_t computeBundlePadding(const MCAssembler &Assembler,
469 const MCEncodedFragment *F, uint64_t FOffset,
470 uint64_t FSize);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100471
472} // end namespace llvm
473
474#endif // LLVM_MC_MCASSEMBLER_H