Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
MachObjectWriter.cpp
Go to the documentation of this file.
1//===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
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
9#include "llvm/ADT/DenseMap.h"
10#include "llvm/ADT/Twine.h"
11#include "llvm/BinaryFormat/MachO.h"
12#include "llvm/MC/MCAsmBackend.h"
13#include "llvm/MC/MCAsmInfoDarwin.h"
14#include "llvm/MC/MCAssembler.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCDirectives.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCFixupKindInfo.h"
19#include "llvm/MC/MCFragment.h"
20#include "llvm/MC/MCMachObjectWriter.h"
21#include "llvm/MC/MCObjectFileInfo.h"
22#include "llvm/MC/MCObjectWriter.h"
23#include "llvm/MC/MCSection.h"
24#include "llvm/MC/MCSectionMachO.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCSymbolMachO.h"
27#include "llvm/MC/MCValue.h"
28#include "llvm/Support/Alignment.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/raw_ostream.h"
34#include <algorithm>
35#include <cassert>
36#include <cstdint>
37#include <string>
38#include <utility>
39#include <vector>
40
41using namespacellvm;
42
43#define DEBUG_TYPE "mc"
44
45voidMachObjectWriter::reset() {
46 Relocations.clear();
47 IndirectSymBase.clear();
48 IndirectSymbols.clear();
49 DataRegions.clear();
50 SectionAddress.clear();
51 SectionOrder.clear();
52StringTable.clear();
53 LocalSymbolData.clear();
54 ExternalSymbolData.clear();
55 UndefinedSymbolData.clear();
56 LOHContainer.reset();
57 VersionInfo.Major = 0;
58 VersionInfo.SDKVersion =VersionTuple();
59 TargetVariantVersionInfo.Major = 0;
60 TargetVariantVersionInfo.SDKVersion =VersionTuple();
61 LinkerOptions.clear();
62MCObjectWriter::reset();
63}
64
65boolMachObjectWriter::doesSymbolRequireExternRelocation(constMCSymbol &S) {
66// Undefined symbols are always extern.
67if (S.isUndefined())
68returntrue;
69
70// References to weak definitions require external relocation entries; the
71// definition may not always be the one in the same object file.
72if (cast<MCSymbolMachO>(S).isWeakDefinition())
73returntrue;
74
75// Otherwise, we can use an internal relocation.
76returnfalse;
77}
78
79bool MachObjectWriter::
80MachSymbolData::operator<(const MachSymbolData &RHS) const{
81return Symbol->getName() <RHS.Symbol->getName();
82}
83
84boolMachObjectWriter::isFixupKindPCRel(constMCAssembler &Asm,unsigned Kind) {
85constMCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo(
86 (MCFixupKind) Kind);
87
88return FKI.Flags &MCFixupKindInfo::FKF_IsPCRel;
89}
90
91uint64_t
92MachObjectWriter::getFragmentAddress(constMCAssembler &Asm,
93constMCFragment *Fragment) const{
94returngetSectionAddress(Fragment->getParent()) +
95 Asm.getFragmentOffset(*Fragment);
96}
97
98uint64_tMachObjectWriter::getSymbolAddress(constMCSymbol &S,
99constMCAssembler &Asm) const{
100// If this is a variable, then recursively evaluate now.
101if (S.isVariable()) {
102if (constMCConstantExpr *C =
103 dyn_cast<const MCConstantExpr>(S.getVariableValue()))
104returnC->getValue();
105
106MCValueTarget;
107if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Asm,nullptr))
108report_fatal_error("unable to evaluate offset for variable '" +
109 S.getName() +"'");
110
111// Verify that any used symbols are defined.
112if (Target.getSymA() &&Target.getSymA()->getSymbol().isUndefined())
113report_fatal_error("unable to evaluate offset to undefined symbol '" +
114Target.getSymA()->getSymbol().getName() +"'");
115if (Target.getSymB() &&Target.getSymB()->getSymbol().isUndefined())
116report_fatal_error("unable to evaluate offset to undefined symbol '" +
117Target.getSymB()->getSymbol().getName() +"'");
118
119uint64_tAddress =Target.getConstant();
120if (Target.getSymA())
121Address +=getSymbolAddress(Target.getSymA()->getSymbol(), Asm);
122if (Target.getSymB())
123Address +=getSymbolAddress(Target.getSymB()->getSymbol(), Asm);
124returnAddress;
125 }
126
127returngetSectionAddress(S.getFragment()->getParent()) +
128 Asm.getSymbolOffset(S);
129}
130
131uint64_tMachObjectWriter::getPaddingSize(constMCAssembler &Asm,
132constMCSection *Sec) const{
133uint64_t EndAddr =getSectionAddress(Sec) + Asm.getSectionAddressSize(*Sec);
134unsigned Next = cast<MCSectionMachO>(Sec)->getLayoutOrder() + 1;
135if (Next >= SectionOrder.size())
136return 0;
137
138constMCSection &NextSec = *SectionOrder[Next];
139if (NextSec.isVirtualSection())
140return 0;
141returnoffsetToAlignment(EndAddr, NextSec.getAlign());
142}
143
144staticboolisSymbolLinkerVisible(constMCSymbol &Symbol) {
145// Non-temporary labels should always be visible to the linker.
146if (!Symbol.isTemporary())
147returntrue;
148
149if (Symbol.isUsedInReloc())
150returntrue;
151
152returnfalse;
153}
154
155constMCSymbol *MachObjectWriter::getAtom(constMCSymbol &S) const{
156// Linker visible symbols define atoms.
157if (isSymbolLinkerVisible(S))
158return &S;
159
160// Absolute and undefined symbols have no defining atom.
161if (!S.isInSection())
162returnnullptr;
163
164// Non-linker visible symbols in sections which can't be atomized have no
165// defining atom.
166if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(
167 *S.getFragment()->getParent()))
168returnnullptr;
169
170// Otherwise, return the atom for the containing fragment.
171return S.getFragment()->getAtom();
172}
173
174voidMachObjectWriter::writeHeader(MachO::HeaderFileTypeType,
175unsigned NumLoadCommands,
176unsigned LoadCommandsSize,
177bool SubsectionsViaSymbols) {
178uint32_t Flags = 0;
179
180if (SubsectionsViaSymbols)
181 Flags |=MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
182
183// struct mach_header (28 bytes) or
184// struct mach_header_64 (32 bytes)
185
186uint64_t Start =W.OS.tell();
187 (void) Start;
188
189W.write<uint32_t>(is64Bit() ?MachO::MH_MAGIC_64 :MachO::MH_MAGIC);
190
191W.write<uint32_t>(TargetObjectWriter->getCPUType());
192
193uint32_t Cpusubtype = TargetObjectWriter->getCPUSubtype();
194
195// Promote arm64e subtypes to always be ptrauth-ABI-versioned, at version 0.
196// We never need to emit unversioned binaries.
197// And we don't support arbitrary ABI versions (or the kernel flag) yet.
198if (TargetObjectWriter->getCPUType() ==MachO::CPU_TYPE_ARM64 &&
199 Cpusubtype ==MachO::CPU_SUBTYPE_ARM64E)
200 Cpusubtype =MachO::CPU_SUBTYPE_ARM64E_WITH_PTRAUTH_VERSION(
201/*PtrAuthABIVersion=*/0,/*PtrAuthKernelABIVersion=*/false);
202
203W.write<uint32_t>(Cpusubtype);
204
205W.write<uint32_t>(Type);
206W.write<uint32_t>(NumLoadCommands);
207W.write<uint32_t>(LoadCommandsSize);
208W.write<uint32_t>(Flags);
209if (is64Bit())
210W.write<uint32_t>(0);// reserved
211
212assert(W.OS.tell() - Start == (is64Bit() ?sizeof(MachO::mach_header_64)
213 :sizeof(MachO::mach_header)));
214}
215
216void MachObjectWriter::writeWithPadding(StringRef Str,uint64_tSize) {
217assert(Size >= Str.size());
218W.OS << Str;
219W.OS.write_zeros(Size - Str.size());
220}
221
222/// writeSegmentLoadCommand - Write a segment load command.
223///
224/// \param NumSections The number of sections in this segment.
225/// \param SectionDataSize The total size of the sections.
226voidMachObjectWriter::writeSegmentLoadCommand(
227StringRefName,unsigned NumSections,uint64_t VMAddr,uint64_t VMSize,
228uint64_t SectionDataStartOffset,uint64_t SectionDataSize,uint32_t MaxProt,
229uint32_t InitProt) {
230// struct segment_command (56 bytes) or
231// struct segment_command_64 (72 bytes)
232
233uint64_t Start =W.OS.tell();
234 (void) Start;
235
236unsigned SegmentLoadCommandSize =
237is64Bit() ?sizeof(MachO::segment_command_64):
238sizeof(MachO::segment_command);
239W.write<uint32_t>(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
240W.write<uint32_t>(SegmentLoadCommandSize +
241 NumSections * (is64Bit() ?sizeof(MachO::section_64) :
242sizeof(MachO::section)));
243
244 writeWithPadding(Name, 16);
245if (is64Bit()) {
246W.write<uint64_t>(VMAddr);// vmaddr
247W.write<uint64_t>(VMSize);// vmsize
248W.write<uint64_t>(SectionDataStartOffset);// file offset
249W.write<uint64_t>(SectionDataSize);// file size
250 }else {
251W.write<uint32_t>(VMAddr);// vmaddr
252W.write<uint32_t>(VMSize);// vmsize
253W.write<uint32_t>(SectionDataStartOffset);// file offset
254W.write<uint32_t>(SectionDataSize);// file size
255 }
256// maxprot
257W.write<uint32_t>(MaxProt);
258// initprot
259W.write<uint32_t>(InitProt);
260W.write<uint32_t>(NumSections);
261W.write<uint32_t>(0);// flags
262
263assert(W.OS.tell() - Start == SegmentLoadCommandSize);
264}
265
266voidMachObjectWriter::writeSection(constMCAssembler &Asm,
267constMCSection &Sec,uint64_t VMAddr,
268uint64_t FileOffset,unsigned Flags,
269uint64_t RelocationsStart,
270unsigned NumRelocations) {
271uint64_t SectionSize = Asm.getSectionAddressSize(Sec);
272constMCSectionMachO &Section = cast<MCSectionMachO>(Sec);
273
274// The offset is unused for virtual sections.
275if (Section.isVirtualSection()) {
276assert(Asm.getSectionFileSize(Sec) == 0 &&"Invalid file size!");
277 FileOffset = 0;
278 }
279
280// struct section (68 bytes) or
281// struct section_64 (80 bytes)
282
283uint64_t Start =W.OS.tell();
284 (void) Start;
285
286 writeWithPadding(Section.getName(), 16);
287 writeWithPadding(Section.getSegmentName(), 16);
288if (is64Bit()) {
289W.write<uint64_t>(VMAddr);// address
290W.write<uint64_t>(SectionSize);// size
291 }else {
292W.write<uint32_t>(VMAddr);// address
293W.write<uint32_t>(SectionSize);// size
294 }
295assert(isUInt<32>(FileOffset) &&"Cannot encode offset of section");
296W.write<uint32_t>(FileOffset);
297
298W.write<uint32_t>(Log2(Section.getAlign()));
299assert((!NumRelocations || isUInt<32>(RelocationsStart)) &&
300"Cannot encode offset of relocations");
301W.write<uint32_t>(NumRelocations ? RelocationsStart : 0);
302W.write<uint32_t>(NumRelocations);
303W.write<uint32_t>(Flags);
304W.write<uint32_t>(IndirectSymBase.lookup(&Sec));// reserved1
305W.write<uint32_t>(Section.getStubSize());// reserved2
306if (is64Bit())
307W.write<uint32_t>(0);// reserved3
308
309assert(W.OS.tell() - Start ==
310 (is64Bit() ?sizeof(MachO::section_64) :sizeof(MachO::section)));
311}
312
313voidMachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset,
314uint32_t NumSymbols,
315uint32_t StringTableOffset,
316uint32_t StringTableSize) {
317// struct symtab_command (24 bytes)
318
319uint64_t Start =W.OS.tell();
320 (void) Start;
321
322W.write<uint32_t>(MachO::LC_SYMTAB);
323W.write<uint32_t>(sizeof(MachO::symtab_command));
324W.write<uint32_t>(SymbolOffset);
325W.write<uint32_t>(NumSymbols);
326W.write<uint32_t>(StringTableOffset);
327W.write<uint32_t>(StringTableSize);
328
329assert(W.OS.tell() - Start ==sizeof(MachO::symtab_command));
330}
331
332voidMachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol,
333uint32_t NumLocalSymbols,
334uint32_t FirstExternalSymbol,
335uint32_t NumExternalSymbols,
336uint32_t FirstUndefinedSymbol,
337uint32_t NumUndefinedSymbols,
338uint32_t IndirectSymbolOffset,
339uint32_t NumIndirectSymbols) {
340// struct dysymtab_command (80 bytes)
341
342uint64_t Start =W.OS.tell();
343 (void) Start;
344
345W.write<uint32_t>(MachO::LC_DYSYMTAB);
346W.write<uint32_t>(sizeof(MachO::dysymtab_command));
347W.write<uint32_t>(FirstLocalSymbol);
348W.write<uint32_t>(NumLocalSymbols);
349W.write<uint32_t>(FirstExternalSymbol);
350W.write<uint32_t>(NumExternalSymbols);
351W.write<uint32_t>(FirstUndefinedSymbol);
352W.write<uint32_t>(NumUndefinedSymbols);
353W.write<uint32_t>(0);// tocoff
354W.write<uint32_t>(0);// ntoc
355W.write<uint32_t>(0);// modtaboff
356W.write<uint32_t>(0);// nmodtab
357W.write<uint32_t>(0);// extrefsymoff
358W.write<uint32_t>(0);// nextrefsyms
359W.write<uint32_t>(IndirectSymbolOffset);
360W.write<uint32_t>(NumIndirectSymbols);
361W.write<uint32_t>(0);// extreloff
362W.write<uint32_t>(0);// nextrel
363W.write<uint32_t>(0);// locreloff
364W.write<uint32_t>(0);// nlocrel
365
366assert(W.OS.tell() - Start ==sizeof(MachO::dysymtab_command));
367}
368
369MachObjectWriter::MachSymbolData *
370MachObjectWriter::findSymbolData(constMCSymbol &Sym) {
371for (auto *SymbolData :
372 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
373for (MachSymbolData &Entry : *SymbolData)
374if (Entry.Symbol == &Sym)
375return &Entry;
376
377returnnullptr;
378}
379
380constMCSymbol &MachObjectWriter::findAliasedSymbol(constMCSymbol &Sym) const{
381constMCSymbol *S = &Sym;
382while (S->isVariable()) {
383constMCExpr *Value = S->getVariableValue();
384constauto *Ref = dyn_cast<MCSymbolRefExpr>(Value);
385if (!Ref)
386return *S;
387 S = &Ref->getSymbol();
388 }
389return *S;
390}
391
392voidMachObjectWriter::writeNlist(MachSymbolData &MSD,constMCAssembler &Asm) {
393constMCSymbol *Symbol = MSD.Symbol;
394constauto &Data = cast<MCSymbolMachO>(*Symbol);
395constMCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol);
396uint8_t SectionIndex = MSD.SectionIndex;
397uint8_tType = 0;
398uint64_tAddress = 0;
399bool IsAlias = Symbol != AliasedSymbol;
400
401constMCSymbol &OrigSymbol = *Symbol;
402 MachSymbolData *AliaseeInfo;
403if (IsAlias) {
404 AliaseeInfo = findSymbolData(*AliasedSymbol);
405if (AliaseeInfo)
406 SectionIndex = AliaseeInfo->SectionIndex;
407 Symbol = AliasedSymbol;
408// FIXME: Should this update Data as well?
409 }
410
411// Set the N_TYPE bits. See <mach-o/nlist.h>.
412//
413// FIXME: Are the prebound or indirect fields possible here?
414if (IsAlias && Symbol->isUndefined())
415Type =MachO::N_INDR;
416elseif (Symbol->isUndefined())
417Type =MachO::N_UNDF;
418elseif (Symbol->isAbsolute())
419Type =MachO::N_ABS;
420else
421Type =MachO::N_SECT;
422
423// FIXME: Set STAB bits.
424
425if (Data.isPrivateExtern())
426Type |=MachO::N_PEXT;
427
428// Set external bit.
429if (Data.isExternal() || (!IsAlias && Symbol->isUndefined()))
430Type |=MachO::N_EXT;
431
432// Compute the symbol address.
433if (IsAlias && Symbol->isUndefined())
434Address = AliaseeInfo->StringIndex;
435elseif (Symbol->isDefined())
436Address =getSymbolAddress(OrigSymbol, Asm);
437elseif (Symbol->isCommon()) {
438// Common symbols are encoded with the size in the address
439// field, and their alignment in the flags.
440Address = Symbol->getCommonSize();
441 }
442
443// struct nlist (12 bytes)
444
445W.write<uint32_t>(MSD.StringIndex);
446W.OS <<char(Type);
447W.OS <<char(SectionIndex);
448
449// The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
450// value.
451bool EncodeAsAltEntry =
452 IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry();
453W.write<uint16_t>(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry));
454if (is64Bit())
455W.write<uint64_t>(Address);
456else
457W.write<uint32_t>(Address);
458}
459
460voidMachObjectWriter::writeLinkeditLoadCommand(uint32_tType,
461uint32_t DataOffset,
462uint32_t DataSize) {
463uint64_t Start =W.OS.tell();
464 (void) Start;
465
466W.write<uint32_t>(Type);
467W.write<uint32_t>(sizeof(MachO::linkedit_data_command));
468W.write<uint32_t>(DataOffset);
469W.write<uint32_t>(DataSize);
470
471assert(W.OS.tell() - Start ==sizeof(MachO::linkedit_data_command));
472}
473
474staticunsignedComputeLinkerOptionsLoadCommandSize(
475const std::vector<std::string> &Options,boolis64Bit)
476{
477unsignedSize =sizeof(MachO::linker_option_command);
478for (const std::string &Option :Options)
479Size += Option.size() + 1;
480returnalignTo(Size,is64Bit ? 8 : 4);
481}
482
483voidMachObjectWriter::writeLinkerOptionsLoadCommand(
484const std::vector<std::string> &Options)
485{
486unsignedSize =ComputeLinkerOptionsLoadCommandSize(Options,is64Bit());
487uint64_t Start =W.OS.tell();
488 (void) Start;
489
490W.write<uint32_t>(MachO::LC_LINKER_OPTION);
491W.write<uint32_t>(Size);
492W.write<uint32_t>(Options.size());
493uint64_t BytesWritten =sizeof(MachO::linker_option_command);
494for (const std::string &Option :Options) {
495// Write each string, including the null byte.
496W.OS << Option <<'\0';
497 BytesWritten += Option.size() + 1;
498 }
499
500// Pad to a multiple of the pointer size.
501W.OS.write_zeros(
502offsetToAlignment(BytesWritten,is64Bit() ?Align(8) :Align(4)));
503
504assert(W.OS.tell() - Start ==Size);
505}
506
507staticboolisFixupTargetValid(constMCValue &Target) {
508// Target is (LHS - RHS + cst).
509// We don't support the form where LHS is null: -RHS + cst
510if (!Target.getSymA() &&Target.getSymB())
511returnfalse;
512returntrue;
513}
514
515voidMachObjectWriter::recordRelocation(MCAssembler &Asm,
516constMCFragment *Fragment,
517constMCFixup &Fixup,MCValueTarget,
518uint64_t &FixedValue) {
519if (!isFixupTargetValid(Target)) {
520 Asm.getContext().reportError(Fixup.getLoc(),
521"unsupported relocation expression");
522return;
523 }
524
525 TargetObjectWriter->recordRelocation(this, Asm, Fragment,Fixup,Target,
526 FixedValue);
527}
528
529voidMachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {
530// This is the point where 'as' creates actual symbols for indirect symbols
531// (in the following two passes). It would be easier for us to do this sooner
532// when we see the attribute, but that makes getting the order in the symbol
533// table much more complicated than it is worth.
534//
535// FIXME: Revisit this when the dust settles.
536
537// Report errors for use of .indirect_symbol not in a symbol pointer section
538// or stub section.
539for (IndirectSymbolData &ISD : IndirectSymbols) {
540constMCSectionMachO &Section = cast<MCSectionMachO>(*ISD.Section);
541
542if (Section.getType() !=MachO::S_NON_LAZY_SYMBOL_POINTERS &&
543 Section.getType() !=MachO::S_LAZY_SYMBOL_POINTERS &&
544 Section.getType() !=MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
545 Section.getType() !=MachO::S_SYMBOL_STUBS) {
546MCSymbol &Symbol = *ISD.Symbol;
547report_fatal_error("indirect symbol '" + Symbol.getName() +
548"' not in a symbol pointer or stub section");
549 }
550 }
551
552// Bind non-lazy symbol pointers first.
553for (auto [IndirectIndex, ISD] :enumerate(IndirectSymbols)) {
554constauto &Section = cast<MCSectionMachO>(*ISD.Section);
555
556if (Section.getType() !=MachO::S_NON_LAZY_SYMBOL_POINTERS &&
557 Section.getType() !=MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
558continue;
559
560// Initialize the section indirect symbol base, if necessary.
561 IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));
562
563 Asm.registerSymbol(*ISD.Symbol);
564 }
565
566// Then lazy symbol pointers and symbol stubs.
567for (auto [IndirectIndex, ISD] :enumerate(IndirectSymbols)) {
568constauto &Section = cast<MCSectionMachO>(*ISD.Section);
569
570if (Section.getType() !=MachO::S_LAZY_SYMBOL_POINTERS &&
571 Section.getType() !=MachO::S_SYMBOL_STUBS)
572continue;
573
574// Initialize the section indirect symbol base, if necessary.
575 IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));
576
577// Set the symbol type to undefined lazy, but only on construction.
578//
579// FIXME: Do not hardcode.
580if (Asm.registerSymbol(*ISD.Symbol))
581 cast<MCSymbolMachO>(ISD.Symbol)->setReferenceTypeUndefinedLazy(true);
582 }
583}
584
585/// computeSymbolTable - Compute the symbol table data
586voidMachObjectWriter::computeSymbolTable(
587MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
588 std::vector<MachSymbolData> &ExternalSymbolData,
589 std::vector<MachSymbolData> &UndefinedSymbolData) {
590// Build section lookup table.
591DenseMap<const MCSection*, uint8_t> SectionIndexMap;
592unsigned Index = 1;
593for (MCSection &Sec : Asm)
594 SectionIndexMap[&Sec] = Index++;
595assert(Index <= 256 &&"Too many sections!");
596
597// Build the string table.
598for (constMCSymbol &Symbol : Asm.symbols()) {
599if (!cast<MCSymbolMachO>(Symbol).isSymbolLinkerVisible())
600continue;
601
602StringTable.add(Symbol.getName());
603 }
604StringTable.finalize();
605
606// Build the symbol arrays but only for non-local symbols.
607//
608// The particular order that we collect and then sort the symbols is chosen to
609// match 'as'. Even though it doesn't matter for correctness, this is
610// important for letting us diff .o files.
611for (constMCSymbol &Symbol : Asm.symbols()) {
612// Ignore non-linker visible symbols.
613if (!cast<MCSymbolMachO>(Symbol).isSymbolLinkerVisible())
614continue;
615
616if (!Symbol.isExternal() && !Symbol.isUndefined())
617continue;
618
619 MachSymbolData MSD;
620 MSD.Symbol = &Symbol;
621 MSD.StringIndex =StringTable.getOffset(Symbol.getName());
622
623if (Symbol.isUndefined()) {
624 MSD.SectionIndex = 0;
625 UndefinedSymbolData.push_back(MSD);
626 }elseif (Symbol.isAbsolute()) {
627 MSD.SectionIndex = 0;
628 ExternalSymbolData.push_back(MSD);
629 }else {
630 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
631assert(MSD.SectionIndex &&"Invalid section index!");
632 ExternalSymbolData.push_back(MSD);
633 }
634 }
635
636// Now add the data for local symbols.
637for (constMCSymbol &Symbol : Asm.symbols()) {
638// Ignore non-linker visible symbols.
639if (!cast<MCSymbolMachO>(Symbol).isSymbolLinkerVisible())
640continue;
641
642if (Symbol.isExternal() || Symbol.isUndefined())
643continue;
644
645 MachSymbolData MSD;
646 MSD.Symbol = &Symbol;
647 MSD.StringIndex =StringTable.getOffset(Symbol.getName());
648
649if (Symbol.isAbsolute()) {
650 MSD.SectionIndex = 0;
651 LocalSymbolData.push_back(MSD);
652 }else {
653 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
654assert(MSD.SectionIndex &&"Invalid section index!");
655 LocalSymbolData.push_back(MSD);
656 }
657 }
658
659// External and undefined symbols are required to be in lexicographic order.
660llvm::sort(ExternalSymbolData);
661llvm::sort(UndefinedSymbolData);
662
663// Set the symbol indices.
664 Index = 0;
665for (auto *SymbolData :
666 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
667for (MachSymbolData &Entry : *SymbolData)
668 Entry.Symbol->setIndex(Index++);
669
670for (constMCSection &Section : Asm) {
671for (RelAndSymbol &Rel : Relocations[&Section]) {
672if (!Rel.Sym)
673continue;
674
675// Set the Index and the IsExtern bit.
676unsigned Index = Rel.Sym->getIndex();
677assert(isInt<24>(Index));
678if (W.Endian ==llvm::endianness::little)
679 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27);
680else
681 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4);
682 }
683 }
684}
685
686voidMachObjectWriter::computeSectionAddresses(constMCAssembler &Asm) {
687// Assign layout order indices to sections.
688unsigned i = 0;
689// Compute the section layout order. Virtual sections must go last.
690for (MCSection &Sec : Asm) {
691if (!Sec.isVirtualSection()) {
692 SectionOrder.push_back(&Sec);
693 cast<MCSectionMachO>(Sec).setLayoutOrder(i++);
694 }
695 }
696for (MCSection &Sec : Asm) {
697if (Sec.isVirtualSection()) {
698 SectionOrder.push_back(&Sec);
699 cast<MCSectionMachO>(Sec).setLayoutOrder(i++);
700 }
701 }
702
703uint64_t StartAddress = 0;
704for (constMCSection *Sec : SectionOrder) {
705 StartAddress =alignTo(StartAddress, Sec->getAlign());
706 SectionAddress[Sec] = StartAddress;
707 StartAddress += Asm.getSectionAddressSize(*Sec);
708
709// Explicitly pad the section to match the alignment requirements of the
710// following one. This is for 'gas' compatibility, it shouldn't
711 /// strictly be necessary.
712 StartAddress +=getPaddingSize(Asm, Sec);
713 }
714}
715
716voidMachObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
717computeSectionAddresses(Asm);
718
719// Create symbol data for any indirect symbols.
720bindIndirectSymbols(Asm);
721}
722
723boolMachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
724constMCAssembler &Asm,constMCSymbol &SymA,constMCFragment &FB,
725bool InSet,bool IsPCRel) const{
726if (InSet)
727returntrue;
728
729// The effective address is
730// addr(atom(A)) + offset(A)
731// - addr(atom(B)) - offset(B)
732// and the offsets are not relocatable, so the fixup is fully resolved when
733// addr(atom(A)) - addr(atom(B)) == 0.
734constMCSymbol &SA =findAliasedSymbol(SymA);
735constMCSection &SecA = SA.getSection();
736constMCSection &SecB = *FB.getParent();
737
738if (IsPCRel) {
739// The simple (Darwin, except on x86_64) way of dealing with this was to
740// assume that any reference to a temporary symbol *must* be a temporary
741// symbol in the same atom, unless the sections differ. Therefore, any PCrel
742// relocation to a temporary symbol (in the same section) is fully
743// resolved. This also works in conjunction with absolutized .set, which
744// requires the compiler to use .set to absolutize the differences between
745// symbols which the compiler knows to be assembly time constants, so we
746// don't need to worry about considering symbol differences fully resolved.
747//
748// If the file isn't using sub-sections-via-symbols, we can make the
749// same assumptions about any symbol that we normally make about
750// assembler locals.
751
752bool hasReliableSymbolDifference =isX86_64();
753if (!hasReliableSymbolDifference) {
754if (!SA.isInSection() || &SecA != &SecB ||
755 (!SA.isTemporary() && FB.getAtom() != SA.getFragment()->getAtom() &&
756SubsectionsViaSymbols))
757returnfalse;
758returntrue;
759 }
760 }
761
762// If they are not in the same section, we can't compute the diff.
763if (&SecA != &SecB)
764returnfalse;
765
766// If the atoms are the same, they are guaranteed to have the same address.
767return SA.getFragment()->getAtom() == FB.getAtom();
768}
769
770staticMachO::LoadCommandTypegetLCFromMCVM(MCVersionMinTypeType) {
771switch (Type) {
772caseMCVM_OSXVersionMin:return MachO::LC_VERSION_MIN_MACOSX;
773caseMCVM_IOSVersionMin:return MachO::LC_VERSION_MIN_IPHONEOS;
774caseMCVM_TvOSVersionMin:return MachO::LC_VERSION_MIN_TVOS;
775caseMCVM_WatchOSVersionMin:return MachO::LC_VERSION_MIN_WATCHOS;
776 }
777llvm_unreachable("Invalid mc version min type");
778}
779
780voidMachObjectWriter::populateAddrSigSection(MCAssembler &Asm) {
781MCSection *AddrSigSection =
782 Asm.getContext().getObjectFileInfo()->getAddrSigSection();
783unsigned Log2Size =is64Bit() ? 3 : 2;
784for (constMCSymbol *S :getAddrsigSyms()) {
785if (!S->isRegistered())
786continue;
787MachO::any_relocation_info MRE;
788 MRE.r_word0 = 0;
789 MRE.r_word1 = (Log2Size << 25) | (MachO::GENERIC_RELOC_VANILLA << 28);
790addRelocation(S, AddrSigSection, MRE);
791 }
792}
793
794uint64_tMachObjectWriter::writeObject(MCAssembler &Asm) {
795uint64_t StartOffset =W.OS.tell();
796auto NumBytesWritten = [&] {returnW.OS.tell() - StartOffset; };
797
798populateAddrSigSection(Asm);
799
800// Compute symbol table information and bind symbol indices.
801computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData,
802 UndefinedSymbolData);
803
804if (!CGProfile.empty()) {
805MCSection *CGProfileSection = Asm.getContext().getMachOSection(
806"__LLVM","__cg_profile", 0,SectionKind::getMetadata());
807auto &Frag = cast<MCDataFragment>(*CGProfileSection->begin());
808 Frag.getContents().clear();
809raw_svector_ostreamOS(Frag.getContents());
810for (constMCObjectWriter::CGProfileEntry &CGPE :CGProfile) {
811uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
812uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
813support::endian::write(OS, FromIndex,W.Endian);
814support::endian::write(OS, ToIndex,W.Endian);
815support::endian::write(OS, CGPE.Count,W.Endian);
816 }
817 }
818
819unsigned NumSections = Asm.end() - Asm.begin();
820
821// The section data starts after the header, the segment load command (and
822// section headers) and the symbol table.
823unsigned NumLoadCommands = 1;
824uint64_t LoadCommandsSize =is64Bit() ?
825sizeof(MachO::segment_command_64) + NumSections *sizeof(MachO::section_64):
826sizeof(MachO::segment_command) + NumSections *sizeof(MachO::section);
827
828// Add the deployment target version info load command size, if used.
829if (VersionInfo.Major != 0) {
830 ++NumLoadCommands;
831if (VersionInfo.EmitBuildVersion)
832 LoadCommandsSize +=sizeof(MachO::build_version_command);
833else
834 LoadCommandsSize +=sizeof(MachO::version_min_command);
835 }
836
837// Add the target variant version info load command size, if used.
838if (TargetVariantVersionInfo.Major != 0) {
839 ++NumLoadCommands;
840assert(TargetVariantVersionInfo.EmitBuildVersion &&
841"target variant should use build version");
842 LoadCommandsSize +=sizeof(MachO::build_version_command);
843 }
844
845// Add the data-in-code load command size, if used.
846unsigned NumDataRegions = DataRegions.size();
847if (NumDataRegions) {
848 ++NumLoadCommands;
849 LoadCommandsSize +=sizeof(MachO::linkedit_data_command);
850 }
851
852// Add the loh load command size, if used.
853uint64_t LOHRawSize = LOHContainer.getEmitSize(Asm, *this);
854uint64_t LOHSize =alignTo(LOHRawSize,is64Bit() ? 8 : 4);
855if (LOHSize) {
856 ++NumLoadCommands;
857 LoadCommandsSize +=sizeof(MachO::linkedit_data_command);
858 }
859
860// Add the symbol table load command sizes, if used.
861unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
862 UndefinedSymbolData.size();
863if (NumSymbols) {
864 NumLoadCommands += 2;
865 LoadCommandsSize += (sizeof(MachO::symtab_command) +
866sizeof(MachO::dysymtab_command));
867 }
868
869// Add the linker option load commands sizes.
870for (constauto &Option : LinkerOptions) {
871 ++NumLoadCommands;
872 LoadCommandsSize +=ComputeLinkerOptionsLoadCommandSize(Option,is64Bit());
873 }
874
875// Compute the total size of the section data, as well as its file size and vm
876// size.
877uint64_t SectionDataStart = (is64Bit() ?sizeof(MachO::mach_header_64) :
878sizeof(MachO::mach_header)) + LoadCommandsSize;
879uint64_t SectionDataSize = 0;
880uint64_t SectionDataFileSize = 0;
881uint64_t VMSize = 0;
882for (constMCSection &Sec : Asm) {
883uint64_tAddress =getSectionAddress(&Sec);
884uint64_tSize = Asm.getSectionAddressSize(Sec);
885uint64_t FileSize = Asm.getSectionFileSize(Sec);
886 FileSize +=getPaddingSize(Asm, &Sec);
887
888 VMSize = std::max(VMSize,Address +Size);
889
890if (Sec.isVirtualSection())
891continue;
892
893 SectionDataSize = std::max(SectionDataSize,Address +Size);
894 SectionDataFileSize = std::max(SectionDataFileSize,Address + FileSize);
895 }
896
897// The section data is padded to pointer size bytes.
898//
899// FIXME: Is this machine dependent?
900unsigned SectionDataPadding =
901offsetToAlignment(SectionDataFileSize,is64Bit() ?Align(8) :Align(4));
902 SectionDataFileSize += SectionDataPadding;
903
904// Write the prolog, starting with the header and load command...
905writeHeader(MachO::MH_OBJECT, NumLoadCommands, LoadCommandsSize,
906SubsectionsViaSymbols);
907uint32_t Prot =
908MachO::VM_PROT_READ |MachO::VM_PROT_WRITE |MachO::VM_PROT_EXECUTE;
909writeSegmentLoadCommand("", NumSections, 0, VMSize, SectionDataStart,
910 SectionDataSize, Prot, Prot);
911
912// ... and then the section headers.
913uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
914for (constMCSection &Section : Asm) {
915constauto &Sec = cast<MCSectionMachO>(Section);
916 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
917unsigned NumRelocs = Relocs.size();
918uint64_t SectionStart = SectionDataStart +getSectionAddress(&Sec);
919unsigned Flags = Sec.getTypeAndAttributes();
920if (Sec.hasInstructions())
921 Flags |=MachO::S_ATTR_SOME_INSTRUCTIONS;
922if (!cast<MCSectionMachO>(Sec).isVirtualSection() &&
923 !isUInt<32>(SectionStart)) {
924 Asm.getContext().reportError(
925SMLoc(),"cannot encode offset of section; object file too large");
926return NumBytesWritten();
927 }
928if (NumRelocs && !isUInt<32>(RelocTableEnd)) {
929 Asm.getContext().reportError(
930SMLoc(),
931"cannot encode offset of relocations; object file too large");
932return NumBytesWritten();
933 }
934writeSection(Asm, Sec,getSectionAddress(&Sec), SectionStart, Flags,
935 RelocTableEnd, NumRelocs);
936 RelocTableEnd += NumRelocs *sizeof(MachO::any_relocation_info);
937 }
938
939// Write out the deployment target information, if it's available.
940auto EmitDeploymentTargetVersion =
941 [&](constVersionInfoType &VersionInfo) {
942auto EncodeVersion = [](VersionTuple V) ->uint32_t {
943assert(!V.empty() &&"empty version");
944unsignedUpdate = V.getSubminor().value_or(0);
945unsignedMinor = V.getMinor().value_or(0);
946assert(Update < 256 &&"unencodable update target version");
947assert(Minor < 256 &&"unencodable minor target version");
948assert(V.getMajor() < 65536 &&"unencodable major target version");
949returnUpdate | (Minor << 8) | (V.getMajor() << 16);
950 };
951uint32_t EncodedVersion = EncodeVersion(VersionTuple(
952 VersionInfo.Major, VersionInfo.Minor, VersionInfo.Update));
953uint32_tSDKVersion = !VersionInfo.SDKVersion.empty()
954 ? EncodeVersion(VersionInfo.SDKVersion)
955 : 0;
956if (VersionInfo.EmitBuildVersion) {
957// FIXME: Currently empty tools. Add clang version in the future.
958W.write<uint32_t>(MachO::LC_BUILD_VERSION);
959W.write<uint32_t>(sizeof(MachO::build_version_command));
960W.write<uint32_t>(VersionInfo.TypeOrPlatform.Platform);
961W.write<uint32_t>(EncodedVersion);
962W.write<uint32_t>(SDKVersion);
963W.write<uint32_t>(0);// Empty tools list.
964 }else {
965MachO::LoadCommandType LCType =
966getLCFromMCVM(VersionInfo.TypeOrPlatform.Type);
967W.write<uint32_t>(LCType);
968W.write<uint32_t>(sizeof(MachO::version_min_command));
969W.write<uint32_t>(EncodedVersion);
970W.write<uint32_t>(SDKVersion);
971 }
972 };
973if (VersionInfo.Major != 0)
974 EmitDeploymentTargetVersion(VersionInfo);
975if (TargetVariantVersionInfo.Major != 0)
976 EmitDeploymentTargetVersion(TargetVariantVersionInfo);
977
978// Write the data-in-code load command, if used.
979uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
980if (NumDataRegions) {
981uint64_t DataRegionsOffset = RelocTableEnd;
982uint64_t DataRegionsSize = NumDataRegions * 8;
983writeLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset,
984 DataRegionsSize);
985 }
986
987// Write the loh load command, if used.
988uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
989if (LOHSize)
990writeLinkeditLoadCommand(MachO::LC_LINKER_OPTIMIZATION_HINT,
991 DataInCodeTableEnd, LOHSize);
992
993// Write the symbol table load command, if used.
994if (NumSymbols) {
995unsigned FirstLocalSymbol = 0;
996unsigned NumLocalSymbols = LocalSymbolData.size();
997unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
998unsigned NumExternalSymbols = ExternalSymbolData.size();
999unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
1000unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
1001unsigned NumIndirectSymbols = IndirectSymbols.size();
1002unsigned NumSymTabSymbols =
1003 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
1004uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
1005uint64_t IndirectSymbolOffset = 0;
1006
1007// If used, the indirect symbols are written after the section data.
1008if (NumIndirectSymbols)
1009 IndirectSymbolOffset = LOHTableEnd;
1010
1011// The symbol table is written after the indirect symbol data.
1012uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
1013
1014// The string table is written after symbol table.
1015uint64_t StringTableOffset =
1016 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ?
1017sizeof(MachO::nlist_64) :
1018sizeof(MachO::nlist));
1019writeSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
1020 StringTableOffset,StringTable.getSize());
1021
1022writeDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
1023 FirstExternalSymbol, NumExternalSymbols,
1024 FirstUndefinedSymbol, NumUndefinedSymbols,
1025 IndirectSymbolOffset, NumIndirectSymbols);
1026 }
1027
1028// Write the linker options load commands.
1029for (constauto &Option : LinkerOptions)
1030writeLinkerOptionsLoadCommand(Option);
1031
1032// Write the actual section data.
1033for (constMCSection &Sec : Asm) {
1034 Asm.writeSectionData(W.OS, &Sec);
1035
1036uint64_t Pad =getPaddingSize(Asm, &Sec);
1037W.OS.write_zeros(Pad);
1038 }
1039
1040// Write the extra padding.
1041W.OS.write_zeros(SectionDataPadding);
1042
1043// Write the relocation entries.
1044for (constMCSection &Sec : Asm) {
1045// Write the section relocation entries, in reverse order to match 'as'
1046// (approximately, the exact algorithm is more complicated than this).
1047 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
1048for (const RelAndSymbol &Rel :llvm::reverse(Relocs)) {
1049W.write<uint32_t>(Rel.MRE.r_word0);
1050W.write<uint32_t>(Rel.MRE.r_word1);
1051 }
1052 }
1053
1054// Write out the data-in-code region payload, if there is one.
1055for (DataRegionDataData : DataRegions) {
1056uint64_t Start =getSymbolAddress(*Data.Start, Asm);
1057uint64_tEnd;
1058if (Data.End)
1059End =getSymbolAddress(*Data.End, Asm);
1060else
1061report_fatal_error("Data region not terminated");
1062
1063LLVM_DEBUG(dbgs() <<"data in code region-- kind: " <<Data.Kind
1064 <<" start: " << Start <<"(" <<Data.Start->getName()
1065 <<")" <<" end: " <<End <<"(" <<Data.End->getName()
1066 <<")" <<" size: " <<End - Start <<"\n");
1067W.write<uint32_t>(Start);
1068W.write<uint16_t>(End - Start);
1069W.write<uint16_t>(Data.Kind);
1070 }
1071
1072// Write out the loh commands, if there is one.
1073if (LOHSize) {
1074#ifndef NDEBUG
1075unsigned Start =W.OS.tell();
1076#endif
1077 LOHContainer.emit(Asm, *this);
1078// Pad to a multiple of the pointer size.
1079W.OS.write_zeros(
1080offsetToAlignment(LOHRawSize,is64Bit() ?Align(8) :Align(4)));
1081assert(W.OS.tell() - Start == LOHSize);
1082 }
1083
1084// Write the symbol table data, if used.
1085if (NumSymbols) {
1086// Write the indirect symbol entries.
1087for (auto &ISD : IndirectSymbols) {
1088// Indirect symbols in the non-lazy symbol pointer section have some
1089// special handling.
1090constMCSectionMachO &Section =
1091static_cast<constMCSectionMachO &>(*ISD.Section);
1092if (Section.getType() ==MachO::S_NON_LAZY_SYMBOL_POINTERS) {
1093// If this symbol is defined and internal, mark it as such.
1094if (ISD.Symbol->isDefined() && !ISD.Symbol->isExternal()) {
1095uint32_t Flags =MachO::INDIRECT_SYMBOL_LOCAL;
1096if (ISD.Symbol->isAbsolute())
1097 Flags |=MachO::INDIRECT_SYMBOL_ABS;
1098W.write<uint32_t>(Flags);
1099continue;
1100 }
1101 }
1102
1103W.write<uint32_t>(ISD.Symbol->getIndex());
1104 }
1105
1106// FIXME: Check that offsets match computed ones.
1107
1108// Write the symbol table entries.
1109for (auto *SymbolData :
1110 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
1111for (MachSymbolData &Entry : *SymbolData)
1112writeNlist(Entry, Asm);
1113
1114// Write the string table.
1115StringTable.write(W.OS);
1116 }
1117
1118return NumBytesWritten();
1119}
Alignment.h
MachO.h
Casting.h
Debug.h
LLVM_DEBUG
#define LLVM_DEBUG(...)
Definition:Debug.h:106
DenseMap.h
This file defines the DenseMap class.
Name
std::string Name
Definition:ELFObjHandler.cpp:77
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
End
bool End
Definition:ELF_riscv.cpp:480
Sym
Symbol * Sym
Definition:ELF_riscv.cpp:479
Options
static LVOptions Options
Definition:LVOptions.cpp:25
MCAsmBackend.h
MCAsmInfoDarwin.h
MCAssembler.h
MCContext.h
MCDirectives.h
MCExpr.h
MCFixupKindInfo.h
MCFragment.h
MCMachObjectWriter.h
MCObjectFileInfo.h
MCObjectWriter.h
MCSectionMachO.h
MCSection.h
MCSymbolMachO.h
MCSymbol.h
MCValue.h
ComputeLinkerOptionsLoadCommandSize
static unsigned ComputeLinkerOptionsLoadCommandSize(const std::vector< std::string > &Options, bool is64Bit)
Definition:MachObjectWriter.cpp:474
isFixupTargetValid
static bool isFixupTargetValid(const MCValue &Target)
Definition:MachObjectWriter.cpp:507
getLCFromMCVM
static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type)
Definition:MachObjectWriter.cpp:770
isSymbolLinkerVisible
static bool isSymbolLinkerVisible(const MCSymbol &Symbol)
Definition:MachObjectWriter.cpp:144
MathExtras.h
Fixup
PowerPC TLS Dynamic Call Fixup
Definition:PPCTLSDynamicCall.cpp:339
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
Twine.h
is64Bit
static bool is64Bit(const char *name)
Definition:X86Disassembler.cpp:1085
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
char
llvm::DenseMapBase::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition:DenseMap.h:194
llvm::DenseMapBase::clear
void clear()
Definition:DenseMap.h:110
llvm::DenseMap
Definition:DenseMap.h:727
llvm::MCAsmInfoDarwin::isSectionAtomizableBySymbols
static bool isSectionAtomizableBySymbols(const MCSection &Section)
True if the section is atomized using the symbols in it.
Definition:MCAsmInfoDarwin.cpp:21
llvm::MCAssembler
Definition:MCAssembler.h:53
llvm::MCConstantExpr
Definition:MCExpr.h:144
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition:MCExpr.h:34
llvm::MCExpr::evaluateAsRelocatable
bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition:MCExpr.cpp:819
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition:MCFixup.h:71
llvm::MCFragment
Definition:MCFragment.h:32
llvm::MCFragment::getAtom
const MCSymbol * getAtom() const
Definition:MCFragment.cpp:81
llvm::MCFragment::getParent
MCSection * getParent() const
Definition:MCFragment.h:99
llvm::MCLOHContainer::emit
void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const
Emit all Linker Optimization Hint in one big table.
Definition:MCLinkerOptimizationHint.h:171
llvm::MCLOHContainer::getEmitSize
uint64_t getEmitSize(const MCAssembler &Asm, const MachObjectWriter &ObjWriter) const
Get the size of the directives if emitted.
Definition:MCLinkerOptimizationHint.h:160
llvm::MCLOHContainer::reset
void reset()
Definition:MCLinkerOptimizationHint.h:176
llvm::MCObjectWriter::CGProfile
SmallVector< CGProfileEntry, 0 > CGProfile
Definition:MCObjectWriter.h:48
llvm::MCObjectWriter::SubsectionsViaSymbols
bool SubsectionsViaSymbols
Definition:MCObjectWriter.h:41
llvm::MCObjectWriter::getAddrsigSyms
std::vector< const MCSymbol * > & getAddrsigSyms()
Definition:MCObjectWriter.h:115
llvm::MCObjectWriter::reset
virtual void reset()
lifetime management
Definition:MCObjectWriter.cpp:22
llvm::MCSectionMachO
This represents a section on a Mach-O system (used by Mac OS X).
Definition:MCSectionMachO.h:24
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition:MCSection.h:36
llvm::MCSection::getAlign
Align getAlign() const
Definition:MCSection.h:146
llvm::MCSection::isVirtualSection
bool isVirtualSection() const
Check whether this section is "virtual", that is has no actual object file contents.
Definition:MCSection.h:197
llvm::MCSection::begin
iterator begin() const
Definition:MCSection.h:182
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition:MCSymbol.h:41
llvm::MCSymbol::getVariableValue
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition:MCSymbol.h:305
llvm::MCSymbol::isInSection
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition:MCSymbol.h:254
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition:MCSymbol.h:205
llvm::MCSymbol::isVariable
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition:MCSymbol.h:300
llvm::MCSymbol::isUndefined
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition:MCSymbol.h:259
llvm::MCSymbol::getSection
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition:MCSymbol.h:269
llvm::MCSymbol::isTemporary
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition:MCSymbol.h:222
llvm::MCSymbol::getFragment
MCFragment * getFragment(bool SetUsed=true) const
Definition:MCSymbol.h:397
llvm::MCValue
This represents an "assembler immediate".
Definition:MCValue.h:36
llvm::MachObjectWriter::getPaddingSize
uint64_t getPaddingSize(const MCAssembler &Asm, const MCSection *SD) const
Definition:MachObjectWriter.cpp:131
llvm::MachObjectWriter::computeSectionAddresses
void computeSectionAddresses(const MCAssembler &Asm)
Definition:MachObjectWriter.cpp:686
llvm::MachObjectWriter::writeSection
void writeSection(const MCAssembler &Asm, const MCSection &Sec, uint64_t VMAddr, uint64_t FileOffset, unsigned Flags, uint64_t RelocationsStart, unsigned NumRelocations)
Definition:MachObjectWriter.cpp:266
llvm::MachObjectWriter::doesSymbolRequireExternRelocation
bool doesSymbolRequireExternRelocation(const MCSymbol &S)
Definition:MachObjectWriter.cpp:65
llvm::MachObjectWriter::Minor
unsigned Minor
Definition:MCMachObjectWriter.h:103
llvm::MachObjectWriter::is64Bit
bool is64Bit() const
Definition:MCMachObjectWriter.h:264
llvm::MachObjectWriter::computeSymbolTable
void computeSymbolTable(MCAssembler &Asm, std::vector< MachSymbolData > &LocalSymbolData, std::vector< MachSymbolData > &ExternalSymbolData, std::vector< MachSymbolData > &UndefinedSymbolData)
Compute the symbol table data.
Definition:MachObjectWriter.cpp:586
llvm::MachObjectWriter::getFragmentAddress
uint64_t getFragmentAddress(const MCAssembler &Asm, const MCFragment *Fragment) const
Definition:MachObjectWriter.cpp:92
llvm::MachObjectWriter::VersionInfoType
{ bool EmitBuildVersion VersionInfoType
Definition:MCMachObjectWriter.h:97
llvm::MachObjectWriter::executePostLayoutBinding
void executePostLayoutBinding(MCAssembler &Asm) override
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
Definition:MachObjectWriter.cpp:716
llvm::MachObjectWriter::getSectionAddress
uint64_t getSectionAddress(const MCSection *Sec) const
Definition:MCMachObjectWriter.h:209
llvm::MachObjectWriter::addRelocation
void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec, MachO::any_relocation_info &MRE)
Definition:MCMachObjectWriter.h:325
llvm::MachObjectWriter::populateAddrSigSection
void populateAddrSigSection(MCAssembler &Asm)
Definition:MachObjectWriter.cpp:780
llvm::MachObjectWriter::isFixupKindPCRel
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind)
Definition:MachObjectWriter.cpp:84
llvm::MachObjectWriter::W
support::endian::Writer W
Definition:MCMachObjectWriter.h:183
llvm::MachObjectWriter::writeLinkerOptionsLoadCommand
void writeLinkerOptionsLoadCommand(const std::vector< std::string > &Options)
Definition:MachObjectWriter.cpp:483
llvm::MachObjectWriter::recordRelocation
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
Definition:MachObjectWriter.cpp:515
llvm::MachObjectWriter::writeNlist
void writeNlist(MachSymbolData &MSD, const MCAssembler &Asm)
Definition:MachObjectWriter.cpp:392
llvm::MachObjectWriter::SDKVersion
VersionTuple SDKVersion
An optional version of the SDK that was used to build the source.
Definition:MCMachObjectWriter.h:106
llvm::MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const override
Definition:MachObjectWriter.cpp:723
llvm::MachObjectWriter::writeDysymtabLoadCommand
void writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols, uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols, uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols, uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols)
Definition:MachObjectWriter.cpp:332
llvm::MachObjectWriter::findAliasedSymbol
const MCSymbol & findAliasedSymbol(const MCSymbol &Sym) const
Definition:MachObjectWriter.cpp:380
llvm::MachObjectWriter::getSymbolAddress
uint64_t getSymbolAddress(const MCSymbol &S, const MCAssembler &Asm) const
Definition:MachObjectWriter.cpp:98
llvm::MachObjectWriter::Type
MCVersionMinType Type
Used when EmitBuildVersion==false.
Definition:MCMachObjectWriter.h:99
llvm::MachObjectWriter::writeSegmentLoadCommand
void writeSegmentLoadCommand(StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize, uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt, uint32_t InitProt)
Write a segment load command.
Definition:MachObjectWriter.cpp:226
llvm::MachObjectWriter::getAtom
const MCSymbol * getAtom(const MCSymbol &S) const
Definition:MachObjectWriter.cpp:155
llvm::MachObjectWriter::writeLinkeditLoadCommand
void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset, uint32_t DataSize)
Definition:MachObjectWriter.cpp:460
llvm::MachObjectWriter::Update
unsigned Update
Definition:MCMachObjectWriter.h:104
llvm::MachObjectWriter::writeHeader
void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands, unsigned LoadCommandsSize, bool SubsectionsViaSymbols)
Definition:MachObjectWriter.cpp:174
llvm::MachObjectWriter::reset
void reset() override
lifetime management
Definition:MachObjectWriter.cpp:45
llvm::MachObjectWriter::isX86_64
bool isX86_64() const
Definition:MCMachObjectWriter.h:265
llvm::MachObjectWriter::writeSymtabLoadCommand
void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, uint32_t StringTableOffset, uint32_t StringTableSize)
Definition:MachObjectWriter.cpp:313
llvm::MachObjectWriter::bindIndirectSymbols
void bindIndirectSymbols(MCAssembler &Asm)
Definition:MachObjectWriter.cpp:529
llvm::MachObjectWriter::writeObject
uint64_t writeObject(MCAssembler &Asm) override
Write the object file and returns the number of bytes written.
Definition:MachObjectWriter.cpp:794
llvm::SMLoc
Represents a location in source code.
Definition:SMLoc.h:23
llvm::SectionKind::getMetadata
static SectionKind getMetadata()
Definition:SectionKind.h:188
llvm::SmallVectorBase::size
size_t size() const
Definition:SmallVector.h:78
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringTable
A table of densely packed, null-terminated strings indexed by offset.
Definition:StringTable.h:33
llvm::Target
Target - Wrapper for Target specific information.
Definition:TargetRegistry.h:144
llvm::Target::getName
const char * getName() const
getName - Get the target name.
Definition:TargetRegistry.h:353
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition:Value.cpp:309
llvm::VersionTuple
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition:VersionTuple.h:29
llvm::raw_ostream::write_zeros
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
Definition:raw_ostream.cpp:500
llvm::raw_ostream::tell
uint64_t tell() const
tell - Return the current offset with the file.
Definition:raw_ostream.h:147
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition:raw_ostream.h:691
uint16_t
uint32_t
uint64_t
uint8_t
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::MachO::MH_MAGIC
@ MH_MAGIC
Definition:MachO.h:30
llvm::MachO::MH_MAGIC_64
@ MH_MAGIC_64
Definition:MachO.h:32
llvm::MachO::S_ATTR_SOME_INSTRUCTIONS
@ S_ATTR_SOME_INSTRUCTIONS
S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
Definition:MachO.h:213
llvm::MachO::INDIRECT_SYMBOL_ABS
@ INDIRECT_SYMBOL_ABS
Definition:MachO.h:222
llvm::MachO::INDIRECT_SYMBOL_LOCAL
@ INDIRECT_SYMBOL_LOCAL
Definition:MachO.h:221
llvm::MachO::HeaderFileType
HeaderFileType
Definition:MachO.h:40
llvm::MachO::MH_OBJECT
@ MH_OBJECT
Definition:MachO.h:43
llvm::MachO::N_EXT
@ N_EXT
Definition:MachO.h:310
llvm::MachO::N_PEXT
@ N_PEXT
Definition:MachO.h:308
llvm::MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
@ S_THREAD_LOCAL_VARIABLE_POINTERS
S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread local structures.
Definition:MachO.h:175
llvm::MachO::S_LAZY_SYMBOL_POINTERS
@ S_LAZY_SYMBOL_POINTERS
S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
Definition:MachO.h:141
llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS
@ S_NON_LAZY_SYMBOL_POINTERS
S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
Definition:MachO.h:139
llvm::MachO::S_SYMBOL_STUBS
@ S_SYMBOL_STUBS
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
Definition:MachO.h:144
llvm::MachO::VM_PROT_EXECUTE
@ VM_PROT_EXECUTE
Definition:MachO.h:497
llvm::MachO::VM_PROT_READ
@ VM_PROT_READ
Definition:MachO.h:497
llvm::MachO::VM_PROT_WRITE
@ VM_PROT_WRITE
Definition:MachO.h:497
llvm::MachO::N_SECT
@ N_SECT
Definition:MachO.h:318
llvm::MachO::N_ABS
@ N_ABS
Definition:MachO.h:317
llvm::MachO::N_INDR
@ N_INDR
Definition:MachO.h:320
llvm::MachO::N_UNDF
@ N_UNDF
Definition:MachO.h:316
llvm::MachO::CPU_SUBTYPE_ARM64E
@ CPU_SUBTYPE_ARM64E
Definition:MachO.h:1643
llvm::MachO::GENERIC_RELOC_VANILLA
@ GENERIC_RELOC_VANILLA
Definition:MachO.h:410
llvm::MachO::CPU_SUBTYPE_ARM64E_WITH_PTRAUTH_VERSION
uint32_t CPU_SUBTYPE_ARM64E_WITH_PTRAUTH_VERSION(unsigned PtrAuthABIVersion, bool PtrAuthKernelABIVersion)
Definition:MachO.h:1659
llvm::MachO::LoadCommandType
LoadCommandType
Definition:MachO.h:98
llvm::MachO::CPU_TYPE_ARM64
@ CPU_TYPE_ARM64
Definition:MachO.h:1570
llvm::MachO::MH_SUBSECTIONS_VIA_SYMBOLS
@ MH_SUBSECTIONS_VIA_SYMBOLS
Definition:MachO.h:73
llvm::support::endian::write
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition:Endian.h:92
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::enumerate
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition:STLExtras.h:2448
llvm::reverse
auto reverse(ContainerTy &&C)
Definition:STLExtras.h:420
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition:STLExtras.h:1664
llvm::MCVersionMinType
MCVersionMinType
Definition:MCDirectives.h:69
llvm::MCVM_WatchOSVersionMin
@ MCVM_WatchOSVersionMin
.watchos_version_min
Definition:MCDirectives.h:73
llvm::MCVM_OSXVersionMin
@ MCVM_OSXVersionMin
.macosx_version_min
Definition:MCDirectives.h:71
llvm::MCVM_TvOSVersionMin
@ MCVM_TvOSVersionMin
.tvos_version_min
Definition:MCDirectives.h:72
llvm::MCVM_IOSVersionMin
@ MCVM_IOSVersionMin
.ios_version_min
Definition:MCDirectives.h:70
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition:Error.cpp:167
llvm::MCFixupKind
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition:MCFixup.h:21
llvm::CaptureComponents::Address
@ Address
llvm::offsetToAlignment
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition:Alignment.h:197
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition:Alignment.h:155
llvm::Log2
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition:Alignment.h:208
llvm::endianness::little
@ little
llvm::Data
@ Data
Definition:SIMachineScheduler.h:55
raw_ostream.h
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition:Alignment.h:39
llvm::MCFixupKindInfo
Target independent information on a fixup kind.
Definition:MCFixupKindInfo.h:15
llvm::MCFixupKindInfo::FKF_IsPCRel
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
Definition:MCFixupKindInfo.h:19
llvm::MCFixupKindInfo::Flags
unsigned Flags
Flags describing additional information on this fixup kind.
Definition:MCFixupKindInfo.h:45
llvm::MCObjectWriter::CGProfileEntry
Definition:MCObjectWriter.h:43
llvm::MachO::any_relocation_info
Definition:MachO.h:997
llvm::MachO::any_relocation_info::r_word0
uint32_t r_word0
Definition:MachO.h:998
llvm::MachO::any_relocation_info::r_word1
uint32_t r_word1
Definition:MachO.h:998
llvm::MachO::build_version_command
Definition:MachO.h:858
llvm::MachO::dysymtab_command
Definition:MachO.h:705
llvm::MachO::linkedit_data_command
Definition:MachO.h:801
llvm::MachO::linker_option_command
Definition:MachO.h:889
llvm::MachO::mach_header_64
Definition:MachO.h:522
llvm::MachO::mach_header
Definition:MachO.h:512
llvm::MachO::nlist_64
Definition:MachO.h:1017
llvm::MachO::nlist
Definition:MachO.h:1009
llvm::MachO::section_64
Definition:MachO.h:580
llvm::MachO::section
Definition:MachO.h:566
llvm::MachO::segment_command_64
Definition:MachO.h:552
llvm::MachO::segment_command
Definition:MachO.h:538
llvm::MachO::symtab_command
Definition:MachO.h:696
llvm::MachO::version_min_command
Definition:MachO.h:837
llvm::MachObjectWriter::DataRegionData
Definition:MCMachObjectWriter.h:88
llvm::support::endian::Writer::write
void write(ArrayRef< value_type > Val)
Definition:EndianStream.h:71
llvm::support::endian::Writer::Endian
endianness Endian
Definition:EndianStream.h:69
llvm::support::endian::Writer::OS
raw_ostream & OS
Definition:EndianStream.h:68

Generated on Sun Jul 20 2025 09:43:28 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp