blob: 8805773a3ec4fb860e29bf728f749c113a3a06a2 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/MC/MCELFObjectWriter.h"
Rafael Espindola3963d612011-12-22 03:38:00 +000015#include "llvm/ADT/OwningPtr.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/STLExtras.h"
Rafael Espindola3963d612011-12-22 03:38:00 +000017#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallString.h"
Matt Fleming3565a062010-08-16 18:57:57 +000019#include "llvm/ADT/StringMap.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000020#include "llvm/MC/MCAsmBackend.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCAsmLayout.h"
Rafael Espindola3963d612011-12-22 03:38:00 +000022#include "llvm/MC/MCAssembler.h"
Matt Fleming3565a062010-08-16 18:57:57 +000023#include "llvm/MC/MCContext.h"
Tim Northover6eb3e872012-12-07 16:50:23 +000024#include "llvm/MC/MCELF.h"
Rafael Espindola3963d612011-12-22 03:38:00 +000025#include "llvm/MC/MCELFSymbolFlags.h"
Matt Fleming3565a062010-08-16 18:57:57 +000026#include "llvm/MC/MCExpr.h"
Craig Topperf1d0f772012-03-26 06:58:25 +000027#include "llvm/MC/MCFixupKindInfo.h"
28#include "llvm/MC/MCObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000029#include "llvm/MC/MCSectionELF.h"
Matt Fleming3565a062010-08-16 18:57:57 +000030#include "llvm/MC/MCValue.h"
31#include "llvm/Support/Debug.h"
Matt Fleming3565a062010-08-16 18:57:57 +000032#include "llvm/Support/ELF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000033#include "llvm/Support/ErrorHandling.h"
Matt Fleming3565a062010-08-16 18:57:57 +000034#include <vector>
35using namespace llvm;
36
Jason W Kime964d112011-05-11 22:53:06 +000037#undef DEBUG_TYPE
38#define DEBUG_TYPE "reloc-info"
39
Rafael Espindola3963d612011-12-22 03:38:00 +000040namespace {
41class ELFObjectWriter : public MCObjectWriter {
42 protected:
43
44 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
45 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
46 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
47 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
48 bool Used, bool Renamed);
49 static bool isLocal(const MCSymbolData &Data, bool isSignature,
50 bool isUsedInReloc);
51 static bool IsELFMetaDataSection(const MCSectionData &SD);
52 static uint64_t DataSectionSize(const MCSectionData &SD);
53 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
54 const MCSectionData &SD);
55 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
56 const MCSectionData &SD);
57
58 void WriteDataSectionData(MCAssembler &Asm,
59 const MCAsmLayout &Layout,
60 const MCSectionELF &Section);
61
62 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
63 return Kind == X86::reloc_riprel_4byte ||
64 Kind == X86::reloc_riprel_4byte_movq_load;
65 }*/
66
67 /// ELFSymbolData - Helper struct for containing some precomputed
68 /// information on symbols.
69 struct ELFSymbolData {
70 MCSymbolData *SymbolData;
71 uint64_t StringIndex;
72 uint32_t SectionIndex;
73
74 // Support lexicographic sorting.
75 bool operator<(const ELFSymbolData &RHS) const {
76 if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
77 return true;
78 if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
79 return false;
80 return SymbolData->getSymbol().getName() <
81 RHS.SymbolData->getSymbol().getName();
82 }
83 };
84
Rafael Espindola3963d612011-12-22 03:38:00 +000085 /// The target specific ELF writer instance.
86 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
87
88 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
89 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
90 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
91
92 llvm::DenseMap<const MCSectionData*,
93 std::vector<ELFRelocationEntry> > Relocations;
94 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
95
96 /// @}
97 /// @name Symbol Table Data
98 /// @{
99
100 SmallString<256> StringTable;
101 std::vector<ELFSymbolData> LocalSymbolData;
102 std::vector<ELFSymbolData> ExternalSymbolData;
103 std::vector<ELFSymbolData> UndefinedSymbolData;
104
105 /// @}
106
107 bool NeedsGOT;
108
109 bool NeedsSymtabShndx;
110
111 // This holds the symbol table index of the last local symbol.
112 unsigned LastLocalSymbolIndex;
113 // This holds the .strtab section index.
114 unsigned StringTableIndex;
115 // This holds the .symtab section index.
116 unsigned SymbolTableIndex;
117
118 unsigned ShstrtabIndex;
119
120
121 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
122 const MCValue &Target,
123 const MCFragment &F,
124 const MCFixup &Fixup,
125 bool IsPCRel) const;
126
127 // TargetObjectWriter wrappers.
128 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
129 const MCValue &Target,
130 const MCFragment &F,
131 const MCFixup &Fixup,
132 bool IsPCRel) const {
133 return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
134 }
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000135 const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
136 const MCFixup &Fixup,
137 bool IsPCRel) const {
138 return TargetObjectWriter->undefinedExplicitRelSym(Target, Fixup, IsPCRel);
139 }
Rafael Espindola3963d612011-12-22 03:38:00 +0000140
141 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
142 bool hasRelocationAddend() const {
143 return TargetObjectWriter->hasRelocationAddend();
144 }
Rafael Espindola3963d612011-12-22 03:38:00 +0000145 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
146 bool IsPCRel, bool IsRelocWithSymbol,
147 int64_t Addend) const {
148 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
149 IsRelocWithSymbol, Addend);
150 }
151
Rafael Espindola3963d612011-12-22 03:38:00 +0000152 public:
153 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
154 raw_ostream &_OS, bool IsLittleEndian)
155 : MCObjectWriter(_OS, IsLittleEndian),
156 TargetObjectWriter(MOTW),
157 NeedsGOT(false), NeedsSymtabShndx(false){
158 }
159
160 virtual ~ELFObjectWriter();
161
162 void WriteWord(uint64_t W) {
163 if (is64Bit())
164 Write64(W);
165 else
166 Write32(W);
167 }
168
169 void StringLE16(char *buf, uint16_t Value) {
170 buf[0] = char(Value >> 0);
171 buf[1] = char(Value >> 8);
172 }
173
174 void StringLE32(char *buf, uint32_t Value) {
175 StringLE16(buf, uint16_t(Value >> 0));
176 StringLE16(buf + 2, uint16_t(Value >> 16));
177 }
178
179 void StringLE64(char *buf, uint64_t Value) {
180 StringLE32(buf, uint32_t(Value >> 0));
181 StringLE32(buf + 4, uint32_t(Value >> 32));
182 }
183
184 void StringBE16(char *buf ,uint16_t Value) {
185 buf[0] = char(Value >> 8);
186 buf[1] = char(Value >> 0);
187 }
188
189 void StringBE32(char *buf, uint32_t Value) {
190 StringBE16(buf, uint16_t(Value >> 16));
191 StringBE16(buf + 2, uint16_t(Value >> 0));
192 }
193
194 void StringBE64(char *buf, uint64_t Value) {
195 StringBE32(buf, uint32_t(Value >> 32));
196 StringBE32(buf + 4, uint32_t(Value >> 0));
197 }
198
199 void String8(MCDataFragment &F, uint8_t Value) {
200 char buf[1];
201 buf[0] = Value;
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000202 F.getContents().append(&buf[0], &buf[1]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000203 }
204
205 void String16(MCDataFragment &F, uint16_t Value) {
206 char buf[2];
207 if (isLittleEndian())
208 StringLE16(buf, Value);
209 else
210 StringBE16(buf, Value);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000211 F.getContents().append(&buf[0], &buf[2]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000212 }
213
214 void String32(MCDataFragment &F, uint32_t Value) {
215 char buf[4];
216 if (isLittleEndian())
217 StringLE32(buf, Value);
218 else
219 StringBE32(buf, Value);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000220 F.getContents().append(&buf[0], &buf[4]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000221 }
222
223 void String64(MCDataFragment &F, uint64_t Value) {
224 char buf[8];
225 if (isLittleEndian())
226 StringLE64(buf, Value);
227 else
228 StringBE64(buf, Value);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000229 F.getContents().append(&buf[0], &buf[8]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000230 }
231
Jack Carter9a7bf432013-01-30 02:09:52 +0000232 void WriteHeader(const MCAssembler &Asm,
233 uint64_t SectionDataSize,
Rafael Espindola3963d612011-12-22 03:38:00 +0000234 unsigned NumberOfSections);
235
236 void WriteSymbolEntry(MCDataFragment *SymtabF,
237 MCDataFragment *ShndxF,
238 uint64_t name, uint8_t info,
239 uint64_t value, uint64_t size,
240 uint8_t other, uint32_t shndx,
241 bool Reserved);
242
243 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
244 ELFSymbolData &MSD,
245 const MCAsmLayout &Layout);
246
247 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
248 void WriteSymbolTable(MCDataFragment *SymtabF,
249 MCDataFragment *ShndxF,
250 const MCAssembler &Asm,
251 const MCAsmLayout &Layout,
252 const SectionIndexMapTy &SectionIndexMap);
253
254 virtual void RecordRelocation(const MCAssembler &Asm,
255 const MCAsmLayout &Layout,
256 const MCFragment *Fragment,
257 const MCFixup &Fixup,
258 MCValue Target, uint64_t &FixedValue);
259
260 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
261 const MCSymbol *S);
262
263 // Map from a group section to the signature symbol
264 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
265 // Map from a signature symbol to the group section
266 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
267 // Map from a section to the section with the relocations
268 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
269 // Map from a section to its offset
270 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
271
272 /// ComputeSymbolTable - Compute the symbol table data
273 ///
Rafael Espindola828ea382012-08-27 16:04:24 +0000274 /// \param Asm - The assembler.
275 /// \param SectionIndexMap - Maps a section to its index.
276 /// \param RevGroupMap - Maps a signature symbol to the group section.
277 /// \param NumRegularSections - Number of non-relocation sections.
Rafael Espindola3963d612011-12-22 03:38:00 +0000278 void ComputeSymbolTable(MCAssembler &Asm,
279 const SectionIndexMapTy &SectionIndexMap,
280 RevGroupMapTy RevGroupMap,
281 unsigned NumRegularSections);
282
283 void ComputeIndexMap(MCAssembler &Asm,
284 SectionIndexMapTy &SectionIndexMap,
285 const RelMapTy &RelMap);
286
287 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
288 RelMapTy &RelMap);
289
290 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
291 const RelMapTy &RelMap);
292
293 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
294 SectionIndexMapTy &SectionIndexMap,
295 const RelMapTy &RelMap);
296
297 // Create the sections that show up in the symbol table. Currently
298 // those are the .note.GNU-stack section and the group sections.
299 void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
300 GroupMapTy &GroupMap,
301 RevGroupMapTy &RevGroupMap,
302 SectionIndexMapTy &SectionIndexMap,
303 const RelMapTy &RelMap);
304
305 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
306 const MCAsmLayout &Layout);
307
308 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
309 const MCAsmLayout &Layout,
310 const SectionIndexMapTy &SectionIndexMap,
311 const SectionOffsetMapTy &SectionOffsetMap);
312
313 void ComputeSectionOrder(MCAssembler &Asm,
314 std::vector<const MCSectionELF*> &Sections);
315
316 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
317 uint64_t Address, uint64_t Offset,
318 uint64_t Size, uint32_t Link, uint32_t Info,
319 uint64_t Alignment, uint64_t EntrySize);
320
321 void WriteRelocationsFragment(const MCAssembler &Asm,
322 MCDataFragment *F,
323 const MCSectionData *SD);
324
325 virtual bool
326 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
327 const MCSymbolData &DataA,
328 const MCFragment &FB,
329 bool InSet,
330 bool IsPCRel) const;
331
332 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
333 void WriteSection(MCAssembler &Asm,
334 const SectionIndexMapTy &SectionIndexMap,
335 uint32_t GroupSymbolIndex,
336 uint64_t Offset, uint64_t Size, uint64_t Alignment,
337 const MCSectionELF &Section);
338 };
339}
340
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000341bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
342 const MCFixupKindInfo &FKI =
343 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
344
345 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
346}
347
348bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
349 switch (Variant) {
350 default:
351 return false;
352 case MCSymbolRefExpr::VK_GOT:
353 case MCSymbolRefExpr::VK_PLT:
354 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000355 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000356 case MCSymbolRefExpr::VK_TPOFF:
357 case MCSymbolRefExpr::VK_TLSGD:
358 case MCSymbolRefExpr::VK_GOTTPOFF:
359 case MCSymbolRefExpr::VK_INDNTPOFF:
360 case MCSymbolRefExpr::VK_NTPOFF:
361 case MCSymbolRefExpr::VK_GOTNTPOFF:
362 case MCSymbolRefExpr::VK_TLSLDM:
363 case MCSymbolRefExpr::VK_DTPOFF:
364 case MCSymbolRefExpr::VK_TLSLD:
365 return true;
366 }
367}
368
Jason W Kimd3443e92010-11-15 16:18:39 +0000369ELFObjectWriter::~ELFObjectWriter()
370{}
371
Matt Fleming3565a062010-08-16 18:57:57 +0000372// Emit the ELF header.
Jack Carter9a7bf432013-01-30 02:09:52 +0000373void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
374 uint64_t SectionDataSize,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000375 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000376 // ELF Header
377 // ----------
378 //
379 // Note
380 // ----
381 // emitWord method behaves differently for ELF32 and ELF64, writing
382 // 4 bytes in the former and 8 in the latter.
383
384 Write8(0x7f); // e_ident[EI_MAG0]
385 Write8('E'); // e_ident[EI_MAG1]
386 Write8('L'); // e_ident[EI_MAG2]
387 Write8('F'); // e_ident[EI_MAG3]
388
Rafael Espindolabff66a82010-12-18 03:27:34 +0000389 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000390
391 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000392 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000393
394 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000395 // e_ident[EI_OSABI]
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000396 Write8(TargetObjectWriter->getOSABI());
Matt Fleming3565a062010-08-16 18:57:57 +0000397 Write8(0); // e_ident[EI_ABIVERSION]
398
399 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
400
401 Write16(ELF::ET_REL); // e_type
402
Rafael Espindolabff66a82010-12-18 03:27:34 +0000403 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000404
405 Write32(ELF::EV_CURRENT); // e_version
406 WriteWord(0); // e_entry, no entry point in .o file
407 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000408 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000409 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000410
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000411 // e_flags = whatever the target wants
Jack Carter9a7bf432013-01-30 02:09:52 +0000412 Write32(Asm.getELFHeaderEFlags());
Matt Fleming3565a062010-08-16 18:57:57 +0000413
414 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000415 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000416
417 Write16(0); // e_phentsize = prog header entry size
418 Write16(0); // e_phnum = # prog header entries = 0
419
420 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000421 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000422
423 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000424 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000425 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000426 else
427 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000428
429 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000430 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000431 Write16(ELF::SHN_XINDEX);
432 else
433 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000434}
435
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000436void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
437 MCDataFragment *ShndxF,
438 uint64_t name,
439 uint8_t info, uint64_t value,
440 uint64_t size, uint8_t other,
441 uint32_t shndx,
442 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000443 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000444 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000445 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000446 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000447 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000448 }
449
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000450 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
451 uint16_t(ELF::SHN_XINDEX) : shndx;
452
Rafael Espindolabff66a82010-12-18 03:27:34 +0000453 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000454 String32(*SymtabF, name); // st_name
455 String8(*SymtabF, info); // st_info
456 String8(*SymtabF, other); // st_other
457 String16(*SymtabF, Index); // st_shndx
458 String64(*SymtabF, value); // st_value
459 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000460 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000461 String32(*SymtabF, name); // st_name
462 String32(*SymtabF, value); // st_value
463 String32(*SymtabF, size); // st_size
464 String8(*SymtabF, info); // st_info
465 String8(*SymtabF, other); // st_other
466 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000467 }
468}
469
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000470uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
471 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000472 if (Data.isCommon() && Data.isExternal())
473 return Data.getCommonAlignment();
474
475 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000476
477 if (Symbol.isAbsolute() && Symbol.isVariable()) {
478 if (const MCExpr *Value = Symbol.getVariableValue()) {
479 int64_t IntValue;
480 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000481 return (uint64_t)IntValue;
Roman Divackyd1491862010-12-20 21:14:39 +0000482 }
483 }
484
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000485 if (!Symbol.isInSection())
486 return 0;
487
Rafael Espindola64695402011-05-16 16:17:21 +0000488
489 if (Data.getFragment()) {
490 if (Data.getFlags() & ELF_Other_ThumbFunc)
491 return Layout.getSymbolOffset(&Data)+1;
492 else
493 return Layout.getSymbolOffset(&Data);
494 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000495
496 return 0;
497}
498
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000499void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
500 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000501 // The presence of symbol versions causes undefined symbols and
502 // versions declared with @@@ to be renamed.
503
504 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
505 ie = Asm.symbol_end(); it != ie; ++it) {
506 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000507 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000508 MCSymbolData &SD = Asm.getSymbolData(Symbol);
509
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000510 // Not an alias.
511 if (&Symbol == &Alias)
512 continue;
513
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000514 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000515 size_t Pos = AliasName.find('@');
516 if (Pos == StringRef::npos)
517 continue;
518
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000519 // Aliases defined with .symvar copy the binding from the symbol they alias.
520 // This is the first place we are able to copy this information.
521 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000522 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000523
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000524 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000525 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
526 continue;
527
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000528 // FIXME: produce a better error message.
529 if (Symbol.isUndefined() && Rest.startswith("@@") &&
530 !Rest.startswith("@@@"))
531 report_fatal_error("A @@ version cannot be undefined");
532
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000533 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000534 }
535}
536
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000537void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
538 MCDataFragment *ShndxF,
539 ELFSymbolData &MSD,
540 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000541 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000542 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000543 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000544
Rafael Espindola7be2c332010-10-31 00:16:26 +0000545 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
546 Data.getSymbol().isVariable();
547
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000548 uint8_t Binding = MCELF::GetBinding(OrigData);
549 uint8_t Visibility = MCELF::GetVisibility(OrigData);
550 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000551
552 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
553 uint8_t Other = Visibility;
554
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000555 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000556 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000557
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000558 assert(!(Data.isCommon() && !Data.isExternal()));
559
Rafael Espindolaf0121242010-12-22 16:03:00 +0000560 const MCExpr *ESize = Data.getSize();
561 if (ESize) {
562 int64_t Res;
563 if (!ESize->EvaluateAsAbsolute(Res, Layout))
564 report_fatal_error("Size expression must be absolute.");
565 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000566 }
567
568 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000569 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
570 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000571}
572
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000573void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
574 MCDataFragment *ShndxF,
575 const MCAssembler &Asm,
576 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000577 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000578 // The string table must be emitted first because we need the index
579 // into the string table for all the symbol names.
580 assert(StringTable.size() && "Missing string table");
581
582 // FIXME: Make sure the start of the symbol table is aligned.
583
584 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000585 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000586
587 // Write the symbol table entries.
588 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
589 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
590 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000591 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000592 }
593
Rafael Espindola71859c62010-09-16 19:46:31 +0000594 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000595 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
596 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000597 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000598 static_cast<const MCSectionELF&>(i->getSection());
599 if (Section.getType() == ELF::SHT_RELA ||
600 Section.getType() == ELF::SHT_REL ||
601 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000602 Section.getType() == ELF::SHT_SYMTAB ||
603 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000604 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000605 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000606 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
607 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000608 LastLocalSymbolIndex++;
609 }
Matt Fleming3565a062010-08-16 18:57:57 +0000610
611 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
612 ELFSymbolData &MSD = ExternalSymbolData[i];
613 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000614 assert(((Data.getFlags() & ELF_STB_Global) ||
615 (Data.getFlags() & ELF_STB_Weak)) &&
616 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000617 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000618 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000619 LastLocalSymbolIndex++;
620 }
621
622 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
623 ELFSymbolData &MSD = UndefinedSymbolData[i];
624 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000625 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000626 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000627 LastLocalSymbolIndex++;
628 }
629}
630
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000631const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
632 const MCValue &Target,
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000633 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +0000634 const MCFixup &Fixup,
635 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000636 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000637 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
638 const MCSymbol *Renamed = Renames.lookup(&Symbol);
639 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000640
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000641 if (ASymbol.isUndefined()) {
642 if (Renamed)
643 return Renamed;
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000644 return undefinedExplicitRelSym(Target, Fixup, IsPCRel);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000645 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000646
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000647 if (SD.isExternal()) {
648 if (Renamed)
649 return Renamed;
650 return &Symbol;
651 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000652
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000653 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000654 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000655 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000656
Rafael Espindola25958732010-11-24 21:57:39 +0000657 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000658 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000659
Rafael Espindola25958732010-11-24 21:57:39 +0000660 if (secKind.isThreadLocal()) {
661 if (Renamed)
662 return Renamed;
663 return &Symbol;
664 }
665
Rafael Espindola8cecf252010-10-06 16:23:36 +0000666 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000667 const MCSectionELF &Sec2 =
668 static_cast<const MCSectionELF&>(F.getParent()->getSection());
669
Rafael Espindola8cecf252010-10-06 16:23:36 +0000670 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000671 (Kind == MCSymbolRefExpr::VK_PLT ||
672 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000673 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000674 if (Renamed)
675 return Renamed;
676 return &Symbol;
677 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000678
Rafael Espindola1c130262011-01-23 04:43:11 +0000679 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000680 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000681 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000682 if (Renamed)
683 return Renamed;
684 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000685 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000686
Jason W Kime964d112011-05-11 22:53:06 +0000687 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
688
Rafael Espindola73ffea42010-09-25 05:42:19 +0000689}
690
Matt Fleming3565a062010-08-16 18:57:57 +0000691
Jason W Kim56a39902010-12-06 21:57:34 +0000692void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
693 const MCAsmLayout &Layout,
694 const MCFragment *Fragment,
695 const MCFixup &Fixup,
696 MCValue Target,
697 uint64_t &FixedValue) {
698 int64_t Addend = 0;
699 int Index = 0;
700 int64_t Value = Target.getConstant();
701 const MCSymbol *RelocSymbol = NULL;
702
Rafael Espindola127a6a42010-12-17 07:28:17 +0000703 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000704 if (!Target.isAbsolute()) {
705 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
706 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000707 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000708
709 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
710 const MCSymbol &SymbolB = RefB->getSymbol();
711 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
712 IsPCRel = true;
713
714 // Offset of the symbol in the section
715 int64_t a = Layout.getSymbolOffset(&SDB);
716
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000717 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000718 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
719 Value += b - a;
720 }
721
722 if (!RelocSymbol) {
723 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
724 MCFragment *F = SD.getFragment();
725
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000726 if (F) {
727 Index = F->getParent()->getOrdinal() + 1;
728 // Offset of the symbol in the section
729 Value += Layout.getSymbolOffset(&SD);
730 } else {
731 Index = 0;
732 }
Jason W Kim56a39902010-12-06 21:57:34 +0000733 } else {
734 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
735 WeakrefUsedInReloc.insert(RelocSymbol);
736 else
737 UsedInReloc.insert(RelocSymbol);
738 Index = -1;
739 }
740 Addend = Value;
Michael Liaob21d9ae2012-10-16 19:49:51 +0000741 if (hasRelocationAddend())
Jason W Kim56a39902010-12-06 21:57:34 +0000742 Value = 0;
743 }
744
745 FixedValue = Value;
746 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
747 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000748 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
749 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
750 if (RelocNeedsGOT(Modifier))
751 NeedsGOT = true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000752
Jason W Kim56a39902010-12-06 21:57:34 +0000753 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
754 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000755
Rafael Espindolaf3a86fb2011-12-22 01:57:09 +0000756 // FIXME: no tests cover this. Is adjustFixupOffset dead code?
757 TargetObjectWriter->adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000758
Rafael Espindolabff66a82010-12-18 03:27:34 +0000759 if (!hasRelocationAddend())
760 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000761
762 if (is64Bit())
763 assert(isInt<64>(Addend));
764 else
765 assert(isInt<32>(Addend));
766
Akira Hatanaka00ca8882012-03-23 23:06:45 +0000767 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
Jason W Kim56a39902010-12-06 21:57:34 +0000768 Relocations[Fragment->getParent()].push_back(ERE);
769}
770
771
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000772uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000773ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
774 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000775 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000776 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000777}
778
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000779bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
780 const MCSymbolData &Data,
781 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000782 if (Data.getFlags() & ELF_Other_Weakref)
783 return false;
784
Rafael Espindolabd701182010-10-19 19:31:37 +0000785 if (Used)
786 return true;
787
Rafael Espindola88182132010-10-27 15:18:17 +0000788 if (Renamed)
789 return false;
790
Rafael Espindola737cd212010-10-05 18:01:23 +0000791 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000792
Rafael Espindolad1798862010-10-29 23:09:31 +0000793 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
794 return true;
795
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000796 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000797 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
798 return false;
799
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000800 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000801 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000802 return false;
803
Rafael Espindola737cd212010-10-05 18:01:23 +0000804 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
805 return false;
806
Rafael Espindolabd701182010-10-19 19:31:37 +0000807 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000808 return false;
809
810 return true;
811}
812
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000813bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
814 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000815 if (Data.isExternal())
816 return false;
817
818 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000819 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000820
821 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
822 if (isSignature && !isUsedInReloc)
823 return true;
824
Rafael Espindola737cd212010-10-05 18:01:23 +0000825 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000826 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000827
828 return true;
829}
830
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000831void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000832 SectionIndexMapTy &SectionIndexMap,
833 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000834 unsigned Index = 1;
835 for (MCAssembler::iterator it = Asm.begin(),
836 ie = Asm.end(); it != ie; ++it) {
837 const MCSectionELF &Section =
838 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000839 if (Section.getType() != ELF::SHT_GROUP)
840 continue;
841 SectionIndexMap[&Section] = Index++;
842 }
843
844 for (MCAssembler::iterator it = Asm.begin(),
845 ie = Asm.end(); it != ie; ++it) {
846 const MCSectionELF &Section =
847 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000848 if (Section.getType() == ELF::SHT_GROUP ||
849 Section.getType() == ELF::SHT_REL ||
850 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000851 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000852 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000853 const MCSectionELF *RelSection = RelMap.lookup(&Section);
854 if (RelSection)
855 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000856 }
857}
858
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000859void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000860 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000861 RevGroupMapTy RevGroupMap,
862 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000863 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000864 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000865 if (NeedsGOT) {
Dmitri Gribenko96f498b2013-01-13 16:01:15 +0000866 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
Rafael Espindola5c77c162010-10-05 15:48:37 +0000867 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
868 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
869 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000870 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000871 }
872
Matt Fleming3565a062010-08-16 18:57:57 +0000873 // Index 0 is always the empty string.
874 StringMap<uint64_t> StringIndexMap;
875 StringTable += '\x00';
876
Rafael Espindolad5321da2011-04-07 23:21:52 +0000877 // FIXME: We could optimize suffixes in strtab in the same way we
878 // optimize them in shstrtab.
879
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000880 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000881 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
882 ie = Asm.symbol_end(); it != ie; ++it) {
883 const MCSymbol &Symbol = it->getSymbol();
884
Rafael Espindola484291c2010-11-01 14:28:48 +0000885 bool Used = UsedInReloc.count(&Symbol);
886 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000887 bool isSignature = RevGroupMap.count(&Symbol);
888
889 if (!isInSymtab(Asm, *it,
890 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000891 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000892 continue;
893
Matt Fleming3565a062010-08-16 18:57:57 +0000894 ELFSymbolData MSD;
895 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000896 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000897
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000898 // Undefined symbols are global, but this is the first place we
899 // are able to set it.
900 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000901 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000902 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000903 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
904 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000905 }
906
Rafael Espindola484291c2010-11-01 14:28:48 +0000907 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000908 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000909
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000910 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000911 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000912 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000913 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000914 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000915 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000916 if (isSignature && !Used)
917 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
918 else
919 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000920 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000921 const MCSectionELF &Section =
922 static_cast<const MCSectionELF&>(RefSymbol.getSection());
923 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000924 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
925 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000926 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000927 }
928
Rafael Espindola88182132010-10-27 15:18:17 +0000929 // The @@@ in symbol version is replaced with @ in undefined symbols and
930 // @@ in defined ones.
931 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000932 SmallString<32> Buf;
933
Rafael Espindola88182132010-10-27 15:18:17 +0000934 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000935 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000936 Buf += Name.substr(0, Pos);
937 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
938 Buf += Name.substr(Pos + Skip);
939 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000940 }
941
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000942 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000943 if (!Entry) {
944 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000945 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000946 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000947 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000948 MSD.StringIndex = Entry;
949 if (MSD.SectionIndex == ELF::SHN_UNDEF)
950 UndefinedSymbolData.push_back(MSD);
951 else if (Local)
952 LocalSymbolData.push_back(MSD);
953 else
954 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000955 }
956
957 // Symbols are required to be in lexicographic order.
958 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
959 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
960 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
961
962 // Set the symbol indices. Local symbols must come before all other
963 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000964 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000965 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
966 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000967
968 Index += NumRegularSections;
969
Matt Fleming3565a062010-08-16 18:57:57 +0000970 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
971 ExternalSymbolData[i].SymbolData->setIndex(Index++);
972 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
973 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000974
975 if (NumRegularSections > ELF::SHN_LORESERVE)
976 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000977}
978
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000979void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
980 MCAsmLayout &Layout,
981 RelMapTy &RelMap) {
982 for (MCAssembler::const_iterator it = Asm.begin(),
983 ie = Asm.end(); it != ie; ++it) {
984 const MCSectionData &SD = *it;
985 if (Relocations[&SD].empty())
986 continue;
987
Matt Fleming3565a062010-08-16 18:57:57 +0000988 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000989 const MCSectionELF &Section =
990 static_cast<const MCSectionELF&>(SD.getSection());
991
992 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000993 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000994 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000995
996 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000997 if (hasRelocationAddend())
998 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000999 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001000 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001001
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001002 const MCSectionELF *RelaSection =
1003 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
1004 ELF::SHT_RELA : ELF::SHT_REL, 0,
1005 SectionKind::getReadOnly(),
1006 EntrySize, "");
1007 RelMap[&Section] = RelaSection;
1008 Asm.getOrCreateSectionData(*RelaSection);
1009 }
1010}
Matt Fleming3565a062010-08-16 18:57:57 +00001011
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001012void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1013 const RelMapTy &RelMap) {
1014 for (MCAssembler::const_iterator it = Asm.begin(),
1015 ie = Asm.end(); it != ie; ++it) {
1016 const MCSectionData &SD = *it;
1017 const MCSectionELF &Section =
1018 static_cast<const MCSectionELF&>(SD.getSection());
1019
1020 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1021 if (!RelaSection)
1022 continue;
Matt Fleming3565a062010-08-16 18:57:57 +00001023 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001024 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001025
1026 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001027 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +00001028 }
1029}
1030
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001031void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1032 uint64_t Flags, uint64_t Address,
1033 uint64_t Offset, uint64_t Size,
1034 uint32_t Link, uint32_t Info,
1035 uint64_t Alignment,
1036 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001037 Write32(Name); // sh_name: index into string table
1038 Write32(Type); // sh_type
1039 WriteWord(Flags); // sh_flags
1040 WriteWord(Address); // sh_addr
1041 WriteWord(Offset); // sh_offset
1042 WriteWord(Size); // sh_size
1043 Write32(Link); // sh_link
1044 Write32(Info); // sh_info
1045 WriteWord(Alignment); // sh_addralign
1046 WriteWord(EntrySize); // sh_entsize
1047}
1048
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001049void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1050 MCDataFragment *F,
1051 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001052 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka00ca8882012-03-23 23:06:45 +00001053
1054 // Sort the relocation entries. Most targets just sort by r_offset, but some
1055 // (e.g., MIPS) have additional constraints.
1056 TargetObjectWriter->sortRelocs(Asm, Relocs);
Matt Fleming3565a062010-08-16 18:57:57 +00001057
1058 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1059 ELFRelocationEntry entry = Relocs[e - i - 1];
1060
Rafael Espindola12203cc2010-11-21 00:48:25 +00001061 if (!entry.Index)
1062 ;
1063 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001064 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1065 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001066 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001067 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001068 String64(*F, entry.r_offset);
Jack Carter93ee2862012-06-27 22:28:30 +00001069 if (TargetObjectWriter->isN64()) {
1070 String32(*F, entry.Index);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001071
Jack Carter93ee2862012-06-27 22:28:30 +00001072 String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1073 String8(*F, TargetObjectWriter->getRType3(entry.Type));
1074 String8(*F, TargetObjectWriter->getRType2(entry.Type));
1075 String8(*F, TargetObjectWriter->getRType(entry.Type));
1076 }
1077 else {
1078 struct ELF::Elf64_Rela ERE64;
1079 ERE64.setSymbolAndType(entry.Index, entry.Type);
1080 String64(*F, ERE64.r_info);
1081 }
Rafael Espindolabff66a82010-12-18 03:27:34 +00001082 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001083 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001084 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001085 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001086
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001087 struct ELF::Elf32_Rela ERE32;
1088 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001089 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001090
Rafael Espindolabff66a82010-12-18 03:27:34 +00001091 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001092 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001093 }
Matt Fleming3565a062010-08-16 18:57:57 +00001094 }
1095}
1096
Rafael Espindolad5321da2011-04-07 23:21:52 +00001097static int compareBySuffix(const void *a, const void *b) {
1098 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
1099 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
1100 const StringRef &NameA = secA->getSectionName();
1101 const StringRef &NameB = secB->getSectionName();
1102 const unsigned sizeA = NameA.size();
1103 const unsigned sizeB = NameB.size();
1104 const unsigned len = std::min(sizeA, sizeB);
1105 for (unsigned int i = 0; i < len; ++i) {
1106 char ca = NameA[sizeA - i - 1];
1107 char cb = NameB[sizeB - i - 1];
1108 if (ca != cb)
1109 return cb - ca;
1110 }
1111
1112 return sizeB - sizeA;
1113}
1114
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001115void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1116 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001117 SectionIndexMapTy &SectionIndexMap,
1118 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001119 MCContext &Ctx = Asm.getContext();
1120 MCDataFragment *F;
1121
Rafael Espindolabff66a82010-12-18 03:27:34 +00001122 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001123
Rafael Espindola38738bf2010-09-22 19:04:41 +00001124 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001125 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001126 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001127 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001128 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1129 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +00001130
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001131 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001132 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1133 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001134 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001135 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001136 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001137
1138 MCSectionData *SymtabShndxSD = NULL;
1139
1140 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001141 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001142 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001143 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001144 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1145 SymtabShndxSD->setAlignment(4);
1146 }
Matt Fleming3565a062010-08-16 18:57:57 +00001147
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001148 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001149 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001150 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001151 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1152 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001153
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001154 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1155
1156 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1157 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1158 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +00001159
1160 // Symbol table
1161 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001162 MCDataFragment *ShndxF = NULL;
1163 if (NeedsSymtabShndx) {
1164 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001165 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001166 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001167
Matt Fleming3565a062010-08-16 18:57:57 +00001168 F = new MCDataFragment(&StrtabSD);
1169 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001170
Matt Fleming3565a062010-08-16 18:57:57 +00001171 F = new MCDataFragment(&ShstrtabSD);
1172
Rafael Espindolad5321da2011-04-07 23:21:52 +00001173 std::vector<const MCSectionELF*> Sections;
1174 for (MCAssembler::const_iterator it = Asm.begin(),
1175 ie = Asm.end(); it != ie; ++it) {
1176 const MCSectionELF &Section =
1177 static_cast<const MCSectionELF&>(it->getSection());
1178 Sections.push_back(&Section);
1179 }
1180 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1181
Matt Fleming3565a062010-08-16 18:57:57 +00001182 // Section header string table.
1183 //
1184 // The first entry of a string table holds a null character so skip
1185 // section 0.
1186 uint64_t Index = 1;
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001187 F->getContents().push_back('\x00');
Matt Fleming3565a062010-08-16 18:57:57 +00001188
Rafael Espindolad5321da2011-04-07 23:21:52 +00001189 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1190 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +00001191
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001192 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +00001193 if (I != 0) {
1194 StringRef PreviousName = Sections[I - 1]->getSectionName();
1195 if (PreviousName.endswith(Name)) {
1196 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1197 continue;
1198 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001199 }
Matt Fleming3565a062010-08-16 18:57:57 +00001200 // Remember the index into the string table so we can write it
1201 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001202 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001203
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001204 Index += Name.size() + 1;
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001205 F->getContents().append(Name.begin(), Name.end());
1206 F->getContents().push_back('\x00');
Matt Fleming3565a062010-08-16 18:57:57 +00001207 }
Rafael Espindola70703872010-09-30 02:22:20 +00001208}
1209
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001210void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1211 MCAsmLayout &Layout,
1212 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001213 RevGroupMapTy &RevGroupMap,
1214 SectionIndexMapTy &SectionIndexMap,
1215 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001216 // Create the .note.GNU-stack section if needed.
1217 MCContext &Ctx = Asm.getContext();
1218 if (Asm.getNoExecStack()) {
1219 const MCSectionELF *GnuStackSection =
1220 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1221 SectionKind::getReadOnly());
1222 Asm.getOrCreateSectionData(*GnuStackSection);
1223 }
1224
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001225 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001226 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1227 it != ie; ++it) {
1228 const MCSectionELF &Section =
1229 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001230 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001231 continue;
1232
1233 const MCSymbol *SignatureSymbol = Section.getGroup();
1234 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001235 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001236 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001237 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001238 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1239 Data.setAlignment(4);
1240 MCDataFragment *F = new MCDataFragment(&Data);
1241 String32(*F, ELF::GRP_COMDAT);
1242 }
1243 GroupMap[Group] = SignatureSymbol;
1244 }
1245
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001246 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1247
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001248 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001249 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001250 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001251 const MCSectionELF &Section =
1252 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001253 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001254 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001255 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001256 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1257 // FIXME: we could use the previous fragment
1258 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001259 unsigned Index = SectionIndexMap.lookup(&Section);
1260 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001261 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001262}
1263
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001264void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1265 const SectionIndexMapTy &SectionIndexMap,
1266 uint32_t GroupSymbolIndex,
1267 uint64_t Offset, uint64_t Size,
1268 uint64_t Alignment,
1269 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001270 uint64_t sh_link = 0;
1271 uint64_t sh_info = 0;
1272
1273 switch(Section.getType()) {
1274 case ELF::SHT_DYNAMIC:
1275 sh_link = SectionStringTableIndex[&Section];
1276 sh_info = 0;
1277 break;
1278
1279 case ELF::SHT_REL:
1280 case ELF::SHT_RELA: {
1281 const MCSectionELF *SymtabSection;
1282 const MCSectionELF *InfoSection;
1283 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1284 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001285 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001286 sh_link = SectionIndexMap.lookup(SymtabSection);
1287 assert(sh_link && ".symtab not found");
1288
1289 // Remove ".rel" and ".rela" prefixes.
1290 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1291 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1292
1293 InfoSection = Asm.getContext().getELFSection(SectionName,
1294 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001295 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001296 sh_info = SectionIndexMap.lookup(InfoSection);
1297 break;
1298 }
1299
1300 case ELF::SHT_SYMTAB:
1301 case ELF::SHT_DYNSYM:
1302 sh_link = StringTableIndex;
1303 sh_info = LastLocalSymbolIndex;
1304 break;
1305
1306 case ELF::SHT_SYMTAB_SHNDX:
1307 sh_link = SymbolTableIndex;
1308 break;
1309
1310 case ELF::SHT_PROGBITS:
1311 case ELF::SHT_STRTAB:
1312 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001313 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001314 case ELF::SHT_NULL:
1315 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001316 case ELF::SHT_INIT_ARRAY:
1317 case ELF::SHT_FINI_ARRAY:
1318 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001319 case ELF::SHT_X86_64_UNWIND:
Jack Carterc91cbb92013-01-18 21:20:38 +00001320 case ELF::SHT_MIPS_REGINFO:
1321 case ELF::SHT_MIPS_OPTIONS:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001322 // Nothing to do.
1323 break;
1324
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001325 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001326 sh_link = SymbolTableIndex;
1327 sh_info = GroupSymbolIndex;
1328 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001329
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001330 default:
1331 assert(0 && "FIXME: sh_type value not supported!");
1332 break;
1333 }
1334
1335 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1336 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1337 Alignment, Section.getEntrySize());
1338}
1339
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001340bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001341 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001342 !SD.getSection().isVirtualSection();
1343}
1344
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001345uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001346 uint64_t Ret = 0;
1347 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1348 ++i) {
1349 const MCFragment &F = *i;
1350 assert(F.getKind() == MCFragment::FT_Data);
1351 Ret += cast<MCDataFragment>(F).getContents().size();
1352 }
1353 return Ret;
1354}
1355
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001356uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1357 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001358 if (IsELFMetaDataSection(SD))
1359 return DataSectionSize(SD);
1360 return Layout.getSectionFileSize(&SD);
1361}
1362
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001363uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1364 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001365 if (IsELFMetaDataSection(SD))
1366 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001367 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001368}
1369
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001370void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1371 const MCAsmLayout &Layout,
1372 const MCSectionELF &Section) {
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001373 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1374
Benjamin Kramer263109d2012-01-20 14:42:32 +00001375 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001376 WriteZeros(Padding);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001377
1378 if (IsELFMetaDataSection(SD)) {
1379 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1380 ++i) {
1381 const MCFragment &F = *i;
1382 assert(F.getKind() == MCFragment::FT_Data);
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001383 WriteBytes(cast<MCDataFragment>(F).getContents());
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001384 }
1385 } else {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001386 Asm.writeSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001387 }
1388}
1389
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001390void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1391 const GroupMapTy &GroupMap,
1392 const MCAsmLayout &Layout,
1393 const SectionIndexMapTy &SectionIndexMap,
1394 const SectionOffsetMapTy &SectionOffsetMap) {
1395 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001396
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001397 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001398 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001399
1400 for (SectionIndexMapTy::const_iterator i=
1401 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1402 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001403 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001404 }
1405
Matt Fleming3565a062010-08-16 18:57:57 +00001406 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001407 uint64_t FirstSectionSize =
1408 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1409 uint32_t FirstSectionLink =
1410 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1411 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001412
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001413 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001414 const MCSectionELF &Section = *Sections[i];
1415 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1416 uint32_t GroupSymbolIndex;
1417 if (Section.getType() != ELF::SHT_GROUP)
1418 GroupSymbolIndex = 0;
1419 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001420 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1421 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001422
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001423 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001424
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001425 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001426 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001427 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001428 }
1429}
1430
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001431void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1432 std::vector<const MCSectionELF*> &Sections) {
1433 for (MCAssembler::iterator it = Asm.begin(),
1434 ie = Asm.end(); it != ie; ++it) {
1435 const MCSectionELF &Section =
1436 static_cast<const MCSectionELF &>(it->getSection());
1437 if (Section.getType() == ELF::SHT_GROUP)
1438 Sections.push_back(&Section);
1439 }
1440
1441 for (MCAssembler::iterator it = Asm.begin(),
1442 ie = Asm.end(); it != ie; ++it) {
1443 const MCSectionELF &Section =
1444 static_cast<const MCSectionELF &>(it->getSection());
1445 if (Section.getType() != ELF::SHT_GROUP &&
1446 Section.getType() != ELF::SHT_REL &&
1447 Section.getType() != ELF::SHT_RELA)
1448 Sections.push_back(&Section);
1449 }
1450
1451 for (MCAssembler::iterator it = Asm.begin(),
1452 ie = Asm.end(); it != ie; ++it) {
1453 const MCSectionELF &Section =
1454 static_cast<const MCSectionELF &>(it->getSection());
1455 if (Section.getType() == ELF::SHT_REL ||
1456 Section.getType() == ELF::SHT_RELA)
1457 Sections.push_back(&Section);
1458 }
1459}
1460
1461void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1462 const MCAsmLayout &Layout) {
1463 GroupMapTy GroupMap;
1464 RevGroupMapTy RevGroupMap;
1465 SectionIndexMapTy SectionIndexMap;
1466
1467 unsigned NumUserSections = Asm.size();
1468
1469 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1470 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1471
1472 const unsigned NumUserAndRelocSections = Asm.size();
1473 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1474 RevGroupMap, SectionIndexMap, RelMap);
1475 const unsigned AllSections = Asm.size();
1476 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1477
1478 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1479
1480 // Compute symbol table information.
1481 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1482
1483
1484 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1485
1486 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1487 const_cast<MCAsmLayout&>(Layout),
1488 SectionIndexMap,
1489 RelMap);
1490
1491 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1492 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1493 sizeof(ELF::Elf32_Ehdr);
1494 uint64_t FileOff = HeaderSize;
1495
1496 std::vector<const MCSectionELF*> Sections;
1497 ComputeSectionOrder(Asm, Sections);
1498 unsigned NumSections = Sections.size();
1499 SectionOffsetMapTy SectionOffsetMap;
1500 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1501 const MCSectionELF &Section = *Sections[i];
1502 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1503
1504 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1505
1506 // Remember the offset into the file for this section.
1507 SectionOffsetMap[&Section] = FileOff;
1508
1509 // Get the size of the section in the output file (including padding).
1510 FileOff += GetSectionFileSize(Layout, SD);
1511 }
1512
1513 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1514
1515 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1516
1517 uint64_t SectionHeaderEntrySize = is64Bit() ?
1518 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1519 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1520
1521 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1522 const MCSectionELF &Section = *Sections[i];
1523 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1524
1525 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1526
1527 // Remember the offset into the file for this section.
1528 SectionOffsetMap[&Section] = FileOff;
1529
1530 // Get the size of the section in the output file (including padding).
1531 FileOff += GetSectionFileSize(Layout, SD);
1532 }
1533
1534 // Write out the ELF header ...
Jack Carter9a7bf432013-01-30 02:09:52 +00001535 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001536
1537 // ... then the regular sections ...
1538 // + because of .shstrtab
1539 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1540 WriteDataSectionData(Asm, Layout, *Sections[i]);
1541
Benjamin Kramer263109d2012-01-20 14:42:32 +00001542 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001543 WriteZeros(Padding);
1544
1545 // ... then the section header table ...
1546 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1547 SectionOffsetMap);
1548
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001549 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001550 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1551 WriteDataSectionData(Asm, Layout, *Sections[i]);
1552}
1553
Eli Friedman78c1e172011-03-03 07:24:36 +00001554bool
1555ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1556 const MCSymbolData &DataA,
1557 const MCFragment &FB,
1558 bool InSet,
1559 bool IsPCRel) const {
1560 if (DataA.getFlags() & ELF_STB_Weak)
1561 return false;
1562 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1563 Asm, DataA, FB,InSet, IsPCRel);
1564}
1565
Rafael Espindola6024c972010-12-17 17:45:22 +00001566MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1567 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001568 bool IsLittleEndian) {
Rafael Espindola7bd27802011-12-22 03:24:43 +00001569 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolaedae8e12011-12-21 17:30:17 +00001570}