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