blob: fb14dc96b390ee7fc71b92cd71c9822e681875e2 [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 {
Jack Carterdc08bfb2013-02-12 21:29:39 +0000138 return TargetObjectWriter->undefinedExplicitRelSym(Target, Fixup,
139 IsPCRel);
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000140 }
Rafael Espindola3963d612011-12-22 03:38:00 +0000141
142 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
143 bool hasRelocationAddend() const {
144 return TargetObjectWriter->hasRelocationAddend();
145 }
Rafael Espindola3963d612011-12-22 03:38:00 +0000146 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
147 bool IsPCRel, bool IsRelocWithSymbol,
148 int64_t Addend) const {
149 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
150 IsRelocWithSymbol, Addend);
151 }
152
Rafael Espindola3963d612011-12-22 03:38:00 +0000153 public:
154 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
155 raw_ostream &_OS, bool IsLittleEndian)
156 : MCObjectWriter(_OS, IsLittleEndian),
157 TargetObjectWriter(MOTW),
158 NeedsGOT(false), NeedsSymtabShndx(false){
159 }
160
161 virtual ~ELFObjectWriter();
162
163 void WriteWord(uint64_t W) {
164 if (is64Bit())
165 Write64(W);
166 else
167 Write32(W);
168 }
169
170 void StringLE16(char *buf, uint16_t Value) {
171 buf[0] = char(Value >> 0);
172 buf[1] = char(Value >> 8);
173 }
174
175 void StringLE32(char *buf, uint32_t Value) {
176 StringLE16(buf, uint16_t(Value >> 0));
177 StringLE16(buf + 2, uint16_t(Value >> 16));
178 }
179
180 void StringLE64(char *buf, uint64_t Value) {
181 StringLE32(buf, uint32_t(Value >> 0));
182 StringLE32(buf + 4, uint32_t(Value >> 32));
183 }
184
185 void StringBE16(char *buf ,uint16_t Value) {
186 buf[0] = char(Value >> 8);
187 buf[1] = char(Value >> 0);
188 }
189
190 void StringBE32(char *buf, uint32_t Value) {
191 StringBE16(buf, uint16_t(Value >> 16));
192 StringBE16(buf + 2, uint16_t(Value >> 0));
193 }
194
195 void StringBE64(char *buf, uint64_t Value) {
196 StringBE32(buf, uint32_t(Value >> 32));
197 StringBE32(buf + 4, uint32_t(Value >> 0));
198 }
199
200 void String8(MCDataFragment &F, uint8_t Value) {
201 char buf[1];
202 buf[0] = Value;
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000203 F.getContents().append(&buf[0], &buf[1]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000204 }
205
206 void String16(MCDataFragment &F, uint16_t Value) {
207 char buf[2];
208 if (isLittleEndian())
209 StringLE16(buf, Value);
210 else
211 StringBE16(buf, Value);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000212 F.getContents().append(&buf[0], &buf[2]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000213 }
214
215 void String32(MCDataFragment &F, uint32_t Value) {
216 char buf[4];
217 if (isLittleEndian())
218 StringLE32(buf, Value);
219 else
220 StringBE32(buf, Value);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000221 F.getContents().append(&buf[0], &buf[4]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000222 }
223
224 void String64(MCDataFragment &F, uint64_t Value) {
225 char buf[8];
226 if (isLittleEndian())
227 StringLE64(buf, Value);
228 else
229 StringBE64(buf, Value);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000230 F.getContents().append(&buf[0], &buf[8]);
Rafael Espindola3963d612011-12-22 03:38:00 +0000231 }
232
Jack Carter9a7bf432013-01-30 02:09:52 +0000233 void WriteHeader(const MCAssembler &Asm,
234 uint64_t SectionDataSize,
Rafael Espindola3963d612011-12-22 03:38:00 +0000235 unsigned NumberOfSections);
236
237 void WriteSymbolEntry(MCDataFragment *SymtabF,
238 MCDataFragment *ShndxF,
239 uint64_t name, uint8_t info,
240 uint64_t value, uint64_t size,
241 uint8_t other, uint32_t shndx,
242 bool Reserved);
243
244 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
245 ELFSymbolData &MSD,
246 const MCAsmLayout &Layout);
247
248 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
249 void WriteSymbolTable(MCDataFragment *SymtabF,
250 MCDataFragment *ShndxF,
251 const MCAssembler &Asm,
252 const MCAsmLayout &Layout,
253 const SectionIndexMapTy &SectionIndexMap);
254
255 virtual void RecordRelocation(const MCAssembler &Asm,
256 const MCAsmLayout &Layout,
257 const MCFragment *Fragment,
258 const MCFixup &Fixup,
259 MCValue Target, uint64_t &FixedValue);
260
261 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
262 const MCSymbol *S);
263
264 // Map from a group section to the signature symbol
265 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
266 // Map from a signature symbol to the group section
267 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
268 // Map from a section to the section with the relocations
269 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
270 // Map from a section to its offset
271 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
272
273 /// ComputeSymbolTable - Compute the symbol table data
274 ///
Rafael Espindola828ea382012-08-27 16:04:24 +0000275 /// \param Asm - The assembler.
276 /// \param SectionIndexMap - Maps a section to its index.
277 /// \param RevGroupMap - Maps a signature symbol to the group section.
278 /// \param NumRegularSections - Number of non-relocation sections.
Rafael Espindola3963d612011-12-22 03:38:00 +0000279 void ComputeSymbolTable(MCAssembler &Asm,
280 const SectionIndexMapTy &SectionIndexMap,
281 RevGroupMapTy RevGroupMap,
282 unsigned NumRegularSections);
283
284 void ComputeIndexMap(MCAssembler &Asm,
285 SectionIndexMapTy &SectionIndexMap,
286 const RelMapTy &RelMap);
287
288 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
289 RelMapTy &RelMap);
290
291 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
292 const RelMapTy &RelMap);
293
294 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
295 SectionIndexMapTy &SectionIndexMap,
296 const RelMapTy &RelMap);
297
298 // Create the sections that show up in the symbol table. Currently
299 // those are the .note.GNU-stack section and the group sections.
300 void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
301 GroupMapTy &GroupMap,
302 RevGroupMapTy &RevGroupMap,
303 SectionIndexMapTy &SectionIndexMap,
304 const RelMapTy &RelMap);
305
306 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
307 const MCAsmLayout &Layout);
308
309 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
310 const MCAsmLayout &Layout,
311 const SectionIndexMapTy &SectionIndexMap,
312 const SectionOffsetMapTy &SectionOffsetMap);
313
314 void ComputeSectionOrder(MCAssembler &Asm,
315 std::vector<const MCSectionELF*> &Sections);
316
317 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
318 uint64_t Address, uint64_t Offset,
319 uint64_t Size, uint32_t Link, uint32_t Info,
320 uint64_t Alignment, uint64_t EntrySize);
321
322 void WriteRelocationsFragment(const MCAssembler &Asm,
323 MCDataFragment *F,
324 const MCSectionData *SD);
325
326 virtual bool
327 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
328 const MCSymbolData &DataA,
329 const MCFragment &FB,
330 bool InSet,
331 bool IsPCRel) const;
332
333 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
334 void WriteSection(MCAssembler &Asm,
335 const SectionIndexMapTy &SectionIndexMap,
336 uint32_t GroupSymbolIndex,
337 uint64_t Offset, uint64_t Size, uint64_t Alignment,
338 const MCSectionELF &Section);
339 };
340}
341
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000342bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
343 const MCFixupKindInfo &FKI =
344 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
345
346 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
347}
348
349bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
350 switch (Variant) {
351 default:
352 return false;
353 case MCSymbolRefExpr::VK_GOT:
354 case MCSymbolRefExpr::VK_PLT:
355 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000356 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000357 case MCSymbolRefExpr::VK_TPOFF:
358 case MCSymbolRefExpr::VK_TLSGD:
359 case MCSymbolRefExpr::VK_GOTTPOFF:
360 case MCSymbolRefExpr::VK_INDNTPOFF:
361 case MCSymbolRefExpr::VK_NTPOFF:
362 case MCSymbolRefExpr::VK_GOTNTPOFF:
363 case MCSymbolRefExpr::VK_TLSLDM:
364 case MCSymbolRefExpr::VK_DTPOFF:
365 case MCSymbolRefExpr::VK_TLSLD:
366 return true;
367 }
368}
369
Jason W Kimd3443e92010-11-15 16:18:39 +0000370ELFObjectWriter::~ELFObjectWriter()
371{}
372
Matt Fleming3565a062010-08-16 18:57:57 +0000373// Emit the ELF header.
Jack Carter9a7bf432013-01-30 02:09:52 +0000374void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
375 uint64_t SectionDataSize,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000376 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000377 // ELF Header
378 // ----------
379 //
380 // Note
381 // ----
382 // emitWord method behaves differently for ELF32 and ELF64, writing
383 // 4 bytes in the former and 8 in the latter.
384
385 Write8(0x7f); // e_ident[EI_MAG0]
386 Write8('E'); // e_ident[EI_MAG1]
387 Write8('L'); // e_ident[EI_MAG2]
388 Write8('F'); // e_ident[EI_MAG3]
389
Rafael Espindolabff66a82010-12-18 03:27:34 +0000390 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000391
392 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000393 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000394
395 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000396 // e_ident[EI_OSABI]
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000397 Write8(TargetObjectWriter->getOSABI());
Matt Fleming3565a062010-08-16 18:57:57 +0000398 Write8(0); // e_ident[EI_ABIVERSION]
399
400 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
401
402 Write16(ELF::ET_REL); // e_type
403
Rafael Espindolabff66a82010-12-18 03:27:34 +0000404 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000405
406 Write32(ELF::EV_CURRENT); // e_version
407 WriteWord(0); // e_entry, no entry point in .o file
408 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000409 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000410 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000411
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000412 // e_flags = whatever the target wants
Jack Carter9a7bf432013-01-30 02:09:52 +0000413 Write32(Asm.getELFHeaderEFlags());
Matt Fleming3565a062010-08-16 18:57:57 +0000414
415 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000416 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000417
418 Write16(0); // e_phentsize = prog header entry size
419 Write16(0); // e_phnum = # prog header entries = 0
420
421 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000422 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000423
424 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000425 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000426 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000427 else
428 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000429
430 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000431 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000432 Write16(ELF::SHN_XINDEX);
433 else
434 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000435}
436
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000437void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
438 MCDataFragment *ShndxF,
439 uint64_t name,
440 uint8_t info, uint64_t value,
441 uint64_t size, uint8_t other,
442 uint32_t shndx,
443 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000444 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000445 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000446 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000447 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000448 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000449 }
450
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000451 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
452 uint16_t(ELF::SHN_XINDEX) : shndx;
453
Rafael Espindolabff66a82010-12-18 03:27:34 +0000454 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000455 String32(*SymtabF, name); // st_name
456 String8(*SymtabF, info); // st_info
457 String8(*SymtabF, other); // st_other
458 String16(*SymtabF, Index); // st_shndx
459 String64(*SymtabF, value); // st_value
460 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000461 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000462 String32(*SymtabF, name); // st_name
463 String32(*SymtabF, value); // st_value
464 String32(*SymtabF, size); // st_size
465 String8(*SymtabF, info); // st_info
466 String8(*SymtabF, other); // st_other
467 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000468 }
469}
470
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000471uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
472 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000473 if (Data.isCommon() && Data.isExternal())
474 return Data.getCommonAlignment();
475
476 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000477
478 if (Symbol.isAbsolute() && Symbol.isVariable()) {
479 if (const MCExpr *Value = Symbol.getVariableValue()) {
480 int64_t IntValue;
481 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000482 return (uint64_t)IntValue;
Roman Divackyd1491862010-12-20 21:14:39 +0000483 }
484 }
485
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000486 if (!Symbol.isInSection())
487 return 0;
488
Rafael Espindola64695402011-05-16 16:17:21 +0000489
490 if (Data.getFragment()) {
491 if (Data.getFlags() & ELF_Other_ThumbFunc)
492 return Layout.getSymbolOffset(&Data)+1;
493 else
494 return Layout.getSymbolOffset(&Data);
495 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000496
497 return 0;
498}
499
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000500void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
501 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000502 // The presence of symbol versions causes undefined symbols and
503 // versions declared with @@@ to be renamed.
504
505 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
506 ie = Asm.symbol_end(); it != ie; ++it) {
507 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000508 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000509 MCSymbolData &SD = Asm.getSymbolData(Symbol);
510
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000511 // Not an alias.
512 if (&Symbol == &Alias)
513 continue;
514
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000515 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000516 size_t Pos = AliasName.find('@');
517 if (Pos == StringRef::npos)
518 continue;
519
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000520 // Aliases defined with .symvar copy the binding from the symbol they alias.
521 // This is the first place we are able to copy this information.
522 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000523 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000524
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000525 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000526 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
527 continue;
528
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000529 // FIXME: produce a better error message.
530 if (Symbol.isUndefined() && Rest.startswith("@@") &&
531 !Rest.startswith("@@@"))
532 report_fatal_error("A @@ version cannot be undefined");
533
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000534 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000535 }
536}
537
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000538void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
539 MCDataFragment *ShndxF,
540 ELFSymbolData &MSD,
541 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000542 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000543 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000544 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000545
Rafael Espindola7be2c332010-10-31 00:16:26 +0000546 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
547 Data.getSymbol().isVariable();
548
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000549 uint8_t Binding = MCELF::GetBinding(OrigData);
550 uint8_t Visibility = MCELF::GetVisibility(OrigData);
551 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000552
553 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
554 uint8_t Other = Visibility;
555
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000556 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000557 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000558
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000559 assert(!(Data.isCommon() && !Data.isExternal()));
560
Rafael Espindolaf0121242010-12-22 16:03:00 +0000561 const MCExpr *ESize = Data.getSize();
562 if (ESize) {
563 int64_t Res;
564 if (!ESize->EvaluateAsAbsolute(Res, Layout))
565 report_fatal_error("Size expression must be absolute.");
566 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000567 }
568
569 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000570 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
571 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000572}
573
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000574void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
575 MCDataFragment *ShndxF,
576 const MCAssembler &Asm,
577 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000578 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000579 // The string table must be emitted first because we need the index
580 // into the string table for all the symbol names.
581 assert(StringTable.size() && "Missing string table");
582
583 // FIXME: Make sure the start of the symbol table is aligned.
584
585 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000586 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000587
588 // Write the symbol table entries.
589 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
590 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
591 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000592 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000593 }
594
Rafael Espindola71859c62010-09-16 19:46:31 +0000595 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000596 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
597 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000598 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000599 static_cast<const MCSectionELF&>(i->getSection());
600 if (Section.getType() == ELF::SHT_RELA ||
601 Section.getType() == ELF::SHT_REL ||
602 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000603 Section.getType() == ELF::SHT_SYMTAB ||
604 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000605 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000606 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000607 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
608 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000609 LastLocalSymbolIndex++;
610 }
Matt Fleming3565a062010-08-16 18:57:57 +0000611
612 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
613 ELFSymbolData &MSD = ExternalSymbolData[i];
614 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000615 assert(((Data.getFlags() & ELF_STB_Global) ||
616 (Data.getFlags() & ELF_STB_Weak)) &&
617 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000618 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000619 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000620 LastLocalSymbolIndex++;
621 }
622
623 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
624 ELFSymbolData &MSD = UndefinedSymbolData[i];
625 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000626 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000627 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000628 LastLocalSymbolIndex++;
629 }
630}
631
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000632const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
633 const MCValue &Target,
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000634 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +0000635 const MCFixup &Fixup,
636 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000637 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000638 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
639 const MCSymbol *Renamed = Renames.lookup(&Symbol);
640 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000641
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000642 if (ASymbol.isUndefined()) {
643 if (Renamed)
644 return Renamed;
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000645 return undefinedExplicitRelSym(Target, Fixup, IsPCRel);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000646 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000647
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000648 if (SD.isExternal()) {
649 if (Renamed)
650 return Renamed;
651 return &Symbol;
652 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000653
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000654 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000655 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000656 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000657
Rafael Espindola25958732010-11-24 21:57:39 +0000658 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000659 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000660
Rafael Espindola25958732010-11-24 21:57:39 +0000661 if (secKind.isThreadLocal()) {
662 if (Renamed)
663 return Renamed;
664 return &Symbol;
665 }
666
Rafael Espindola8cecf252010-10-06 16:23:36 +0000667 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000668 const MCSectionELF &Sec2 =
669 static_cast<const MCSectionELF&>(F.getParent()->getSection());
670
Rafael Espindola8cecf252010-10-06 16:23:36 +0000671 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000672 (Kind == MCSymbolRefExpr::VK_PLT ||
673 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000674 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000675 if (Renamed)
676 return Renamed;
677 return &Symbol;
678 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000679
Rafael Espindola1c130262011-01-23 04:43:11 +0000680 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000681 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000682 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000683 if (Renamed)
684 return Renamed;
685 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000686 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000687
Jason W Kime964d112011-05-11 22:53:06 +0000688 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
689
Rafael Espindola73ffea42010-09-25 05:42:19 +0000690}
691
Matt Fleming3565a062010-08-16 18:57:57 +0000692
Jason W Kim56a39902010-12-06 21:57:34 +0000693void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
694 const MCAsmLayout &Layout,
695 const MCFragment *Fragment,
696 const MCFixup &Fixup,
697 MCValue Target,
698 uint64_t &FixedValue) {
699 int64_t Addend = 0;
700 int Index = 0;
701 int64_t Value = Target.getConstant();
702 const MCSymbol *RelocSymbol = NULL;
703
Rafael Espindola127a6a42010-12-17 07:28:17 +0000704 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000705 if (!Target.isAbsolute()) {
706 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
707 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000708 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000709
710 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
711 const MCSymbol &SymbolB = RefB->getSymbol();
712 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
713 IsPCRel = true;
714
715 // Offset of the symbol in the section
716 int64_t a = Layout.getSymbolOffset(&SDB);
717
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000718 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000719 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
720 Value += b - a;
721 }
722
723 if (!RelocSymbol) {
724 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
725 MCFragment *F = SD.getFragment();
726
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000727 if (F) {
728 Index = F->getParent()->getOrdinal() + 1;
729 // Offset of the symbol in the section
730 Value += Layout.getSymbolOffset(&SD);
731 } else {
732 Index = 0;
733 }
Jason W Kim56a39902010-12-06 21:57:34 +0000734 } else {
735 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
736 WeakrefUsedInReloc.insert(RelocSymbol);
737 else
738 UsedInReloc.insert(RelocSymbol);
739 Index = -1;
740 }
741 Addend = Value;
Michael Liaob21d9ae2012-10-16 19:49:51 +0000742 if (hasRelocationAddend())
Jason W Kim56a39902010-12-06 21:57:34 +0000743 Value = 0;
744 }
745
746 FixedValue = Value;
747 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
748 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000749 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
750 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
751 if (RelocNeedsGOT(Modifier))
752 NeedsGOT = true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000753
Jason W Kim56a39902010-12-06 21:57:34 +0000754 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
755 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000756
Rafael Espindolaf3a86fb2011-12-22 01:57:09 +0000757 // FIXME: no tests cover this. Is adjustFixupOffset dead code?
758 TargetObjectWriter->adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000759
Rafael Espindolabff66a82010-12-18 03:27:34 +0000760 if (!hasRelocationAddend())
761 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000762
763 if (is64Bit())
764 assert(isInt<64>(Addend));
765 else
766 assert(isInt<32>(Addend));
767
Akira Hatanaka00ca8882012-03-23 23:06:45 +0000768 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
Jason W Kim56a39902010-12-06 21:57:34 +0000769 Relocations[Fragment->getParent()].push_back(ERE);
770}
771
772
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000773uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000774ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
775 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000776 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000777 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000778}
779
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000780bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
781 const MCSymbolData &Data,
782 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000783 if (Data.getFlags() & ELF_Other_Weakref)
784 return false;
785
Rafael Espindolabd701182010-10-19 19:31:37 +0000786 if (Used)
787 return true;
788
Rafael Espindola88182132010-10-27 15:18:17 +0000789 if (Renamed)
790 return false;
791
Rafael Espindola737cd212010-10-05 18:01:23 +0000792 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000793
Rafael Espindolad1798862010-10-29 23:09:31 +0000794 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
795 return true;
796
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000797 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000798 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
799 return false;
800
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000801 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000802 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000803 return false;
804
Rafael Espindola737cd212010-10-05 18:01:23 +0000805 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
806 return false;
807
Rafael Espindolabd701182010-10-19 19:31:37 +0000808 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000809 return false;
810
811 return true;
812}
813
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000814bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
815 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000816 if (Data.isExternal())
817 return false;
818
819 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000820 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000821
822 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
823 if (isSignature && !isUsedInReloc)
824 return true;
825
Rafael Espindola737cd212010-10-05 18:01:23 +0000826 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000827 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000828
829 return true;
830}
831
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000832void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000833 SectionIndexMapTy &SectionIndexMap,
834 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000835 unsigned Index = 1;
836 for (MCAssembler::iterator it = Asm.begin(),
837 ie = Asm.end(); it != ie; ++it) {
838 const MCSectionELF &Section =
839 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000840 if (Section.getType() != ELF::SHT_GROUP)
841 continue;
842 SectionIndexMap[&Section] = Index++;
843 }
844
845 for (MCAssembler::iterator it = Asm.begin(),
846 ie = Asm.end(); it != ie; ++it) {
847 const MCSectionELF &Section =
848 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000849 if (Section.getType() == ELF::SHT_GROUP ||
850 Section.getType() == ELF::SHT_REL ||
851 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000852 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000853 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000854 const MCSectionELF *RelSection = RelMap.lookup(&Section);
855 if (RelSection)
856 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000857 }
858}
859
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000860void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000861 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000862 RevGroupMapTy RevGroupMap,
863 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000864 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000865 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000866 if (NeedsGOT) {
Dmitri Gribenko96f498b2013-01-13 16:01:15 +0000867 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
Rafael Espindola5c77c162010-10-05 15:48:37 +0000868 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
869 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
870 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000871 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000872 }
873
Matt Fleming3565a062010-08-16 18:57:57 +0000874 // Index 0 is always the empty string.
875 StringMap<uint64_t> StringIndexMap;
876 StringTable += '\x00';
877
Rafael Espindolad5321da2011-04-07 23:21:52 +0000878 // FIXME: We could optimize suffixes in strtab in the same way we
879 // optimize them in shstrtab.
880
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000881 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000882 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
883 ie = Asm.symbol_end(); it != ie; ++it) {
884 const MCSymbol &Symbol = it->getSymbol();
885
Rafael Espindola484291c2010-11-01 14:28:48 +0000886 bool Used = UsedInReloc.count(&Symbol);
887 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000888 bool isSignature = RevGroupMap.count(&Symbol);
889
890 if (!isInSymtab(Asm, *it,
891 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000892 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000893 continue;
894
Matt Fleming3565a062010-08-16 18:57:57 +0000895 ELFSymbolData MSD;
896 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000897 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000898
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000899 // Undefined symbols are global, but this is the first place we
900 // are able to set it.
901 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000902 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000903 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000904 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
905 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000906 }
907
Rafael Espindola484291c2010-11-01 14:28:48 +0000908 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000909 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000910
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000911 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000912 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000913 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000914 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000915 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000916 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000917 if (isSignature && !Used)
918 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
919 else
920 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000921 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000922 const MCSectionELF &Section =
923 static_cast<const MCSectionELF&>(RefSymbol.getSection());
924 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000925 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
926 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000927 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000928 }
929
Rafael Espindola88182132010-10-27 15:18:17 +0000930 // The @@@ in symbol version is replaced with @ in undefined symbols and
931 // @@ in defined ones.
932 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000933 SmallString<32> Buf;
934
Rafael Espindola88182132010-10-27 15:18:17 +0000935 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000936 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000937 Buf += Name.substr(0, Pos);
938 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
939 Buf += Name.substr(Pos + Skip);
940 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000941 }
942
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000943 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000944 if (!Entry) {
945 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000946 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000947 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000948 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000949 MSD.StringIndex = Entry;
950 if (MSD.SectionIndex == ELF::SHN_UNDEF)
951 UndefinedSymbolData.push_back(MSD);
952 else if (Local)
953 LocalSymbolData.push_back(MSD);
954 else
955 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000956 }
957
958 // Symbols are required to be in lexicographic order.
959 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
960 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
961 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
962
963 // Set the symbol indices. Local symbols must come before all other
964 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000965 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000966 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
967 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000968
969 Index += NumRegularSections;
970
Matt Fleming3565a062010-08-16 18:57:57 +0000971 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
972 ExternalSymbolData[i].SymbolData->setIndex(Index++);
973 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
974 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000975
976 if (NumRegularSections > ELF::SHN_LORESERVE)
977 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000978}
979
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000980void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
981 MCAsmLayout &Layout,
982 RelMapTy &RelMap) {
983 for (MCAssembler::const_iterator it = Asm.begin(),
984 ie = Asm.end(); it != ie; ++it) {
985 const MCSectionData &SD = *it;
986 if (Relocations[&SD].empty())
987 continue;
988
Matt Fleming3565a062010-08-16 18:57:57 +0000989 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000990 const MCSectionELF &Section =
991 static_cast<const MCSectionELF&>(SD.getSection());
992
993 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000994 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000995 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000996
997 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000998 if (hasRelocationAddend())
999 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001000 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001001 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001002
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001003 const MCSectionELF *RelaSection =
1004 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
1005 ELF::SHT_RELA : ELF::SHT_REL, 0,
1006 SectionKind::getReadOnly(),
1007 EntrySize, "");
1008 RelMap[&Section] = RelaSection;
1009 Asm.getOrCreateSectionData(*RelaSection);
1010 }
1011}
Matt Fleming3565a062010-08-16 18:57:57 +00001012
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001013void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1014 const RelMapTy &RelMap) {
1015 for (MCAssembler::const_iterator it = Asm.begin(),
1016 ie = Asm.end(); it != ie; ++it) {
1017 const MCSectionData &SD = *it;
1018 const MCSectionELF &Section =
1019 static_cast<const MCSectionELF&>(SD.getSection());
1020
1021 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1022 if (!RelaSection)
1023 continue;
Matt Fleming3565a062010-08-16 18:57:57 +00001024 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001025 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001026
1027 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001028 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +00001029 }
1030}
1031
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001032void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1033 uint64_t Flags, uint64_t Address,
1034 uint64_t Offset, uint64_t Size,
1035 uint32_t Link, uint32_t Info,
1036 uint64_t Alignment,
1037 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001038 Write32(Name); // sh_name: index into string table
1039 Write32(Type); // sh_type
1040 WriteWord(Flags); // sh_flags
1041 WriteWord(Address); // sh_addr
1042 WriteWord(Offset); // sh_offset
1043 WriteWord(Size); // sh_size
1044 Write32(Link); // sh_link
1045 Write32(Info); // sh_info
1046 WriteWord(Alignment); // sh_addralign
1047 WriteWord(EntrySize); // sh_entsize
1048}
1049
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001050void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1051 MCDataFragment *F,
1052 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001053 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka00ca8882012-03-23 23:06:45 +00001054
1055 // Sort the relocation entries. Most targets just sort by r_offset, but some
1056 // (e.g., MIPS) have additional constraints.
1057 TargetObjectWriter->sortRelocs(Asm, Relocs);
Matt Fleming3565a062010-08-16 18:57:57 +00001058
1059 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1060 ELFRelocationEntry entry = Relocs[e - i - 1];
1061
Rafael Espindola12203cc2010-11-21 00:48:25 +00001062 if (!entry.Index)
1063 ;
1064 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001065 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1066 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001067 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001068 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001069 String64(*F, entry.r_offset);
Jack Carter93ee2862012-06-27 22:28:30 +00001070 if (TargetObjectWriter->isN64()) {
1071 String32(*F, entry.Index);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001072
Jack Carter93ee2862012-06-27 22:28:30 +00001073 String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1074 String8(*F, TargetObjectWriter->getRType3(entry.Type));
1075 String8(*F, TargetObjectWriter->getRType2(entry.Type));
1076 String8(*F, TargetObjectWriter->getRType(entry.Type));
1077 }
1078 else {
1079 struct ELF::Elf64_Rela ERE64;
1080 ERE64.setSymbolAndType(entry.Index, entry.Type);
1081 String64(*F, ERE64.r_info);
1082 }
Rafael Espindolabff66a82010-12-18 03:27:34 +00001083 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001084 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001085 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001086 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001087
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001088 struct ELF::Elf32_Rela ERE32;
1089 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001090 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001091
Rafael Espindolabff66a82010-12-18 03:27:34 +00001092 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001093 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001094 }
Matt Fleming3565a062010-08-16 18:57:57 +00001095 }
1096}
1097
Rafael Espindolad5321da2011-04-07 23:21:52 +00001098static int compareBySuffix(const void *a, const void *b) {
1099 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
1100 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
1101 const StringRef &NameA = secA->getSectionName();
1102 const StringRef &NameB = secB->getSectionName();
1103 const unsigned sizeA = NameA.size();
1104 const unsigned sizeB = NameB.size();
1105 const unsigned len = std::min(sizeA, sizeB);
1106 for (unsigned int i = 0; i < len; ++i) {
1107 char ca = NameA[sizeA - i - 1];
1108 char cb = NameB[sizeB - i - 1];
1109 if (ca != cb)
1110 return cb - ca;
1111 }
1112
1113 return sizeB - sizeA;
1114}
1115
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001116void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1117 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001118 SectionIndexMapTy &SectionIndexMap,
1119 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001120 MCContext &Ctx = Asm.getContext();
1121 MCDataFragment *F;
1122
Rafael Espindolabff66a82010-12-18 03:27:34 +00001123 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001124
Rafael Espindola38738bf2010-09-22 19:04:41 +00001125 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001126 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001127 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001128 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001129 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1130 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +00001131
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001132 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001133 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1134 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001135 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001136 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001137 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001138
1139 MCSectionData *SymtabShndxSD = NULL;
1140
1141 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001142 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001143 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001144 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001145 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1146 SymtabShndxSD->setAlignment(4);
1147 }
Matt Fleming3565a062010-08-16 18:57:57 +00001148
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001149 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001150 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001151 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001152 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1153 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001154
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001155 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1156
1157 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1158 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1159 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +00001160
1161 // Symbol table
1162 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001163 MCDataFragment *ShndxF = NULL;
1164 if (NeedsSymtabShndx) {
1165 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001166 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001167 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001168
Matt Fleming3565a062010-08-16 18:57:57 +00001169 F = new MCDataFragment(&StrtabSD);
1170 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001171
Matt Fleming3565a062010-08-16 18:57:57 +00001172 F = new MCDataFragment(&ShstrtabSD);
1173
Rafael Espindolad5321da2011-04-07 23:21:52 +00001174 std::vector<const MCSectionELF*> Sections;
1175 for (MCAssembler::const_iterator it = Asm.begin(),
1176 ie = Asm.end(); it != ie; ++it) {
1177 const MCSectionELF &Section =
1178 static_cast<const MCSectionELF&>(it->getSection());
1179 Sections.push_back(&Section);
1180 }
1181 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1182
Matt Fleming3565a062010-08-16 18:57:57 +00001183 // Section header string table.
1184 //
1185 // The first entry of a string table holds a null character so skip
1186 // section 0.
1187 uint64_t Index = 1;
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001188 F->getContents().push_back('\x00');
Matt Fleming3565a062010-08-16 18:57:57 +00001189
Rafael Espindolad5321da2011-04-07 23:21:52 +00001190 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1191 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +00001192
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001193 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +00001194 if (I != 0) {
1195 StringRef PreviousName = Sections[I - 1]->getSectionName();
1196 if (PreviousName.endswith(Name)) {
1197 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1198 continue;
1199 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001200 }
Matt Fleming3565a062010-08-16 18:57:57 +00001201 // Remember the index into the string table so we can write it
1202 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001203 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001204
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001205 Index += Name.size() + 1;
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001206 F->getContents().append(Name.begin(), Name.end());
1207 F->getContents().push_back('\x00');
Matt Fleming3565a062010-08-16 18:57:57 +00001208 }
Rafael Espindola70703872010-09-30 02:22:20 +00001209}
1210
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001211void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1212 MCAsmLayout &Layout,
1213 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001214 RevGroupMapTy &RevGroupMap,
1215 SectionIndexMapTy &SectionIndexMap,
1216 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001217 // Create the .note.GNU-stack section if needed.
1218 MCContext &Ctx = Asm.getContext();
1219 if (Asm.getNoExecStack()) {
1220 const MCSectionELF *GnuStackSection =
1221 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1222 SectionKind::getReadOnly());
1223 Asm.getOrCreateSectionData(*GnuStackSection);
1224 }
1225
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001226 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001227 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1228 it != ie; ++it) {
1229 const MCSectionELF &Section =
1230 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001231 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001232 continue;
1233
1234 const MCSymbol *SignatureSymbol = Section.getGroup();
1235 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001236 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001237 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001238 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001239 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1240 Data.setAlignment(4);
1241 MCDataFragment *F = new MCDataFragment(&Data);
1242 String32(*F, ELF::GRP_COMDAT);
1243 }
1244 GroupMap[Group] = SignatureSymbol;
1245 }
1246
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001247 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1248
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001249 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001250 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001251 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001252 const MCSectionELF &Section =
1253 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001254 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001255 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001256 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001257 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1258 // FIXME: we could use the previous fragment
1259 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001260 unsigned Index = SectionIndexMap.lookup(&Section);
1261 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001262 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001263}
1264
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001265void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1266 const SectionIndexMapTy &SectionIndexMap,
1267 uint32_t GroupSymbolIndex,
1268 uint64_t Offset, uint64_t Size,
1269 uint64_t Alignment,
1270 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001271 uint64_t sh_link = 0;
1272 uint64_t sh_info = 0;
1273
1274 switch(Section.getType()) {
1275 case ELF::SHT_DYNAMIC:
1276 sh_link = SectionStringTableIndex[&Section];
1277 sh_info = 0;
1278 break;
1279
1280 case ELF::SHT_REL:
1281 case ELF::SHT_RELA: {
1282 const MCSectionELF *SymtabSection;
1283 const MCSectionELF *InfoSection;
1284 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1285 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001286 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001287 sh_link = SectionIndexMap.lookup(SymtabSection);
1288 assert(sh_link && ".symtab not found");
1289
1290 // Remove ".rel" and ".rela" prefixes.
1291 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1292 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1293
1294 InfoSection = Asm.getContext().getELFSection(SectionName,
1295 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001296 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001297 sh_info = SectionIndexMap.lookup(InfoSection);
1298 break;
1299 }
1300
1301 case ELF::SHT_SYMTAB:
1302 case ELF::SHT_DYNSYM:
1303 sh_link = StringTableIndex;
1304 sh_info = LastLocalSymbolIndex;
1305 break;
1306
1307 case ELF::SHT_SYMTAB_SHNDX:
1308 sh_link = SymbolTableIndex;
1309 break;
1310
1311 case ELF::SHT_PROGBITS:
1312 case ELF::SHT_STRTAB:
1313 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001314 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001315 case ELF::SHT_NULL:
1316 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001317 case ELF::SHT_INIT_ARRAY:
1318 case ELF::SHT_FINI_ARRAY:
1319 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001320 case ELF::SHT_X86_64_UNWIND:
Jack Carterc91cbb92013-01-18 21:20:38 +00001321 case ELF::SHT_MIPS_REGINFO:
1322 case ELF::SHT_MIPS_OPTIONS:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001323 // Nothing to do.
1324 break;
1325
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001326 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001327 sh_link = SymbolTableIndex;
1328 sh_info = GroupSymbolIndex;
1329 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001330
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001331 default:
1332 assert(0 && "FIXME: sh_type value not supported!");
1333 break;
1334 }
1335
Logan Chienb0c89962013-02-05 14:18:59 +00001336 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1337 Section.getType() == ELF::SHT_ARM_EXIDX) {
1338 StringRef SecName(Section.getSectionName());
1339 if (SecName == ".ARM.exidx") {
1340 sh_link = SectionIndexMap.lookup(
1341 Asm.getContext().getELFSection(".text",
1342 ELF::SHT_PROGBITS,
1343 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1344 SectionKind::getText()));
1345 } else if (SecName.startswith(".ARM.exidx")) {
1346 sh_link = SectionIndexMap.lookup(
1347 Asm.getContext().getELFSection(SecName.substr(sizeof(".ARM.exidx") - 1),
1348 ELF::SHT_PROGBITS,
1349 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1350 SectionKind::getText()));
1351 }
1352 }
1353
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001354 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1355 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1356 Alignment, Section.getEntrySize());
1357}
1358
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001359bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001360 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001361 !SD.getSection().isVirtualSection();
1362}
1363
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001364uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001365 uint64_t Ret = 0;
1366 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1367 ++i) {
1368 const MCFragment &F = *i;
1369 assert(F.getKind() == MCFragment::FT_Data);
1370 Ret += cast<MCDataFragment>(F).getContents().size();
1371 }
1372 return Ret;
1373}
1374
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001375uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1376 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001377 if (IsELFMetaDataSection(SD))
1378 return DataSectionSize(SD);
1379 return Layout.getSectionFileSize(&SD);
1380}
1381
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001382uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1383 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001384 if (IsELFMetaDataSection(SD))
1385 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001386 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001387}
1388
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001389void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1390 const MCAsmLayout &Layout,
1391 const MCSectionELF &Section) {
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001392 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1393
Benjamin Kramer263109d2012-01-20 14:42:32 +00001394 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001395 WriteZeros(Padding);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001396
1397 if (IsELFMetaDataSection(SD)) {
1398 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1399 ++i) {
1400 const MCFragment &F = *i;
1401 assert(F.getKind() == MCFragment::FT_Data);
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001402 WriteBytes(cast<MCDataFragment>(F).getContents());
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001403 }
1404 } else {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001405 Asm.writeSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001406 }
1407}
1408
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001409void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1410 const GroupMapTy &GroupMap,
1411 const MCAsmLayout &Layout,
1412 const SectionIndexMapTy &SectionIndexMap,
1413 const SectionOffsetMapTy &SectionOffsetMap) {
1414 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001415
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001416 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001417 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001418
1419 for (SectionIndexMapTy::const_iterator i=
1420 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1421 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001422 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001423 }
1424
Matt Fleming3565a062010-08-16 18:57:57 +00001425 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001426 uint64_t FirstSectionSize =
1427 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1428 uint32_t FirstSectionLink =
1429 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1430 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001431
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001432 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001433 const MCSectionELF &Section = *Sections[i];
1434 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1435 uint32_t GroupSymbolIndex;
1436 if (Section.getType() != ELF::SHT_GROUP)
1437 GroupSymbolIndex = 0;
1438 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001439 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1440 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001441
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001442 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001443
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001444 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001445 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001446 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001447 }
1448}
1449
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001450void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1451 std::vector<const MCSectionELF*> &Sections) {
1452 for (MCAssembler::iterator it = Asm.begin(),
1453 ie = Asm.end(); it != ie; ++it) {
1454 const MCSectionELF &Section =
1455 static_cast<const MCSectionELF &>(it->getSection());
1456 if (Section.getType() == ELF::SHT_GROUP)
1457 Sections.push_back(&Section);
1458 }
1459
1460 for (MCAssembler::iterator it = Asm.begin(),
1461 ie = Asm.end(); it != ie; ++it) {
1462 const MCSectionELF &Section =
1463 static_cast<const MCSectionELF &>(it->getSection());
1464 if (Section.getType() != ELF::SHT_GROUP &&
1465 Section.getType() != ELF::SHT_REL &&
1466 Section.getType() != ELF::SHT_RELA)
1467 Sections.push_back(&Section);
1468 }
1469
1470 for (MCAssembler::iterator it = Asm.begin(),
1471 ie = Asm.end(); it != ie; ++it) {
1472 const MCSectionELF &Section =
1473 static_cast<const MCSectionELF &>(it->getSection());
1474 if (Section.getType() == ELF::SHT_REL ||
1475 Section.getType() == ELF::SHT_RELA)
1476 Sections.push_back(&Section);
1477 }
1478}
1479
1480void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1481 const MCAsmLayout &Layout) {
1482 GroupMapTy GroupMap;
1483 RevGroupMapTy RevGroupMap;
1484 SectionIndexMapTy SectionIndexMap;
1485
1486 unsigned NumUserSections = Asm.size();
1487
1488 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1489 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1490
1491 const unsigned NumUserAndRelocSections = Asm.size();
1492 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1493 RevGroupMap, SectionIndexMap, RelMap);
1494 const unsigned AllSections = Asm.size();
1495 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1496
1497 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1498
1499 // Compute symbol table information.
1500 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1501
1502
1503 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1504
1505 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1506 const_cast<MCAsmLayout&>(Layout),
1507 SectionIndexMap,
1508 RelMap);
1509
1510 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1511 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1512 sizeof(ELF::Elf32_Ehdr);
1513 uint64_t FileOff = HeaderSize;
1514
1515 std::vector<const MCSectionELF*> Sections;
1516 ComputeSectionOrder(Asm, Sections);
1517 unsigned NumSections = Sections.size();
1518 SectionOffsetMapTy SectionOffsetMap;
1519 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1520 const MCSectionELF &Section = *Sections[i];
1521 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1522
1523 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1524
1525 // Remember the offset into the file for this section.
1526 SectionOffsetMap[&Section] = FileOff;
1527
1528 // Get the size of the section in the output file (including padding).
1529 FileOff += GetSectionFileSize(Layout, SD);
1530 }
1531
1532 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1533
1534 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1535
1536 uint64_t SectionHeaderEntrySize = is64Bit() ?
1537 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1538 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1539
1540 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1541 const MCSectionELF &Section = *Sections[i];
1542 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1543
1544 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1545
1546 // Remember the offset into the file for this section.
1547 SectionOffsetMap[&Section] = FileOff;
1548
1549 // Get the size of the section in the output file (including padding).
1550 FileOff += GetSectionFileSize(Layout, SD);
1551 }
1552
1553 // Write out the ELF header ...
Jack Carter9a7bf432013-01-30 02:09:52 +00001554 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001555
1556 // ... then the regular sections ...
1557 // + because of .shstrtab
1558 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1559 WriteDataSectionData(Asm, Layout, *Sections[i]);
1560
Benjamin Kramer263109d2012-01-20 14:42:32 +00001561 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001562 WriteZeros(Padding);
1563
1564 // ... then the section header table ...
1565 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1566 SectionOffsetMap);
1567
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001568 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001569 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1570 WriteDataSectionData(Asm, Layout, *Sections[i]);
1571}
1572
Eli Friedman78c1e172011-03-03 07:24:36 +00001573bool
1574ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1575 const MCSymbolData &DataA,
1576 const MCFragment &FB,
1577 bool InSet,
1578 bool IsPCRel) const {
1579 if (DataA.getFlags() & ELF_STB_Weak)
1580 return false;
1581 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1582 Asm, DataA, FB,InSet, IsPCRel);
1583}
1584
Rafael Espindola6024c972010-12-17 17:45:22 +00001585MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1586 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001587 bool IsLittleEndian) {
Rafael Espindola7bd27802011-12-22 03:24:43 +00001588 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolaedae8e12011-12-21 17:30:17 +00001589}