Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===// |
| 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 | |
| 10 | #include "llvm/MC/MCAssembler.h" |
Chris Lattner | bea2c95 | 2009-08-22 19:19:12 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCSectionMachO.h" |
| 12 | #include "llvm/Target/TargetMachOWriterInfo.h" |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
| 15 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 19 | #include <vector> |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 22 | class MachObjectWriter; |
| 23 | |
| 24 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 25 | MachObjectWriter &MOW); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 26 | |
| 27 | class MachObjectWriter { |
| 28 | // See <mach-o/loader.h>. |
| 29 | enum { |
| 30 | Header_Magic32 = 0xFEEDFACE, |
| 31 | Header_Magic64 = 0xFEEDFACF |
| 32 | }; |
| 33 | |
| 34 | static const unsigned Header32Size = 28; |
| 35 | static const unsigned Header64Size = 32; |
| 36 | static const unsigned SegmentLoadCommand32Size = 56; |
| 37 | static const unsigned Section32Size = 68; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 38 | static const unsigned SymtabLoadCommandSize = 24; |
| 39 | static const unsigned DysymtabLoadCommandSize = 80; |
| 40 | static const unsigned Nlist32Size = 12; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 41 | |
| 42 | enum HeaderFileType { |
| 43 | HFT_Object = 0x1 |
| 44 | }; |
| 45 | |
| 46 | enum LoadCommandType { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 47 | LCT_Segment = 0x1, |
| 48 | LCT_Symtab = 0x2, |
| 49 | LCT_Dysymtab = 0xb |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 52 | // See <mach-o/nlist.h>. |
| 53 | enum SymbolTypeType { |
| 54 | STT_Undefined = 0x00, |
| 55 | STT_Absolute = 0x02, |
| 56 | STT_Section = 0x0e |
| 57 | }; |
| 58 | |
| 59 | enum SymbolTypeFlags { |
| 60 | // If any of these bits are set, then the entry is a stab entry number (see |
| 61 | // <mach-o/stab.h>. Otherwise the other masks apply. |
| 62 | STF_StabsEntryMask = 0xe0, |
| 63 | |
| 64 | STF_TypeMask = 0x0e, |
| 65 | STF_External = 0x01, |
| 66 | STF_PrivateExtern = 0x10 |
| 67 | }; |
| 68 | |
| 69 | /// MachSymbolData - Helper struct for containing some precomputed information |
| 70 | /// on symbols. |
| 71 | struct MachSymbolData { |
| 72 | MCSymbolData *SymbolData; |
| 73 | uint64_t StringIndex; |
| 74 | uint8_t SectionIndex; |
| 75 | |
| 76 | // Support lexicographic sorting. |
| 77 | bool operator<(const MachSymbolData &RHS) const { |
| 78 | const std::string &Name = SymbolData->getSymbol().getName(); |
| 79 | return Name < RHS.SymbolData->getSymbol().getName(); |
| 80 | } |
| 81 | }; |
| 82 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 83 | raw_ostream &OS; |
| 84 | bool IsLSB; |
| 85 | |
| 86 | public: |
| 87 | MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true) |
| 88 | : OS(_OS), IsLSB(_IsLSB) { |
| 89 | } |
| 90 | |
| 91 | /// @name Helper Methods |
| 92 | /// @{ |
| 93 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 94 | void Write8(uint8_t Value) { |
| 95 | OS << char(Value); |
| 96 | } |
| 97 | |
| 98 | void Write16(uint16_t Value) { |
| 99 | if (IsLSB) { |
| 100 | Write8(uint8_t(Value >> 0)); |
| 101 | Write8(uint8_t(Value >> 8)); |
| 102 | } else { |
| 103 | Write8(uint8_t(Value >> 8)); |
| 104 | Write8(uint8_t(Value >> 0)); |
| 105 | } |
| 106 | } |
| 107 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 108 | void Write32(uint32_t Value) { |
| 109 | if (IsLSB) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 110 | Write16(uint16_t(Value >> 0)); |
| 111 | Write16(uint16_t(Value >> 16)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 112 | } else { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 113 | Write16(uint16_t(Value >> 16)); |
| 114 | Write16(uint16_t(Value >> 0)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void Write64(uint64_t Value) { |
| 119 | if (IsLSB) { |
| 120 | Write32(uint32_t(Value >> 0)); |
| 121 | Write32(uint32_t(Value >> 32)); |
| 122 | } else { |
| 123 | Write32(uint32_t(Value >> 32)); |
| 124 | Write32(uint32_t(Value >> 0)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | void WriteZeros(unsigned N) { |
| 129 | const char Zeros[16] = { 0 }; |
| 130 | |
| 131 | for (unsigned i = 0, e = N / 16; i != e; ++i) |
| 132 | OS << StringRef(Zeros, 16); |
| 133 | |
| 134 | OS << StringRef(Zeros, N % 16); |
| 135 | } |
| 136 | |
| 137 | void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) { |
| 138 | OS << Str; |
| 139 | if (ZeroFillSize) |
| 140 | WriteZeros(ZeroFillSize - Str.size()); |
| 141 | } |
| 142 | |
| 143 | /// @} |
| 144 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 145 | void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 146 | // struct mach_header (28 bytes) |
| 147 | |
| 148 | uint64_t Start = OS.tell(); |
| 149 | (void) Start; |
| 150 | |
| 151 | Write32(Header_Magic32); |
| 152 | |
| 153 | // FIXME: Support cputype. |
| 154 | Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386); |
| 155 | |
| 156 | // FIXME: Support cpusubtype. |
| 157 | Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL); |
| 158 | |
| 159 | Write32(HFT_Object); |
| 160 | |
| 161 | // Object files have a single load command, the segment. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 162 | Write32(NumLoadCommands); |
| 163 | Write32(LoadCommandsSize); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 164 | Write32(0); // Flags |
| 165 | |
| 166 | assert(OS.tell() - Start == Header32Size); |
| 167 | } |
| 168 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 169 | /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command. |
| 170 | /// |
| 171 | /// \arg NumSections - The number of sections in this segment. |
| 172 | /// \arg SectionDataSize - The total size of the sections. |
| 173 | void WriteSegmentLoadCommand32(unsigned NumSections, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 174 | uint64_t SectionDataStartOffset, |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 175 | uint64_t SectionDataSize) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 176 | // struct segment_command (56 bytes) |
| 177 | |
| 178 | uint64_t Start = OS.tell(); |
| 179 | (void) Start; |
| 180 | |
| 181 | Write32(LCT_Segment); |
| 182 | Write32(SegmentLoadCommand32Size + NumSections * Section32Size); |
| 183 | |
| 184 | WriteString("", 16); |
| 185 | Write32(0); // vmaddr |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 186 | Write32(SectionDataSize); // vmsize |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 187 | Write32(SectionDataStartOffset); // file offset |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 188 | Write32(SectionDataSize); // file size |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 189 | Write32(0x7); // maxprot |
| 190 | Write32(0x7); // initprot |
| 191 | Write32(NumSections); |
| 192 | Write32(0); // flags |
| 193 | |
| 194 | assert(OS.tell() - Start == SegmentLoadCommand32Size); |
| 195 | } |
| 196 | |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 197 | void WriteSection32(const MCSectionData &SD, uint64_t FileOffset) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 198 | // struct section (68 bytes) |
| 199 | |
| 200 | uint64_t Start = OS.tell(); |
| 201 | (void) Start; |
| 202 | |
| 203 | // FIXME: cast<> support! |
| 204 | const MCSectionMachO &Section = |
| 205 | static_cast<const MCSectionMachO&>(SD.getSection()); |
| 206 | WriteString(Section.getSectionName(), 16); |
| 207 | WriteString(Section.getSegmentName(), 16); |
| 208 | Write32(0); // address |
| 209 | Write32(SD.getFileSize()); // size |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 210 | Write32(FileOffset); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 211 | |
| 212 | assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!"); |
| 213 | Write32(Log2_32(SD.getAlignment())); |
| 214 | Write32(0); // file offset of relocation entries |
| 215 | Write32(0); // number of relocation entrions |
| 216 | Write32(Section.getTypeAndAttributes()); |
| 217 | Write32(0); // reserved1 |
| 218 | Write32(Section.getStubSize()); // reserved2 |
| 219 | |
| 220 | assert(OS.tell() - Start == Section32Size); |
| 221 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 222 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 223 | void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, |
| 224 | uint32_t StringTableOffset, |
| 225 | uint32_t StringTableSize) { |
| 226 | // struct symtab_command (24 bytes) |
| 227 | |
| 228 | uint64_t Start = OS.tell(); |
| 229 | (void) Start; |
| 230 | |
| 231 | Write32(LCT_Symtab); |
| 232 | Write32(SymtabLoadCommandSize); |
| 233 | Write32(SymbolOffset); |
| 234 | Write32(NumSymbols); |
| 235 | Write32(StringTableOffset); |
| 236 | Write32(StringTableSize); |
| 237 | |
| 238 | assert(OS.tell() - Start == SymtabLoadCommandSize); |
| 239 | } |
| 240 | |
| 241 | void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol, |
| 242 | uint32_t NumLocalSymbols, |
| 243 | uint32_t FirstExternalSymbol, |
| 244 | uint32_t NumExternalSymbols, |
| 245 | uint32_t FirstUndefinedSymbol, |
| 246 | uint32_t NumUndefinedSymbols, |
| 247 | uint32_t IndirectSymbolOffset, |
| 248 | uint32_t NumIndirectSymbols) { |
| 249 | // struct dysymtab_command (80 bytes) |
| 250 | |
| 251 | uint64_t Start = OS.tell(); |
| 252 | (void) Start; |
| 253 | |
| 254 | Write32(LCT_Dysymtab); |
| 255 | Write32(DysymtabLoadCommandSize); |
| 256 | Write32(FirstLocalSymbol); |
| 257 | Write32(NumLocalSymbols); |
| 258 | Write32(FirstExternalSymbol); |
| 259 | Write32(NumExternalSymbols); |
| 260 | Write32(FirstUndefinedSymbol); |
| 261 | Write32(NumUndefinedSymbols); |
| 262 | Write32(0); // tocoff |
| 263 | Write32(0); // ntoc |
| 264 | Write32(0); // modtaboff |
| 265 | Write32(0); // nmodtab |
| 266 | Write32(0); // extrefsymoff |
| 267 | Write32(0); // nextrefsyms |
| 268 | Write32(IndirectSymbolOffset); |
| 269 | Write32(NumIndirectSymbols); |
| 270 | Write32(0); // extreloff |
| 271 | Write32(0); // nextrel |
| 272 | Write32(0); // locreloff |
| 273 | Write32(0); // nlocrel |
| 274 | |
| 275 | assert(OS.tell() - Start == DysymtabLoadCommandSize); |
| 276 | } |
| 277 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 278 | void WriteNlist32(MachSymbolData &MSD) { |
| 279 | MCSymbol &Symbol = MSD.SymbolData->getSymbol(); |
| 280 | uint8_t Type = 0; |
| 281 | |
| 282 | // Set the N_TYPE bits. See <mach-o/nlist.h>. |
| 283 | // |
| 284 | // FIXME: Are the prebound or indirect fields possible here? |
| 285 | if (Symbol.isUndefined()) |
| 286 | Type = STT_Undefined; |
| 287 | else if (Symbol.isAbsolute()) |
| 288 | Type = STT_Absolute; |
| 289 | else |
| 290 | Type = STT_Section; |
| 291 | |
| 292 | // FIXME: Set STAB bits. |
| 293 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame^] | 294 | if (MSD.SymbolData->isPrivateExtern()) |
| 295 | Type |= STF_PrivateExtern; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 296 | |
| 297 | // Set external bit. |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 298 | if (MSD.SymbolData->isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 299 | Type |= STF_External; |
| 300 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 301 | // struct nlist (12 bytes) |
| 302 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 303 | Write32(MSD.StringIndex); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 304 | Write8(Type); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 305 | Write8(MSD.SectionIndex); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame^] | 306 | |
| 307 | // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' |
| 308 | // value. |
| 309 | Write16(MSD.SymbolData->getFlags() & 0xFFFF); |
| 310 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 311 | Write32(0); // FIXME: Value |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 314 | /// ComputeSymbolTable - Compute the symbol table data |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 315 | /// |
| 316 | /// \param StringTable [out] - The string table data. |
| 317 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 318 | /// string table. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 319 | |
| 320 | void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable, |
| 321 | std::vector<MachSymbolData> &LocalSymbolData, |
| 322 | std::vector<MachSymbolData> &ExternalSymbolData, |
| 323 | std::vector<MachSymbolData> &UndefinedSymbolData) { |
| 324 | // Build section lookup table. |
| 325 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 326 | unsigned Index = 1; |
| 327 | for (MCAssembler::iterator it = Asm.begin(), |
| 328 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 329 | SectionIndexMap[&it->getSection()] = Index; |
| 330 | assert(Index <= 256 && "Too many sections!"); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 331 | |
| 332 | // Index 0 is always the empty string. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 333 | StringMap<uint64_t> StringIndexMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 334 | StringTable += '\x00'; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 335 | |
| 336 | // Build the symbol arrays and the string table, but only for non-local |
| 337 | // symbols. |
| 338 | // |
| 339 | // The particular order that we collect the symbols and create the string |
| 340 | // table, then sort the symbols is chosen to match 'as'. Even though it |
| 341 | // doesn't matter for correctness, this is important for letting us diff .o |
| 342 | // files. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 343 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 344 | ie = Asm.symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 345 | MCSymbol &Symbol = it->getSymbol(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 346 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 347 | if (!it->isExternal() && !Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 348 | continue; |
| 349 | |
| 350 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 351 | if (!Entry) { |
| 352 | Entry = StringTable.size(); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 353 | StringTable += Symbol.getName(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 354 | StringTable += '\x00'; |
| 355 | } |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 356 | |
| 357 | MachSymbolData MSD; |
| 358 | MSD.SymbolData = it; |
| 359 | MSD.StringIndex = Entry; |
| 360 | |
| 361 | if (Symbol.isUndefined()) { |
| 362 | MSD.SectionIndex = 0; |
| 363 | UndefinedSymbolData.push_back(MSD); |
| 364 | } else if (Symbol.isAbsolute()) { |
| 365 | MSD.SectionIndex = 0; |
| 366 | ExternalSymbolData.push_back(MSD); |
| 367 | } else { |
| 368 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 369 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 370 | ExternalSymbolData.push_back(MSD); |
| 371 | } |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 374 | // Now add the data for local symbols. |
| 375 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 376 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 377 | MCSymbol &Symbol = it->getSymbol(); |
| 378 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 379 | if (it->isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 380 | continue; |
| 381 | |
| 382 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 383 | if (!Entry) { |
| 384 | Entry = StringTable.size(); |
| 385 | StringTable += Symbol.getName(); |
| 386 | StringTable += '\x00'; |
| 387 | } |
| 388 | |
| 389 | MachSymbolData MSD; |
| 390 | MSD.SymbolData = it; |
| 391 | MSD.StringIndex = Entry; |
| 392 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 393 | if (Symbol.isAbsolute()) { |
| 394 | MSD.SectionIndex = 0; |
| 395 | LocalSymbolData.push_back(MSD); |
| 396 | } else { |
| 397 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 398 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 399 | LocalSymbolData.push_back(MSD); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // External and undefined symbols are required to be in lexicographic order. |
| 404 | std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 405 | std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 406 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 407 | // The string table is padded to a multiple of 4. |
| 408 | // |
| 409 | // FIXME: Check to see if this varies per arch. |
| 410 | while (StringTable.size() % 4) |
| 411 | StringTable += '\x00'; |
| 412 | } |
| 413 | |
| 414 | void WriteObject(MCAssembler &Asm) { |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 415 | unsigned NumSections = Asm.size(); |
| 416 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 417 | // Compute symbol table information. |
| 418 | SmallString<256> StringTable; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 419 | std::vector<MachSymbolData> LocalSymbolData; |
| 420 | std::vector<MachSymbolData> ExternalSymbolData; |
| 421 | std::vector<MachSymbolData> UndefinedSymbolData; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 422 | unsigned NumSymbols = Asm.symbol_size(); |
| 423 | |
| 424 | // No symbol table command is written if there are no symbols. |
| 425 | if (NumSymbols) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 426 | ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData, |
| 427 | UndefinedSymbolData); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 428 | |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 429 | // Compute the file offsets for all the sections in advance, so that we can |
| 430 | // write things out in order. |
| 431 | SmallVector<uint64_t, 16> SectionFileOffsets; |
| 432 | SectionFileOffsets.resize(NumSections); |
| 433 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 434 | // The section data starts after the header, the segment load command (and |
| 435 | // section headers) and the symbol table. |
| 436 | unsigned NumLoadCommands = 1; |
| 437 | uint64_t LoadCommandsSize = |
| 438 | SegmentLoadCommand32Size + NumSections * Section32Size; |
| 439 | |
| 440 | // Add the symbol table load command sizes, if used. |
| 441 | if (NumSymbols) { |
| 442 | NumLoadCommands += 2; |
| 443 | LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize; |
| 444 | } |
| 445 | |
| 446 | uint64_t FileOffset = Header32Size + LoadCommandsSize; |
| 447 | uint64_t SectionDataStartOffset = FileOffset; |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 448 | uint64_t SectionDataSize = 0; |
| 449 | unsigned Index = 0; |
| 450 | for (MCAssembler::iterator it = Asm.begin(), |
| 451 | ie = Asm.end(); it != ie; ++it, ++Index) { |
| 452 | SectionFileOffsets[Index] = FileOffset; |
| 453 | FileOffset += it->getFileSize(); |
| 454 | SectionDataSize += it->getFileSize(); |
| 455 | } |
| 456 | |
| 457 | // Write the prolog, starting with the header and load command... |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 458 | WriteHeader32(NumLoadCommands, LoadCommandsSize); |
| 459 | WriteSegmentLoadCommand32(NumSections, SectionDataStartOffset, |
| 460 | SectionDataSize); |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 461 | |
| 462 | // ... and then the section headers. |
| 463 | Index = 0; |
| 464 | for (MCAssembler::iterator it = Asm.begin(), |
| 465 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 466 | WriteSection32(*it, SectionFileOffsets[Index]); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 467 | |
| 468 | // Write the symbol table load command, if used. |
| 469 | if (NumSymbols) { |
| 470 | // The string table is written after all the section data. |
| 471 | uint64_t SymbolTableOffset = SectionDataStartOffset + SectionDataSize; |
| 472 | uint64_t StringTableOffset = |
| 473 | SymbolTableOffset + NumSymbols * Nlist32Size; |
| 474 | WriteSymtabLoadCommand(SymbolTableOffset, NumSymbols, |
| 475 | StringTableOffset, StringTable.size()); |
| 476 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 477 | unsigned FirstLocalSymbol = 0; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 478 | unsigned NumLocalSymbols = LocalSymbolData.size(); |
| 479 | unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; |
| 480 | unsigned NumExternalSymbols = ExternalSymbolData.size(); |
| 481 | unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; |
| 482 | unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); |
| 483 | // FIXME: Get correct symbol indices and counts for indirect symbols. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 484 | unsigned IndirectSymbolOffset = 0; |
| 485 | unsigned NumIndirectSymbols = 0; |
| 486 | WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, |
| 487 | FirstExternalSymbol, NumExternalSymbols, |
| 488 | FirstUndefinedSymbol, NumUndefinedSymbols, |
| 489 | IndirectSymbolOffset, NumIndirectSymbols); |
| 490 | } |
| 491 | |
| 492 | // Write the actual section data. |
| 493 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
| 494 | WriteFileData(OS, *it, *this); |
| 495 | |
| 496 | // Write the symbol table data, if used. |
| 497 | if (NumSymbols) { |
| 498 | // FIXME: Check that offsets match computed ones. |
| 499 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 500 | // FIXME: Some of these are ordered by name to help the linker. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 501 | |
| 502 | // Write the symbol table entries. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 503 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 504 | WriteNlist32(LocalSymbolData[i]); |
| 505 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 506 | WriteNlist32(ExternalSymbolData[i]); |
| 507 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 508 | WriteNlist32(UndefinedSymbolData[i]); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 509 | |
| 510 | // Write the string table. |
| 511 | OS << StringTable.str(); |
| 512 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 513 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 514 | }; |
| 515 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 516 | /* *** */ |
| 517 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 518 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 519 | } |
| 520 | |
| 521 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *SD) |
| 522 | : Kind(_Kind), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 523 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 524 | { |
| 525 | if (SD) |
| 526 | SD->getFragmentList().push_back(this); |
| 527 | } |
| 528 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 529 | MCFragment::~MCFragment() { |
| 530 | } |
| 531 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 532 | /* *** */ |
| 533 | |
| 534 | MCSectionData::MCSectionData() : Section(*(MCSection*)0) {} |
| 535 | |
| 536 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
| 537 | : Section(_Section), |
| 538 | Alignment(1), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 539 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 540 | { |
| 541 | if (A) |
| 542 | A->getSectionList().push_back(this); |
| 543 | } |
| 544 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 545 | /* *** */ |
| 546 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 547 | MCSymbolData::MCSymbolData() : Symbol(*(MCSymbol*)0) {} |
| 548 | |
| 549 | MCSymbolData::MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment, |
| 550 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 551 | : Symbol(_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame^] | 552 | IsExternal(false), IsPrivateExtern(false), Flags(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 553 | { |
| 554 | if (A) |
| 555 | A->getSymbolList().push_back(this); |
| 556 | } |
| 557 | |
| 558 | /* *** */ |
| 559 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 560 | MCAssembler::MCAssembler(raw_ostream &_OS) : OS(_OS) {} |
| 561 | |
| 562 | MCAssembler::~MCAssembler() { |
| 563 | } |
| 564 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 565 | void MCAssembler::LayoutSection(MCSectionData &SD) { |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 566 | uint64_t Offset = 0; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 567 | |
| 568 | for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { |
| 569 | MCFragment &F = *it; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 570 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 571 | F.setOffset(Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 572 | |
| 573 | // Evaluate fragment size. |
| 574 | switch (F.getKind()) { |
| 575 | case MCFragment::FT_Align: { |
| 576 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 577 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 578 | uint64_t AlignedOffset = RoundUpToAlignment(Offset, AF.getAlignment()); |
| 579 | uint64_t PaddingBytes = AlignedOffset - Offset; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 580 | |
| 581 | if (PaddingBytes > AF.getMaxBytesToEmit()) |
| 582 | AF.setFileSize(0); |
| 583 | else |
| 584 | AF.setFileSize(PaddingBytes); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | case MCFragment::FT_Data: |
| 589 | case MCFragment::FT_Fill: |
| 590 | F.setFileSize(F.getMaxFileSize()); |
| 591 | break; |
| 592 | |
| 593 | case MCFragment::FT_Org: { |
| 594 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 595 | |
| 596 | if (!OF.getOffset().isAbsolute()) |
| 597 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 598 | uint64_t OrgOffset = OF.getOffset().getConstant(); |
| 599 | |
| 600 | // FIXME: We need a way to communicate this error. |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 601 | if (OrgOffset < Offset) |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 602 | llvm_report_error("invalid .org offset '" + Twine(OrgOffset) + |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 603 | "' (section offset '" + Twine(Offset) + "'"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 604 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 605 | F.setFileSize(OrgOffset - Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 606 | break; |
| 607 | } |
| 608 | } |
| 609 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 610 | Offset += F.getFileSize(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | // FIXME: Pad section? |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 614 | SD.setFileSize(Offset); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /// WriteFileData - Write the \arg F data to the output file. |
| 618 | static void WriteFileData(raw_ostream &OS, const MCFragment &F, |
| 619 | MachObjectWriter &MOW) { |
| 620 | uint64_t Start = OS.tell(); |
| 621 | (void) Start; |
| 622 | |
| 623 | // FIXME: Embed in fragments instead? |
| 624 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 625 | case MCFragment::FT_Align: { |
| 626 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 627 | uint64_t Count = AF.getFileSize() / AF.getValueSize(); |
| 628 | |
| 629 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 630 | // multiple .align directives to enforce the semantics it wants), but is |
| 631 | // severe enough that we want to report it. How to handle this? |
| 632 | if (Count * AF.getValueSize() != AF.getFileSize()) |
| 633 | llvm_report_error("undefined .align directive, value size '" + |
| 634 | Twine(AF.getValueSize()) + |
| 635 | "' is not a divisor of padding size '" + |
| 636 | Twine(AF.getFileSize()) + "'"); |
| 637 | |
| 638 | for (uint64_t i = 0; i != Count; ++i) { |
| 639 | switch (AF.getValueSize()) { |
| 640 | default: |
| 641 | assert(0 && "Invalid size!"); |
| 642 | case 1: MOW.Write8 (uint8_t (AF.getValue())); break; |
| 643 | case 2: MOW.Write16(uint16_t(AF.getValue())); break; |
| 644 | case 4: MOW.Write32(uint32_t(AF.getValue())); break; |
| 645 | case 8: MOW.Write64(uint64_t(AF.getValue())); break; |
| 646 | } |
| 647 | } |
| 648 | break; |
| 649 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 650 | |
| 651 | case MCFragment::FT_Data: |
| 652 | OS << cast<MCDataFragment>(F).getContents().str(); |
| 653 | break; |
| 654 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 655 | case MCFragment::FT_Fill: { |
| 656 | MCFillFragment &FF = cast<MCFillFragment>(F); |
| 657 | |
| 658 | if (!FF.getValue().isAbsolute()) |
| 659 | llvm_unreachable("FIXME: Not yet implemented!"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 660 | int64_t Value = FF.getValue().getConstant(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 661 | |
| 662 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
| 663 | switch (FF.getValueSize()) { |
| 664 | default: |
| 665 | assert(0 && "Invalid size!"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 666 | case 1: MOW.Write8 (uint8_t (Value)); break; |
| 667 | case 2: MOW.Write16(uint16_t(Value)); break; |
| 668 | case 4: MOW.Write32(uint32_t(Value)); break; |
| 669 | case 8: MOW.Write64(uint64_t(Value)); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | break; |
| 673 | } |
| 674 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 675 | case MCFragment::FT_Org: { |
| 676 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 677 | |
| 678 | for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i) |
| 679 | MOW.Write8(uint8_t(OF.getValue())); |
| 680 | |
| 681 | break; |
| 682 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | assert(OS.tell() - Start == F.getFileSize()); |
| 686 | } |
| 687 | |
| 688 | /// WriteFileData - Write the \arg SD data to the output file. |
| 689 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 690 | MachObjectWriter &MOW) { |
| 691 | uint64_t Start = OS.tell(); |
| 692 | (void) Start; |
| 693 | |
| 694 | for (MCSectionData::const_iterator it = SD.begin(), |
| 695 | ie = SD.end(); it != ie; ++it) |
| 696 | WriteFileData(OS, *it, MOW); |
| 697 | |
| 698 | assert(OS.tell() - Start == SD.getFileSize()); |
| 699 | } |
| 700 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 701 | void MCAssembler::Finish() { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 702 | // Layout the sections and fragments. |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 703 | for (iterator it = begin(), ie = end(); it != ie; ++it) |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 704 | LayoutSection(*it); |
| 705 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 706 | // Write the object file. |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 707 | MachObjectWriter MOW(OS); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 708 | MOW.WriteObject(*this); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 709 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 710 | OS.flush(); |
| 711 | } |