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