blob: eb197f2cec719f2737f8a02fc3d515c61cc32afd [file] [log] [blame]
Nick Lewycky7394c5a2013-03-09 09:32:16 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
Matt Fleming3565a062010-08-16 18:57:57 +00002//
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),
Nick Lewycky7394c5a2013-03-09 09:32:16 +0000158 NeedsGOT(false), NeedsSymtabShndx(false) {
Rafael Espindola3963d612011-12-22 03:38:00 +0000159 }
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
Jack Carter77afbdc2013-02-19 21:57:35 +0000549 // Binding and Type share the same byte as upper and lower nibbles
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000550 uint8_t Binding = MCELF::GetBinding(OrigData);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000551 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000552 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
Jack Carter77afbdc2013-02-19 21:57:35 +0000553
554 // Other and Visibility share the same byte with Visability using the lower
555 // 2 bits
556 uint8_t Visibility = MCELF::GetVisibility(OrigData);
557 uint8_t Other = MCELF::getOther(OrigData) <<
558 (ELF_Other_Shift - ELF_STV_Shift);
559 Other |= Visibility;
Rafael Espindola152c1062010-10-06 21:02:29 +0000560
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000561 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000562 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000563
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000564 assert(!(Data.isCommon() && !Data.isExternal()));
565
Rafael Espindolaf0121242010-12-22 16:03:00 +0000566 const MCExpr *ESize = Data.getSize();
567 if (ESize) {
568 int64_t Res;
569 if (!ESize->EvaluateAsAbsolute(Res, Layout))
570 report_fatal_error("Size expression must be absolute.");
571 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000572 }
573
574 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000575 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
576 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000577}
578
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000579void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
580 MCDataFragment *ShndxF,
581 const MCAssembler &Asm,
582 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000583 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000584 // The string table must be emitted first because we need the index
585 // into the string table for all the symbol names.
586 assert(StringTable.size() && "Missing string table");
587
588 // FIXME: Make sure the start of the symbol table is aligned.
589
590 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000591 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000592
593 // Write the symbol table entries.
594 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
595 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
596 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000597 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000598 }
599
Rafael Espindola71859c62010-09-16 19:46:31 +0000600 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000601 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
602 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000603 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000604 static_cast<const MCSectionELF&>(i->getSection());
605 if (Section.getType() == ELF::SHT_RELA ||
606 Section.getType() == ELF::SHT_REL ||
607 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000608 Section.getType() == ELF::SHT_SYMTAB ||
609 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000610 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000611 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000612 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
613 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000614 LastLocalSymbolIndex++;
615 }
Matt Fleming3565a062010-08-16 18:57:57 +0000616
617 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
618 ELFSymbolData &MSD = ExternalSymbolData[i];
619 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000620 assert(((Data.getFlags() & ELF_STB_Global) ||
621 (Data.getFlags() & ELF_STB_Weak)) &&
622 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000623 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000624 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000625 LastLocalSymbolIndex++;
626 }
627
628 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
629 ELFSymbolData &MSD = UndefinedSymbolData[i];
630 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000631 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000632 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000633 LastLocalSymbolIndex++;
634 }
635}
636
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000637const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
638 const MCValue &Target,
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000639 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +0000640 const MCFixup &Fixup,
641 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000642 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000643 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
644 const MCSymbol *Renamed = Renames.lookup(&Symbol);
645 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000646
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000647 if (ASymbol.isUndefined()) {
648 if (Renamed)
649 return Renamed;
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000650 return undefinedExplicitRelSym(Target, Fixup, IsPCRel);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000651 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000652
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000653 if (SD.isExternal()) {
654 if (Renamed)
655 return Renamed;
656 return &Symbol;
657 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000658
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000659 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000660 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000661 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000662
Rafael Espindola25958732010-11-24 21:57:39 +0000663 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000664 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000665
Rafael Espindola25958732010-11-24 21:57:39 +0000666 if (secKind.isThreadLocal()) {
667 if (Renamed)
668 return Renamed;
669 return &Symbol;
670 }
671
Rafael Espindola8cecf252010-10-06 16:23:36 +0000672 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000673 const MCSectionELF &Sec2 =
674 static_cast<const MCSectionELF&>(F.getParent()->getSection());
675
Rafael Espindola8cecf252010-10-06 16:23:36 +0000676 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000677 (Kind == MCSymbolRefExpr::VK_PLT ||
678 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000679 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000680 if (Renamed)
681 return Renamed;
682 return &Symbol;
683 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000684
Rafael Espindola1c130262011-01-23 04:43:11 +0000685 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000686 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000687 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000688 if (Renamed)
689 return Renamed;
690 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000691 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000692
Jason W Kime964d112011-05-11 22:53:06 +0000693 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
694
Rafael Espindola73ffea42010-09-25 05:42:19 +0000695}
696
Matt Fleming3565a062010-08-16 18:57:57 +0000697
Jason W Kim56a39902010-12-06 21:57:34 +0000698void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
699 const MCAsmLayout &Layout,
700 const MCFragment *Fragment,
701 const MCFixup &Fixup,
702 MCValue Target,
703 uint64_t &FixedValue) {
704 int64_t Addend = 0;
705 int Index = 0;
706 int64_t Value = Target.getConstant();
707 const MCSymbol *RelocSymbol = NULL;
708
Rafael Espindola127a6a42010-12-17 07:28:17 +0000709 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000710 if (!Target.isAbsolute()) {
711 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
712 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000713 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000714
715 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
716 const MCSymbol &SymbolB = RefB->getSymbol();
717 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
718 IsPCRel = true;
719
720 // Offset of the symbol in the section
721 int64_t a = Layout.getSymbolOffset(&SDB);
722
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000723 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000724 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
725 Value += b - a;
726 }
727
728 if (!RelocSymbol) {
729 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
730 MCFragment *F = SD.getFragment();
731
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000732 if (F) {
733 Index = F->getParent()->getOrdinal() + 1;
734 // Offset of the symbol in the section
735 Value += Layout.getSymbolOffset(&SD);
736 } else {
737 Index = 0;
738 }
Jason W Kim56a39902010-12-06 21:57:34 +0000739 } else {
740 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
741 WeakrefUsedInReloc.insert(RelocSymbol);
742 else
743 UsedInReloc.insert(RelocSymbol);
744 Index = -1;
745 }
746 Addend = Value;
Michael Liaob21d9ae2012-10-16 19:49:51 +0000747 if (hasRelocationAddend())
Jason W Kim56a39902010-12-06 21:57:34 +0000748 Value = 0;
749 }
750
751 FixedValue = Value;
752 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
753 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000754 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
755 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
756 if (RelocNeedsGOT(Modifier))
757 NeedsGOT = true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000758
Jason W Kim56a39902010-12-06 21:57:34 +0000759 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
760 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000761
Rafael Espindolabff66a82010-12-18 03:27:34 +0000762 if (!hasRelocationAddend())
763 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000764
765 if (is64Bit())
766 assert(isInt<64>(Addend));
767 else
768 assert(isInt<32>(Addend));
769
Akira Hatanaka00ca8882012-03-23 23:06:45 +0000770 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
Jason W Kim56a39902010-12-06 21:57:34 +0000771 Relocations[Fragment->getParent()].push_back(ERE);
772}
773
774
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000775uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000776ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
777 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000778 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000779 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000780}
781
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000782bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
783 const MCSymbolData &Data,
784 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000785 if (Data.getFlags() & ELF_Other_Weakref)
786 return false;
787
Rafael Espindolabd701182010-10-19 19:31:37 +0000788 if (Used)
789 return true;
790
Rafael Espindola88182132010-10-27 15:18:17 +0000791 if (Renamed)
792 return false;
793
Rafael Espindola737cd212010-10-05 18:01:23 +0000794 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000795
Rafael Espindolad1798862010-10-29 23:09:31 +0000796 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
797 return true;
798
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000799 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000800 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
801 return false;
802
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000803 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000804 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000805 return false;
806
Rafael Espindola737cd212010-10-05 18:01:23 +0000807 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
808 return false;
809
Rafael Espindolabd701182010-10-19 19:31:37 +0000810 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000811 return false;
812
813 return true;
814}
815
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000816bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
817 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000818 if (Data.isExternal())
819 return false;
820
821 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000822 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000823
824 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
825 if (isSignature && !isUsedInReloc)
826 return true;
827
Rafael Espindola737cd212010-10-05 18:01:23 +0000828 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000829 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000830
831 return true;
832}
833
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000834void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000835 SectionIndexMapTy &SectionIndexMap,
836 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000837 unsigned Index = 1;
838 for (MCAssembler::iterator it = Asm.begin(),
839 ie = Asm.end(); it != ie; ++it) {
840 const MCSectionELF &Section =
841 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000842 if (Section.getType() != ELF::SHT_GROUP)
843 continue;
844 SectionIndexMap[&Section] = Index++;
845 }
846
847 for (MCAssembler::iterator it = Asm.begin(),
848 ie = Asm.end(); it != ie; ++it) {
849 const MCSectionELF &Section =
850 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000851 if (Section.getType() == ELF::SHT_GROUP ||
852 Section.getType() == ELF::SHT_REL ||
853 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000854 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000855 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000856 const MCSectionELF *RelSection = RelMap.lookup(&Section);
857 if (RelSection)
858 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000859 }
860}
861
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000862void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000863 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000864 RevGroupMapTy RevGroupMap,
865 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000866 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000867 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000868 if (NeedsGOT) {
Dmitri Gribenko96f498b2013-01-13 16:01:15 +0000869 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
Rafael Espindola5c77c162010-10-05 15:48:37 +0000870 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
871 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
872 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000873 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000874 }
875
Matt Fleming3565a062010-08-16 18:57:57 +0000876 // Index 0 is always the empty string.
877 StringMap<uint64_t> StringIndexMap;
878 StringTable += '\x00';
879
Rafael Espindolad5321da2011-04-07 23:21:52 +0000880 // FIXME: We could optimize suffixes in strtab in the same way we
881 // optimize them in shstrtab.
882
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000883 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000884 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
885 ie = Asm.symbol_end(); it != ie; ++it) {
886 const MCSymbol &Symbol = it->getSymbol();
887
Rafael Espindola484291c2010-11-01 14:28:48 +0000888 bool Used = UsedInReloc.count(&Symbol);
889 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000890 bool isSignature = RevGroupMap.count(&Symbol);
891
892 if (!isInSymtab(Asm, *it,
893 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000894 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000895 continue;
896
Matt Fleming3565a062010-08-16 18:57:57 +0000897 ELFSymbolData MSD;
898 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000899 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000900
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000901 // Undefined symbols are global, but this is the first place we
902 // are able to set it.
903 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000904 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000905 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000906 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
907 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000908 }
909
Rafael Espindola484291c2010-11-01 14:28:48 +0000910 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000911 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000912
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000913 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000914 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000915 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000916 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000917 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000918 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000919 if (isSignature && !Used)
920 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
921 else
922 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000923 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000924 const MCSectionELF &Section =
925 static_cast<const MCSectionELF&>(RefSymbol.getSection());
926 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000927 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
928 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000929 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000930 }
931
Rafael Espindola88182132010-10-27 15:18:17 +0000932 // The @@@ in symbol version is replaced with @ in undefined symbols and
933 // @@ in defined ones.
934 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000935 SmallString<32> Buf;
936
Rafael Espindola88182132010-10-27 15:18:17 +0000937 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000938 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000939 Buf += Name.substr(0, Pos);
940 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
941 Buf += Name.substr(Pos + Skip);
942 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000943 }
944
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000945 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000946 if (!Entry) {
947 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000948 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000949 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000950 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000951 MSD.StringIndex = Entry;
952 if (MSD.SectionIndex == ELF::SHN_UNDEF)
953 UndefinedSymbolData.push_back(MSD);
954 else if (Local)
955 LocalSymbolData.push_back(MSD);
956 else
957 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000958 }
959
960 // Symbols are required to be in lexicographic order.
961 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
962 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
963 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
964
965 // Set the symbol indices. Local symbols must come before all other
966 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000967 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000968 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
969 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000970
971 Index += NumRegularSections;
972
Matt Fleming3565a062010-08-16 18:57:57 +0000973 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
974 ExternalSymbolData[i].SymbolData->setIndex(Index++);
975 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
976 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000977
Nick Lewycky7394c5a2013-03-09 09:32:16 +0000978 if (Index >= ELF::SHN_LORESERVE)
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000979 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000980}
981
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000982void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
983 MCAsmLayout &Layout,
984 RelMapTy &RelMap) {
985 for (MCAssembler::const_iterator it = Asm.begin(),
986 ie = Asm.end(); it != ie; ++it) {
987 const MCSectionData &SD = *it;
988 if (Relocations[&SD].empty())
989 continue;
990
Matt Fleming3565a062010-08-16 18:57:57 +0000991 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000992 const MCSectionELF &Section =
993 static_cast<const MCSectionELF&>(SD.getSection());
994
995 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000996 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000997 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000998
999 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001000 if (hasRelocationAddend())
1001 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001002 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001003 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001004
Tim Northoverf5b1c502013-07-10 20:58:17 +00001005 unsigned Flags = 0;
1006 StringRef Group = "";
1007 if (Section.getFlags() & ELF::SHF_GROUP) {
David Blaikiedd620a92013-10-22 20:34:30 +00001008 Flags = ELF::SHF_GROUP;
1009 Group = Section.getGroup()->getName();
Tim Northoverf5b1c502013-07-10 20:58:17 +00001010 }
1011
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001012 const MCSectionELF *RelaSection =
1013 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Tim Northoverf5b1c502013-07-10 20:58:17 +00001014 ELF::SHT_RELA : ELF::SHT_REL, Flags,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001015 SectionKind::getReadOnly(),
Tim Northoverf5b1c502013-07-10 20:58:17 +00001016 EntrySize, Group);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001017 RelMap[&Section] = RelaSection;
1018 Asm.getOrCreateSectionData(*RelaSection);
1019 }
1020}
Matt Fleming3565a062010-08-16 18:57:57 +00001021
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001022void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1023 const RelMapTy &RelMap) {
1024 for (MCAssembler::const_iterator it = Asm.begin(),
1025 ie = Asm.end(); it != ie; ++it) {
1026 const MCSectionData &SD = *it;
1027 const MCSectionELF &Section =
1028 static_cast<const MCSectionELF&>(SD.getSection());
1029
1030 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1031 if (!RelaSection)
1032 continue;
Matt Fleming3565a062010-08-16 18:57:57 +00001033 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001034 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001035
1036 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001037 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +00001038 }
1039}
1040
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001041void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1042 uint64_t Flags, uint64_t Address,
1043 uint64_t Offset, uint64_t Size,
1044 uint32_t Link, uint32_t Info,
1045 uint64_t Alignment,
1046 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001047 Write32(Name); // sh_name: index into string table
1048 Write32(Type); // sh_type
1049 WriteWord(Flags); // sh_flags
1050 WriteWord(Address); // sh_addr
1051 WriteWord(Offset); // sh_offset
1052 WriteWord(Size); // sh_size
1053 Write32(Link); // sh_link
1054 Write32(Info); // sh_info
1055 WriteWord(Alignment); // sh_addralign
1056 WriteWord(EntrySize); // sh_entsize
1057}
1058
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001059void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1060 MCDataFragment *F,
1061 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001062 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka00ca8882012-03-23 23:06:45 +00001063
1064 // Sort the relocation entries. Most targets just sort by r_offset, but some
1065 // (e.g., MIPS) have additional constraints.
1066 TargetObjectWriter->sortRelocs(Asm, Relocs);
Matt Fleming3565a062010-08-16 18:57:57 +00001067
1068 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1069 ELFRelocationEntry entry = Relocs[e - i - 1];
1070
Rafael Espindola12203cc2010-11-21 00:48:25 +00001071 if (!entry.Index)
1072 ;
1073 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001074 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1075 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001076 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001077 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001078 String64(*F, entry.r_offset);
Jack Carter93ee2862012-06-27 22:28:30 +00001079 if (TargetObjectWriter->isN64()) {
1080 String32(*F, entry.Index);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001081
Jack Carter93ee2862012-06-27 22:28:30 +00001082 String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1083 String8(*F, TargetObjectWriter->getRType3(entry.Type));
1084 String8(*F, TargetObjectWriter->getRType2(entry.Type));
1085 String8(*F, TargetObjectWriter->getRType(entry.Type));
1086 }
1087 else {
1088 struct ELF::Elf64_Rela ERE64;
1089 ERE64.setSymbolAndType(entry.Index, entry.Type);
1090 String64(*F, ERE64.r_info);
1091 }
Rafael Espindolabff66a82010-12-18 03:27:34 +00001092 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001093 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001094 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001095 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001096
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001097 struct ELF::Elf32_Rela ERE32;
1098 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001099 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001100
Rafael Espindolabff66a82010-12-18 03:27:34 +00001101 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001102 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001103 }
Matt Fleming3565a062010-08-16 18:57:57 +00001104 }
1105}
1106
Benjamin Kramer0d293e42013-09-22 14:09:50 +00001107static int compareBySuffix(const MCSectionELF *const *a,
1108 const MCSectionELF *const *b) {
1109 const StringRef &NameA = (*a)->getSectionName();
1110 const StringRef &NameB = (*b)->getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +00001111 const unsigned sizeA = NameA.size();
1112 const unsigned sizeB = NameB.size();
1113 const unsigned len = std::min(sizeA, sizeB);
1114 for (unsigned int i = 0; i < len; ++i) {
1115 char ca = NameA[sizeA - i - 1];
1116 char cb = NameB[sizeB - i - 1];
1117 if (ca != cb)
1118 return cb - ca;
1119 }
1120
1121 return sizeB - sizeA;
1122}
1123
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001124void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1125 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001126 SectionIndexMapTy &SectionIndexMap,
1127 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001128 MCContext &Ctx = Asm.getContext();
1129 MCDataFragment *F;
1130
Rafael Espindolabff66a82010-12-18 03:27:34 +00001131 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001132
Rafael Espindola38738bf2010-09-22 19:04:41 +00001133 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001134 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001135 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001136 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001137 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1138 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +00001139
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001140 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001141 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1142 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001143 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001144 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001145 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001146
1147 MCSectionData *SymtabShndxSD = NULL;
1148
1149 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001150 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001151 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001152 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001153 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1154 SymtabShndxSD->setAlignment(4);
1155 }
Matt Fleming3565a062010-08-16 18:57:57 +00001156
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001157 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001158 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001159 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001160 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1161 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001162
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001163 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1164
1165 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1166 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1167 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +00001168
1169 // Symbol table
1170 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001171 MCDataFragment *ShndxF = NULL;
1172 if (NeedsSymtabShndx) {
1173 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001174 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001175 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001176
Matt Fleming3565a062010-08-16 18:57:57 +00001177 F = new MCDataFragment(&StrtabSD);
1178 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001179
Matt Fleming3565a062010-08-16 18:57:57 +00001180 F = new MCDataFragment(&ShstrtabSD);
1181
Rafael Espindolad5321da2011-04-07 23:21:52 +00001182 std::vector<const MCSectionELF*> Sections;
1183 for (MCAssembler::const_iterator it = Asm.begin(),
1184 ie = Asm.end(); it != ie; ++it) {
1185 const MCSectionELF &Section =
1186 static_cast<const MCSectionELF&>(it->getSection());
1187 Sections.push_back(&Section);
1188 }
1189 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1190
Matt Fleming3565a062010-08-16 18:57:57 +00001191 // Section header string table.
1192 //
1193 // The first entry of a string table holds a null character so skip
1194 // section 0.
1195 uint64_t Index = 1;
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001196 F->getContents().push_back('\x00');
Matt Fleming3565a062010-08-16 18:57:57 +00001197
Rafael Espindolad5321da2011-04-07 23:21:52 +00001198 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1199 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +00001200
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001201 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +00001202 if (I != 0) {
1203 StringRef PreviousName = Sections[I - 1]->getSectionName();
1204 if (PreviousName.endswith(Name)) {
1205 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1206 continue;
1207 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001208 }
Matt Fleming3565a062010-08-16 18:57:57 +00001209 // Remember the index into the string table so we can write it
1210 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001211 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001212
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001213 Index += Name.size() + 1;
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001214 F->getContents().append(Name.begin(), Name.end());
1215 F->getContents().push_back('\x00');
Matt Fleming3565a062010-08-16 18:57:57 +00001216 }
Rafael Espindola70703872010-09-30 02:22:20 +00001217}
1218
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001219void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1220 MCAsmLayout &Layout,
1221 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001222 RevGroupMapTy &RevGroupMap,
1223 SectionIndexMapTy &SectionIndexMap,
1224 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001225 // Create the .note.GNU-stack section if needed.
1226 MCContext &Ctx = Asm.getContext();
1227 if (Asm.getNoExecStack()) {
1228 const MCSectionELF *GnuStackSection =
1229 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1230 SectionKind::getReadOnly());
1231 Asm.getOrCreateSectionData(*GnuStackSection);
1232 }
1233
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001234 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001235 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1236 it != ie; ++it) {
1237 const MCSectionELF &Section =
1238 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001239 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001240 continue;
1241
1242 const MCSymbol *SignatureSymbol = Section.getGroup();
1243 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001244 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001245 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001246 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001247 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1248 Data.setAlignment(4);
1249 MCDataFragment *F = new MCDataFragment(&Data);
1250 String32(*F, ELF::GRP_COMDAT);
1251 }
1252 GroupMap[Group] = SignatureSymbol;
1253 }
1254
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001255 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1256
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001257 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001258 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001259 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001260 const MCSectionELF &Section =
1261 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001262 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001263 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001264 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001265 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1266 // FIXME: we could use the previous fragment
1267 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001268 unsigned Index = SectionIndexMap.lookup(&Section);
1269 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001270 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001271}
1272
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001273void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1274 const SectionIndexMapTy &SectionIndexMap,
1275 uint32_t GroupSymbolIndex,
1276 uint64_t Offset, uint64_t Size,
1277 uint64_t Alignment,
1278 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001279 uint64_t sh_link = 0;
1280 uint64_t sh_info = 0;
1281
1282 switch(Section.getType()) {
1283 case ELF::SHT_DYNAMIC:
1284 sh_link = SectionStringTableIndex[&Section];
1285 sh_info = 0;
1286 break;
1287
1288 case ELF::SHT_REL:
1289 case ELF::SHT_RELA: {
1290 const MCSectionELF *SymtabSection;
1291 const MCSectionELF *InfoSection;
1292 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1293 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001294 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001295 sh_link = SectionIndexMap.lookup(SymtabSection);
1296 assert(sh_link && ".symtab not found");
1297
1298 // Remove ".rel" and ".rela" prefixes.
1299 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1300 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
David Blaikie2b3ea3c2013-10-22 23:41:52 +00001301 StringRef GroupName =
1302 Section.getGroup() ? Section.getGroup()->getName() : "";
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001303
David Blaikie2b3ea3c2013-10-22 23:41:52 +00001304 InfoSection = Asm.getContext().getELFSection(SectionName, ELF::SHT_PROGBITS,
1305 0, SectionKind::getReadOnly(),
1306 0, GroupName);
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001307 sh_info = SectionIndexMap.lookup(InfoSection);
1308 break;
1309 }
1310
1311 case ELF::SHT_SYMTAB:
1312 case ELF::SHT_DYNSYM:
1313 sh_link = StringTableIndex;
1314 sh_info = LastLocalSymbolIndex;
1315 break;
1316
1317 case ELF::SHT_SYMTAB_SHNDX:
1318 sh_link = SymbolTableIndex;
1319 break;
1320
1321 case ELF::SHT_PROGBITS:
1322 case ELF::SHT_STRTAB:
1323 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001324 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001325 case ELF::SHT_NULL:
1326 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001327 case ELF::SHT_INIT_ARRAY:
1328 case ELF::SHT_FINI_ARRAY:
1329 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001330 case ELF::SHT_X86_64_UNWIND:
Jack Carterc91cbb92013-01-18 21:20:38 +00001331 case ELF::SHT_MIPS_REGINFO:
1332 case ELF::SHT_MIPS_OPTIONS:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001333 // Nothing to do.
1334 break;
1335
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001336 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001337 sh_link = SymbolTableIndex;
1338 sh_info = GroupSymbolIndex;
1339 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001340
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001341 default:
1342 assert(0 && "FIXME: sh_type value not supported!");
1343 break;
1344 }
1345
Logan Chienb0c89962013-02-05 14:18:59 +00001346 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1347 Section.getType() == ELF::SHT_ARM_EXIDX) {
1348 StringRef SecName(Section.getSectionName());
1349 if (SecName == ".ARM.exidx") {
1350 sh_link = SectionIndexMap.lookup(
1351 Asm.getContext().getELFSection(".text",
1352 ELF::SHT_PROGBITS,
1353 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1354 SectionKind::getText()));
1355 } else if (SecName.startswith(".ARM.exidx")) {
David Blaikie2b3ea3c2013-10-22 23:41:52 +00001356 StringRef GroupName =
1357 Section.getGroup() ? Section.getGroup()->getName() : "";
1358 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection(
1359 SecName.substr(sizeof(".ARM.exidx") - 1), ELF::SHT_PROGBITS,
1360 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, SectionKind::getText(), 0,
1361 GroupName));
Logan Chienb0c89962013-02-05 14:18:59 +00001362 }
1363 }
1364
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001365 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1366 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1367 Alignment, Section.getEntrySize());
1368}
1369
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001370bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001371 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001372 !SD.getSection().isVirtualSection();
1373}
1374
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001375uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001376 uint64_t Ret = 0;
1377 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1378 ++i) {
1379 const MCFragment &F = *i;
1380 assert(F.getKind() == MCFragment::FT_Data);
1381 Ret += cast<MCDataFragment>(F).getContents().size();
1382 }
1383 return Ret;
1384}
1385
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001386uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1387 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001388 if (IsELFMetaDataSection(SD))
1389 return DataSectionSize(SD);
1390 return Layout.getSectionFileSize(&SD);
1391}
1392
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001393uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1394 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001395 if (IsELFMetaDataSection(SD))
1396 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001397 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001398}
1399
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001400void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1401 const MCAsmLayout &Layout,
1402 const MCSectionELF &Section) {
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001403 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1404
Benjamin Kramer263109d2012-01-20 14:42:32 +00001405 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001406 WriteZeros(Padding);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001407
1408 if (IsELFMetaDataSection(SD)) {
1409 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1410 ++i) {
1411 const MCFragment &F = *i;
1412 assert(F.getKind() == MCFragment::FT_Data);
Eli Bendersky550f0ad2012-12-07 22:06:56 +00001413 WriteBytes(cast<MCDataFragment>(F).getContents());
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001414 }
1415 } else {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001416 Asm.writeSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001417 }
1418}
1419
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001420void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1421 const GroupMapTy &GroupMap,
1422 const MCAsmLayout &Layout,
1423 const SectionIndexMapTy &SectionIndexMap,
1424 const SectionOffsetMapTy &SectionOffsetMap) {
1425 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001426
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001427 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001428 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001429
1430 for (SectionIndexMapTy::const_iterator i=
1431 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1432 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001433 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001434 }
1435
Matt Fleming3565a062010-08-16 18:57:57 +00001436 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001437 uint64_t FirstSectionSize =
1438 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1439 uint32_t FirstSectionLink =
1440 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1441 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001442
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001443 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001444 const MCSectionELF &Section = *Sections[i];
1445 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1446 uint32_t GroupSymbolIndex;
1447 if (Section.getType() != ELF::SHT_GROUP)
1448 GroupSymbolIndex = 0;
1449 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001450 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1451 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001452
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001453 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001454
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001455 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001456 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001457 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001458 }
1459}
1460
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001461void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1462 std::vector<const MCSectionELF*> &Sections) {
1463 for (MCAssembler::iterator it = Asm.begin(),
1464 ie = Asm.end(); it != ie; ++it) {
1465 const MCSectionELF &Section =
1466 static_cast<const MCSectionELF &>(it->getSection());
1467 if (Section.getType() == ELF::SHT_GROUP)
1468 Sections.push_back(&Section);
1469 }
1470
1471 for (MCAssembler::iterator it = Asm.begin(),
1472 ie = Asm.end(); it != ie; ++it) {
1473 const MCSectionELF &Section =
1474 static_cast<const MCSectionELF &>(it->getSection());
1475 if (Section.getType() != ELF::SHT_GROUP &&
1476 Section.getType() != ELF::SHT_REL &&
1477 Section.getType() != ELF::SHT_RELA)
1478 Sections.push_back(&Section);
1479 }
1480
1481 for (MCAssembler::iterator it = Asm.begin(),
1482 ie = Asm.end(); it != ie; ++it) {
1483 const MCSectionELF &Section =
1484 static_cast<const MCSectionELF &>(it->getSection());
1485 if (Section.getType() == ELF::SHT_REL ||
1486 Section.getType() == ELF::SHT_RELA)
1487 Sections.push_back(&Section);
1488 }
1489}
1490
1491void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1492 const MCAsmLayout &Layout) {
1493 GroupMapTy GroupMap;
1494 RevGroupMapTy RevGroupMap;
1495 SectionIndexMapTy SectionIndexMap;
1496
1497 unsigned NumUserSections = Asm.size();
1498
1499 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1500 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1501
1502 const unsigned NumUserAndRelocSections = Asm.size();
1503 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1504 RevGroupMap, SectionIndexMap, RelMap);
1505 const unsigned AllSections = Asm.size();
1506 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1507
1508 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1509
1510 // Compute symbol table information.
1511 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1512
1513
1514 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1515
1516 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1517 const_cast<MCAsmLayout&>(Layout),
1518 SectionIndexMap,
1519 RelMap);
1520
1521 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1522 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1523 sizeof(ELF::Elf32_Ehdr);
1524 uint64_t FileOff = HeaderSize;
1525
1526 std::vector<const MCSectionELF*> Sections;
1527 ComputeSectionOrder(Asm, Sections);
1528 unsigned NumSections = Sections.size();
1529 SectionOffsetMapTy SectionOffsetMap;
1530 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1531 const MCSectionELF &Section = *Sections[i];
1532 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1533
1534 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1535
1536 // Remember the offset into the file for this section.
1537 SectionOffsetMap[&Section] = FileOff;
1538
1539 // Get the size of the section in the output file (including padding).
1540 FileOff += GetSectionFileSize(Layout, SD);
1541 }
1542
1543 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1544
1545 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1546
1547 uint64_t SectionHeaderEntrySize = is64Bit() ?
1548 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1549 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1550
1551 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1552 const MCSectionELF &Section = *Sections[i];
1553 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1554
1555 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1556
1557 // Remember the offset into the file for this section.
1558 SectionOffsetMap[&Section] = FileOff;
1559
1560 // Get the size of the section in the output file (including padding).
1561 FileOff += GetSectionFileSize(Layout, SD);
1562 }
1563
1564 // Write out the ELF header ...
Jack Carter9a7bf432013-01-30 02:09:52 +00001565 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001566
1567 // ... then the regular sections ...
1568 // + because of .shstrtab
1569 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1570 WriteDataSectionData(Asm, Layout, *Sections[i]);
1571
Benjamin Kramer263109d2012-01-20 14:42:32 +00001572 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001573 WriteZeros(Padding);
1574
1575 // ... then the section header table ...
1576 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1577 SectionOffsetMap);
1578
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001579 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001580 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1581 WriteDataSectionData(Asm, Layout, *Sections[i]);
1582}
1583
Eli Friedman78c1e172011-03-03 07:24:36 +00001584bool
1585ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1586 const MCSymbolData &DataA,
1587 const MCFragment &FB,
1588 bool InSet,
1589 bool IsPCRel) const {
1590 if (DataA.getFlags() & ELF_STB_Weak)
1591 return false;
1592 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1593 Asm, DataA, FB,InSet, IsPCRel);
1594}
1595
Rafael Espindola6024c972010-12-17 17:45:22 +00001596MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1597 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001598 bool IsLittleEndian) {
Rafael Espindola7bd27802011-12-22 03:24:43 +00001599 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolaedae8e12011-12-21 17:30:17 +00001600}