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