blob: 396683ffceadbbaa194bc795f66b05f575bb77dc [file] [log] [blame]
Chris Lattner2c52b792010-07-11 22:07:02 +00001//===-- 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
Rafael Espindola908d2ed2011-12-24 02:14:02 +000014#include "llvm/MC/MCWinCOFFObjectWriter.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/StringRef.h"
David Blaikief564ab62014-04-15 05:25:03 +000018#include "llvm/ADT/STLExtras.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCAssembler.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCSectionCOFF.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCValue.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000029#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000030#include "llvm/Support/COFF.h"
31#include "llvm/Support/Debug.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000032#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000033#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000034#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include <cstdio>
36
Chris Lattner2c52b792010-07-11 22:07:02 +000037using namespace llvm;
38
Chandler Carruthf58e3762014-04-22 03:04:17 +000039#define DEBUG_TYPE "WinCOFFObjectWriter"
40
Chris Lattner2c52b792010-07-11 22:07:02 +000041namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000042typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000043
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000044enum AuxiliaryType {
45 ATFunctionDefinition,
46 ATbfAndefSymbol,
47 ATWeakExternal,
48 ATFile,
49 ATSectionDefinition
50};
Chris Lattner2c52b792010-07-11 22:07:02 +000051
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000052struct AuxSymbol {
53 AuxiliaryType AuxType;
54 COFF::Auxiliary Aux;
55};
Chris Lattner2c52b792010-07-11 22:07:02 +000056
Michael J. Spencerd6283772010-09-27 08:58:26 +000057class COFFSymbol;
58class COFFSection;
59
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000060class COFFSymbol {
61public:
62 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000063
Dmitri Gribenko226fea52013-01-13 16:01:15 +000064 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000065
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000066 name Name;
Michael J. Spencerce2e5352010-09-27 21:17:39 +000067 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000068 AuxiliarySymbols Aux;
69 COFFSymbol *Other;
Michael J. Spencerd6283772010-09-27 08:58:26 +000070 COFFSection *Section;
71 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000072
73 MCSymbolData const *MCData;
74
Dmitri Gribenko226fea52013-01-13 16:01:15 +000075 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000076 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000077
78 bool should_keep() const;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000079};
80
81// This class contains staging data for a COFF relocation entry.
82struct COFFRelocation {
83 COFF::relocation Data;
84 COFFSymbol *Symb;
85
Craig Topperbb694de2014-04-13 04:57:38 +000086 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000087 static size_t size() { return COFF::RelocationSize; }
88};
89
90typedef std::vector<COFFRelocation> relocations;
91
92class COFFSection {
93public:
94 COFF::section Header;
95
96 std::string Name;
Michael J. Spencerce2e5352010-09-27 21:17:39 +000097 int Number;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000098 MCSectionData const *MCData;
Michael J. Spencerd6283772010-09-27 08:58:26 +000099 COFFSymbol *Symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000100 relocations Relocations;
101
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000102 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000103 static size_t size();
104};
105
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000106class WinCOFFObjectWriter : public MCObjectWriter {
107public:
108
David Blaikief564ab62014-04-15 05:25:03 +0000109 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
110 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000111
Michael J. Spencer17990d52010-10-16 08:25:57 +0000112 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
113 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000114
Ahmed Charles56440fd2014-03-06 05:51:42 +0000115 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000116
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000117 // Root level file contents.
118 COFF::header Header;
119 sections Sections;
120 symbols Symbols;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000121 StringTableBuilder Strings;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000122
123 // Maps used during object file creation.
124 section_map SectionMap;
125 symbol_map SymbolMap;
126
David Majnemer4d571592014-09-15 19:42:42 +0000127 bool UseBigObj;
128
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000129 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_ostream &OS);
Yaron Kerencca43c12014-09-16 21:31:04 +0000130
131 void reset() override {
132 memset(&Header, 0, sizeof(Header));
133 Header.Machine = TargetObjectWriter->getMachine();
134 Sections.clear();
135 Symbols.clear();
136 Strings.clear();
137 SectionMap.clear();
138 SymbolMap.clear();
139 MCObjectWriter::reset();
140 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000141
Michael J. Spencer17990d52010-10-16 08:25:57 +0000142 COFFSymbol *createSymbol(StringRef Name);
143 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol * Symbol);
144 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000145
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000146 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000147 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000148
149 void DefineSection(MCSectionData const &SectionData);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000150 void DefineSymbol(MCSymbolData const &SymbolData, MCAssembler &Assembler,
151 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000152
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000153 void SetSymbolName(COFFSymbol &S);
154 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000155
David Majnemer299674e2014-07-13 04:31:19 +0000156 bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm);
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000157
Michael J. Spencerd6283772010-09-27 08:58:26 +0000158 bool IsPhysicalSection(COFFSection *S);
159
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000160 // Entity writing methods.
161
162 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000163 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000164 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
165 void WriteSectionHeader(const COFF::section &S);
166 void WriteRelocation(const COFF::relocation &R);
167
168 // MCObjectWriter interface implementation.
169
Craig Topper34a61bc2014-03-08 07:02:02 +0000170 void ExecutePostLayoutBinding(MCAssembler &Asm,
171 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000172
Craig Topper34a61bc2014-03-08 07:02:02 +0000173 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
174 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000175 MCValue Target, bool &IsPCRel,
176 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000177
Craig Topper34a61bc2014-03-08 07:02:02 +0000178 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000179};
Chris Lattner2c52b792010-07-11 22:07:02 +0000180}
181
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000182static inline void write_uint32_le(void *Data, uint32_t Value) {
183 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
184 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000185}
186
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000187//------------------------------------------------------------------------------
188// Symbol class implementation
189
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000190COFFSymbol::COFFSymbol(StringRef name)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000191 : Name(name.begin(), name.end())
Craig Topperbb694de2014-04-13 04:57:38 +0000192 , Other(nullptr)
193 , Section(nullptr)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000194 , Relocations(0)
Craig Topperbb694de2014-04-13 04:57:38 +0000195 , MCData(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000196 memset(&Data, 0, sizeof(Data));
197}
198
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000199// In the case that the name does not fit within 8 bytes, the offset
200// into the string table is stored in the last 4 bytes instead, leaving
201// the first 4 bytes as 0.
202void COFFSymbol::set_name_offset(uint32_t Offset) {
203 write_uint32_le(Data.Name + 0, 0);
204 write_uint32_le(Data.Name + 4, Offset);
205}
206
Michael J. Spencerd6283772010-09-27 08:58:26 +0000207/// logic to decide if the symbol should be reported in the symbol table
208bool COFFSymbol::should_keep() const {
209 // no section means its external, keep it
Craig Topperbb694de2014-04-13 04:57:38 +0000210 if (!Section)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000211 return true;
212
213 // if it has relocations pointing at it, keep it
214 if (Relocations > 0) {
215 assert(Section->Number != -1 && "Sections with relocations must be real!");
216 return true;
217 }
218
219 // if the section its in is being droped, drop it
220 if (Section->Number == -1)
221 return false;
222
223 // if it is the section symbol, keep it
224 if (Section->Symbol == this)
225 return true;
226
227 // if its temporary, drop it
228 if (MCData && MCData->getSymbol().isTemporary())
229 return false;
230
231 // otherwise, keep it
232 return true;
233}
234
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000235//------------------------------------------------------------------------------
236// Section class implementation
237
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000238COFFSection::COFFSection(StringRef name)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000239 : Name(name)
Craig Topperbb694de2014-04-13 04:57:38 +0000240 , MCData(nullptr)
241 , Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000242 memset(&Header, 0, sizeof(Header));
243}
244
245size_t COFFSection::size() {
246 return COFF::SectionSize;
247}
248
249//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000250// WinCOFFObjectWriter class implementation
251
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000252WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
253 raw_ostream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000254 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000255 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000256
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000257 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000258}
259
Michael J. Spencer17990d52010-10-16 08:25:57 +0000260COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000261 return createCOFFEntity<COFFSymbol>(Name, Symbols);
262}
263
Michael J. Spencer17990d52010-10-16 08:25:57 +0000264COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol * Symbol){
265 symbol_map::iterator i = SymbolMap.find(Symbol);
266 if (i != SymbolMap.end())
267 return i->second;
268 COFFSymbol *RetSymbol
269 = createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
270 SymbolMap[Symbol] = RetSymbol;
271 return RetSymbol;
272}
273
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000274COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000275 return createCOFFEntity<COFFSection>(Name, Sections);
276}
277
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000278/// A template used to lookup or create a symbol/section, and initialize it if
279/// needed.
280template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000281object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name,
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000282 list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000283 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000284
David Blaikief564ab62014-04-15 05:25:03 +0000285 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000286}
287
288/// This function takes a section data object from the assembler
289/// and creates the associated COFF section staging object.
290void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000291 assert(SectionData.getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000292 && "Got non-COFF section in the COFF backend!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000293 // FIXME: Not sure how to verify this (at least in a debug build).
294 MCSectionCOFF const &Sec =
295 static_cast<MCSectionCOFF const &>(SectionData.getSection());
296
297 COFFSection *coff_section = createSection(Sec.getSectionName());
298 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000299 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
300 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
301 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
302 if (COMDATSymbol->Section)
303 report_fatal_error("two sections have the same comdat");
304 COMDATSymbol->Section = coff_section;
305 }
306 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000307
Michael J. Spencerd6283772010-09-27 08:58:26 +0000308 coff_section->Symbol = coff_symbol;
309 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000310 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000311
312 // In this case the auxiliary symbol is a Section Definition.
313 coff_symbol->Aux.resize(1);
314 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
315 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000316 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
317
318 coff_section->Header.Characteristics = Sec.getCharacteristics();
319
320 uint32_t &Characteristics = coff_section->Header.Characteristics;
321 switch (SectionData.getAlignment()) {
322 case 1: Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES; break;
323 case 2: Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES; break;
324 case 4: Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES; break;
325 case 8: Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES; break;
326 case 16: Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES; break;
327 case 32: Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES; break;
328 case 64: Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES; break;
329 case 128: Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES; break;
330 case 256: Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES; break;
331 case 512: Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES; break;
332 case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break;
333 case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break;
334 case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break;
335 case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break;
336 default:
337 llvm_unreachable("unsupported section alignment");
338 }
339
340 // Bind internal COFF section to MC section.
341 coff_section->MCData = &SectionData;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000342 SectionMap[&SectionData.getSection()] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000343}
344
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000345static uint64_t getSymbolValue(const MCSymbolData &Data,
346 const MCAsmLayout &Layout) {
347 if (Data.isCommon() && Data.isExternal())
348 return Data.getCommonSize();
349
350 uint64_t Res;
351 if (!Layout.getSymbolOffset(&Data, Res))
352 return 0;
353
354 return Res;
355}
356
David Majnemera9bdb322014-04-08 22:33:40 +0000357/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000358/// and creates the associated COFF symbol staging object.
Peter Collingbourne89886872013-04-22 18:48:56 +0000359void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000360 MCAssembler &Assembler,
361 const MCAsmLayout &Layout) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000362 MCSymbol const &Symbol = SymbolData.getSymbol();
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000363 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000364 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000365
Michael J. Spencer17990d52010-10-16 08:25:57 +0000366 if (SymbolData.getFlags() & COFF::SF_WeakExternal) {
367 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
368
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000369 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000370 const MCSymbolRefExpr *SymRef =
371 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000372
Peter Collingbourne89886872013-04-22 18:48:56 +0000373 if (!SymRef)
374 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000375
Peter Collingbourne89886872013-04-22 18:48:56 +0000376 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000377 } else {
378 std::string WeakName = std::string(".weak.")
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000379 + Symbol.getName().str()
Michael J. Spencer17990d52010-10-16 08:25:57 +0000380 + ".default";
381 COFFSymbol *WeakDefault = createSymbol(WeakName);
382 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
383 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
384 WeakDefault->Data.Type = 0;
385 WeakDefault->Data.Value = 0;
386 coff_symbol->Other = WeakDefault;
387 }
388
389 // Setup the Weak External auxiliary symbol.
390 coff_symbol->Aux.resize(1);
391 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
392 coff_symbol->Aux[0].AuxType = ATWeakExternal;
393 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
394 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
395 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000396
397 coff_symbol->MCData = &SymbolData;
398 } else {
Rafael Espindolaea9f9d42014-05-01 19:02:03 +0000399 const MCSymbolData &ResSymData = Assembler.getSymbolData(Symbol);
Rafael Espindola575f79a2014-05-01 13:37:57 +0000400 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000401 coff_symbol->Data.Value = getSymbolValue(ResSymData, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000402
Peter Collingbourne89886872013-04-22 18:48:56 +0000403 coff_symbol->Data.Type = (ResSymData.getFlags() & 0x0000FFFF) >> 0;
404 coff_symbol->Data.StorageClass = (ResSymData.getFlags() & 0x00FF0000) >> 16;
405
406 // If no storage class was specified in the streamer, define it here.
407 if (coff_symbol->Data.StorageClass == 0) {
David Majnemer299674e2014-07-13 04:31:19 +0000408 bool IsExternal =
409 ResSymData.isExternal() ||
410 (!ResSymData.getFragment() && !ResSymData.getSymbol().isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000411
David Majnemer299674e2014-07-13 04:31:19 +0000412 coff_symbol->Data.StorageClass = IsExternal
413 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
414 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000415 }
416
Rafael Espindola575f79a2014-05-01 13:37:57 +0000417 if (!Base) {
Reid Klecknerc1e76212013-09-17 23:18:05 +0000418 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola575f79a2014-05-01 13:37:57 +0000419 } else {
420 const MCSymbolData &BaseData = Assembler.getSymbolData(*Base);
Rafael Espindola0766ae02014-06-06 19:26:12 +0000421 if (BaseData.Fragment) {
422 COFFSection *Sec =
Rafael Espindola575f79a2014-05-01 13:37:57 +0000423 SectionMap[&BaseData.Fragment->getParent()->getSection()];
Rafael Espindola0766ae02014-06-06 19:26:12 +0000424
425 if (coff_symbol->Section && coff_symbol->Section != Sec)
426 report_fatal_error("conflicting sections for symbol");
427
428 coff_symbol->Section = Sec;
429 }
Rafael Espindola575f79a2014-05-01 13:37:57 +0000430 }
Peter Collingbourne89886872013-04-22 18:48:56 +0000431
432 coff_symbol->MCData = &ResSymData;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000433 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000434}
435
Nico Rieck01143f92014-02-25 09:50:40 +0000436// Maximum offsets for different string table entry encodings.
437static const unsigned Max6DecimalOffset = 999999;
438static const unsigned Max7DecimalOffset = 9999999;
439static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
440
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000441// Encode a string table entry offset in base 64, padded to 6 chars, and
442// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
443// Buffer must be at least 8 bytes large. No terminating null appended.
444static void encodeBase64StringEntry(char* Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000445 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000446 "Illegal section name encoding for value");
447
448 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
449 "abcdefghijklmnopqrstuvwxyz"
450 "0123456789+/";
451
452 Buffer[0] = '/';
453 Buffer[1] = '/';
454
455 char* Ptr = Buffer + 7;
456 for (unsigned i = 0; i < 6; ++i) {
457 unsigned Rem = Value % 64;
458 Value /= 64;
459 *(Ptr--) = Alphabet[Rem];
460 }
461}
462
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000463void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000464 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000465 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000466
Nico Rieck01143f92014-02-25 09:50:40 +0000467 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000468 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000469 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000470 // With seven digits, we have to skip the terminating null. Because
471 // sprintf always appends it, we use a larger temporary buffer.
472 char buffer[9] = { };
473 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
474 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000475 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000476 // Starting with 10,000,000, offsets are encoded as base64.
477 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000478 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000479 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000480 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000481 } else
482 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000483}
484
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000485void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
486 if (S.Name.size() > COFF::NameSize)
487 S.set_name_offset(Strings.getOffset(S.Name));
488 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000489 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000490}
491
David Majnemer299674e2014-07-13 04:31:19 +0000492bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol,
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000493 MCAssembler &Asm) {
494 // This doesn't seem to be right. Strings referred to from the .data section
495 // need symbols so they can be linked to code in the .text section right?
496
David Majnemer299674e2014-07-13 04:31:19 +0000497 // return Asm.isSymbolLinkerVisible(Symbol);
498
499 // Non-temporary labels should always be visible to the linker.
500 if (!Symbol.isTemporary())
501 return true;
502
503 // Absolute temporary labels are never visible.
504 if (!Symbol.isInSection())
505 return false;
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000506
507 // For now, all non-variable symbols are exported,
508 // the linker will sort the rest out for us.
David Majnemer299674e2014-07-13 04:31:19 +0000509 return !Symbol.isVariable();
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000510}
511
Michael J. Spencerd6283772010-09-27 08:58:26 +0000512bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
513 return (S->Header.Characteristics
514 & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000515}
516
517//------------------------------------------------------------------------------
518// entity writing methods
519
520void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000521 if (UseBigObj) {
522 WriteLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
523 WriteLE16(0xFFFF);
524 WriteLE16(COFF::BigObjHeader::MinBigObjectVersion);
525 WriteLE16(Header.Machine);
526 WriteLE32(Header.TimeDateStamp);
527 for (uint8_t MagicChar : COFF::BigObjMagic)
528 Write8(MagicChar);
David Majnemer15f7ed92014-09-15 20:28:38 +0000529 WriteLE32(0);
530 WriteLE32(0);
531 WriteLE32(0);
532 WriteLE32(0);
David Majnemer4d571592014-09-15 19:42:42 +0000533 WriteLE32(Header.NumberOfSections);
534 WriteLE32(Header.PointerToSymbolTable);
535 WriteLE32(Header.NumberOfSymbols);
536 } else {
537 WriteLE16(Header.Machine);
538 WriteLE16(static_cast<int16_t>(Header.NumberOfSections));
539 WriteLE32(Header.TimeDateStamp);
540 WriteLE32(Header.PointerToSymbolTable);
541 WriteLE32(Header.NumberOfSymbols);
542 WriteLE16(Header.SizeOfOptionalHeader);
543 WriteLE16(Header.Characteristics);
544 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000545}
546
David Blaikief564ab62014-04-15 05:25:03 +0000547void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
548 WriteBytes(StringRef(S.Data.Name, COFF::NameSize));
549 WriteLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000550 if (UseBigObj)
551 WriteLE32(S.Data.SectionNumber);
552 else
553 WriteLE16(static_cast<int16_t>(S.Data.SectionNumber));
David Blaikief564ab62014-04-15 05:25:03 +0000554 WriteLE16(S.Data.Type);
555 Write8(S.Data.StorageClass);
556 Write8(S.Data.NumberOfAuxSymbols);
557 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000558}
559
560void WinCOFFObjectWriter::WriteAuxiliarySymbols(
561 const COFFSymbol::AuxiliarySymbols &S) {
562 for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
563 i != e; ++i) {
564 switch(i->AuxType) {
565 case ATFunctionDefinition:
566 WriteLE32(i->Aux.FunctionDefinition.TagIndex);
567 WriteLE32(i->Aux.FunctionDefinition.TotalSize);
568 WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
569 WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
570 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000571 if (UseBigObj)
572 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000573 break;
574 case ATbfAndefSymbol:
575 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
576 WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
577 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
578 WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
579 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000580 if (UseBigObj)
581 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000582 break;
583 case ATWeakExternal:
584 WriteLE32(i->Aux.WeakExternal.TagIndex);
585 WriteLE32(i->Aux.WeakExternal.Characteristics);
586 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000587 if (UseBigObj)
588 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000589 break;
590 case ATFile:
David Majnemer4d571592014-09-15 19:42:42 +0000591 WriteBytes(
592 StringRef(reinterpret_cast<const char *>(&i->Aux),
593 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000594 break;
595 case ATSectionDefinition:
596 WriteLE32(i->Aux.SectionDefinition.Length);
597 WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations);
598 WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
599 WriteLE32(i->Aux.SectionDefinition.CheckSum);
David Majnemer4d571592014-09-15 19:42:42 +0000600 WriteLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000601 Write8(i->Aux.SectionDefinition.Selection);
602 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000603 WriteLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
604 if (UseBigObj)
605 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000606 break;
607 }
608 }
609}
610
611void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
612 WriteBytes(StringRef(S.Name, COFF::NameSize));
613
614 WriteLE32(S.VirtualSize);
615 WriteLE32(S.VirtualAddress);
616 WriteLE32(S.SizeOfRawData);
617 WriteLE32(S.PointerToRawData);
618 WriteLE32(S.PointerToRelocations);
619 WriteLE32(S.PointerToLineNumbers);
620 WriteLE16(S.NumberOfRelocations);
621 WriteLE16(S.NumberOfLineNumbers);
622 WriteLE32(S.Characteristics);
623}
624
625void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
626 WriteLE32(R.VirtualAddress);
627 WriteLE32(R.SymbolTableIndex);
628 WriteLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000629}
630
631////////////////////////////////////////////////////////////////////////////////
632// MCObjectWriter interface implementations
633
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000634void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
635 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000636 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000637 // entries in the staging area.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000638 for (const auto & Section : Asm)
639 DefineSection(Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000640
David Blaikie583a31c2014-04-18 18:24:25 +0000641 for (MCSymbolData &SD : Asm.symbols())
David Majnemer299674e2014-07-13 04:31:19 +0000642 if (ExportSymbol(SD.getSymbol(), Asm))
David Blaikie583a31c2014-04-18 18:24:25 +0000643 DefineSymbol(SD, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000644}
645
646void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
647 const MCAsmLayout &Layout,
648 const MCFragment *Fragment,
649 const MCFixup &Fixup,
650 MCValue Target,
Rafael Espindola5904e122014-03-29 06:26:49 +0000651 bool &IsPCRel,
Chris Lattner2c52b792010-07-11 22:07:02 +0000652 uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000653 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000654
Reid Klecknerdae7b4e2013-07-15 19:41:21 +0000655 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
656 const MCSymbol &A = Symbol.AliasedSymbol();
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000657 if (!Asm.hasSymbolData(A))
658 Asm.getContext().FatalError(
659 Fixup.getLoc(),
660 Twine("symbol '") + A.getName() + "' can not be undefined");
661
David Blaikie908f4d42014-04-24 16:59:40 +0000662 const MCSymbolData &A_SD = Asm.getSymbolData(A);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000663
664 MCSectionData const *SectionData = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000665
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000666 // Mark this symbol as requiring an entry in the symbol table.
Michael J. Spencer17990d52010-10-16 08:25:57 +0000667 assert(SectionMap.find(&SectionData->getSection()) != SectionMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000668 "Section must already have been defined in ExecutePostLayoutBinding!");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000669 assert(SymbolMap.find(&A_SD.getSymbol()) != SymbolMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000670 "Symbol must already have been defined in ExecutePostLayoutBinding!");
671
Michael J. Spencer17990d52010-10-16 08:25:57 +0000672 COFFSection *coff_section = SectionMap[&SectionData->getSection()];
673 COFFSymbol *coff_symbol = SymbolMap[&A_SD.getSymbol()];
Rafael Espindolaed164772011-04-20 14:01:45 +0000674 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000675 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000676
David Majnemera45a1762014-01-06 07:39:46 +0000677 if (SymB) {
678 const MCSymbol *B = &SymB->getSymbol();
David Blaikie908f4d42014-04-24 16:59:40 +0000679 const MCSymbolData &B_SD = Asm.getSymbolData(*B);
David Majnemera45a1762014-01-06 07:39:46 +0000680 if (!B_SD.getFragment())
681 Asm.getContext().FatalError(
682 Fixup.getLoc(),
683 Twine("symbol '") + B->getName() +
684 "' can not be undefined in a subtraction expression");
685
686 if (!A_SD.getFragment())
687 Asm.getContext().FatalError(
688 Fixup.getLoc(),
689 Twine("symbol '") + Symbol.getName() +
690 "' can not be undefined in a subtraction expression");
691
692 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000693
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000694 // Offset of the symbol in the section
695 int64_t a = Layout.getSymbolOffset(&B_SD);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000696
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000697 // Ofeset of the relocation in the section
698 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
699
700 FixedValue = b - a;
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000701 // In the case where we have SymbA and SymB, we just need to store the delta
702 // between the two symbols. Update FixedValue to account for the delta, and
703 // skip recording the relocation.
Rafael Espindolaed164772011-04-20 14:01:45 +0000704 if (!CrossSection)
705 return;
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000706 } else {
707 FixedValue = Target.getConstant();
708 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000709
710 COFFRelocation Reloc;
711
Daniel Dunbar727be432010-07-31 21:08:54 +0000712 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000713 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000714
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000715 // Turn relocations for temporary symbols into section relocations.
Rafael Espindolaed164772011-04-20 14:01:45 +0000716 if (coff_symbol->MCData->getSymbol().isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000717 Reloc.Symb = coff_symbol->Section->Symbol;
Michael J. Spencera3b34ed2010-10-05 19:48:03 +0000718 FixedValue += Layout.getFragmentOffset(coff_symbol->MCData->Fragment)
719 + coff_symbol->MCData->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000720 } else
721 Reloc.Symb = coff_symbol;
722
723 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000724
725 Reloc.Data.VirtualAddress += Fixup.getOffset();
Nico Rieck1da45292013-04-10 23:28:17 +0000726 Reloc.Data.Type = TargetObjectWriter->getRelocType(Target, Fixup,
727 CrossSection);
Rafael Espindolae61724a2011-12-22 22:21:47 +0000728
729 // FIXME: Can anyone explain what this does other than adjust for the size
730 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000731 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
732 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
733 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
734 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000735 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000736
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000737 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
738 switch (Reloc.Data.Type) {
739 case COFF::IMAGE_REL_ARM_ABSOLUTE:
740 case COFF::IMAGE_REL_ARM_ADDR32:
741 case COFF::IMAGE_REL_ARM_ADDR32NB:
742 case COFF::IMAGE_REL_ARM_TOKEN:
743 case COFF::IMAGE_REL_ARM_SECTION:
744 case COFF::IMAGE_REL_ARM_SECREL:
745 break;
746 case COFF::IMAGE_REL_ARM_BRANCH11:
747 case COFF::IMAGE_REL_ARM_BLX11:
748 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
749 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
750 // for Windows CE).
751 case COFF::IMAGE_REL_ARM_BRANCH24:
752 case COFF::IMAGE_REL_ARM_BLX24:
753 case COFF::IMAGE_REL_ARM_MOV32A:
754 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
755 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000756 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000757 // generate the relocations however the rest of the MSVC toolchain is
758 // unable to handle it.
759 llvm_unreachable("unsupported relocation");
760 break;
761 case COFF::IMAGE_REL_ARM_MOV32T:
762 break;
763 case COFF::IMAGE_REL_ARM_BRANCH20T:
764 case COFF::IMAGE_REL_ARM_BRANCH24T:
765 case COFF::IMAGE_REL_ARM_BLX23T:
766 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
767 // perform a 4 byte adjustment to the relocation. Relative branches are
768 // offset by 4 on ARM, however, because there is no RELA relocations, all
769 // branches are offset by 4.
770 FixedValue = FixedValue + 4;
771 break;
772 }
773 }
774
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000775 if (TargetObjectWriter->recordRelocation(Fixup))
776 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000777}
778
Rafael Espindolabce26a12010-10-05 15:11:03 +0000779void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000780 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000781 size_t SectionsSize = Sections.size();
782 if (SectionsSize > static_cast<size_t>(INT32_MAX))
783 report_fatal_error(
784 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000785
David Majnemer4d571592014-09-15 19:42:42 +0000786 // Assign symbol and section indexes and offsets.
787 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
788
789 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
790
791 DenseMap<COFFSection *, int32_t> SectionIndices(
792 NextPowerOf2(NumberOfSections));
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000793
794 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000795 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000796 for (const auto &Section : Sections) {
David Majnemer4d571592014-09-15 19:42:42 +0000797 SectionIndices[Section.get()] = Number;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000798 Section->Number = Number;
799 Section->Symbol->Data.SectionNumber = Number;
800 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000801 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000802 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000803
David Majnemer4d571592014-09-15 19:42:42 +0000804 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000805 Header.NumberOfSymbols = 0;
806
David Majnemer4d571592014-09-15 19:42:42 +0000807 for (auto FI = Asm.file_names_begin(), FE = Asm.file_names_end();
808 FI != FE; ++FI) {
809 // round up to calculate the number of auxiliary symbols required
810 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
811 unsigned Count = (FI->size() + SymbolSize - 1) / SymbolSize;
812
813 COFFSymbol *file = createSymbol(".file");
814 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
815 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
816 file->Aux.resize(Count);
817
818 unsigned Offset = 0;
819 unsigned Length = FI->size();
820 for (auto & Aux : file->Aux) {
821 Aux.AuxType = ATFile;
822
823 if (Length > SymbolSize) {
824 memcpy(&Aux.Aux, FI->c_str() + Offset, SymbolSize);
825 Length = Length - SymbolSize;
826 } else {
827 memcpy(&Aux.Aux, FI->c_str() + Offset, Length);
828 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
829 break;
830 }
831
832 Offset += SymbolSize;
833 }
834 }
835
David Majnemer9ab5ff12014-08-28 04:02:50 +0000836 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000837 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000838 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000839 Symbol->Data.SectionNumber = Symbol->Section->Number;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000840 if (Symbol->should_keep()) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000841 Symbol->Index = Header.NumberOfSymbols++;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000842 // Update auxiliary symbol info.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000843 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
844 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000845 } else
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000846 Symbol->Index = -1;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000847 }
848
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000849 // Build string table.
850 for (const auto &S : Sections)
851 if (S->Name.size() > COFF::NameSize)
852 Strings.add(S->Name);
853 for (const auto &S : Symbols)
854 if (S->should_keep() && S->Name.size() > COFF::NameSize)
855 Strings.add(S->Name);
856 Strings.finalize(StringTableBuilder::WinCOFF);
857
858 // Set names.
859 for (const auto &S : Sections)
860 SetSectionName(*S);
861 for (auto &S : Symbols)
862 if (S->should_keep())
863 SetSymbolName(*S);
864
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000865 // Fixup weak external references.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000866 for (auto & Symbol : Symbols) {
867 if (Symbol->Other) {
868 assert(Symbol->Index != -1);
869 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
870 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000871 "Symbol's aux symbol must be a Weak External!");
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000872 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000873 }
874 }
875
Nico Riecka37acf72013-07-06 12:13:10 +0000876 // Fixup associative COMDAT sections.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000877 for (auto & Section : Sections) {
878 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000879 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
880 continue;
881
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000882 const MCSectionCOFF &MCSec =
883 static_cast<const MCSectionCOFF &>(Section->MCData->getSection());
Nico Riecka37acf72013-07-06 12:13:10 +0000884
Rafael Espindola0766ae02014-06-06 19:26:12 +0000885 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
886 assert(COMDAT);
887 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
888 assert(COMDATSymbol);
889 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000890 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000891 report_fatal_error(
892 Twine("Missing associated COMDAT section for section ") +
893 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000894
895 // Skip this section if the associated section is unused.
896 if (Assoc->Number == -1)
897 continue;
898
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000899 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = SectionIndices[Assoc];
Nico Riecka37acf72013-07-06 12:13:10 +0000900 }
901
902
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000903 // Assign file offsets to COFF object file structures.
904
905 unsigned offset = 0;
906
David Majnemer4d571592014-09-15 19:42:42 +0000907 if (UseBigObj)
908 offset += COFF::Header32Size;
909 else
910 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000911 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000912
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000913 for (const auto & Section : Asm) {
914 COFFSection *Sec = SectionMap[&Section.getSection()];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000915
Michael J. Spencerd6283772010-09-27 08:58:26 +0000916 if (Sec->Number == -1)
917 continue;
918
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000919 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000920
Michael J. Spencerd6283772010-09-27 08:58:26 +0000921 if (IsPhysicalSection(Sec)) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000922 Sec->Header.PointerToRawData = offset;
923
924 offset += Sec->Header.SizeOfRawData;
925 }
926
927 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000928 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
929
930 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000931 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000932 // size is found in reloc #0. Microsoft tools understand this.
933 Sec->Header.NumberOfRelocations = 0xffff;
934 } else {
935 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
936 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000937 Sec->Header.PointerToRelocations = offset;
938
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000939 if (RelocationsOverflow) {
940 // Reloc #0 will contain actual count, so make room for it.
941 offset += COFF::RelocationSize;
942 }
943
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000944 offset += COFF::RelocationSize * Sec->Relocations.size();
945
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000946 for (auto & Relocation : Sec->Relocations) {
947 assert(Relocation.Symb->Index != -1);
948 Relocation.Data.SymbolTableIndex = Relocation.Symb->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000949 }
950 }
951
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000952 assert(Sec->Symbol->Aux.size() == 1 &&
953 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000954 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000955 assert(Aux.AuxType == ATSectionDefinition &&
956 "Section's symbol's aux symbol must be a Section Definition!");
957 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
958 Aux.Aux.SectionDefinition.NumberOfRelocations =
959 Sec->Header.NumberOfRelocations;
960 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
961 Sec->Header.NumberOfLineNumbers;
962 }
963
964 Header.PointerToSymbolTable = offset;
965
Rafael Espindola5ac4ad22013-12-04 02:26:54 +0000966 // We want a deterministic output. It looks like GNU as also writes 0 in here.
Rafael Espindola9201e6b2013-12-04 02:02:55 +0000967 Header.TimeDateStamp = 0;
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000968
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000969 // Write it all to disk...
970 WriteFileHeader(Header);
971
972 {
973 sections::iterator i, ie;
974 MCAssembler::const_iterator j, je;
975
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000976 for (auto & Section : Sections) {
977 if (Section->Number != -1) {
978 if (Section->Relocations.size() >= 0xffff)
979 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
980 WriteSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000981 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000982 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000983
984 for (i = Sections.begin(), ie = Sections.end(),
985 j = Asm.begin(), je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000986 (i != ie) && (j != je); ++i, ++j) {
987
988 if ((*i)->Number == -1)
989 continue;
990
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000991 if ((*i)->Header.PointerToRawData != 0) {
992 assert(OS.tell() == (*i)->Header.PointerToRawData &&
993 "Section::PointerToRawData is insane!");
994
Jim Grosbach18e2fe42011-12-06 00:03:48 +0000995 Asm.writeSectionData(j, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000996 }
997
998 if ((*i)->Relocations.size() > 0) {
999 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
1000 "Section::PointerToRelocations is insane!");
1001
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001002 if ((*i)->Relocations.size() >= 0xffff) {
1003 // In case of overflow, write actual relocation count as first
1004 // relocation. Including the synthetic reloc itself (+ 1).
1005 COFF::relocation r;
1006 r.VirtualAddress = (*i)->Relocations.size() + 1;
1007 r.SymbolTableIndex = 0;
1008 r.Type = 0;
1009 WriteRelocation(r);
1010 }
1011
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001012 for (const auto & Relocation : (*i)->Relocations)
1013 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001014 } else
1015 assert((*i)->Header.PointerToRelocations == 0 &&
1016 "Section::PointerToRelocations is insane!");
1017 }
1018 }
1019
1020 assert(OS.tell() == Header.PointerToSymbolTable &&
1021 "Header::PointerToSymbolTable is insane!");
1022
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001023 for (auto & Symbol : Symbols)
1024 if (Symbol->Index != -1)
1025 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001026
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001027 OS.write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001028}
1029
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001030MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_) :
1031 Machine(Machine_) {
1032}
1033
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001034// Pin the vtable to this file.
1035void MCWinCOFFObjectTargetWriter::anchor() {}
1036
Chris Lattner2c52b792010-07-11 22:07:02 +00001037//------------------------------------------------------------------------------
1038// WinCOFFObjectWriter factory function
1039
1040namespace llvm {
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001041 MCObjectWriter *createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
1042 raw_ostream &OS) {
1043 return new WinCOFFObjectWriter(MOTW, OS);
Chris Lattner2c52b792010-07-11 22:07:02 +00001044 }
Michael J. Spencerc2129372010-07-26 03:01:28 +00001045}