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