blob: 6e37b5c1a0329c500241bb9194669b11763a214d [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
Rafael Espindola3963d612011-12-22 03:38:00 +000014#include "MCELF.h"
15#include "llvm/ADT/OwningPtr.h"
16#include "llvm/ADT/SmallPtrSet.h"
17#include "llvm/ADT/SmallString.h"
Matt Fleming3565a062010-08-16 18:57:57 +000018#include "llvm/ADT/STLExtras.h"
19#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"
Rafael Espindola3963d612011-12-22 03:38:00 +000024#include "llvm/MC/MCELFObjectWriter.h"
25#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"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/ELF.h"
Matt Fleming3565a062010-08-16 18:57:57 +000034
Matt Fleming3565a062010-08-16 18:57:57 +000035#include <vector>
36using namespace llvm;
37
Jason W Kime964d112011-05-11 22:53:06 +000038#undef DEBUG_TYPE
39#define DEBUG_TYPE "reloc-info"
40
Rafael Espindola3963d612011-12-22 03:38:00 +000041namespace {
42class ELFObjectWriter : public MCObjectWriter {
43 protected:
44
45 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
46 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
47 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
48 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
49 bool Used, bool Renamed);
50 static bool isLocal(const MCSymbolData &Data, bool isSignature,
51 bool isUsedInReloc);
52 static bool IsELFMetaDataSection(const MCSectionData &SD);
53 static uint64_t DataSectionSize(const MCSectionData &SD);
54 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
55 const MCSectionData &SD);
56 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
57 const MCSectionData &SD);
58
59 void WriteDataSectionData(MCAssembler &Asm,
60 const MCAsmLayout &Layout,
61 const MCSectionELF &Section);
62
63 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
64 return Kind == X86::reloc_riprel_4byte ||
65 Kind == X86::reloc_riprel_4byte_movq_load;
66 }*/
67
68 /// ELFSymbolData - Helper struct for containing some precomputed
69 /// information on symbols.
70 struct ELFSymbolData {
71 MCSymbolData *SymbolData;
72 uint64_t StringIndex;
73 uint32_t SectionIndex;
74
75 // Support lexicographic sorting.
76 bool operator<(const ELFSymbolData &RHS) const {
77 if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
78 return true;
79 if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
80 return false;
81 return SymbolData->getSymbol().getName() <
82 RHS.SymbolData->getSymbol().getName();
83 }
84 };
85
Rafael Espindola3963d612011-12-22 03:38:00 +000086 /// The target specific ELF writer instance.
87 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
88
89 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
90 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
91 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
92
93 llvm::DenseMap<const MCSectionData*,
94 std::vector<ELFRelocationEntry> > Relocations;
95 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
96
97 /// @}
98 /// @name Symbol Table Data
99 /// @{
100
101 SmallString<256> StringTable;
102 std::vector<ELFSymbolData> LocalSymbolData;
103 std::vector<ELFSymbolData> ExternalSymbolData;
104 std::vector<ELFSymbolData> UndefinedSymbolData;
105
106 /// @}
107
108 bool NeedsGOT;
109
110 bool NeedsSymtabShndx;
111
112 // This holds the symbol table index of the last local symbol.
113 unsigned LastLocalSymbolIndex;
114 // This holds the .strtab section index.
115 unsigned StringTableIndex;
116 // This holds the .symtab section index.
117 unsigned SymbolTableIndex;
118
119 unsigned ShstrtabIndex;
120
121
122 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
123 const MCValue &Target,
124 const MCFragment &F,
125 const MCFixup &Fixup,
126 bool IsPCRel) const;
127
128 // TargetObjectWriter wrappers.
129 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
130 const MCValue &Target,
131 const MCFragment &F,
132 const MCFixup &Fixup,
133 bool IsPCRel) const {
134 return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
135 }
136
137 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
138 bool hasRelocationAddend() const {
139 return TargetObjectWriter->hasRelocationAddend();
140 }
141 unsigned getEFlags() const {
142 return TargetObjectWriter->getEFlags();
143 }
144 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
145 bool IsPCRel, bool IsRelocWithSymbol,
146 int64_t Addend) const {
147 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
148 IsRelocWithSymbol, Addend);
149 }
150
151
152 public:
153 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
154 raw_ostream &_OS, bool IsLittleEndian)
155 : MCObjectWriter(_OS, IsLittleEndian),
156 TargetObjectWriter(MOTW),
157 NeedsGOT(false), NeedsSymtabShndx(false){
158 }
159
160 virtual ~ELFObjectWriter();
161
162 void WriteWord(uint64_t W) {
163 if (is64Bit())
164 Write64(W);
165 else
166 Write32(W);
167 }
168
169 void StringLE16(char *buf, uint16_t Value) {
170 buf[0] = char(Value >> 0);
171 buf[1] = char(Value >> 8);
172 }
173
174 void StringLE32(char *buf, uint32_t Value) {
175 StringLE16(buf, uint16_t(Value >> 0));
176 StringLE16(buf + 2, uint16_t(Value >> 16));
177 }
178
179 void StringLE64(char *buf, uint64_t Value) {
180 StringLE32(buf, uint32_t(Value >> 0));
181 StringLE32(buf + 4, uint32_t(Value >> 32));
182 }
183
184 void StringBE16(char *buf ,uint16_t Value) {
185 buf[0] = char(Value >> 8);
186 buf[1] = char(Value >> 0);
187 }
188
189 void StringBE32(char *buf, uint32_t Value) {
190 StringBE16(buf, uint16_t(Value >> 16));
191 StringBE16(buf + 2, uint16_t(Value >> 0));
192 }
193
194 void StringBE64(char *buf, uint64_t Value) {
195 StringBE32(buf, uint32_t(Value >> 32));
196 StringBE32(buf + 4, uint32_t(Value >> 0));
197 }
198
199 void String8(MCDataFragment &F, uint8_t Value) {
200 char buf[1];
201 buf[0] = Value;
202 F.getContents() += StringRef(buf, 1);
203 }
204
205 void String16(MCDataFragment &F, uint16_t Value) {
206 char buf[2];
207 if (isLittleEndian())
208 StringLE16(buf, Value);
209 else
210 StringBE16(buf, Value);
211 F.getContents() += StringRef(buf, 2);
212 }
213
214 void String32(MCDataFragment &F, uint32_t Value) {
215 char buf[4];
216 if (isLittleEndian())
217 StringLE32(buf, Value);
218 else
219 StringBE32(buf, Value);
220 F.getContents() += StringRef(buf, 4);
221 }
222
223 void String64(MCDataFragment &F, uint64_t Value) {
224 char buf[8];
225 if (isLittleEndian())
226 StringLE64(buf, Value);
227 else
228 StringBE64(buf, Value);
229 F.getContents() += StringRef(buf, 8);
230 }
231
232 void WriteHeader(uint64_t SectionDataSize,
233 unsigned NumberOfSections);
234
235 void WriteSymbolEntry(MCDataFragment *SymtabF,
236 MCDataFragment *ShndxF,
237 uint64_t name, uint8_t info,
238 uint64_t value, uint64_t size,
239 uint8_t other, uint32_t shndx,
240 bool Reserved);
241
242 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
243 ELFSymbolData &MSD,
244 const MCAsmLayout &Layout);
245
246 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
247 void WriteSymbolTable(MCDataFragment *SymtabF,
248 MCDataFragment *ShndxF,
249 const MCAssembler &Asm,
250 const MCAsmLayout &Layout,
251 const SectionIndexMapTy &SectionIndexMap);
252
253 virtual void RecordRelocation(const MCAssembler &Asm,
254 const MCAsmLayout &Layout,
255 const MCFragment *Fragment,
256 const MCFixup &Fixup,
257 MCValue Target, uint64_t &FixedValue);
258
259 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
260 const MCSymbol *S);
261
262 // Map from a group section to the signature symbol
263 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
264 // Map from a signature symbol to the group section
265 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
266 // Map from a section to the section with the relocations
267 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
268 // Map from a section to its offset
269 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
270
271 /// ComputeSymbolTable - Compute the symbol table data
272 ///
Rafael Espindola828ea382012-08-27 16:04:24 +0000273 /// \param Asm - The assembler.
274 /// \param SectionIndexMap - Maps a section to its index.
275 /// \param RevGroupMap - Maps a signature symbol to the group section.
276 /// \param NumRegularSections - Number of non-relocation sections.
Rafael Espindola3963d612011-12-22 03:38:00 +0000277 void ComputeSymbolTable(MCAssembler &Asm,
278 const SectionIndexMapTy &SectionIndexMap,
279 RevGroupMapTy RevGroupMap,
280 unsigned NumRegularSections);
281
282 void ComputeIndexMap(MCAssembler &Asm,
283 SectionIndexMapTy &SectionIndexMap,
284 const RelMapTy &RelMap);
285
286 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
287 RelMapTy &RelMap);
288
289 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
290 const RelMapTy &RelMap);
291
292 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
293 SectionIndexMapTy &SectionIndexMap,
294 const RelMapTy &RelMap);
295
296 // Create the sections that show up in the symbol table. Currently
297 // those are the .note.GNU-stack section and the group sections.
298 void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
299 GroupMapTy &GroupMap,
300 RevGroupMapTy &RevGroupMap,
301 SectionIndexMapTy &SectionIndexMap,
302 const RelMapTy &RelMap);
303
304 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
305 const MCAsmLayout &Layout);
306
307 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
308 const MCAsmLayout &Layout,
309 const SectionIndexMapTy &SectionIndexMap,
310 const SectionOffsetMapTy &SectionOffsetMap);
311
312 void ComputeSectionOrder(MCAssembler &Asm,
313 std::vector<const MCSectionELF*> &Sections);
314
315 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
316 uint64_t Address, uint64_t Offset,
317 uint64_t Size, uint32_t Link, uint32_t Info,
318 uint64_t Alignment, uint64_t EntrySize);
319
320 void WriteRelocationsFragment(const MCAssembler &Asm,
321 MCDataFragment *F,
322 const MCSectionData *SD);
323
324 virtual bool
325 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
326 const MCSymbolData &DataA,
327 const MCFragment &FB,
328 bool InSet,
329 bool IsPCRel) const;
330
331 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
332 void WriteSection(MCAssembler &Asm,
333 const SectionIndexMapTy &SectionIndexMap,
334 uint32_t GroupSymbolIndex,
335 uint64_t Offset, uint64_t Size, uint64_t Alignment,
336 const MCSectionELF &Section);
337 };
338}
339
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000340bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
341 const MCFixupKindInfo &FKI =
342 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
343
344 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
345}
346
347bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
348 switch (Variant) {
349 default:
350 return false;
351 case MCSymbolRefExpr::VK_GOT:
352 case MCSymbolRefExpr::VK_PLT:
353 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000354 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000355 case MCSymbolRefExpr::VK_TPOFF:
356 case MCSymbolRefExpr::VK_TLSGD:
357 case MCSymbolRefExpr::VK_GOTTPOFF:
358 case MCSymbolRefExpr::VK_INDNTPOFF:
359 case MCSymbolRefExpr::VK_NTPOFF:
360 case MCSymbolRefExpr::VK_GOTNTPOFF:
361 case MCSymbolRefExpr::VK_TLSLDM:
362 case MCSymbolRefExpr::VK_DTPOFF:
363 case MCSymbolRefExpr::VK_TLSLD:
364 return true;
365 }
366}
367
Jason W Kimd3443e92010-11-15 16:18:39 +0000368ELFObjectWriter::~ELFObjectWriter()
369{}
370
Matt Fleming3565a062010-08-16 18:57:57 +0000371// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000372void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
373 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000374 // ELF Header
375 // ----------
376 //
377 // Note
378 // ----
379 // emitWord method behaves differently for ELF32 and ELF64, writing
380 // 4 bytes in the former and 8 in the latter.
381
382 Write8(0x7f); // e_ident[EI_MAG0]
383 Write8('E'); // e_ident[EI_MAG1]
384 Write8('L'); // e_ident[EI_MAG2]
385 Write8('F'); // e_ident[EI_MAG3]
386
Rafael Espindolabff66a82010-12-18 03:27:34 +0000387 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000388
389 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000390 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000391
392 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000393 // e_ident[EI_OSABI]
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000394 Write8(TargetObjectWriter->getOSABI());
Matt Fleming3565a062010-08-16 18:57:57 +0000395 Write8(0); // e_ident[EI_ABIVERSION]
396
397 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
398
399 Write16(ELF::ET_REL); // e_type
400
Rafael Espindolabff66a82010-12-18 03:27:34 +0000401 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000402
403 Write32(ELF::EV_CURRENT); // e_version
404 WriteWord(0); // e_entry, no entry point in .o file
405 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000406 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000407 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000408
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000409 // e_flags = whatever the target wants
Rafael Espindolae8526d02011-12-21 20:09:46 +0000410 Write32(getEFlags());
Matt Fleming3565a062010-08-16 18:57:57 +0000411
412 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000413 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000414
415 Write16(0); // e_phentsize = prog header entry size
416 Write16(0); // e_phnum = # prog header entries = 0
417
418 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000419 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000420
421 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000422 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000423 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000424 else
425 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000426
427 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000428 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000429 Write16(ELF::SHN_XINDEX);
430 else
431 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000432}
433
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000434void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
435 MCDataFragment *ShndxF,
436 uint64_t name,
437 uint8_t info, uint64_t value,
438 uint64_t size, uint8_t other,
439 uint32_t shndx,
440 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000441 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000442 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000443 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000444 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000445 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000446 }
447
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000448 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
449 uint16_t(ELF::SHN_XINDEX) : shndx;
450
Rafael Espindolabff66a82010-12-18 03:27:34 +0000451 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000452 String32(*SymtabF, name); // st_name
453 String8(*SymtabF, info); // st_info
454 String8(*SymtabF, other); // st_other
455 String16(*SymtabF, Index); // st_shndx
456 String64(*SymtabF, value); // st_value
457 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000458 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000459 String32(*SymtabF, name); // st_name
460 String32(*SymtabF, value); // st_value
461 String32(*SymtabF, size); // st_size
462 String8(*SymtabF, info); // st_info
463 String8(*SymtabF, other); // st_other
464 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000465 }
466}
467
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000468uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
469 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000470 if (Data.isCommon() && Data.isExternal())
471 return Data.getCommonAlignment();
472
473 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000474
475 if (Symbol.isAbsolute() && Symbol.isVariable()) {
476 if (const MCExpr *Value = Symbol.getVariableValue()) {
477 int64_t IntValue;
478 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000479 return (uint64_t)IntValue;
Roman Divackyd1491862010-12-20 21:14:39 +0000480 }
481 }
482
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000483 if (!Symbol.isInSection())
484 return 0;
485
Rafael Espindola64695402011-05-16 16:17:21 +0000486
487 if (Data.getFragment()) {
488 if (Data.getFlags() & ELF_Other_ThumbFunc)
489 return Layout.getSymbolOffset(&Data)+1;
490 else
491 return Layout.getSymbolOffset(&Data);
492 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000493
494 return 0;
495}
496
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000497void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
498 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000499 // The presence of symbol versions causes undefined symbols and
500 // versions declared with @@@ to be renamed.
501
502 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
503 ie = Asm.symbol_end(); it != ie; ++it) {
504 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000505 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000506 MCSymbolData &SD = Asm.getSymbolData(Symbol);
507
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000508 // Not an alias.
509 if (&Symbol == &Alias)
510 continue;
511
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000512 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000513 size_t Pos = AliasName.find('@');
514 if (Pos == StringRef::npos)
515 continue;
516
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000517 // Aliases defined with .symvar copy the binding from the symbol they alias.
518 // This is the first place we are able to copy this information.
519 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000520 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000521
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000522 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000523 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
524 continue;
525
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000526 // FIXME: produce a better error message.
527 if (Symbol.isUndefined() && Rest.startswith("@@") &&
528 !Rest.startswith("@@@"))
529 report_fatal_error("A @@ version cannot be undefined");
530
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000531 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000532 }
533}
534
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000535void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
536 MCDataFragment *ShndxF,
537 ELFSymbolData &MSD,
538 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000539 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000540 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000541 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000542
Rafael Espindola7be2c332010-10-31 00:16:26 +0000543 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
544 Data.getSymbol().isVariable();
545
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000546 uint8_t Binding = MCELF::GetBinding(OrigData);
547 uint8_t Visibility = MCELF::GetVisibility(OrigData);
548 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000549
550 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
551 uint8_t Other = Visibility;
552
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000553 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000554 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000555
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000556 assert(!(Data.isCommon() && !Data.isExternal()));
557
Rafael Espindolaf0121242010-12-22 16:03:00 +0000558 const MCExpr *ESize = Data.getSize();
559 if (ESize) {
560 int64_t Res;
561 if (!ESize->EvaluateAsAbsolute(Res, Layout))
562 report_fatal_error("Size expression must be absolute.");
563 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000564 }
565
566 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000567 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
568 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000569}
570
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000571void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
572 MCDataFragment *ShndxF,
573 const MCAssembler &Asm,
574 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000575 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000576 // The string table must be emitted first because we need the index
577 // into the string table for all the symbol names.
578 assert(StringTable.size() && "Missing string table");
579
580 // FIXME: Make sure the start of the symbol table is aligned.
581
582 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000583 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000584
585 // Write the symbol table entries.
586 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
587 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
588 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000589 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000590 }
591
Rafael Espindola71859c62010-09-16 19:46:31 +0000592 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000593 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
594 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000595 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000596 static_cast<const MCSectionELF&>(i->getSection());
597 if (Section.getType() == ELF::SHT_RELA ||
598 Section.getType() == ELF::SHT_REL ||
599 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000600 Section.getType() == ELF::SHT_SYMTAB ||
601 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000602 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000603 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000604 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
605 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000606 LastLocalSymbolIndex++;
607 }
Matt Fleming3565a062010-08-16 18:57:57 +0000608
609 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
610 ELFSymbolData &MSD = ExternalSymbolData[i];
611 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000612 assert(((Data.getFlags() & ELF_STB_Global) ||
613 (Data.getFlags() & ELF_STB_Weak)) &&
614 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000615 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000616 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000617 LastLocalSymbolIndex++;
618 }
619
620 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
621 ELFSymbolData &MSD = UndefinedSymbolData[i];
622 MCSymbolData &Data = *MSD.SymbolData;
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
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000629const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
630 const MCValue &Target,
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000631 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +0000632 const MCFixup &Fixup,
633 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000634 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000635 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
636 const MCSymbol *Renamed = Renames.lookup(&Symbol);
637 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000638
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000639 if (ASymbol.isUndefined()) {
640 if (Renamed)
641 return Renamed;
642 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000643 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000644
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000645 if (SD.isExternal()) {
646 if (Renamed)
647 return Renamed;
648 return &Symbol;
649 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000650
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000651 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000652 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000653 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000654
Rafael Espindola25958732010-11-24 21:57:39 +0000655 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000656 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000657
Rafael Espindola25958732010-11-24 21:57:39 +0000658 if (secKind.isThreadLocal()) {
659 if (Renamed)
660 return Renamed;
661 return &Symbol;
662 }
663
Rafael Espindola8cecf252010-10-06 16:23:36 +0000664 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000665 const MCSectionELF &Sec2 =
666 static_cast<const MCSectionELF&>(F.getParent()->getSection());
667
Rafael Espindola8cecf252010-10-06 16:23:36 +0000668 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000669 (Kind == MCSymbolRefExpr::VK_PLT ||
670 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000671 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000672 if (Renamed)
673 return Renamed;
674 return &Symbol;
675 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000676
Rafael Espindola1c130262011-01-23 04:43:11 +0000677 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000678 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000679 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000680 if (Renamed)
681 return Renamed;
682 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000683 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000684
Jason W Kime964d112011-05-11 22:53:06 +0000685 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
686
Rafael Espindola73ffea42010-09-25 05:42:19 +0000687}
688
Matt Fleming3565a062010-08-16 18:57:57 +0000689
Jason W Kim56a39902010-12-06 21:57:34 +0000690void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
691 const MCAsmLayout &Layout,
692 const MCFragment *Fragment,
693 const MCFixup &Fixup,
694 MCValue Target,
695 uint64_t &FixedValue) {
696 int64_t Addend = 0;
697 int Index = 0;
698 int64_t Value = Target.getConstant();
699 const MCSymbol *RelocSymbol = NULL;
700
Rafael Espindola127a6a42010-12-17 07:28:17 +0000701 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000702 if (!Target.isAbsolute()) {
703 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
704 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000705 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000706
707 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
708 const MCSymbol &SymbolB = RefB->getSymbol();
709 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
710 IsPCRel = true;
711
712 // Offset of the symbol in the section
713 int64_t a = Layout.getSymbolOffset(&SDB);
714
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000715 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000716 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
717 Value += b - a;
718 }
719
720 if (!RelocSymbol) {
721 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
722 MCFragment *F = SD.getFragment();
723
724 Index = F->getParent()->getOrdinal() + 1;
725
726 // Offset of the symbol in the section
727 Value += Layout.getSymbolOffset(&SD);
728 } else {
729 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
730 WeakrefUsedInReloc.insert(RelocSymbol);
731 else
732 UsedInReloc.insert(RelocSymbol);
733 Index = -1;
734 }
735 Addend = Value;
736 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000737 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000738 Value = 0;
739 }
740
741 FixedValue = Value;
742 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
743 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000744 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
745 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
746 if (RelocNeedsGOT(Modifier))
747 NeedsGOT = true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000748
Jason W Kim56a39902010-12-06 21:57:34 +0000749 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
750 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000751
Rafael Espindolaf3a86fb2011-12-22 01:57:09 +0000752 // FIXME: no tests cover this. Is adjustFixupOffset dead code?
753 TargetObjectWriter->adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000754
Rafael Espindolabff66a82010-12-18 03:27:34 +0000755 if (!hasRelocationAddend())
756 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000757
758 if (is64Bit())
759 assert(isInt<64>(Addend));
760 else
761 assert(isInt<32>(Addend));
762
Akira Hatanaka00ca8882012-03-23 23:06:45 +0000763 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
Jason W Kim56a39902010-12-06 21:57:34 +0000764 Relocations[Fragment->getParent()].push_back(ERE);
765}
766
767
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000768uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000769ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
770 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000771 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000772 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000773}
774
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000775bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
776 const MCSymbolData &Data,
777 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000778 if (Data.getFlags() & ELF_Other_Weakref)
779 return false;
780
Rafael Espindolabd701182010-10-19 19:31:37 +0000781 if (Used)
782 return true;
783
Rafael Espindola88182132010-10-27 15:18:17 +0000784 if (Renamed)
785 return false;
786
Rafael Espindola737cd212010-10-05 18:01:23 +0000787 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000788
Rafael Espindolad1798862010-10-29 23:09:31 +0000789 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
790 return true;
791
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000792 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000793 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
794 return false;
795
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000796 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000797 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000798 return false;
799
Rafael Espindola737cd212010-10-05 18:01:23 +0000800 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
801 return false;
802
Rafael Espindolabd701182010-10-19 19:31:37 +0000803 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000804 return false;
805
806 return true;
807}
808
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000809bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
810 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000811 if (Data.isExternal())
812 return false;
813
814 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000815 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000816
817 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
818 if (isSignature && !isUsedInReloc)
819 return true;
820
Rafael Espindola737cd212010-10-05 18:01:23 +0000821 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000822 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000823
824 return true;
825}
826
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000827void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000828 SectionIndexMapTy &SectionIndexMap,
829 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000830 unsigned Index = 1;
831 for (MCAssembler::iterator it = Asm.begin(),
832 ie = Asm.end(); it != ie; ++it) {
833 const MCSectionELF &Section =
834 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000835 if (Section.getType() != ELF::SHT_GROUP)
836 continue;
837 SectionIndexMap[&Section] = Index++;
838 }
839
840 for (MCAssembler::iterator it = Asm.begin(),
841 ie = Asm.end(); it != ie; ++it) {
842 const MCSectionELF &Section =
843 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000844 if (Section.getType() == ELF::SHT_GROUP ||
845 Section.getType() == ELF::SHT_REL ||
846 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000847 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000848 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000849 const MCSectionELF *RelSection = RelMap.lookup(&Section);
850 if (RelSection)
851 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000852 }
853}
854
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000855void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000856 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000857 RevGroupMapTy RevGroupMap,
858 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000859 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000860 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000861 if (NeedsGOT) {
862 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
863 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
864 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
865 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000866 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000867 }
868
Matt Fleming3565a062010-08-16 18:57:57 +0000869 // Index 0 is always the empty string.
870 StringMap<uint64_t> StringIndexMap;
871 StringTable += '\x00';
872
Rafael Espindolad5321da2011-04-07 23:21:52 +0000873 // FIXME: We could optimize suffixes in strtab in the same way we
874 // optimize them in shstrtab.
875
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000876 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000877 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
878 ie = Asm.symbol_end(); it != ie; ++it) {
879 const MCSymbol &Symbol = it->getSymbol();
880
Rafael Espindola484291c2010-11-01 14:28:48 +0000881 bool Used = UsedInReloc.count(&Symbol);
882 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000883 bool isSignature = RevGroupMap.count(&Symbol);
884
885 if (!isInSymtab(Asm, *it,
886 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000887 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000888 continue;
889
Matt Fleming3565a062010-08-16 18:57:57 +0000890 ELFSymbolData MSD;
891 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000892 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000893
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000894 // Undefined symbols are global, but this is the first place we
895 // are able to set it.
896 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000897 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000898 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000899 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
900 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000901 }
902
Rafael Espindola484291c2010-11-01 14:28:48 +0000903 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000904 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000905
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000906 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000907 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000908 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000909 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000910 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000911 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000912 if (isSignature && !Used)
913 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
914 else
915 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000916 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000917 const MCSectionELF &Section =
918 static_cast<const MCSectionELF&>(RefSymbol.getSection());
919 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000920 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
921 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000922 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000923 }
924
Rafael Espindola88182132010-10-27 15:18:17 +0000925 // The @@@ in symbol version is replaced with @ in undefined symbols and
926 // @@ in defined ones.
927 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000928 SmallString<32> Buf;
929
Rafael Espindola88182132010-10-27 15:18:17 +0000930 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000931 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000932 Buf += Name.substr(0, Pos);
933 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
934 Buf += Name.substr(Pos + Skip);
935 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000936 }
937
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000938 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000939 if (!Entry) {
940 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000941 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000942 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000943 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000944 MSD.StringIndex = Entry;
945 if (MSD.SectionIndex == ELF::SHN_UNDEF)
946 UndefinedSymbolData.push_back(MSD);
947 else if (Local)
948 LocalSymbolData.push_back(MSD);
949 else
950 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000951 }
952
953 // Symbols are required to be in lexicographic order.
954 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
955 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
956 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
957
958 // Set the symbol indices. Local symbols must come before all other
959 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000960 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000961 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
962 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000963
964 Index += NumRegularSections;
965
Matt Fleming3565a062010-08-16 18:57:57 +0000966 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
967 ExternalSymbolData[i].SymbolData->setIndex(Index++);
968 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
969 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000970
971 if (NumRegularSections > ELF::SHN_LORESERVE)
972 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000973}
974
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000975void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
976 MCAsmLayout &Layout,
977 RelMapTy &RelMap) {
978 for (MCAssembler::const_iterator it = Asm.begin(),
979 ie = Asm.end(); it != ie; ++it) {
980 const MCSectionData &SD = *it;
981 if (Relocations[&SD].empty())
982 continue;
983
Matt Fleming3565a062010-08-16 18:57:57 +0000984 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000985 const MCSectionELF &Section =
986 static_cast<const MCSectionELF&>(SD.getSection());
987
988 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000989 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000990 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000991
992 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000993 if (hasRelocationAddend())
994 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000995 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000996 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000997
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000998 const MCSectionELF *RelaSection =
999 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
1000 ELF::SHT_RELA : ELF::SHT_REL, 0,
1001 SectionKind::getReadOnly(),
1002 EntrySize, "");
1003 RelMap[&Section] = RelaSection;
1004 Asm.getOrCreateSectionData(*RelaSection);
1005 }
1006}
Matt Fleming3565a062010-08-16 18:57:57 +00001007
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001008void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1009 const RelMapTy &RelMap) {
1010 for (MCAssembler::const_iterator it = Asm.begin(),
1011 ie = Asm.end(); it != ie; ++it) {
1012 const MCSectionData &SD = *it;
1013 const MCSectionELF &Section =
1014 static_cast<const MCSectionELF&>(SD.getSection());
1015
1016 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1017 if (!RelaSection)
1018 continue;
Matt Fleming3565a062010-08-16 18:57:57 +00001019 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001020 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001021
1022 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001023 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +00001024 }
1025}
1026
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001027void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1028 uint64_t Flags, uint64_t Address,
1029 uint64_t Offset, uint64_t Size,
1030 uint32_t Link, uint32_t Info,
1031 uint64_t Alignment,
1032 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001033 Write32(Name); // sh_name: index into string table
1034 Write32(Type); // sh_type
1035 WriteWord(Flags); // sh_flags
1036 WriteWord(Address); // sh_addr
1037 WriteWord(Offset); // sh_offset
1038 WriteWord(Size); // sh_size
1039 Write32(Link); // sh_link
1040 Write32(Info); // sh_info
1041 WriteWord(Alignment); // sh_addralign
1042 WriteWord(EntrySize); // sh_entsize
1043}
1044
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001045void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1046 MCDataFragment *F,
1047 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001048 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka00ca8882012-03-23 23:06:45 +00001049
1050 // Sort the relocation entries. Most targets just sort by r_offset, but some
1051 // (e.g., MIPS) have additional constraints.
1052 TargetObjectWriter->sortRelocs(Asm, Relocs);
Matt Fleming3565a062010-08-16 18:57:57 +00001053
1054 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1055 ELFRelocationEntry entry = Relocs[e - i - 1];
1056
Rafael Espindola12203cc2010-11-21 00:48:25 +00001057 if (!entry.Index)
1058 ;
1059 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001060 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1061 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001062 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001063 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001064 String64(*F, entry.r_offset);
Jack Carter93ee2862012-06-27 22:28:30 +00001065 if (TargetObjectWriter->isN64()) {
1066 String32(*F, entry.Index);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001067
Jack Carter93ee2862012-06-27 22:28:30 +00001068 String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1069 String8(*F, TargetObjectWriter->getRType3(entry.Type));
1070 String8(*F, TargetObjectWriter->getRType2(entry.Type));
1071 String8(*F, TargetObjectWriter->getRType(entry.Type));
1072 }
1073 else {
1074 struct ELF::Elf64_Rela ERE64;
1075 ERE64.setSymbolAndType(entry.Index, entry.Type);
1076 String64(*F, ERE64.r_info);
1077 }
Rafael Espindolabff66a82010-12-18 03:27:34 +00001078 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001079 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001080 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001081 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001082
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001083 struct ELF::Elf32_Rela ERE32;
1084 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001085 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001086
Rafael Espindolabff66a82010-12-18 03:27:34 +00001087 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001088 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001089 }
Matt Fleming3565a062010-08-16 18:57:57 +00001090 }
1091}
1092
Rafael Espindolad5321da2011-04-07 23:21:52 +00001093static int compareBySuffix(const void *a, const void *b) {
1094 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
1095 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
1096 const StringRef &NameA = secA->getSectionName();
1097 const StringRef &NameB = secB->getSectionName();
1098 const unsigned sizeA = NameA.size();
1099 const unsigned sizeB = NameB.size();
1100 const unsigned len = std::min(sizeA, sizeB);
1101 for (unsigned int i = 0; i < len; ++i) {
1102 char ca = NameA[sizeA - i - 1];
1103 char cb = NameB[sizeB - i - 1];
1104 if (ca != cb)
1105 return cb - ca;
1106 }
1107
1108 return sizeB - sizeA;
1109}
1110
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001111void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1112 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001113 SectionIndexMapTy &SectionIndexMap,
1114 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001115 MCContext &Ctx = Asm.getContext();
1116 MCDataFragment *F;
1117
Rafael Espindolabff66a82010-12-18 03:27:34 +00001118 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001119
Rafael Espindola38738bf2010-09-22 19:04:41 +00001120 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001121 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001122 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001123 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001124 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1125 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +00001126
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001127 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001128 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1129 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001130 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001131 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001132 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001133
1134 MCSectionData *SymtabShndxSD = NULL;
1135
1136 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001137 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001138 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001139 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001140 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1141 SymtabShndxSD->setAlignment(4);
1142 }
Matt Fleming3565a062010-08-16 18:57:57 +00001143
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001144 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001145 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001146 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001147 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1148 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001149
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001150 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1151
1152 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1153 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1154 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +00001155
1156 // Symbol table
1157 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001158 MCDataFragment *ShndxF = NULL;
1159 if (NeedsSymtabShndx) {
1160 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001161 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001162 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001163
Matt Fleming3565a062010-08-16 18:57:57 +00001164 F = new MCDataFragment(&StrtabSD);
1165 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001166
Matt Fleming3565a062010-08-16 18:57:57 +00001167 F = new MCDataFragment(&ShstrtabSD);
1168
Rafael Espindolad5321da2011-04-07 23:21:52 +00001169 std::vector<const MCSectionELF*> Sections;
1170 for (MCAssembler::const_iterator it = Asm.begin(),
1171 ie = Asm.end(); it != ie; ++it) {
1172 const MCSectionELF &Section =
1173 static_cast<const MCSectionELF&>(it->getSection());
1174 Sections.push_back(&Section);
1175 }
1176 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1177
Matt Fleming3565a062010-08-16 18:57:57 +00001178 // Section header string table.
1179 //
1180 // The first entry of a string table holds a null character so skip
1181 // section 0.
1182 uint64_t Index = 1;
1183 F->getContents() += '\x00';
1184
Rafael Espindolad5321da2011-04-07 23:21:52 +00001185 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1186 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +00001187
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001188 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +00001189 if (I != 0) {
1190 StringRef PreviousName = Sections[I - 1]->getSectionName();
1191 if (PreviousName.endswith(Name)) {
1192 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1193 continue;
1194 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001195 }
Matt Fleming3565a062010-08-16 18:57:57 +00001196 // Remember the index into the string table so we can write it
1197 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001198 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001199
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001200 Index += Name.size() + 1;
1201 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001202 F->getContents() += '\x00';
1203 }
Rafael Espindola70703872010-09-30 02:22:20 +00001204}
1205
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001206void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1207 MCAsmLayout &Layout,
1208 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001209 RevGroupMapTy &RevGroupMap,
1210 SectionIndexMapTy &SectionIndexMap,
1211 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001212 // Create the .note.GNU-stack section if needed.
1213 MCContext &Ctx = Asm.getContext();
1214 if (Asm.getNoExecStack()) {
1215 const MCSectionELF *GnuStackSection =
1216 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1217 SectionKind::getReadOnly());
1218 Asm.getOrCreateSectionData(*GnuStackSection);
1219 }
1220
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001221 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001222 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1223 it != ie; ++it) {
1224 const MCSectionELF &Section =
1225 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001226 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001227 continue;
1228
1229 const MCSymbol *SignatureSymbol = Section.getGroup();
1230 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001231 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001232 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001233 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001234 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1235 Data.setAlignment(4);
1236 MCDataFragment *F = new MCDataFragment(&Data);
1237 String32(*F, ELF::GRP_COMDAT);
1238 }
1239 GroupMap[Group] = SignatureSymbol;
1240 }
1241
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001242 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1243
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001244 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001245 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001246 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001247 const MCSectionELF &Section =
1248 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001249 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001250 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001251 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001252 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1253 // FIXME: we could use the previous fragment
1254 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001255 unsigned Index = SectionIndexMap.lookup(&Section);
1256 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001257 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001258}
1259
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001260void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1261 const SectionIndexMapTy &SectionIndexMap,
1262 uint32_t GroupSymbolIndex,
1263 uint64_t Offset, uint64_t Size,
1264 uint64_t Alignment,
1265 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001266 uint64_t sh_link = 0;
1267 uint64_t sh_info = 0;
1268
1269 switch(Section.getType()) {
1270 case ELF::SHT_DYNAMIC:
1271 sh_link = SectionStringTableIndex[&Section];
1272 sh_info = 0;
1273 break;
1274
1275 case ELF::SHT_REL:
1276 case ELF::SHT_RELA: {
1277 const MCSectionELF *SymtabSection;
1278 const MCSectionELF *InfoSection;
1279 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1280 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001281 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001282 sh_link = SectionIndexMap.lookup(SymtabSection);
1283 assert(sh_link && ".symtab not found");
1284
1285 // Remove ".rel" and ".rela" prefixes.
1286 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1287 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1288
1289 InfoSection = Asm.getContext().getELFSection(SectionName,
1290 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001291 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001292 sh_info = SectionIndexMap.lookup(InfoSection);
1293 break;
1294 }
1295
1296 case ELF::SHT_SYMTAB:
1297 case ELF::SHT_DYNSYM:
1298 sh_link = StringTableIndex;
1299 sh_info = LastLocalSymbolIndex;
1300 break;
1301
1302 case ELF::SHT_SYMTAB_SHNDX:
1303 sh_link = SymbolTableIndex;
1304 break;
1305
1306 case ELF::SHT_PROGBITS:
1307 case ELF::SHT_STRTAB:
1308 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001309 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001310 case ELF::SHT_NULL:
1311 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001312 case ELF::SHT_INIT_ARRAY:
1313 case ELF::SHT_FINI_ARRAY:
1314 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001315 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001316 // Nothing to do.
1317 break;
1318
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001319 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001320 sh_link = SymbolTableIndex;
1321 sh_info = GroupSymbolIndex;
1322 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001323
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001324 default:
1325 assert(0 && "FIXME: sh_type value not supported!");
1326 break;
1327 }
1328
1329 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1330 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1331 Alignment, Section.getEntrySize());
1332}
1333
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001334bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001335 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001336 !SD.getSection().isVirtualSection();
1337}
1338
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001339uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001340 uint64_t Ret = 0;
1341 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1342 ++i) {
1343 const MCFragment &F = *i;
1344 assert(F.getKind() == MCFragment::FT_Data);
1345 Ret += cast<MCDataFragment>(F).getContents().size();
1346 }
1347 return Ret;
1348}
1349
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001350uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1351 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001352 if (IsELFMetaDataSection(SD))
1353 return DataSectionSize(SD);
1354 return Layout.getSectionFileSize(&SD);
1355}
1356
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001357uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1358 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001359 if (IsELFMetaDataSection(SD))
1360 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001361 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001362}
1363
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001364void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1365 const MCAsmLayout &Layout,
1366 const MCSectionELF &Section) {
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001367 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1368
Benjamin Kramer263109d2012-01-20 14:42:32 +00001369 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001370 WriteZeros(Padding);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001371
1372 if (IsELFMetaDataSection(SD)) {
1373 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1374 ++i) {
1375 const MCFragment &F = *i;
1376 assert(F.getKind() == MCFragment::FT_Data);
1377 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1378 }
1379 } else {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001380 Asm.writeSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001381 }
1382}
1383
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001384void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1385 const GroupMapTy &GroupMap,
1386 const MCAsmLayout &Layout,
1387 const SectionIndexMapTy &SectionIndexMap,
1388 const SectionOffsetMapTy &SectionOffsetMap) {
1389 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001390
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001391 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001392 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001393
1394 for (SectionIndexMapTy::const_iterator i=
1395 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1396 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001397 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001398 }
1399
Matt Fleming3565a062010-08-16 18:57:57 +00001400 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001401 uint64_t FirstSectionSize =
1402 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1403 uint32_t FirstSectionLink =
1404 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1405 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001406
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001407 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001408 const MCSectionELF &Section = *Sections[i];
1409 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1410 uint32_t GroupSymbolIndex;
1411 if (Section.getType() != ELF::SHT_GROUP)
1412 GroupSymbolIndex = 0;
1413 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001414 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1415 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001416
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001417 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001418
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001419 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001420 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001421 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001422 }
1423}
1424
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001425void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1426 std::vector<const MCSectionELF*> &Sections) {
1427 for (MCAssembler::iterator it = Asm.begin(),
1428 ie = Asm.end(); it != ie; ++it) {
1429 const MCSectionELF &Section =
1430 static_cast<const MCSectionELF &>(it->getSection());
1431 if (Section.getType() == ELF::SHT_GROUP)
1432 Sections.push_back(&Section);
1433 }
1434
1435 for (MCAssembler::iterator it = Asm.begin(),
1436 ie = Asm.end(); it != ie; ++it) {
1437 const MCSectionELF &Section =
1438 static_cast<const MCSectionELF &>(it->getSection());
1439 if (Section.getType() != ELF::SHT_GROUP &&
1440 Section.getType() != ELF::SHT_REL &&
1441 Section.getType() != ELF::SHT_RELA)
1442 Sections.push_back(&Section);
1443 }
1444
1445 for (MCAssembler::iterator it = Asm.begin(),
1446 ie = Asm.end(); it != ie; ++it) {
1447 const MCSectionELF &Section =
1448 static_cast<const MCSectionELF &>(it->getSection());
1449 if (Section.getType() == ELF::SHT_REL ||
1450 Section.getType() == ELF::SHT_RELA)
1451 Sections.push_back(&Section);
1452 }
1453}
1454
1455void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1456 const MCAsmLayout &Layout) {
1457 GroupMapTy GroupMap;
1458 RevGroupMapTy RevGroupMap;
1459 SectionIndexMapTy SectionIndexMap;
1460
1461 unsigned NumUserSections = Asm.size();
1462
1463 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1464 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1465
1466 const unsigned NumUserAndRelocSections = Asm.size();
1467 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1468 RevGroupMap, SectionIndexMap, RelMap);
1469 const unsigned AllSections = Asm.size();
1470 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1471
1472 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1473
1474 // Compute symbol table information.
1475 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1476
1477
1478 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1479
1480 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1481 const_cast<MCAsmLayout&>(Layout),
1482 SectionIndexMap,
1483 RelMap);
1484
1485 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1486 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1487 sizeof(ELF::Elf32_Ehdr);
1488 uint64_t FileOff = HeaderSize;
1489
1490 std::vector<const MCSectionELF*> Sections;
1491 ComputeSectionOrder(Asm, Sections);
1492 unsigned NumSections = Sections.size();
1493 SectionOffsetMapTy SectionOffsetMap;
1494 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1495 const MCSectionELF &Section = *Sections[i];
1496 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1497
1498 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1499
1500 // Remember the offset into the file for this section.
1501 SectionOffsetMap[&Section] = FileOff;
1502
1503 // Get the size of the section in the output file (including padding).
1504 FileOff += GetSectionFileSize(Layout, SD);
1505 }
1506
1507 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1508
1509 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1510
1511 uint64_t SectionHeaderEntrySize = is64Bit() ?
1512 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1513 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1514
1515 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1516 const MCSectionELF &Section = *Sections[i];
1517 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1518
1519 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1520
1521 // Remember the offset into the file for this section.
1522 SectionOffsetMap[&Section] = FileOff;
1523
1524 // Get the size of the section in the output file (including padding).
1525 FileOff += GetSectionFileSize(Layout, SD);
1526 }
1527
1528 // Write out the ELF header ...
1529 WriteHeader(SectionHeaderOffset, NumSections + 1);
1530
1531 // ... then the regular sections ...
1532 // + because of .shstrtab
1533 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1534 WriteDataSectionData(Asm, Layout, *Sections[i]);
1535
Benjamin Kramer263109d2012-01-20 14:42:32 +00001536 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001537 WriteZeros(Padding);
1538
1539 // ... then the section header table ...
1540 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1541 SectionOffsetMap);
1542
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001543 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001544 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1545 WriteDataSectionData(Asm, Layout, *Sections[i]);
1546}
1547
Eli Friedman78c1e172011-03-03 07:24:36 +00001548bool
1549ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1550 const MCSymbolData &DataA,
1551 const MCFragment &FB,
1552 bool InSet,
1553 bool IsPCRel) const {
1554 if (DataA.getFlags() & ELF_STB_Weak)
1555 return false;
1556 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1557 Asm, DataA, FB,InSet, IsPCRel);
1558}
1559
Rafael Espindola6024c972010-12-17 17:45:22 +00001560MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1561 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001562 bool IsLittleEndian) {
Rafael Espindola7bd27802011-12-22 03:24:43 +00001563 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolaedae8e12011-12-21 17:30:17 +00001564}