Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 1 | //===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===// |
| 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 | // This file contains an implementation of a Win32 COFF object file writer. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "WinCOFFObjectWriter" |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 15 | |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCObjectWriter.h" |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSection.h" |
| 18 | #include "llvm/MC/MCContext.h" |
| 19 | #include "llvm/MC/MCSymbol.h" |
| 20 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCValue.h" |
| 22 | #include "llvm/MC/MCAssembler.h" |
| 23 | #include "llvm/MC/MCAsmLayout.h" |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSectionCOFF.h" |
| 25 | |
| 26 | #include "llvm/ADT/DenseMap.h" |
| 27 | #include "llvm/ADT/StringMap.h" |
| 28 | #include "llvm/ADT/StringRef.h" |
| 29 | |
| 30 | #include "llvm/Support/COFF.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/ErrorHandling.h" |
| 33 | |
Michael J. Spencer | ab3de49 | 2010-08-03 04:43:33 +0000 | [diff] [blame] | 34 | #include "llvm/System/TimeValue.h" |
| 35 | |
Michael J. Spencer | da0bfcd | 2010-08-21 05:58:13 +0000 | [diff] [blame] | 36 | #include "../Target/X86/X86FixupKinds.h" |
| 37 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 38 | #include <cstdio> |
| 39 | |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
| 42 | namespace { |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 43 | typedef llvm::SmallString<COFF::NameSize> name; |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 44 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 45 | enum AuxiliaryType { |
| 46 | ATFunctionDefinition, |
| 47 | ATbfAndefSymbol, |
| 48 | ATWeakExternal, |
| 49 | ATFile, |
| 50 | ATSectionDefinition |
| 51 | }; |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 52 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 53 | struct AuxSymbol { |
| 54 | AuxiliaryType AuxType; |
| 55 | COFF::Auxiliary Aux; |
| 56 | }; |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 57 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 58 | class COFFSymbol { |
| 59 | public: |
| 60 | COFF::symbol Data; |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 61 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 62 | typedef llvm::SmallVector<AuxSymbol, 1> AuxiliarySymbols; |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 63 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 64 | name Name; |
| 65 | size_t Index; |
| 66 | AuxiliarySymbols Aux; |
| 67 | COFFSymbol *Other; |
| 68 | |
| 69 | MCSymbolData const *MCData; |
| 70 | |
| 71 | COFFSymbol(llvm::StringRef name, size_t index); |
| 72 | size_t size() const; |
| 73 | void set_name_offset(uint32_t Offset); |
| 74 | }; |
| 75 | |
| 76 | // This class contains staging data for a COFF relocation entry. |
| 77 | struct COFFRelocation { |
| 78 | COFF::relocation Data; |
| 79 | COFFSymbol *Symb; |
| 80 | |
| 81 | COFFRelocation() : Symb(NULL) {} |
| 82 | static size_t size() { return COFF::RelocationSize; } |
| 83 | }; |
| 84 | |
| 85 | typedef std::vector<COFFRelocation> relocations; |
| 86 | |
| 87 | class COFFSection { |
| 88 | public: |
| 89 | COFF::section Header; |
| 90 | |
| 91 | std::string Name; |
| 92 | size_t Number; |
| 93 | MCSectionData const *MCData; |
| 94 | COFFSymbol *Symb; |
| 95 | relocations Relocations; |
| 96 | |
| 97 | COFFSection(llvm::StringRef name, size_t Index); |
| 98 | static size_t size(); |
| 99 | }; |
| 100 | |
| 101 | // This class holds the COFF string table. |
| 102 | class StringTable { |
| 103 | typedef llvm::StringMap<size_t> map; |
| 104 | map Map; |
| 105 | |
| 106 | void update_length(); |
| 107 | public: |
| 108 | std::vector<char> Data; |
| 109 | |
| 110 | StringTable(); |
| 111 | size_t size() const; |
| 112 | size_t insert(llvm::StringRef String); |
| 113 | }; |
| 114 | |
| 115 | class WinCOFFObjectWriter : public MCObjectWriter { |
| 116 | public: |
| 117 | |
| 118 | typedef std::vector<COFFSymbol*> symbols; |
| 119 | typedef std::vector<COFFSection*> sections; |
| 120 | |
| 121 | typedef StringMap<COFFSymbol *> name_symbol_map; |
| 122 | typedef StringMap<COFFSection *> name_section_map; |
| 123 | |
| 124 | typedef DenseMap<MCSymbolData const *, COFFSymbol *> symbol_map; |
| 125 | typedef DenseMap<MCSectionData const *, COFFSection *> section_map; |
| 126 | |
| 127 | // Root level file contents. |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 128 | bool Is64Bit; |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 129 | COFF::header Header; |
| 130 | sections Sections; |
| 131 | symbols Symbols; |
| 132 | StringTable Strings; |
| 133 | |
| 134 | // Maps used during object file creation. |
| 135 | section_map SectionMap; |
| 136 | symbol_map SymbolMap; |
| 137 | |
Michael J. Spencer | da0bfcd | 2010-08-21 05:58:13 +0000 | [diff] [blame] | 138 | WinCOFFObjectWriter(raw_ostream &OS, bool is64Bit); |
Benjamin Kramer | 808ecfc | 2010-07-29 11:57:59 +0000 | [diff] [blame] | 139 | ~WinCOFFObjectWriter(); |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 140 | |
| 141 | COFFSymbol *createSymbol(llvm::StringRef Name); |
| 142 | COFFSection *createSection(llvm::StringRef Name); |
| 143 | |
| 144 | void InitCOFFEntity(COFFSymbol &Symbol); |
| 145 | void InitCOFFEntity(COFFSection &Section); |
| 146 | |
| 147 | template <typename object_t, typename list_t> |
| 148 | object_t *createCOFFEntity(llvm::StringRef Name, list_t &List); |
| 149 | |
| 150 | void DefineSection(MCSectionData const &SectionData); |
| 151 | void DefineSymbol(MCSymbolData const &SymbolData, MCAssembler &Assembler); |
| 152 | |
| 153 | bool ExportSection(COFFSection *S); |
| 154 | bool ExportSymbol(MCSymbolData const &SymbolData, MCAssembler &Asm); |
| 155 | |
| 156 | // Entity writing methods. |
| 157 | |
| 158 | void WriteFileHeader(const COFF::header &Header); |
| 159 | void WriteSymbol(const COFFSymbol *S); |
| 160 | void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S); |
| 161 | void WriteSectionHeader(const COFF::section &S); |
| 162 | void WriteRelocation(const COFF::relocation &R); |
| 163 | |
| 164 | // MCObjectWriter interface implementation. |
| 165 | |
| 166 | void ExecutePostLayoutBinding(MCAssembler &Asm); |
| 167 | |
| 168 | void RecordRelocation(const MCAssembler &Asm, |
| 169 | const MCAsmLayout &Layout, |
| 170 | const MCFragment *Fragment, |
| 171 | const MCFixup &Fixup, |
| 172 | MCValue Target, |
| 173 | uint64_t &FixedValue); |
| 174 | |
| 175 | void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout); |
| 176 | }; |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 179 | static inline void write_uint32_le(void *Data, uint32_t const &Value) { |
| 180 | uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data); |
| 181 | Ptr[0] = (Value & 0x000000FF) >> 0; |
| 182 | Ptr[1] = (Value & 0x0000FF00) >> 8; |
| 183 | Ptr[2] = (Value & 0x00FF0000) >> 16; |
| 184 | Ptr[3] = (Value & 0xFF000000) >> 24; |
| 185 | } |
| 186 | |
| 187 | static inline void write_uint16_le(void *Data, uint16_t const &Value) { |
| 188 | uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data); |
| 189 | Ptr[0] = (Value & 0x00FF) >> 0; |
| 190 | Ptr[1] = (Value & 0xFF00) >> 8; |
| 191 | } |
| 192 | |
| 193 | static inline void write_uint8_le(void *Data, uint8_t const &Value) { |
| 194 | uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data); |
| 195 | Ptr[0] = (Value & 0xFF) >> 0; |
| 196 | } |
| 197 | |
| 198 | //------------------------------------------------------------------------------ |
| 199 | // Symbol class implementation |
| 200 | |
| 201 | COFFSymbol::COFFSymbol(llvm::StringRef name, size_t index) |
| 202 | : Name(name.begin(), name.end()), Index(-1) |
| 203 | , Other(NULL), MCData(NULL) { |
| 204 | memset(&Data, 0, sizeof(Data)); |
| 205 | } |
| 206 | |
| 207 | size_t COFFSymbol::size() const { |
| 208 | return COFF::SymbolSize + (Data.NumberOfAuxSymbols * COFF::SymbolSize); |
| 209 | } |
| 210 | |
| 211 | // In the case that the name does not fit within 8 bytes, the offset |
| 212 | // into the string table is stored in the last 4 bytes instead, leaving |
| 213 | // the first 4 bytes as 0. |
| 214 | void COFFSymbol::set_name_offset(uint32_t Offset) { |
| 215 | write_uint32_le(Data.Name + 0, 0); |
| 216 | write_uint32_le(Data.Name + 4, Offset); |
| 217 | } |
| 218 | |
| 219 | //------------------------------------------------------------------------------ |
| 220 | // Section class implementation |
| 221 | |
| 222 | COFFSection::COFFSection(llvm::StringRef name, size_t Index) |
| 223 | : Name(name), Number(Index + 1) |
| 224 | , MCData(NULL), Symb(NULL) { |
| 225 | memset(&Header, 0, sizeof(Header)); |
| 226 | } |
| 227 | |
| 228 | size_t COFFSection::size() { |
| 229 | return COFF::SectionSize; |
| 230 | } |
| 231 | |
| 232 | //------------------------------------------------------------------------------ |
| 233 | // StringTable class implementation |
| 234 | |
| 235 | /// Write the length of the string table into Data. |
| 236 | /// The length of the string table includes uint32 length header. |
| 237 | void StringTable::update_length() { |
| 238 | write_uint32_le(&Data.front(), Data.size()); |
| 239 | } |
| 240 | |
| 241 | StringTable::StringTable() { |
| 242 | // The string table data begins with the length of the entire string table |
| 243 | // including the length header. Allocate space for this header. |
| 244 | Data.resize(4); |
| 245 | } |
| 246 | |
| 247 | size_t StringTable::size() const { |
| 248 | return Data.size(); |
| 249 | } |
| 250 | |
| 251 | /// Add String to the table iff it is not already there. |
| 252 | /// @returns the index into the string table where the string is now located. |
| 253 | size_t StringTable::insert(llvm::StringRef String) { |
| 254 | map::iterator i = Map.find(String); |
| 255 | |
| 256 | if (i != Map.end()) |
| 257 | return i->second; |
| 258 | |
| 259 | size_t Offset = Data.size(); |
| 260 | |
| 261 | // Insert string data into string table. |
| 262 | Data.insert(Data.end(), String.begin(), String.end()); |
| 263 | Data.push_back('\0'); |
| 264 | |
| 265 | // Put a reference to it in the map. |
| 266 | Map[String] = Offset; |
| 267 | |
| 268 | // Update the internal length field. |
| 269 | update_length(); |
| 270 | |
| 271 | return Offset; |
| 272 | } |
| 273 | |
| 274 | //------------------------------------------------------------------------------ |
| 275 | // WinCOFFObjectWriter class implementation |
| 276 | |
Michael J. Spencer | da0bfcd | 2010-08-21 05:58:13 +0000 | [diff] [blame] | 277 | WinCOFFObjectWriter::WinCOFFObjectWriter(raw_ostream &OS, bool is64Bit) |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 278 | : MCObjectWriter(OS, true) |
| 279 | , Is64Bit(is64Bit) { |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 280 | memset(&Header, 0, sizeof(Header)); |
Michael J. Spencer | da0bfcd | 2010-08-21 05:58:13 +0000 | [diff] [blame] | 281 | |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 282 | Is64Bit ? Header.Machine = COFF::IMAGE_FILE_MACHINE_AMD64 |
Michael J. Spencer | da0bfcd | 2010-08-21 05:58:13 +0000 | [diff] [blame] | 283 | : Header.Machine = COFF::IMAGE_FILE_MACHINE_I386; |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Benjamin Kramer | 808ecfc | 2010-07-29 11:57:59 +0000 | [diff] [blame] | 286 | WinCOFFObjectWriter::~WinCOFFObjectWriter() { |
| 287 | for (symbols::iterator I = Symbols.begin(), E = Symbols.end(); I != E; ++I) |
| 288 | delete *I; |
| 289 | for (sections::iterator I = Sections.begin(), E = Sections.end(); I != E; ++I) |
| 290 | delete *I; |
| 291 | } |
| 292 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 293 | COFFSymbol *WinCOFFObjectWriter::createSymbol(llvm::StringRef Name) { |
| 294 | return createCOFFEntity<COFFSymbol>(Name, Symbols); |
| 295 | } |
| 296 | |
| 297 | COFFSection *WinCOFFObjectWriter::createSection(llvm::StringRef Name) { |
| 298 | return createCOFFEntity<COFFSection>(Name, Sections); |
| 299 | } |
| 300 | |
| 301 | /// This function initializes a symbol by entering its name into the string |
| 302 | /// table if it is too long to fit in the symbol table header. |
| 303 | void WinCOFFObjectWriter::InitCOFFEntity(COFFSymbol &S) { |
| 304 | if (S.Name.size() > COFF::NameSize) { |
| 305 | size_t StringTableEntry = Strings.insert(S.Name.c_str()); |
| 306 | |
| 307 | S.set_name_offset(StringTableEntry); |
| 308 | } else |
| 309 | memcpy(S.Data.Name, S.Name.c_str(), S.Name.size()); |
| 310 | } |
| 311 | |
| 312 | /// This function initializes a section by entering its name into the string |
| 313 | /// table if it is too long to fit in the section table header. |
| 314 | void WinCOFFObjectWriter::InitCOFFEntity(COFFSection &S) { |
| 315 | if (S.Name.size() > COFF::NameSize) { |
| 316 | size_t StringTableEntry = Strings.insert(S.Name.c_str()); |
| 317 | |
| 318 | // FIXME: Why is this number 999999? This number is never mentioned in the |
| 319 | // spec. I'm assuming this is due to the printed value needing to fit into |
| 320 | // the S.Header.Name field. In which case why not 9999999 (7 9's instead of |
| 321 | // 6)? The spec does not state if this entry should be null terminated in |
| 322 | // this case, and thus this seems to be the best way to do it. I think I |
| 323 | // just solved my own FIXME... |
| 324 | if (StringTableEntry > 999999) |
| 325 | report_fatal_error("COFF string table is greater than 999999 bytes."); |
| 326 | |
Douglas Gregor | fd2878c | 2010-07-26 03:55:44 +0000 | [diff] [blame] | 327 | sprintf(S.Header.Name, "/%d", (unsigned)StringTableEntry); |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 328 | } else |
| 329 | memcpy(S.Header.Name, S.Name.c_str(), S.Name.size()); |
| 330 | } |
| 331 | |
| 332 | /// A template used to lookup or create a symbol/section, and initialize it if |
| 333 | /// needed. |
| 334 | template <typename object_t, typename list_t> |
| 335 | object_t *WinCOFFObjectWriter::createCOFFEntity(llvm::StringRef Name, |
| 336 | list_t &List) { |
| 337 | object_t *Object = new object_t(Name, List.size()); |
| 338 | |
| 339 | InitCOFFEntity(*Object); |
| 340 | |
| 341 | List.push_back(Object); |
| 342 | |
| 343 | return Object; |
| 344 | } |
| 345 | |
| 346 | /// This function takes a section data object from the assembler |
| 347 | /// and creates the associated COFF section staging object. |
| 348 | void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) { |
| 349 | // FIXME: Not sure how to verify this (at least in a debug build). |
| 350 | MCSectionCOFF const &Sec = |
| 351 | static_cast<MCSectionCOFF const &>(SectionData.getSection()); |
| 352 | |
| 353 | COFFSection *coff_section = createSection(Sec.getSectionName()); |
| 354 | COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName()); |
| 355 | |
| 356 | coff_section->Symb = coff_symbol; |
| 357 | coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC; |
| 358 | coff_symbol->Data.SectionNumber = coff_section->Number; |
| 359 | |
| 360 | // In this case the auxiliary symbol is a Section Definition. |
| 361 | coff_symbol->Aux.resize(1); |
| 362 | memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0])); |
| 363 | coff_symbol->Aux[0].AuxType = ATSectionDefinition; |
| 364 | coff_symbol->Aux[0].Aux.SectionDefinition.Number = coff_section->Number; |
| 365 | coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection(); |
| 366 | |
| 367 | coff_section->Header.Characteristics = Sec.getCharacteristics(); |
| 368 | |
| 369 | uint32_t &Characteristics = coff_section->Header.Characteristics; |
| 370 | switch (SectionData.getAlignment()) { |
| 371 | case 1: Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES; break; |
| 372 | case 2: Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES; break; |
| 373 | case 4: Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES; break; |
| 374 | case 8: Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES; break; |
| 375 | case 16: Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES; break; |
| 376 | case 32: Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES; break; |
| 377 | case 64: Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES; break; |
| 378 | case 128: Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES; break; |
| 379 | case 256: Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES; break; |
| 380 | case 512: Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES; break; |
| 381 | case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break; |
| 382 | case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break; |
| 383 | case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break; |
| 384 | case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break; |
| 385 | default: |
| 386 | llvm_unreachable("unsupported section alignment"); |
| 387 | } |
| 388 | |
| 389 | // Bind internal COFF section to MC section. |
| 390 | coff_section->MCData = &SectionData; |
| 391 | SectionMap[&SectionData] = coff_section; |
| 392 | } |
| 393 | |
| 394 | /// This function takes a section data object from the assembler |
| 395 | /// and creates the associated COFF symbol staging object. |
| 396 | void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData, |
| 397 | MCAssembler &Assembler) { |
| 398 | COFFSymbol *coff_symbol = createSymbol(SymbolData.getSymbol().getName()); |
| 399 | |
| 400 | coff_symbol->Data.Type = (SymbolData.getFlags() & 0x0000FFFF) >> 0; |
| 401 | coff_symbol->Data.StorageClass = (SymbolData.getFlags() & 0x00FF0000) >> 16; |
| 402 | |
| 403 | // If no storage class was specified in the streamer, define it here. |
| 404 | if (coff_symbol->Data.StorageClass == 0) { |
| 405 | bool external = SymbolData.isExternal() || (SymbolData.Fragment == NULL); |
| 406 | |
| 407 | coff_symbol->Data.StorageClass = |
| 408 | external ? COFF::IMAGE_SYM_CLASS_EXTERNAL : COFF::IMAGE_SYM_CLASS_STATIC; |
| 409 | } |
| 410 | |
| 411 | if (SymbolData.getFlags() & COFF::SF_WeakReference) { |
| 412 | coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL; |
| 413 | |
| 414 | const MCExpr *Value = SymbolData.getSymbol().getVariableValue(); |
| 415 | |
| 416 | // FIXME: This assert message isn't very good. |
| 417 | assert(Value->getKind() == MCExpr::SymbolRef && |
| 418 | "Value must be a SymbolRef!"); |
| 419 | |
| 420 | const MCSymbolRefExpr *SymbolRef = |
| 421 | static_cast<const MCSymbolRefExpr *>(Value); |
| 422 | |
| 423 | const MCSymbolData &OtherSymbolData = |
| 424 | Assembler.getSymbolData(SymbolRef->getSymbol()); |
| 425 | |
| 426 | // FIXME: This assert message isn't very good. |
| 427 | assert(SymbolMap.find(&OtherSymbolData) != SymbolMap.end() && |
| 428 | "OtherSymbolData must be in the symbol map!"); |
| 429 | |
| 430 | coff_symbol->Other = SymbolMap[&OtherSymbolData]; |
| 431 | |
| 432 | // Setup the Weak External auxiliary symbol. |
| 433 | coff_symbol->Aux.resize(1); |
| 434 | memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0])); |
| 435 | coff_symbol->Aux[0].AuxType = ATWeakExternal; |
| 436 | coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0; |
| 437 | coff_symbol->Aux[0].Aux.WeakExternal.Characteristics = |
| 438 | COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY; |
| 439 | } |
| 440 | |
| 441 | // Bind internal COFF symbol to MC symbol. |
| 442 | coff_symbol->MCData = &SymbolData; |
| 443 | SymbolMap[&SymbolData] = coff_symbol; |
| 444 | } |
| 445 | |
| 446 | bool WinCOFFObjectWriter::ExportSection(COFFSection *S) { |
| 447 | return (S->Header.Characteristics |
| 448 | & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0; |
| 449 | } |
| 450 | |
| 451 | bool WinCOFFObjectWriter::ExportSymbol(MCSymbolData const &SymbolData, |
| 452 | MCAssembler &Asm) { |
| 453 | // This doesn't seem to be right. Strings referred to from the .data section |
| 454 | // need symbols so they can be linked to code in the .text section right? |
| 455 | |
| 456 | // return Asm.isSymbolLinkerVisible (&SymbolData); |
| 457 | |
| 458 | // For now, all symbols are exported, the linker will sort it out for us. |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | //------------------------------------------------------------------------------ |
| 463 | // entity writing methods |
| 464 | |
| 465 | void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) { |
| 466 | WriteLE16(Header.Machine); |
| 467 | WriteLE16(Header.NumberOfSections); |
| 468 | WriteLE32(Header.TimeDateStamp); |
| 469 | WriteLE32(Header.PointerToSymbolTable); |
| 470 | WriteLE32(Header.NumberOfSymbols); |
| 471 | WriteLE16(Header.SizeOfOptionalHeader); |
| 472 | WriteLE16(Header.Characteristics); |
| 473 | } |
| 474 | |
| 475 | void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol *S) { |
| 476 | WriteBytes(StringRef(S->Data.Name, COFF::NameSize)); |
| 477 | WriteLE32(S->Data.Value); |
| 478 | WriteLE16(S->Data.SectionNumber); |
| 479 | WriteLE16(S->Data.Type); |
| 480 | Write8(S->Data.StorageClass); |
| 481 | Write8(S->Data.NumberOfAuxSymbols); |
| 482 | WriteAuxiliarySymbols(S->Aux); |
| 483 | } |
| 484 | |
| 485 | void WinCOFFObjectWriter::WriteAuxiliarySymbols( |
| 486 | const COFFSymbol::AuxiliarySymbols &S) { |
| 487 | for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end(); |
| 488 | i != e; ++i) { |
| 489 | switch(i->AuxType) { |
| 490 | case ATFunctionDefinition: |
| 491 | WriteLE32(i->Aux.FunctionDefinition.TagIndex); |
| 492 | WriteLE32(i->Aux.FunctionDefinition.TotalSize); |
| 493 | WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber); |
| 494 | WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction); |
| 495 | WriteZeros(sizeof(i->Aux.FunctionDefinition.unused)); |
| 496 | break; |
| 497 | case ATbfAndefSymbol: |
| 498 | WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1)); |
| 499 | WriteLE16(i->Aux.bfAndefSymbol.Linenumber); |
| 500 | WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2)); |
| 501 | WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction); |
| 502 | WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3)); |
| 503 | break; |
| 504 | case ATWeakExternal: |
| 505 | WriteLE32(i->Aux.WeakExternal.TagIndex); |
| 506 | WriteLE32(i->Aux.WeakExternal.Characteristics); |
| 507 | WriteZeros(sizeof(i->Aux.WeakExternal.unused)); |
| 508 | break; |
| 509 | case ATFile: |
| 510 | WriteBytes(StringRef(reinterpret_cast<const char *>(i->Aux.File.FileName), |
| 511 | sizeof(i->Aux.File.FileName))); |
| 512 | break; |
| 513 | case ATSectionDefinition: |
| 514 | WriteLE32(i->Aux.SectionDefinition.Length); |
| 515 | WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations); |
| 516 | WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers); |
| 517 | WriteLE32(i->Aux.SectionDefinition.CheckSum); |
| 518 | WriteLE16(i->Aux.SectionDefinition.Number); |
| 519 | Write8(i->Aux.SectionDefinition.Selection); |
| 520 | WriteZeros(sizeof(i->Aux.SectionDefinition.unused)); |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) { |
| 527 | WriteBytes(StringRef(S.Name, COFF::NameSize)); |
| 528 | |
| 529 | WriteLE32(S.VirtualSize); |
| 530 | WriteLE32(S.VirtualAddress); |
| 531 | WriteLE32(S.SizeOfRawData); |
| 532 | WriteLE32(S.PointerToRawData); |
| 533 | WriteLE32(S.PointerToRelocations); |
| 534 | WriteLE32(S.PointerToLineNumbers); |
| 535 | WriteLE16(S.NumberOfRelocations); |
| 536 | WriteLE16(S.NumberOfLineNumbers); |
| 537 | WriteLE32(S.Characteristics); |
| 538 | } |
| 539 | |
| 540 | void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) { |
| 541 | WriteLE32(R.VirtualAddress); |
| 542 | WriteLE32(R.SymbolTableIndex); |
| 543 | WriteLE16(R.Type); |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | //////////////////////////////////////////////////////////////////////////////// |
| 547 | // MCObjectWriter interface implementations |
| 548 | |
| 549 | void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) { |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 550 | // "Define" each section & symbol. This creates section & symbol |
| 551 | // entries in the staging area and gives them their final indexes. |
| 552 | |
| 553 | for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; i++) |
| 554 | DefineSection(*i); |
| 555 | |
| 556 | for (MCAssembler::const_symbol_iterator i = Asm.symbol_begin(), |
| 557 | e = Asm.symbol_end(); i != e; i++) { |
| 558 | if (ExportSymbol(*i, Asm)) |
| 559 | DefineSymbol(*i, Asm); |
| 560 | } |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm, |
| 564 | const MCAsmLayout &Layout, |
| 565 | const MCFragment *Fragment, |
| 566 | const MCFixup &Fixup, |
| 567 | MCValue Target, |
| 568 | uint64_t &FixedValue) { |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 569 | assert(Target.getSymA() != NULL && "Relocation must reference a symbol!"); |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 570 | |
| 571 | const MCSymbol *A = &Target.getSymA()->getSymbol(); |
| 572 | MCSymbolData &A_SD = Asm.getSymbolData(*A); |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 573 | |
| 574 | MCSectionData const *SectionData = Fragment->getParent(); |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 575 | |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 576 | // Mark this symbol as requiring an entry in the symbol table. |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 577 | assert(SectionMap.find(SectionData) != SectionMap.end() && |
| 578 | "Section must already have been defined in ExecutePostLayoutBinding!"); |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 579 | assert(SymbolMap.find(&A_SD) != SymbolMap.end() && |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 580 | "Symbol must already have been defined in ExecutePostLayoutBinding!"); |
| 581 | |
| 582 | COFFSection *coff_section = SectionMap[SectionData]; |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 583 | COFFSymbol *coff_symbol = SymbolMap[&A_SD]; |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 584 | |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 585 | if (Target.getSymB()) { |
| 586 | const MCSymbol *B = &Target.getSymB()->getSymbol(); |
| 587 | MCSymbolData &B_SD = Asm.getSymbolData(*B); |
| 588 | |
| 589 | FixedValue = Layout.getSymbolAddress(&A_SD) - Layout.getSymbolAddress(&B_SD); |
| 590 | |
| 591 | // In the case where we have SymbA and SymB, we just need to store the delta |
| 592 | // between the two symbols. Update FixedValue to account for the delta, and |
| 593 | // skip recording the relocation. |
| 594 | return; |
| 595 | } else { |
| 596 | FixedValue = Target.getConstant(); |
| 597 | } |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 598 | |
| 599 | COFFRelocation Reloc; |
| 600 | |
Daniel Dunbar | 425f634 | 2010-07-31 21:08:54 +0000 | [diff] [blame] | 601 | Reloc.Data.SymbolTableIndex = 0; |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 602 | Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment); |
| 603 | Reloc.Symb = coff_symbol; |
| 604 | |
| 605 | Reloc.Data.VirtualAddress += Fixup.getOffset(); |
| 606 | |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 607 | switch (Fixup.getKind()) { |
| 608 | case X86::reloc_pcrel_4byte: |
| 609 | case X86::reloc_riprel_4byte: |
| 610 | case X86::reloc_riprel_4byte_movq_load: |
| 611 | Reloc.Data.Type = Is64Bit ? COFF::IMAGE_REL_AMD64_REL32 |
| 612 | : COFF::IMAGE_REL_I386_REL32; |
| 613 | // FIXME: Can anyone explain what this does other than adjust for the size |
| 614 | // of the offset? |
| 615 | FixedValue += 4; |
| 616 | break; |
| 617 | case FK_Data_4: |
| 618 | Reloc.Data.Type = Is64Bit ? COFF::IMAGE_REL_AMD64_ADDR32 |
| 619 | : COFF::IMAGE_REL_I386_DIR32; |
| 620 | break; |
| 621 | case FK_Data_8: |
| 622 | if (Is64Bit) |
| 623 | Reloc.Data.Type = COFF::IMAGE_REL_AMD64_ADDR64; |
| 624 | else |
Michael J. Spencer | da0bfcd | 2010-08-21 05:58:13 +0000 | [diff] [blame] | 625 | llvm_unreachable("unsupported relocation type"); |
Michael J. Spencer | 82c84fd | 2010-08-24 21:04:52 +0000 | [diff] [blame^] | 626 | break; |
| 627 | default: |
| 628 | llvm_unreachable("unsupported relocation type"); |
| 629 | } |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 630 | |
| 631 | coff_section->Relocations.push_back(Reloc); |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void WinCOFFObjectWriter::WriteObject(const MCAssembler &Asm, |
| 635 | const MCAsmLayout &Layout) { |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 636 | // Assign symbol and section indexes and offsets. |
| 637 | |
| 638 | Header.NumberOfSymbols = 0; |
| 639 | |
| 640 | for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) { |
| 641 | COFFSymbol *coff_symbol = *i; |
| 642 | MCSymbolData const *SymbolData = coff_symbol->MCData; |
| 643 | |
| 644 | coff_symbol->Index = Header.NumberOfSymbols++; |
| 645 | |
| 646 | // Update section number & offset for symbols that have them. |
| 647 | if ((SymbolData != NULL) && (SymbolData->Fragment != NULL)) { |
| 648 | COFFSection *coff_section = SectionMap[SymbolData->Fragment->getParent()]; |
| 649 | |
| 650 | coff_symbol->Data.SectionNumber = coff_section->Number; |
Michael J. Spencer | 237f8fe | 2010-08-03 05:02:46 +0000 | [diff] [blame] | 651 | coff_symbol->Data.Value = Layout.getFragmentOffset(SymbolData->Fragment) |
| 652 | + SymbolData->Offset; |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | // Update auxiliary symbol info. |
| 656 | coff_symbol->Data.NumberOfAuxSymbols = coff_symbol->Aux.size(); |
| 657 | Header.NumberOfSymbols += coff_symbol->Data.NumberOfAuxSymbols; |
| 658 | } |
| 659 | |
| 660 | // Fixup weak external references. |
| 661 | for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) { |
| 662 | COFFSymbol *symb = *i; |
| 663 | |
| 664 | if (symb->Other != NULL) { |
| 665 | assert(symb->Aux.size() == 1 && |
| 666 | "Symbol must contain one aux symbol!"); |
| 667 | assert(symb->Aux[0].AuxType == ATWeakExternal && |
| 668 | "Symbol's aux symbol must be a Weak External!"); |
| 669 | symb->Aux[0].Aux.WeakExternal.TagIndex = symb->Other->Index; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | // Assign file offsets to COFF object file structures. |
| 674 | |
| 675 | unsigned offset = 0; |
| 676 | |
| 677 | offset += COFF::HeaderSize; |
| 678 | offset += COFF::SectionSize * Asm.size(); |
| 679 | |
| 680 | Header.NumberOfSections = Sections.size(); |
| 681 | |
| 682 | for (MCAssembler::const_iterator i = Asm.begin(), |
| 683 | e = Asm.end(); |
| 684 | i != e; i++) { |
| 685 | COFFSection *Sec = SectionMap[i]; |
| 686 | |
| 687 | Sec->Header.SizeOfRawData = Layout.getSectionFileSize(i); |
| 688 | |
| 689 | if (ExportSection(Sec)) { |
| 690 | Sec->Header.PointerToRawData = offset; |
| 691 | |
| 692 | offset += Sec->Header.SizeOfRawData; |
| 693 | } |
| 694 | |
| 695 | if (Sec->Relocations.size() > 0) { |
| 696 | Sec->Header.NumberOfRelocations = Sec->Relocations.size(); |
| 697 | Sec->Header.PointerToRelocations = offset; |
| 698 | |
| 699 | offset += COFF::RelocationSize * Sec->Relocations.size(); |
| 700 | |
| 701 | for (relocations::iterator cr = Sec->Relocations.begin(), |
| 702 | er = Sec->Relocations.end(); |
| 703 | cr != er; cr++) { |
| 704 | (*cr).Data.SymbolTableIndex = (*cr).Symb->Index; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | assert(Sec->Symb->Aux.size() == 1 && "Section's symbol must have one aux!"); |
| 709 | AuxSymbol &Aux = Sec->Symb->Aux[0]; |
| 710 | assert(Aux.AuxType == ATSectionDefinition && |
| 711 | "Section's symbol's aux symbol must be a Section Definition!"); |
| 712 | Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData; |
| 713 | Aux.Aux.SectionDefinition.NumberOfRelocations = |
| 714 | Sec->Header.NumberOfRelocations; |
| 715 | Aux.Aux.SectionDefinition.NumberOfLinenumbers = |
| 716 | Sec->Header.NumberOfLineNumbers; |
| 717 | } |
| 718 | |
| 719 | Header.PointerToSymbolTable = offset; |
| 720 | |
Michael J. Spencer | ab3de49 | 2010-08-03 04:43:33 +0000 | [diff] [blame] | 721 | Header.TimeDateStamp = sys::TimeValue::now().toEpochTime(); |
| 722 | |
Michael J. Spencer | 801a359 | 2010-07-26 02:17:32 +0000 | [diff] [blame] | 723 | // Write it all to disk... |
| 724 | WriteFileHeader(Header); |
| 725 | |
| 726 | { |
| 727 | sections::iterator i, ie; |
| 728 | MCAssembler::const_iterator j, je; |
| 729 | |
| 730 | for (i = Sections.begin(), ie = Sections.end(); i != ie; i++) |
| 731 | WriteSectionHeader((*i)->Header); |
| 732 | |
| 733 | for (i = Sections.begin(), ie = Sections.end(), |
| 734 | j = Asm.begin(), je = Asm.end(); |
| 735 | (i != ie) && (j != je); i++, j++) { |
| 736 | if ((*i)->Header.PointerToRawData != 0) { |
| 737 | assert(OS.tell() == (*i)->Header.PointerToRawData && |
| 738 | "Section::PointerToRawData is insane!"); |
| 739 | |
| 740 | Asm.WriteSectionData(j, Layout, this); |
| 741 | } |
| 742 | |
| 743 | if ((*i)->Relocations.size() > 0) { |
| 744 | assert(OS.tell() == (*i)->Header.PointerToRelocations && |
| 745 | "Section::PointerToRelocations is insane!"); |
| 746 | |
| 747 | for (relocations::const_iterator k = (*i)->Relocations.begin(), |
| 748 | ke = (*i)->Relocations.end(); |
| 749 | k != ke; k++) { |
| 750 | WriteRelocation(k->Data); |
| 751 | } |
| 752 | } else |
| 753 | assert((*i)->Header.PointerToRelocations == 0 && |
| 754 | "Section::PointerToRelocations is insane!"); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | assert(OS.tell() == Header.PointerToSymbolTable && |
| 759 | "Header::PointerToSymbolTable is insane!"); |
| 760 | |
| 761 | for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) |
| 762 | WriteSymbol(*i); |
| 763 | |
| 764 | OS.write((char const *)&Strings.Data.front(), Strings.Data.size()); |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | //------------------------------------------------------------------------------ |
| 768 | // WinCOFFObjectWriter factory function |
| 769 | |
| 770 | namespace llvm { |
Michael J. Spencer | da0bfcd | 2010-08-21 05:58:13 +0000 | [diff] [blame] | 771 | MCObjectWriter *createWinCOFFObjectWriter(raw_ostream &OS, bool is64Bit) { |
| 772 | return new WinCOFFObjectWriter(OS, is64Bit); |
Chris Lattner | b162290 | 2010-07-11 22:07:02 +0000 | [diff] [blame] | 773 | } |
Michael J. Spencer | 933304e | 2010-07-26 03:01:28 +0000 | [diff] [blame] | 774 | } |