Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1 | //===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===// |
| 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 | |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/DenseMap.h" |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/iterator_range.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/MachO.h" |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmBackend.h" |
Daniel Dunbar | 7c96955 | 2010-03-24 03:43:40 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmLayout.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAssembler.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCDirectives.h" |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCFixupKindInfo.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCFragment.h" |
| 21 | #include "llvm/MC/MCMachObjectWriter.h" |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCObjectWriter.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSection.h" |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSectionMachO.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbol.h" |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSymbolMachO.h" |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCValue.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Casting.h" |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 33 | #include <algorithm> |
| 34 | #include <cassert> |
| 35 | #include <cstdint> |
| 36 | #include <string> |
| 37 | #include <utility> |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 38 | #include <vector> |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "mc" |
| 43 | |
Pedro Artigas | b95c53e | 2012-12-14 18:52:11 +0000 | [diff] [blame] | 44 | void MachObjectWriter::reset() { |
| 45 | Relocations.clear(); |
| 46 | IndirectSymBase.clear(); |
| 47 | StringTable.clear(); |
| 48 | LocalSymbolData.clear(); |
| 49 | ExternalSymbolData.clear(); |
| 50 | UndefinedSymbolData.clear(); |
| 51 | MCObjectWriter::reset(); |
| 52 | } |
| 53 | |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 54 | bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) { |
Daniel Dunbar | 7de3106 | 2010-05-10 23:15:13 +0000 | [diff] [blame] | 55 | // Undefined symbols are always extern. |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 56 | if (S.isUndefined()) |
Daniel Dunbar | 7de3106 | 2010-05-10 23:15:13 +0000 | [diff] [blame] | 57 | return true; |
| 58 | |
| 59 | // References to weak definitions require external relocation entries; the |
| 60 | // definition may not always be the one in the same object file. |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 61 | if (cast<MCSymbolMachO>(S).isWeakDefinition()) |
Daniel Dunbar | 7de3106 | 2010-05-10 23:15:13 +0000 | [diff] [blame] | 62 | return true; |
| 63 | |
| 64 | // Otherwise, we can use an internal relocation. |
| 65 | return false; |
| 66 | } |
| 67 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 68 | bool MachObjectWriter:: |
| 69 | MachSymbolData::operator<(const MachSymbolData &RHS) const { |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 70 | return Symbol->getName() < RHS.Symbol->getName(); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 71 | } |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 72 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 73 | bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { |
| 74 | const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo( |
| 75 | (MCFixupKind) Kind); |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 76 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 77 | return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 78 | } |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 79 | |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 80 | uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment, |
| 81 | const MCAsmLayout &Layout) const { |
Rafael Espindola | 079027e | 2015-05-26 00:52:18 +0000 | [diff] [blame] | 82 | return getSectionAddress(Fragment->getParent()) + |
Rafael Espindola | 7549f87 | 2015-05-26 00:36:57 +0000 | [diff] [blame] | 83 | Layout.getFragmentOffset(Fragment); |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 84 | } |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 85 | |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 86 | uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 87 | const MCAsmLayout &Layout) const { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 88 | // If this is a variable, then recursively evaluate now. |
| 89 | if (S.isVariable()) { |
Jim Grosbach | d96ef19 | 2012-09-13 23:11:25 +0000 | [diff] [blame] | 90 | if (const MCConstantExpr *C = |
| 91 | dyn_cast<const MCConstantExpr>(S.getVariableValue())) |
| 92 | return C->getValue(); |
| 93 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 94 | MCValue Target; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 95 | if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Layout, nullptr)) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 96 | report_fatal_error("unable to evaluate offset for variable '" + |
| 97 | S.getName() + "'"); |
| 98 | |
| 99 | // Verify that any used symbols are defined. |
| 100 | if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined()) |
| 101 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
| 102 | Target.getSymA()->getSymbol().getName() + "'"); |
| 103 | if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined()) |
| 104 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
| 105 | Target.getSymB()->getSymbol().getName() + "'"); |
| 106 | |
| 107 | uint64_t Address = Target.getConstant(); |
| 108 | if (Target.getSymA()) |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 109 | Address += getSymbolAddress(Target.getSymA()->getSymbol(), Layout); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 110 | if (Target.getSymB()) |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 111 | Address += getSymbolAddress(Target.getSymB()->getSymbol(), Layout); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 112 | return Address; |
| 113 | } |
| 114 | |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 115 | return getSectionAddress(S.getFragment()->getParent()) + |
Duncan P. N. Exon Smith | 2a40483 | 2015-05-19 23:53:20 +0000 | [diff] [blame] | 116 | Layout.getSymbolOffset(S); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 119 | uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 120 | const MCAsmLayout &Layout) const { |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 121 | uint64_t EndAddr = getSectionAddress(Sec) + Layout.getSectionAddressSize(Sec); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 122 | unsigned Next = Sec->getLayoutOrder() + 1; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 123 | if (Next >= Layout.getSectionOrder().size()) |
| 124 | return 0; |
| 125 | |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 126 | const MCSection &NextSec = *Layout.getSectionOrder()[Next]; |
| 127 | if (NextSec.isVirtualSection()) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 128 | return 0; |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 129 | return OffsetToAlignment(EndAddr, NextSec.getAlignment()); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 132 | void MachObjectWriter::writeHeader(MachO::HeaderFileType Type, |
| 133 | unsigned NumLoadCommands, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 134 | unsigned LoadCommandsSize, |
| 135 | bool SubsectionsViaSymbols) { |
| 136 | uint32_t Flags = 0; |
| 137 | |
| 138 | if (SubsectionsViaSymbols) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 139 | Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 140 | |
| 141 | // struct mach_header (28 bytes) or |
| 142 | // struct mach_header_64 (32 bytes) |
| 143 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 144 | uint64_t Start = getStream().tell(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 145 | (void) Start; |
| 146 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 147 | write32(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 148 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 149 | write32(TargetObjectWriter->getCPUType()); |
| 150 | write32(TargetObjectWriter->getCPUSubtype()); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 151 | |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 152 | write32(Type); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 153 | write32(NumLoadCommands); |
| 154 | write32(LoadCommandsSize); |
| 155 | write32(Flags); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 156 | if (is64Bit()) |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 157 | write32(0); // reserved |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 158 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 159 | assert( |
| 160 | getStream().tell() - Start == |
| 161 | (is64Bit() ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header))); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 164 | /// writeSegmentLoadCommand - Write a segment load command. |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 165 | /// |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 166 | /// \param NumSections The number of sections in this segment. |
| 167 | /// \param SectionDataSize The total size of the sections. |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 168 | void MachObjectWriter::writeSegmentLoadCommand( |
| 169 | StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize, |
| 170 | uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt, |
| 171 | uint32_t InitProt) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 172 | // struct segment_command (56 bytes) or |
| 173 | // struct segment_command_64 (72 bytes) |
| 174 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 175 | uint64_t Start = getStream().tell(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 176 | (void) Start; |
| 177 | |
| 178 | unsigned SegmentLoadCommandSize = |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 179 | is64Bit() ? sizeof(MachO::segment_command_64): |
| 180 | sizeof(MachO::segment_command); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 181 | write32(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT); |
| 182 | write32(SegmentLoadCommandSize + |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 183 | NumSections * (is64Bit() ? sizeof(MachO::section_64) : |
| 184 | sizeof(MachO::section))); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 185 | |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 186 | assert(Name.size() <= 16); |
| 187 | writeBytes(Name, 16); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 188 | if (is64Bit()) { |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 189 | write64(VMAddr); // vmaddr |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 190 | write64(VMSize); // vmsize |
| 191 | write64(SectionDataStartOffset); // file offset |
| 192 | write64(SectionDataSize); // file size |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 193 | } else { |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 194 | write32(VMAddr); // vmaddr |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 195 | write32(VMSize); // vmsize |
| 196 | write32(SectionDataStartOffset); // file offset |
| 197 | write32(SectionDataSize); // file size |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 198 | } |
Nick Kledzik | fe6813f | 2013-09-04 23:53:44 +0000 | [diff] [blame] | 199 | // maxprot |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 200 | write32(MaxProt); |
Nick Kledzik | fe6813f | 2013-09-04 23:53:44 +0000 | [diff] [blame] | 201 | // initprot |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 202 | write32(InitProt); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 203 | write32(NumSections); |
| 204 | write32(0); // flags |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 205 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 206 | assert(getStream().tell() - Start == SegmentLoadCommandSize); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 209 | void MachObjectWriter::writeSection(const MCAsmLayout &Layout, |
| 210 | const MCSection &Sec, uint64_t VMAddr, |
| 211 | uint64_t FileOffset, unsigned Flags, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 212 | uint64_t RelocationsStart, |
| 213 | unsigned NumRelocations) { |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 214 | uint64_t SectionSize = Layout.getSectionAddressSize(&Sec); |
Rafael Espindola | 61e724a | 2015-05-26 01:15:30 +0000 | [diff] [blame] | 215 | const MCSectionMachO &Section = cast<MCSectionMachO>(Sec); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 216 | |
| 217 | // The offset is unused for virtual sections. |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 218 | if (Section.isVirtualSection()) { |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 219 | assert(Layout.getSectionFileSize(&Sec) == 0 && "Invalid file size!"); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 220 | FileOffset = 0; |
| 221 | } |
| 222 | |
| 223 | // struct section (68 bytes) or |
| 224 | // struct section_64 (80 bytes) |
| 225 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 226 | uint64_t Start = getStream().tell(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 227 | (void) Start; |
| 228 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 229 | writeBytes(Section.getSectionName(), 16); |
| 230 | writeBytes(Section.getSegmentName(), 16); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 231 | if (is64Bit()) { |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 232 | write64(VMAddr); // address |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 233 | write64(SectionSize); // size |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 234 | } else { |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 235 | write32(VMAddr); // address |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 236 | write32(SectionSize); // size |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 237 | } |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 238 | write32(FileOffset); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 239 | |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 240 | assert(isPowerOf2_32(Section.getAlignment()) && "Invalid alignment!"); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 241 | write32(Log2_32(Section.getAlignment())); |
| 242 | write32(NumRelocations ? RelocationsStart : 0); |
| 243 | write32(NumRelocations); |
| 244 | write32(Flags); |
| 245 | write32(IndirectSymBase.lookup(&Sec)); // reserved1 |
| 246 | write32(Section.getStubSize()); // reserved2 |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 247 | if (is64Bit()) |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 248 | write32(0); // reserved3 |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 249 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 250 | assert(getStream().tell() - Start == |
| 251 | (is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section))); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 254 | void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 255 | uint32_t NumSymbols, |
| 256 | uint32_t StringTableOffset, |
| 257 | uint32_t StringTableSize) { |
| 258 | // struct symtab_command (24 bytes) |
| 259 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 260 | uint64_t Start = getStream().tell(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 261 | (void) Start; |
| 262 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 263 | write32(MachO::LC_SYMTAB); |
| 264 | write32(sizeof(MachO::symtab_command)); |
| 265 | write32(SymbolOffset); |
| 266 | write32(NumSymbols); |
| 267 | write32(StringTableOffset); |
| 268 | write32(StringTableSize); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 269 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 270 | assert(getStream().tell() - Start == sizeof(MachO::symtab_command)); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 273 | void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 274 | uint32_t NumLocalSymbols, |
| 275 | uint32_t FirstExternalSymbol, |
| 276 | uint32_t NumExternalSymbols, |
| 277 | uint32_t FirstUndefinedSymbol, |
| 278 | uint32_t NumUndefinedSymbols, |
| 279 | uint32_t IndirectSymbolOffset, |
| 280 | uint32_t NumIndirectSymbols) { |
| 281 | // struct dysymtab_command (80 bytes) |
| 282 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 283 | uint64_t Start = getStream().tell(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 284 | (void) Start; |
| 285 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 286 | write32(MachO::LC_DYSYMTAB); |
| 287 | write32(sizeof(MachO::dysymtab_command)); |
| 288 | write32(FirstLocalSymbol); |
| 289 | write32(NumLocalSymbols); |
| 290 | write32(FirstExternalSymbol); |
| 291 | write32(NumExternalSymbols); |
| 292 | write32(FirstUndefinedSymbol); |
| 293 | write32(NumUndefinedSymbols); |
| 294 | write32(0); // tocoff |
| 295 | write32(0); // ntoc |
| 296 | write32(0); // modtaboff |
| 297 | write32(0); // nmodtab |
| 298 | write32(0); // extrefsymoff |
| 299 | write32(0); // nextrefsyms |
| 300 | write32(IndirectSymbolOffset); |
| 301 | write32(NumIndirectSymbols); |
| 302 | write32(0); // extreloff |
| 303 | write32(0); // nextrel |
| 304 | write32(0); // locreloff |
| 305 | write32(0); // nlocrel |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 306 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 307 | assert(getStream().tell() - Start == sizeof(MachO::dysymtab_command)); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 310 | MachObjectWriter::MachSymbolData * |
| 311 | MachObjectWriter::findSymbolData(const MCSymbol &Sym) { |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 312 | for (auto *SymbolData : |
| 313 | {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) |
| 314 | for (MachSymbolData &Entry : *SymbolData) |
| 315 | if (Entry.Symbol == &Sym) |
| 316 | return &Entry; |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 317 | |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
Rafael Espindola | 7f4e07b | 2015-04-17 12:28:43 +0000 | [diff] [blame] | 321 | const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const { |
| 322 | const MCSymbol *S = &Sym; |
| 323 | while (S->isVariable()) { |
| 324 | const MCExpr *Value = S->getVariableValue(); |
| 325 | const auto *Ref = dyn_cast<MCSymbolRefExpr>(Value); |
| 326 | if (!Ref) |
| 327 | return *S; |
| 328 | S = &Ref->getSymbol(); |
| 329 | } |
| 330 | return *S; |
| 331 | } |
| 332 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 333 | void MachObjectWriter::writeNlist(MachSymbolData &MSD, |
Bill Wendling | 2116221 | 2011-06-23 00:09:43 +0000 | [diff] [blame] | 334 | const MCAsmLayout &Layout) { |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 335 | const MCSymbol *Symbol = MSD.Symbol; |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 336 | const MCSymbol &Data = *Symbol; |
Rafael Espindola | 7f4e07b | 2015-04-17 12:28:43 +0000 | [diff] [blame] | 337 | const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol); |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 338 | uint8_t SectionIndex = MSD.SectionIndex; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 339 | uint8_t Type = 0; |
Jim Grosbach | a317160 | 2011-08-09 22:12:37 +0000 | [diff] [blame] | 340 | uint64_t Address = 0; |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 341 | bool IsAlias = Symbol != AliasedSymbol; |
| 342 | |
Duncan P. N. Exon Smith | 24f4775 | 2015-05-21 00:39:24 +0000 | [diff] [blame] | 343 | const MCSymbol &OrigSymbol = *Symbol; |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 344 | MachSymbolData *AliaseeInfo; |
| 345 | if (IsAlias) { |
| 346 | AliaseeInfo = findSymbolData(*AliasedSymbol); |
| 347 | if (AliaseeInfo) |
| 348 | SectionIndex = AliaseeInfo->SectionIndex; |
| 349 | Symbol = AliasedSymbol; |
Lang Hames | 1b640e0 | 2016-03-15 01:43:05 +0000 | [diff] [blame] | 350 | // FIXME: Should this update Data as well? |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 351 | } |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 352 | |
| 353 | // Set the N_TYPE bits. See <mach-o/nlist.h>. |
| 354 | // |
| 355 | // FIXME: Are the prebound or indirect fields possible here? |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 356 | if (IsAlias && Symbol->isUndefined()) |
| 357 | Type = MachO::N_INDR; |
| 358 | else if (Symbol->isUndefined()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 359 | Type = MachO::N_UNDF; |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 360 | else if (Symbol->isAbsolute()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 361 | Type = MachO::N_ABS; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 362 | else |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 363 | Type = MachO::N_SECT; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 364 | |
| 365 | // FIXME: Set STAB bits. |
| 366 | |
| 367 | if (Data.isPrivateExtern()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 368 | Type |= MachO::N_PEXT; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 369 | |
| 370 | // Set external bit. |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 371 | if (Data.isExternal() || (!IsAlias && Symbol->isUndefined())) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 372 | Type |= MachO::N_EXT; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 373 | |
| 374 | // Compute the symbol address. |
Tim Northover | eaef074 | 2014-05-30 13:22:59 +0000 | [diff] [blame] | 375 | if (IsAlias && Symbol->isUndefined()) |
| 376 | Address = AliaseeInfo->StringIndex; |
| 377 | else if (Symbol->isDefined()) |
Duncan P. N. Exon Smith | 24f4775 | 2015-05-21 00:39:24 +0000 | [diff] [blame] | 378 | Address = getSymbolAddress(OrigSymbol, Layout); |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 379 | else if (Symbol->isCommon()) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 380 | // Common symbols are encoded with the size in the address |
| 381 | // field, and their alignment in the flags. |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 382 | Address = Symbol->getCommonSize(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // struct nlist (12 bytes) |
| 386 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 387 | write32(MSD.StringIndex); |
| 388 | write8(Type); |
| 389 | write8(SectionIndex); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 390 | |
| 391 | // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' |
| 392 | // value. |
Lang Hames | 1b640e0 | 2016-03-15 01:43:05 +0000 | [diff] [blame] | 393 | bool EncodeAsAltEntry = |
| 394 | IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry(); |
| 395 | write16(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry)); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 396 | if (is64Bit()) |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 397 | write64(Address); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 398 | else |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 399 | write32(Address); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 402 | void MachObjectWriter::writeLinkeditLoadCommand(uint32_t Type, |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 403 | uint32_t DataOffset, |
| 404 | uint32_t DataSize) { |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 405 | uint64_t Start = getStream().tell(); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 406 | (void) Start; |
| 407 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 408 | write32(Type); |
| 409 | write32(sizeof(MachO::linkedit_data_command)); |
| 410 | write32(DataOffset); |
| 411 | write32(DataSize); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 412 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 413 | assert(getStream().tell() - Start == sizeof(MachO::linkedit_data_command)); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 416 | static unsigned ComputeLinkerOptionsLoadCommandSize( |
Daniel Dunbar | 34ea79f | 2013-01-22 03:42:49 +0000 | [diff] [blame] | 417 | const std::vector<std::string> &Options, bool is64Bit) |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 418 | { |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 419 | unsigned Size = sizeof(MachO::linker_option_command); |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 420 | for (const std::string &Option : Options) |
| 421 | Size += Option.size() + 1; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 422 | return alignTo(Size, is64Bit ? 8 : 4); |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 425 | void MachObjectWriter::writeLinkerOptionsLoadCommand( |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 426 | const std::vector<std::string> &Options) |
| 427 | { |
Daniel Dunbar | 34ea79f | 2013-01-22 03:42:49 +0000 | [diff] [blame] | 428 | unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit()); |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 429 | uint64_t Start = getStream().tell(); |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 430 | (void) Start; |
| 431 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 432 | write32(MachO::LC_LINKER_OPTION); |
| 433 | write32(Size); |
| 434 | write32(Options.size()); |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 435 | uint64_t BytesWritten = sizeof(MachO::linker_option_command); |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 436 | for (const std::string &Option : Options) { |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 437 | // Write each string, including the null byte. |
Malcolm Parsons | 06ac79c | 2016-11-02 16:43:50 +0000 | [diff] [blame] | 438 | writeBytes(Option, Option.size() + 1); |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 439 | BytesWritten += Option.size() + 1; |
| 440 | } |
| 441 | |
Daniel Dunbar | 34ea79f | 2013-01-22 03:42:49 +0000 | [diff] [blame] | 442 | // Pad to a multiple of the pointer size. |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 443 | writeBytes("", OffsetToAlignment(BytesWritten, is64Bit() ? 8 : 4)); |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 444 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 445 | assert(getStream().tell() - Start == Size); |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 448 | void MachObjectWriter::recordRelocation(MCAssembler &Asm, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 449 | const MCAsmLayout &Layout, |
| 450 | const MCFragment *Fragment, |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 451 | const MCFixup &Fixup, MCValue Target, |
Rafael Espindola | ceecfe5b | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 452 | uint64_t &FixedValue) { |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 453 | TargetObjectWriter->recordRelocation(this, Asm, Layout, Fragment, Fixup, |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 454 | Target, FixedValue); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 457 | void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 458 | // This is the point where 'as' creates actual symbols for indirect symbols |
| 459 | // (in the following two passes). It would be easier for us to do this sooner |
| 460 | // when we see the attribute, but that makes getting the order in the symbol |
| 461 | // table much more complicated than it is worth. |
| 462 | // |
| 463 | // FIXME: Revisit this when the dust settles. |
| 464 | |
Kevin Enderby | 3aeada2 | 2013-08-28 17:50:59 +0000 | [diff] [blame] | 465 | // Report errors for use of .indirect_symbol not in a symbol pointer section |
| 466 | // or stub section. |
| 467 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 468 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 469 | const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); |
Kevin Enderby | 3aeada2 | 2013-08-28 17:50:59 +0000 | [diff] [blame] | 470 | |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 471 | if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS && |
| 472 | Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS && |
Tim Northover | 5c3140f | 2016-04-25 21:12:04 +0000 | [diff] [blame] | 473 | Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS && |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 474 | Section.getType() != MachO::S_SYMBOL_STUBS) { |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 475 | MCSymbol &Symbol = *it->Symbol; |
| 476 | report_fatal_error("indirect symbol '" + Symbol.getName() + |
| 477 | "' not in a symbol pointer or stub section"); |
Kevin Enderby | 3aeada2 | 2013-08-28 17:50:59 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 481 | // Bind non-lazy symbol pointers first. |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 482 | unsigned IndirectIndex = 0; |
| 483 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 484 | ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 485 | const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 486 | |
Tim Northover | 4397837 | 2016-04-26 18:29:16 +0000 | [diff] [blame] | 487 | if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS && |
| 488 | Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 489 | continue; |
| 490 | |
| 491 | // Initialize the section indirect symbol base, if necessary. |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 492 | IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex)); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 493 | |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 494 | Asm.registerSymbol(*it->Symbol); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Then lazy symbol pointers and symbol stubs. |
| 498 | IndirectIndex = 0; |
| 499 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 500 | ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 501 | const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 502 | |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 503 | if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS && |
| 504 | Section.getType() != MachO::S_SYMBOL_STUBS) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 505 | continue; |
| 506 | |
| 507 | // Initialize the section indirect symbol base, if necessary. |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 508 | IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex)); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 509 | |
| 510 | // Set the symbol type to undefined lazy, but only on construction. |
| 511 | // |
| 512 | // FIXME: Do not hardcode. |
| 513 | bool Created; |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 514 | Asm.registerSymbol(*it->Symbol, &Created); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 515 | if (Created) |
Pete Cooper | 916f79e | 2015-06-08 17:17:28 +0000 | [diff] [blame] | 516 | cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(true); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 520 | /// computeSymbolTable - Compute the symbol table data |
| 521 | void MachObjectWriter::computeSymbolTable( |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 522 | MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData, |
| 523 | std::vector<MachSymbolData> &ExternalSymbolData, |
| 524 | std::vector<MachSymbolData> &UndefinedSymbolData) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 525 | // Build section lookup table. |
| 526 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 527 | unsigned Index = 1; |
| 528 | for (MCAssembler::iterator it = Asm.begin(), |
| 529 | ie = Asm.end(); it != ie; ++it, ++Index) |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 530 | SectionIndexMap[&*it] = Index; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 531 | assert(Index <= 256 && "Too many sections!"); |
| 532 | |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 533 | // Build the string table. |
Duncan P. N. Exon Smith | f48de1c | 2015-05-16 00:35:24 +0000 | [diff] [blame] | 534 | for (const MCSymbol &Symbol : Asm.symbols()) { |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 535 | if (!Asm.isSymbolLinkerVisible(Symbol)) |
| 536 | continue; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 537 | |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 538 | StringTable.add(Symbol.getName()); |
| 539 | } |
Rafael Espindola | 21956e4 | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 540 | StringTable.finalize(); |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 541 | |
| 542 | // Build the symbol arrays but only for non-local symbols. |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 543 | // |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 544 | // The particular order that we collect and then sort the symbols is chosen to |
| 545 | // match 'as'. Even though it doesn't matter for correctness, this is |
| 546 | // important for letting us diff .o files. |
Duncan P. N. Exon Smith | f48de1c | 2015-05-16 00:35:24 +0000 | [diff] [blame] | 547 | for (const MCSymbol &Symbol : Asm.symbols()) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 548 | // Ignore non-linker visible symbols. |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 549 | if (!Asm.isSymbolLinkerVisible(Symbol)) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 550 | continue; |
| 551 | |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 552 | if (!Symbol.isExternal() && !Symbol.isUndefined()) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 553 | continue; |
| 554 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 555 | MachSymbolData MSD; |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 556 | MSD.Symbol = &Symbol; |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 557 | MSD.StringIndex = StringTable.getOffset(Symbol.getName()); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 558 | |
| 559 | if (Symbol.isUndefined()) { |
| 560 | MSD.SectionIndex = 0; |
| 561 | UndefinedSymbolData.push_back(MSD); |
| 562 | } else if (Symbol.isAbsolute()) { |
| 563 | MSD.SectionIndex = 0; |
| 564 | ExternalSymbolData.push_back(MSD); |
| 565 | } else { |
| 566 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 567 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 568 | ExternalSymbolData.push_back(MSD); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // Now add the data for local symbols. |
Duncan P. N. Exon Smith | f48de1c | 2015-05-16 00:35:24 +0000 | [diff] [blame] | 573 | for (const MCSymbol &Symbol : Asm.symbols()) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 574 | // Ignore non-linker visible symbols. |
Hans Wennborg | 1b1a399 | 2014-10-06 17:05:19 +0000 | [diff] [blame] | 575 | if (!Asm.isSymbolLinkerVisible(Symbol)) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 576 | continue; |
| 577 | |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 578 | if (Symbol.isExternal() || Symbol.isUndefined()) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 579 | continue; |
| 580 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 581 | MachSymbolData MSD; |
Duncan P. N. Exon Smith | 08b8726 | 2015-05-20 15:16:14 +0000 | [diff] [blame] | 582 | MSD.Symbol = &Symbol; |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 583 | MSD.StringIndex = StringTable.getOffset(Symbol.getName()); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 584 | |
| 585 | if (Symbol.isAbsolute()) { |
| 586 | MSD.SectionIndex = 0; |
| 587 | LocalSymbolData.push_back(MSD); |
| 588 | } else { |
| 589 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 590 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 591 | LocalSymbolData.push_back(MSD); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // External and undefined symbols are required to be in lexicographic order. |
| 596 | std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 597 | std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 598 | |
| 599 | // Set the symbol indices. |
| 600 | Index = 0; |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 601 | for (auto *SymbolData : |
| 602 | {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) |
| 603 | for (MachSymbolData &Entry : *SymbolData) |
| 604 | Entry.Symbol->setIndex(Index++); |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 605 | |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 606 | for (const MCSection &Section : Asm) { |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 607 | for (RelAndSymbol &Rel : Relocations[&Section]) { |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 608 | if (!Rel.Sym) |
| 609 | continue; |
| 610 | |
| 611 | // Set the Index and the IsExtern bit. |
Duncan P. N. Exon Smith | 1247bbd | 2015-05-22 05:54:01 +0000 | [diff] [blame] | 612 | unsigned Index = Rel.Sym->getIndex(); |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 613 | assert(isInt<24>(Index)); |
| 614 | if (IsLittleEndian) |
Justin Bogner | d4e08a0 | 2015-05-14 23:54:49 +0000 | [diff] [blame] | 615 | Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27); |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 616 | else |
| 617 | Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4); |
| 618 | } |
| 619 | } |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm, |
| 623 | const MCAsmLayout &Layout) { |
| 624 | uint64_t StartAddress = 0; |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 625 | for (const MCSection *Sec : Layout.getSectionOrder()) { |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 626 | StartAddress = alignTo(StartAddress, Sec->getAlignment()); |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 627 | SectionAddress[Sec] = StartAddress; |
| 628 | StartAddress += Layout.getSectionAddressSize(Sec); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 629 | |
| 630 | // Explicitly pad the section to match the alignment requirements of the |
| 631 | // following one. This is for 'gas' compatibility, it shouldn't |
| 632 | /// strictly be necessary. |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 633 | StartAddress += getPaddingSize(Sec, Layout); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 637 | void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
Bill Wendling | 2116221 | 2011-06-23 00:09:43 +0000 | [diff] [blame] | 638 | const MCAsmLayout &Layout) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 639 | computeSectionAddresses(Asm, Layout); |
| 640 | |
| 641 | // Create symbol data for any indirect symbols. |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 642 | bindIndirectSymbols(Asm); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 645 | bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 646 | const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, |
| 647 | bool InSet) const { |
| 648 | // FIXME: We don't handle things like |
| 649 | // foo = . |
| 650 | // creating atoms. |
| 651 | if (A.isVariable() || B.isVariable()) |
| 652 | return false; |
| 653 | return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, A, B, |
| 654 | InSet); |
| 655 | } |
| 656 | |
| 657 | bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( |
Duncan P. N. Exon Smith | d81ba53 | 2015-05-16 01:01:55 +0000 | [diff] [blame] | 658 | const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, |
Rafael Espindola | 35d6189 | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 659 | bool InSet, bool IsPCRel) const { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 660 | if (InSet) |
| 661 | return true; |
| 662 | |
| 663 | // The effective address is |
| 664 | // addr(atom(A)) + offset(A) |
| 665 | // - addr(atom(B)) - offset(B) |
| 666 | // and the offsets are not relocatable, so the fixup is fully resolved when |
| 667 | // addr(atom(A)) - addr(atom(B)) == 0. |
Duncan P. N. Exon Smith | d81ba53 | 2015-05-16 01:01:55 +0000 | [diff] [blame] | 668 | const MCSymbol &SA = findAliasedSymbol(SymA); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 669 | const MCSection &SecA = SA.getSection(); |
Rafael Espindola | 7549f87 | 2015-05-26 00:36:57 +0000 | [diff] [blame] | 670 | const MCSection &SecB = *FB.getParent(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 671 | |
| 672 | if (IsPCRel) { |
| 673 | // The simple (Darwin, except on x86_64) way of dealing with this was to |
| 674 | // assume that any reference to a temporary symbol *must* be a temporary |
| 675 | // symbol in the same atom, unless the sections differ. Therefore, any PCrel |
| 676 | // relocation to a temporary symbol (in the same section) is fully |
| 677 | // resolved. This also works in conjunction with absolutized .set, which |
| 678 | // requires the compiler to use .set to absolutize the differences between |
| 679 | // symbols which the compiler knows to be assembly time constants, so we |
| 680 | // don't need to worry about considering symbol differences fully resolved. |
Jim Grosbach | d6ae4ba | 2011-12-07 19:46:59 +0000 | [diff] [blame] | 681 | // |
| 682 | // If the file isn't using sub-sections-via-symbols, we can make the |
| 683 | // same assumptions about any symbol that we normally make about |
| 684 | // assembler locals. |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 685 | |
Rafael Espindola | a063bdd | 2014-03-11 21:22:57 +0000 | [diff] [blame] | 686 | bool hasReliableSymbolDifference = isX86_64(); |
| 687 | if (!hasReliableSymbolDifference) { |
Jim Grosbach | 4045507 | 2012-01-17 22:14:39 +0000 | [diff] [blame] | 688 | if (!SA.isInSection() || &SecA != &SecB || |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 689 | (!SA.isTemporary() && FB.getAtom() != SA.getFragment()->getAtom() && |
Jim Grosbach | 35bc8f9 | 2012-01-24 21:45:25 +0000 | [diff] [blame] | 690 | Asm.getSubsectionsViaSymbols())) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 691 | return false; |
| 692 | return true; |
| 693 | } |
Kevin Enderby | 7b46bb8 | 2011-09-08 20:53:44 +0000 | [diff] [blame] | 694 | // For Darwin x86_64, there is one special case when the reference IsPCRel. |
| 695 | // If the fragment with the reference does not have a base symbol but meets |
| 696 | // the simple way of dealing with this, in that it is a temporary symbol in |
| 697 | // the same atom then it is assumed to be fully resolved. This is needed so |
Eric Christopher | 460be99 | 2011-09-08 22:17:40 +0000 | [diff] [blame] | 698 | // a relocation entry is not created and so the static linker does not |
Kevin Enderby | 7b46bb8 | 2011-09-08 20:53:44 +0000 | [diff] [blame] | 699 | // mess up the reference later. |
| 700 | else if(!FB.getAtom() && |
| 701 | SA.isTemporary() && SA.isInSection() && &SecA == &SecB){ |
| 702 | return true; |
| 703 | } |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Rafael Espindola | bfd0f01 | 2014-11-04 22:10:33 +0000 | [diff] [blame] | 706 | // If they are not in the same section, we can't compute the diff. |
| 707 | if (&SecA != &SecB) |
| 708 | return false; |
| 709 | |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 710 | const MCFragment *FA = SA.getFragment(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 711 | |
Benjamin Kramer | 91ea511 | 2011-08-12 01:51:29 +0000 | [diff] [blame] | 712 | // Bail if the symbol has no fragment. |
| 713 | if (!FA) |
| 714 | return false; |
| 715 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 716 | // If the atoms are the same, they are guaranteed to have the same address. |
Duncan P. N. Exon Smith | 09bfa58 | 2015-05-16 00:48:58 +0000 | [diff] [blame] | 717 | if (FA->getAtom() == FB.getAtom()) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 718 | return true; |
| 719 | |
| 720 | // Otherwise, we can't prove this is fully resolved. |
| 721 | return false; |
| 722 | } |
| 723 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 724 | void MachObjectWriter::writeObject(MCAssembler &Asm, |
Jim Grosbach | 46be301 | 2011-12-06 00:13:09 +0000 | [diff] [blame] | 725 | const MCAsmLayout &Layout) { |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 726 | // Compute symbol table information and bind symbol indices. |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 727 | computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData, |
Rafael Espindola | 2658554 | 2015-01-19 21:11:14 +0000 | [diff] [blame] | 728 | UndefinedSymbolData); |
| 729 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 730 | unsigned NumSections = Asm.size(); |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 731 | const MCAssembler::VersionMinInfoType &VersionInfo = |
| 732 | Layout.getAssembler().getVersionMinInfo(); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 733 | |
| 734 | // The section data starts after the header, the segment load command (and |
| 735 | // section headers) and the symbol table. |
| 736 | unsigned NumLoadCommands = 1; |
| 737 | uint64_t LoadCommandsSize = is64Bit() ? |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 738 | sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64): |
| 739 | sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 740 | |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 741 | // Add the deployment target version info load command size, if used. |
| 742 | if (VersionInfo.Major != 0) { |
| 743 | ++NumLoadCommands; |
| 744 | LoadCommandsSize += sizeof(MachO::version_min_command); |
| 745 | } |
| 746 | |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 747 | // Add the data-in-code load command size, if used. |
| 748 | unsigned NumDataRegions = Asm.getDataRegions().size(); |
| 749 | if (NumDataRegions) { |
| 750 | ++NumLoadCommands; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 751 | LoadCommandsSize += sizeof(MachO::linkedit_data_command); |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 754 | // Add the loh load command size, if used. |
| 755 | uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout); |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 756 | uint64_t LOHSize = alignTo(LOHRawSize, is64Bit() ? 8 : 4); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 757 | if (LOHSize) { |
| 758 | ++NumLoadCommands; |
| 759 | LoadCommandsSize += sizeof(MachO::linkedit_data_command); |
| 760 | } |
| 761 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 762 | // Add the symbol table load command sizes, if used. |
| 763 | unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() + |
| 764 | UndefinedSymbolData.size(); |
| 765 | if (NumSymbols) { |
| 766 | NumLoadCommands += 2; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 767 | LoadCommandsSize += (sizeof(MachO::symtab_command) + |
| 768 | sizeof(MachO::dysymtab_command)); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 771 | // Add the linker option load commands sizes. |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 772 | for (const auto &Option : Asm.getLinkerOptions()) { |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 773 | ++NumLoadCommands; |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 774 | LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(Option, is64Bit()); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 775 | } |
NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 776 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 777 | // Compute the total size of the section data, as well as its file size and vm |
| 778 | // size. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 779 | uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) : |
| 780 | sizeof(MachO::mach_header)) + LoadCommandsSize; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 781 | uint64_t SectionDataSize = 0; |
| 782 | uint64_t SectionDataFileSize = 0; |
| 783 | uint64_t VMSize = 0; |
Rafael Espindola | 63702e2 | 2015-06-01 01:30:01 +0000 | [diff] [blame] | 784 | for (const MCSection &Sec : Asm) { |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 785 | uint64_t Address = getSectionAddress(&Sec); |
| 786 | uint64_t Size = Layout.getSectionAddressSize(&Sec); |
| 787 | uint64_t FileSize = Layout.getSectionFileSize(&Sec); |
| 788 | FileSize += getPaddingSize(&Sec, Layout); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 789 | |
| 790 | VMSize = std::max(VMSize, Address + Size); |
| 791 | |
Rafael Espindola | 63702e2 | 2015-06-01 01:30:01 +0000 | [diff] [blame] | 792 | if (Sec.isVirtualSection()) |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 793 | continue; |
| 794 | |
| 795 | SectionDataSize = std::max(SectionDataSize, Address + Size); |
| 796 | SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize); |
| 797 | } |
| 798 | |
| 799 | // The section data is padded to 4 bytes. |
| 800 | // |
| 801 | // FIXME: Is this machine dependent? |
| 802 | unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4); |
| 803 | SectionDataFileSize += SectionDataPadding; |
| 804 | |
| 805 | // Write the prolog, starting with the header and load command... |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 806 | writeHeader(MachO::MH_OBJECT, NumLoadCommands, LoadCommandsSize, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 807 | Asm.getSubsectionsViaSymbols()); |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 808 | uint32_t Prot = |
| 809 | MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE; |
| 810 | writeSegmentLoadCommand("", NumSections, 0, VMSize, SectionDataStart, |
| 811 | SectionDataSize, Prot, Prot); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 812 | |
| 813 | // ... and then the section headers. |
| 814 | uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 815 | for (const MCSection &Section : Asm) { |
| 816 | const auto &Sec = cast<MCSectionMachO>(Section); |
Rafael Espindola | 63702e2 | 2015-06-01 01:30:01 +0000 | [diff] [blame] | 817 | std::vector<RelAndSymbol> &Relocs = Relocations[&Sec]; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 818 | unsigned NumRelocs = Relocs.size(); |
Rafael Espindola | 63702e2 | 2015-06-01 01:30:01 +0000 | [diff] [blame] | 819 | uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec); |
Frederic Riss | 75c0c70 | 2015-08-26 05:09:46 +0000 | [diff] [blame] | 820 | unsigned Flags = Sec.getTypeAndAttributes(); |
| 821 | if (Sec.hasInstructions()) |
| 822 | Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS; |
| 823 | writeSection(Layout, Sec, getSectionAddress(&Sec), SectionStart, Flags, |
| 824 | RelocTableEnd, NumRelocs); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 825 | RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 828 | // Write out the deployment target information, if it's available. |
| 829 | if (VersionInfo.Major != 0) { |
| 830 | assert(VersionInfo.Update < 256 && "unencodable update target version"); |
| 831 | assert(VersionInfo.Minor < 256 && "unencodable minor target version"); |
| 832 | assert(VersionInfo.Major < 65536 && "unencodable major target version"); |
| 833 | uint32_t EncodedVersion = VersionInfo.Update | (VersionInfo.Minor << 8) | |
| 834 | (VersionInfo.Major << 16); |
Tim Northover | 2d4d161 | 2015-10-28 22:36:05 +0000 | [diff] [blame] | 835 | MachO::LoadCommandType LCType; |
| 836 | switch (VersionInfo.Kind) { |
| 837 | case MCVM_OSXVersionMin: |
| 838 | LCType = MachO::LC_VERSION_MIN_MACOSX; |
| 839 | break; |
| 840 | case MCVM_IOSVersionMin: |
| 841 | LCType = MachO::LC_VERSION_MIN_IPHONEOS; |
| 842 | break; |
| 843 | case MCVM_TvOSVersionMin: |
| 844 | LCType = MachO::LC_VERSION_MIN_TVOS; |
| 845 | break; |
| 846 | case MCVM_WatchOSVersionMin: |
| 847 | LCType = MachO::LC_VERSION_MIN_WATCHOS; |
| 848 | break; |
| 849 | } |
| 850 | write32(LCType); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 851 | write32(sizeof(MachO::version_min_command)); |
| 852 | write32(EncodedVersion); |
| 853 | write32(0); // reserved. |
Jim Grosbach | 448334a | 2014-03-18 22:09:05 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 856 | // Write the data-in-code load command, if used. |
| 857 | uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8; |
| 858 | if (NumDataRegions) { |
| 859 | uint64_t DataRegionsOffset = RelocTableEnd; |
| 860 | uint64_t DataRegionsSize = NumDataRegions * 8; |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 861 | writeLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset, |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 862 | DataRegionsSize); |
| 863 | } |
| 864 | |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 865 | // Write the loh load command, if used. |
| 866 | uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize; |
| 867 | if (LOHSize) |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 868 | writeLinkeditLoadCommand(MachO::LC_LINKER_OPTIMIZATION_HINT, |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 869 | DataInCodeTableEnd, LOHSize); |
| 870 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 871 | // Write the symbol table load command, if used. |
| 872 | if (NumSymbols) { |
| 873 | unsigned FirstLocalSymbol = 0; |
| 874 | unsigned NumLocalSymbols = LocalSymbolData.size(); |
| 875 | unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; |
| 876 | unsigned NumExternalSymbols = ExternalSymbolData.size(); |
| 877 | unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; |
| 878 | unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); |
| 879 | unsigned NumIndirectSymbols = Asm.indirect_symbol_size(); |
| 880 | unsigned NumSymTabSymbols = |
| 881 | NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols; |
| 882 | uint64_t IndirectSymbolSize = NumIndirectSymbols * 4; |
| 883 | uint64_t IndirectSymbolOffset = 0; |
| 884 | |
| 885 | // If used, the indirect symbols are written after the section data. |
| 886 | if (NumIndirectSymbols) |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 887 | IndirectSymbolOffset = LOHTableEnd; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 888 | |
| 889 | // The symbol table is written after the indirect symbol data. |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 890 | uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 891 | |
| 892 | // The string table is written after symbol table. |
| 893 | uint64_t StringTableOffset = |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 894 | SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? |
| 895 | sizeof(MachO::nlist_64) : |
| 896 | sizeof(MachO::nlist)); |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 897 | writeSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 898 | StringTableOffset, StringTable.getSize()); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 899 | |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 900 | writeDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 901 | FirstExternalSymbol, NumExternalSymbols, |
| 902 | FirstUndefinedSymbol, NumUndefinedSymbols, |
| 903 | IndirectSymbolOffset, NumIndirectSymbols); |
| 904 | } |
| 905 | |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 906 | // Write the linker options load commands. |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 907 | for (const auto &Option : Asm.getLinkerOptions()) |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 908 | writeLinkerOptionsLoadCommand(Option); |
Daniel Dunbar | eec0f32 | 2013-01-18 01:26:07 +0000 | [diff] [blame] | 909 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 910 | // Write the actual section data. |
Rafael Espindola | 63702e2 | 2015-06-01 01:30:01 +0000 | [diff] [blame] | 911 | for (const MCSection &Sec : Asm) { |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 912 | Asm.writeSectionData(&Sec, Layout); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 913 | |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 914 | uint64_t Pad = getPaddingSize(&Sec, Layout); |
Benjamin Kramer | 97fbdd5 | 2015-04-17 11:12:43 +0000 | [diff] [blame] | 915 | WriteZeros(Pad); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | // Write the extra padding. |
| 919 | WriteZeros(SectionDataPadding); |
| 920 | |
| 921 | // Write the relocation entries. |
Rafael Espindola | 63702e2 | 2015-06-01 01:30:01 +0000 | [diff] [blame] | 922 | for (const MCSection &Sec : Asm) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 923 | // Write the section relocation entries, in reverse order to match 'as' |
| 924 | // (approximately, the exact algorithm is more complicated than this). |
Rafael Espindola | 63702e2 | 2015-06-01 01:30:01 +0000 | [diff] [blame] | 925 | std::vector<RelAndSymbol> &Relocs = Relocations[&Sec]; |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 926 | for (const RelAndSymbol &Rel : make_range(Relocs.rbegin(), Relocs.rend())) { |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 927 | write32(Rel.MRE.r_word0); |
| 928 | write32(Rel.MRE.r_word1); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 932 | // Write out the data-in-code region payload, if there is one. |
| 933 | for (MCAssembler::const_data_region_iterator |
| 934 | it = Asm.data_region_begin(), ie = Asm.data_region_end(); |
| 935 | it != ie; ++it) { |
| 936 | const DataRegionData *Data = &(*it); |
Duncan P. N. Exon Smith | 99d8a8e | 2015-05-20 00:02:39 +0000 | [diff] [blame] | 937 | uint64_t Start = getSymbolAddress(*Data->Start, Layout); |
| 938 | uint64_t End = getSymbolAddress(*Data->End, Layout); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 939 | DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind |
| 940 | << " start: " << Start << "(" << Data->Start->getName() << ")" |
| 941 | << " end: " << End << "(" << Data->End->getName() << ")" |
| 942 | << " size: " << End - Start |
| 943 | << "\n"); |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 944 | write32(Start); |
| 945 | write16(End - Start); |
| 946 | write16(Data->Kind); |
Jim Grosbach | 4b63d2a | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 949 | // Write out the loh commands, if there is one. |
| 950 | if (LOHSize) { |
| 951 | #ifndef NDEBUG |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 952 | unsigned Start = getStream().tell(); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 953 | #endif |
Jim Grosbach | 249af2a | 2015-06-01 23:55:06 +0000 | [diff] [blame] | 954 | Asm.getLOHContainer().emit(*this, Layout); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 955 | // Pad to a multiple of the pointer size. |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 956 | writeBytes("", OffsetToAlignment(LOHRawSize, is64Bit() ? 8 : 4)); |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 957 | assert(getStream().tell() - Start == LOHSize); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 960 | // Write the symbol table data, if used. |
| 961 | if (NumSymbols) { |
| 962 | // Write the indirect symbol entries. |
| 963 | for (MCAssembler::const_indirect_symbol_iterator |
| 964 | it = Asm.indirect_symbol_begin(), |
| 965 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 966 | // Indirect symbols in the non-lazy symbol pointer section have some |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 967 | // special handling. |
| 968 | const MCSectionMachO &Section = |
Rafael Espindola | 64acc7f | 2015-05-26 02:17:21 +0000 | [diff] [blame] | 969 | static_cast<const MCSectionMachO &>(*it->Section); |
David Majnemer | 7b58305 | 2014-03-07 07:36:05 +0000 | [diff] [blame] | 970 | if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) { |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 971 | // If this symbol is defined and internal, mark it as such. |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 972 | if (it->Symbol->isDefined() && !it->Symbol->isExternal()) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 973 | uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL; |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 974 | if (it->Symbol->isAbsolute()) |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 975 | Flags |= MachO::INDIRECT_SYMBOL_ABS; |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 976 | write32(Flags); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 977 | continue; |
| 978 | } |
| 979 | } |
| 980 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 981 | write32(it->Symbol->getIndex()); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | // FIXME: Check that offsets match computed ones. |
| 985 | |
| 986 | // Write the symbol table entries. |
Benjamin Kramer | 1ec70d8 | 2015-06-04 21:17:27 +0000 | [diff] [blame] | 987 | for (auto *SymbolData : |
| 988 | {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) |
| 989 | for (MachSymbolData &Entry : *SymbolData) |
Jim Grosbach | 56ed0bb | 2015-06-04 23:25:54 +0000 | [diff] [blame] | 990 | writeNlist(Entry, Layout); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 991 | |
| 992 | // Write the string table. |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 993 | StringTable.write(getStream()); |
Bill Wendling | eb872e0 | 2011-06-22 21:07:27 +0000 | [diff] [blame] | 994 | } |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Daniel Dunbar | 8888a96 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 997 | MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW, |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 998 | raw_pwrite_stream &OS, |
Daniel Dunbar | fe0c28f | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 999 | bool IsLittleEndian) { |
Daniel Dunbar | 03fcccb | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 1000 | return new MachObjectWriter(MOTW, OS, IsLittleEndian); |
Daniel Dunbar | 79e0e5a | 2010-03-19 10:43:15 +0000 | [diff] [blame] | 1001 | } |