blob: 6b81fc97d98f67c2fbdee0fc3a9f47cb8b61bd9f [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
Jan Sjödin24b17c62011-03-03 14:52:12 +000014#include "ELFObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
Matt Fleming3565a062010-08-16 18:57:57 +000018#include "llvm/MC/MCAsmLayout.h"
19#include "llvm/MC/MCContext.h"
Matt Fleming3565a062010-08-16 18:57:57 +000020#include "llvm/MC/MCExpr.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCSectionELF.h"
Matt Fleming3565a062010-08-16 18:57:57 +000022#include "llvm/MC/MCValue.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/ELF.h"
26#include "llvm/Target/TargetAsmBackend.h"
Jason W Kim953a2a32011-02-07 01:11:15 +000027#include "llvm/ADT/StringSwitch.h"
Jason W Kime964d112011-05-11 22:53:06 +000028#include "llvm/Support/CommandLine.h"
29#include "llvm/ADT/Statistic.h"
Matt Fleming3565a062010-08-16 18:57:57 +000030
31#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000032#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000033
34#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
Jason W Kim97c07da2011-05-16 16:35:21 +000040// FIXME: This switch must be removed. Since GNU as does not
41// need a command line switch for doing its wierd thing with PIC,
42// LLVM should not need it either.
43// --
Jason W Kime964d112011-05-11 22:53:06 +000044// Emulate the wierd behavior of GNU-as for relocation types
45namespace llvm {
46cl::opt<bool>
47ForceARMElfPIC("arm-elf-force-pic", cl::Hidden, cl::init(false),
48 cl::desc("Force ELF emitter to emit PIC style relocations"));
49}
50
Jan Sjödin2ddfd952011-02-28 21:45:04 +000051bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
52 const MCFixupKindInfo &FKI =
53 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
54
55 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
56}
57
58bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
59 switch (Variant) {
60 default:
61 return false;
62 case MCSymbolRefExpr::VK_GOT:
63 case MCSymbolRefExpr::VK_PLT:
64 case MCSymbolRefExpr::VK_GOTPCREL:
65 case MCSymbolRefExpr::VK_TPOFF:
66 case MCSymbolRefExpr::VK_TLSGD:
67 case MCSymbolRefExpr::VK_GOTTPOFF:
68 case MCSymbolRefExpr::VK_INDNTPOFF:
69 case MCSymbolRefExpr::VK_NTPOFF:
70 case MCSymbolRefExpr::VK_GOTNTPOFF:
71 case MCSymbolRefExpr::VK_TLSLDM:
72 case MCSymbolRefExpr::VK_DTPOFF:
73 case MCSymbolRefExpr::VK_TLSLD:
74 return true;
75 }
76}
77
Jason W Kimd3443e92010-11-15 16:18:39 +000078ELFObjectWriter::~ELFObjectWriter()
79{}
80
Matt Fleming3565a062010-08-16 18:57:57 +000081// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000082void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
83 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000084 // ELF Header
85 // ----------
86 //
87 // Note
88 // ----
89 // emitWord method behaves differently for ELF32 and ELF64, writing
90 // 4 bytes in the former and 8 in the latter.
91
92 Write8(0x7f); // e_ident[EI_MAG0]
93 Write8('E'); // e_ident[EI_MAG1]
94 Write8('L'); // e_ident[EI_MAG2]
95 Write8('F'); // e_ident[EI_MAG3]
96
Rafael Espindolabff66a82010-12-18 03:27:34 +000097 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000098
99 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000100 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000101
102 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000103 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000104 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000105 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
106 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
107 default: Write8(ELF::ELFOSABI_NONE); break;
108 }
Matt Fleming3565a062010-08-16 18:57:57 +0000109 Write8(0); // e_ident[EI_ABIVERSION]
110
111 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
112
113 Write16(ELF::ET_REL); // e_type
114
Rafael Espindolabff66a82010-12-18 03:27:34 +0000115 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000116
117 Write32(ELF::EV_CURRENT); // e_version
118 WriteWord(0); // e_entry, no entry point in .o file
119 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000120 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000121 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000122
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000123 // e_flags = whatever the target wants
124 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000125
126 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000127 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000128
129 Write16(0); // e_phentsize = prog header entry size
130 Write16(0); // e_phnum = # prog header entries = 0
131
132 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000133 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000134
135 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000136 if (NumberOfSections >= ELF::SHN_LORESERVE)
137 Write16(0);
138 else
139 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000140
141 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000142 if (NumberOfSections >= ELF::SHN_LORESERVE)
143 Write16(ELF::SHN_XINDEX);
144 else
145 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000146}
147
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000148void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
149 MCDataFragment *ShndxF,
150 uint64_t name,
151 uint8_t info, uint64_t value,
152 uint64_t size, uint8_t other,
153 uint32_t shndx,
154 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000155 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000156 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000157 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000158 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000159 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000160 }
161
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000162 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
163 uint16_t(ELF::SHN_XINDEX) : shndx;
164
Rafael Espindolabff66a82010-12-18 03:27:34 +0000165 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000166 String32(*SymtabF, name); // st_name
167 String8(*SymtabF, info); // st_info
168 String8(*SymtabF, other); // st_other
169 String16(*SymtabF, Index); // st_shndx
170 String64(*SymtabF, value); // st_value
171 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000172 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000173 String32(*SymtabF, name); // st_name
174 String32(*SymtabF, value); // st_value
175 String32(*SymtabF, size); // st_size
176 String8(*SymtabF, info); // st_info
177 String8(*SymtabF, other); // st_other
178 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000179 }
180}
181
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000182uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
183 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000184 if (Data.isCommon() && Data.isExternal())
185 return Data.getCommonAlignment();
186
187 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000188
189 if (Symbol.isAbsolute() && Symbol.isVariable()) {
190 if (const MCExpr *Value = Symbol.getVariableValue()) {
191 int64_t IntValue;
192 if (Value->EvaluateAsAbsolute(IntValue, Layout))
193 return (uint64_t)IntValue;
194 }
195 }
196
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000197 if (!Symbol.isInSection())
198 return 0;
199
Rafael Espindola64695402011-05-16 16:17:21 +0000200
201 if (Data.getFragment()) {
202 if (Data.getFlags() & ELF_Other_ThumbFunc)
203 return Layout.getSymbolOffset(&Data)+1;
204 else
205 return Layout.getSymbolOffset(&Data);
206 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000207
208 return 0;
209}
210
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000211void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
212 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000213 // The presence of symbol versions causes undefined symbols and
214 // versions declared with @@@ to be renamed.
215
216 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
217 ie = Asm.symbol_end(); it != ie; ++it) {
218 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000219 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000220 MCSymbolData &SD = Asm.getSymbolData(Symbol);
221
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000222 // Not an alias.
223 if (&Symbol == &Alias)
224 continue;
225
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000226 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000227 size_t Pos = AliasName.find('@');
228 if (Pos == StringRef::npos)
229 continue;
230
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000231 // Aliases defined with .symvar copy the binding from the symbol they alias.
232 // This is the first place we are able to copy this information.
233 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000234 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000235
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000236 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000237 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
238 continue;
239
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000240 // FIXME: produce a better error message.
241 if (Symbol.isUndefined() && Rest.startswith("@@") &&
242 !Rest.startswith("@@@"))
243 report_fatal_error("A @@ version cannot be undefined");
244
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000245 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000246 }
247}
248
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000249void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
250 MCDataFragment *ShndxF,
251 ELFSymbolData &MSD,
252 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000253 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000254 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000255 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000256
Rafael Espindola7be2c332010-10-31 00:16:26 +0000257 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
258 Data.getSymbol().isVariable();
259
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000260 uint8_t Binding = MCELF::GetBinding(OrigData);
261 uint8_t Visibility = MCELF::GetVisibility(OrigData);
262 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000263
264 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
265 uint8_t Other = Visibility;
266
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000267 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000268 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000269
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000270 assert(!(Data.isCommon() && !Data.isExternal()));
271
Rafael Espindolaf0121242010-12-22 16:03:00 +0000272 const MCExpr *ESize = Data.getSize();
273 if (ESize) {
274 int64_t Res;
275 if (!ESize->EvaluateAsAbsolute(Res, Layout))
276 report_fatal_error("Size expression must be absolute.");
277 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000278 }
279
280 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000281 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
282 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000283}
284
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000285void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
286 MCDataFragment *ShndxF,
287 const MCAssembler &Asm,
288 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000289 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000290 // The string table must be emitted first because we need the index
291 // into the string table for all the symbol names.
292 assert(StringTable.size() && "Missing string table");
293
294 // FIXME: Make sure the start of the symbol table is aligned.
295
296 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000297 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000298
299 // Write the symbol table entries.
300 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
301 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
302 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000303 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000304 }
305
Rafael Espindola71859c62010-09-16 19:46:31 +0000306 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000307 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
308 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000309 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000310 static_cast<const MCSectionELF&>(i->getSection());
311 if (Section.getType() == ELF::SHT_RELA ||
312 Section.getType() == ELF::SHT_REL ||
313 Section.getType() == ELF::SHT_STRTAB ||
314 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000315 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000316 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000317 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000318 LastLocalSymbolIndex++;
319 }
Matt Fleming3565a062010-08-16 18:57:57 +0000320
321 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
322 ELFSymbolData &MSD = ExternalSymbolData[i];
323 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000324 assert(((Data.getFlags() & ELF_STB_Global) ||
325 (Data.getFlags() & ELF_STB_Weak)) &&
326 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000327 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000328 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000329 LastLocalSymbolIndex++;
330 }
331
332 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
333 ELFSymbolData &MSD = UndefinedSymbolData[i];
334 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000335 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000336 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000337 LastLocalSymbolIndex++;
338 }
339}
340
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000341const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
342 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000343 const MCFragment &F,
344 const MCFixup &Fixup,
345 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000346 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000347 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
348 const MCSymbol *Renamed = Renames.lookup(&Symbol);
349 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000350
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000351 if (ASymbol.isUndefined()) {
352 if (Renamed)
353 return Renamed;
354 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000355 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000356
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000357 if (SD.isExternal()) {
358 if (Renamed)
359 return Renamed;
360 return &Symbol;
361 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000362
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000363 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000364 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000365 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000366
Rafael Espindola25958732010-11-24 21:57:39 +0000367 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000368 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000369
Rafael Espindola25958732010-11-24 21:57:39 +0000370 if (secKind.isThreadLocal()) {
371 if (Renamed)
372 return Renamed;
373 return &Symbol;
374 }
375
Rafael Espindola8cecf252010-10-06 16:23:36 +0000376 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000377 const MCSectionELF &Sec2 =
378 static_cast<const MCSectionELF&>(F.getParent()->getSection());
379
Rafael Espindola8cecf252010-10-06 16:23:36 +0000380 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000381 (Kind == MCSymbolRefExpr::VK_PLT ||
382 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000383 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000384 if (Renamed)
385 return Renamed;
386 return &Symbol;
387 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000388
Rafael Espindola1c130262011-01-23 04:43:11 +0000389 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000390 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000391 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000392 if (Renamed)
393 return Renamed;
394 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000395 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000396
Jason W Kime964d112011-05-11 22:53:06 +0000397 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
398
Rafael Espindola73ffea42010-09-25 05:42:19 +0000399}
400
Matt Fleming3565a062010-08-16 18:57:57 +0000401
Jason W Kim56a39902010-12-06 21:57:34 +0000402void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
403 const MCAsmLayout &Layout,
404 const MCFragment *Fragment,
405 const MCFixup &Fixup,
406 MCValue Target,
407 uint64_t &FixedValue) {
408 int64_t Addend = 0;
409 int Index = 0;
410 int64_t Value = Target.getConstant();
411 const MCSymbol *RelocSymbol = NULL;
412
Rafael Espindola127a6a42010-12-17 07:28:17 +0000413 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000414 if (!Target.isAbsolute()) {
415 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
416 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000417 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000418
419 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
420 const MCSymbol &SymbolB = RefB->getSymbol();
421 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
422 IsPCRel = true;
423
424 // Offset of the symbol in the section
425 int64_t a = Layout.getSymbolOffset(&SDB);
426
427 // Ofeset of the relocation in the section
428 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
429 Value += b - a;
430 }
431
432 if (!RelocSymbol) {
433 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
434 MCFragment *F = SD.getFragment();
435
436 Index = F->getParent()->getOrdinal() + 1;
437
438 // Offset of the symbol in the section
439 Value += Layout.getSymbolOffset(&SD);
440 } else {
441 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
442 WeakrefUsedInReloc.insert(RelocSymbol);
443 else
444 UsedInReloc.insert(RelocSymbol);
445 Index = -1;
446 }
447 Addend = Value;
448 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000449 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000450 Value = 0;
451 }
452
453 FixedValue = Value;
454 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
455 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000456
Jason W Kim56a39902010-12-06 21:57:34 +0000457 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
458 Fixup.getOffset();
459
Rafael Espindolabff66a82010-12-18 03:27:34 +0000460 if (!hasRelocationAddend())
461 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000462 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
463 Relocations[Fragment->getParent()].push_back(ERE);
464}
465
466
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000467uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000468ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
469 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000470 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000471 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000472}
473
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000474bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
475 const MCSymbolData &Data,
476 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000477 if (Data.getFlags() & ELF_Other_Weakref)
478 return false;
479
Rafael Espindolabd701182010-10-19 19:31:37 +0000480 if (Used)
481 return true;
482
Rafael Espindola88182132010-10-27 15:18:17 +0000483 if (Renamed)
484 return false;
485
Rafael Espindola737cd212010-10-05 18:01:23 +0000486 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000487
Rafael Espindolad1798862010-10-29 23:09:31 +0000488 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
489 return true;
490
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000491 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000492 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
493 return false;
494
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000495 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000496 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000497 return false;
498
Rafael Espindola737cd212010-10-05 18:01:23 +0000499 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
500 return false;
501
Rafael Espindolabd701182010-10-19 19:31:37 +0000502 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000503 return false;
504
505 return true;
506}
507
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000508bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
509 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000510 if (Data.isExternal())
511 return false;
512
513 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000514 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000515
516 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
517 if (isSignature && !isUsedInReloc)
518 return true;
519
Rafael Espindola737cd212010-10-05 18:01:23 +0000520 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000521 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000522
523 return true;
524}
525
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000526void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000527 SectionIndexMapTy &SectionIndexMap,
528 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000529 unsigned Index = 1;
530 for (MCAssembler::iterator it = Asm.begin(),
531 ie = Asm.end(); it != ie; ++it) {
532 const MCSectionELF &Section =
533 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000534 if (Section.getType() != ELF::SHT_GROUP)
535 continue;
536 SectionIndexMap[&Section] = Index++;
537 }
538
539 for (MCAssembler::iterator it = Asm.begin(),
540 ie = Asm.end(); it != ie; ++it) {
541 const MCSectionELF &Section =
542 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000543 if (Section.getType() == ELF::SHT_GROUP ||
544 Section.getType() == ELF::SHT_REL ||
545 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000546 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000547 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000548 const MCSectionELF *RelSection = RelMap.lookup(&Section);
549 if (RelSection)
550 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000551 }
552}
553
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000554void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000555 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000556 RevGroupMapTy RevGroupMap,
557 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000558 // FIXME: Is this the correct place to do this?
559 if (NeedsGOT) {
560 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
561 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
562 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
563 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000564 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000565 }
566
Matt Fleming3565a062010-08-16 18:57:57 +0000567 // Index 0 is always the empty string.
568 StringMap<uint64_t> StringIndexMap;
569 StringTable += '\x00';
570
Rafael Espindolad5321da2011-04-07 23:21:52 +0000571 // FIXME: We could optimize suffixes in strtab in the same way we
572 // optimize them in shstrtab.
573
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000574 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000575 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
576 ie = Asm.symbol_end(); it != ie; ++it) {
577 const MCSymbol &Symbol = it->getSymbol();
578
Rafael Espindola484291c2010-11-01 14:28:48 +0000579 bool Used = UsedInReloc.count(&Symbol);
580 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000581 bool isSignature = RevGroupMap.count(&Symbol);
582
583 if (!isInSymtab(Asm, *it,
584 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000585 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000586 continue;
587
Matt Fleming3565a062010-08-16 18:57:57 +0000588 ELFSymbolData MSD;
589 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000590 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000591
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000592 // Undefined symbols are global, but this is the first place we
593 // are able to set it.
594 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000595 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000596 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000597 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
598 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000599 }
600
Rafael Espindola484291c2010-11-01 14:28:48 +0000601 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000602 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000603
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000604 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000605 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000606 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000607 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000608 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000609 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000610 if (isSignature && !Used)
611 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
612 else
613 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000614 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000615 const MCSectionELF &Section =
616 static_cast<const MCSectionELF&>(RefSymbol.getSection());
617 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000618 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
619 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000620 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000621 }
622
Rafael Espindola88182132010-10-27 15:18:17 +0000623 // The @@@ in symbol version is replaced with @ in undefined symbols and
624 // @@ in defined ones.
625 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000626 SmallString<32> Buf;
627
Rafael Espindola88182132010-10-27 15:18:17 +0000628 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000629 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000630 Buf += Name.substr(0, Pos);
631 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
632 Buf += Name.substr(Pos + Skip);
633 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000634 }
635
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000636 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000637 if (!Entry) {
638 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000639 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000640 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000641 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000642 MSD.StringIndex = Entry;
643 if (MSD.SectionIndex == ELF::SHN_UNDEF)
644 UndefinedSymbolData.push_back(MSD);
645 else if (Local)
646 LocalSymbolData.push_back(MSD);
647 else
648 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000649 }
650
651 // Symbols are required to be in lexicographic order.
652 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
653 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
654 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
655
656 // Set the symbol indices. Local symbols must come before all other
657 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000658 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000659 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
660 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000661
662 Index += NumRegularSections;
663
Matt Fleming3565a062010-08-16 18:57:57 +0000664 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
665 ExternalSymbolData[i].SymbolData->setIndex(Index++);
666 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
667 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
668}
669
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000670void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
671 MCAsmLayout &Layout,
672 RelMapTy &RelMap) {
673 for (MCAssembler::const_iterator it = Asm.begin(),
674 ie = Asm.end(); it != ie; ++it) {
675 const MCSectionData &SD = *it;
676 if (Relocations[&SD].empty())
677 continue;
678
Matt Fleming3565a062010-08-16 18:57:57 +0000679 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000680 const MCSectionELF &Section =
681 static_cast<const MCSectionELF&>(SD.getSection());
682
683 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000684 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000685 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000686
687 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000688 if (hasRelocationAddend())
689 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000690 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000691 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000692
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000693 const MCSectionELF *RelaSection =
694 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
695 ELF::SHT_RELA : ELF::SHT_REL, 0,
696 SectionKind::getReadOnly(),
697 EntrySize, "");
698 RelMap[&Section] = RelaSection;
699 Asm.getOrCreateSectionData(*RelaSection);
700 }
701}
Matt Fleming3565a062010-08-16 18:57:57 +0000702
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000703void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
704 const RelMapTy &RelMap) {
705 for (MCAssembler::const_iterator it = Asm.begin(),
706 ie = Asm.end(); it != ie; ++it) {
707 const MCSectionData &SD = *it;
708 const MCSectionELF &Section =
709 static_cast<const MCSectionELF&>(SD.getSection());
710
711 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
712 if (!RelaSection)
713 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000714 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000715 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000716
717 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000718 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000719 }
720}
721
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000722void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
723 uint64_t Flags, uint64_t Address,
724 uint64_t Offset, uint64_t Size,
725 uint32_t Link, uint32_t Info,
726 uint64_t Alignment,
727 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000728 Write32(Name); // sh_name: index into string table
729 Write32(Type); // sh_type
730 WriteWord(Flags); // sh_flags
731 WriteWord(Address); // sh_addr
732 WriteWord(Offset); // sh_offset
733 WriteWord(Size); // sh_size
734 Write32(Link); // sh_link
735 Write32(Info); // sh_info
736 WriteWord(Alignment); // sh_addralign
737 WriteWord(EntrySize); // sh_entsize
738}
739
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000740void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
741 MCDataFragment *F,
742 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000743 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
744 // sort by the r_offset just like gnu as does
745 array_pod_sort(Relocs.begin(), Relocs.end());
746
747 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
748 ELFRelocationEntry entry = Relocs[e - i - 1];
749
Rafael Espindola12203cc2010-11-21 00:48:25 +0000750 if (!entry.Index)
751 ;
752 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000753 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
754 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000755 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000756 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000757 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000758
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000759 struct ELF::Elf64_Rela ERE64;
760 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000761 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000762
Rafael Espindolabff66a82010-12-18 03:27:34 +0000763 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000764 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000765 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000766 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000767
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000768 struct ELF::Elf32_Rela ERE32;
769 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000770 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000771
Rafael Espindolabff66a82010-12-18 03:27:34 +0000772 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000773 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000774 }
Matt Fleming3565a062010-08-16 18:57:57 +0000775 }
776}
777
Rafael Espindolad5321da2011-04-07 23:21:52 +0000778static int compareBySuffix(const void *a, const void *b) {
779 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
780 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
781 const StringRef &NameA = secA->getSectionName();
782 const StringRef &NameB = secB->getSectionName();
783 const unsigned sizeA = NameA.size();
784 const unsigned sizeB = NameB.size();
785 const unsigned len = std::min(sizeA, sizeB);
786 for (unsigned int i = 0; i < len; ++i) {
787 char ca = NameA[sizeA - i - 1];
788 char cb = NameB[sizeB - i - 1];
789 if (ca != cb)
790 return cb - ca;
791 }
792
793 return sizeB - sizeA;
794}
795
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000796void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
797 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000798 SectionIndexMapTy &SectionIndexMap,
799 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000800 MCContext &Ctx = Asm.getContext();
801 MCDataFragment *F;
802
Rafael Espindolabff66a82010-12-18 03:27:34 +0000803 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000804
Rafael Espindola38738bf2010-09-22 19:04:41 +0000805 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000806 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000807 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000808 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000809 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
810 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000811
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000812 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000813 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
814 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000815 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000816 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000817 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000818
819 MCSectionData *SymtabShndxSD = NULL;
820
821 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000822 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000823 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000824 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000825 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
826 SymtabShndxSD->setAlignment(4);
827 }
Matt Fleming3565a062010-08-16 18:57:57 +0000828
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000829 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000830 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000831 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000832 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
833 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000834
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000835 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
836
837 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
838 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
839 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000840
841 // Symbol table
842 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000843 MCDataFragment *ShndxF = NULL;
844 if (NeedsSymtabShndx) {
845 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000846 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000847 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000848
Matt Fleming3565a062010-08-16 18:57:57 +0000849 F = new MCDataFragment(&StrtabSD);
850 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000851
Matt Fleming3565a062010-08-16 18:57:57 +0000852 F = new MCDataFragment(&ShstrtabSD);
853
Rafael Espindolad5321da2011-04-07 23:21:52 +0000854 std::vector<const MCSectionELF*> Sections;
855 for (MCAssembler::const_iterator it = Asm.begin(),
856 ie = Asm.end(); it != ie; ++it) {
857 const MCSectionELF &Section =
858 static_cast<const MCSectionELF&>(it->getSection());
859 Sections.push_back(&Section);
860 }
861 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
862
Matt Fleming3565a062010-08-16 18:57:57 +0000863 // Section header string table.
864 //
865 // The first entry of a string table holds a null character so skip
866 // section 0.
867 uint64_t Index = 1;
868 F->getContents() += '\x00';
869
Rafael Espindolad5321da2011-04-07 23:21:52 +0000870 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
871 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000872
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000873 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000874 if (I != 0) {
875 StringRef PreviousName = Sections[I - 1]->getSectionName();
876 if (PreviousName.endswith(Name)) {
877 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
878 continue;
879 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000880 }
Matt Fleming3565a062010-08-16 18:57:57 +0000881 // Remember the index into the string table so we can write it
882 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000883 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000884
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000885 Index += Name.size() + 1;
886 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000887 F->getContents() += '\x00';
888 }
Rafael Espindola70703872010-09-30 02:22:20 +0000889}
890
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000891void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
892 MCAsmLayout &Layout,
893 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000894 RevGroupMapTy &RevGroupMap,
895 SectionIndexMapTy &SectionIndexMap,
896 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000897 // Create the .note.GNU-stack section if needed.
898 MCContext &Ctx = Asm.getContext();
899 if (Asm.getNoExecStack()) {
900 const MCSectionELF *GnuStackSection =
901 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
902 SectionKind::getReadOnly());
903 Asm.getOrCreateSectionData(*GnuStackSection);
904 }
905
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000906 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000907 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
908 it != ie; ++it) {
909 const MCSectionELF &Section =
910 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000911 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000912 continue;
913
914 const MCSymbol *SignatureSymbol = Section.getGroup();
915 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000916 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000917 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000918 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000919 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
920 Data.setAlignment(4);
921 MCDataFragment *F = new MCDataFragment(&Data);
922 String32(*F, ELF::GRP_COMDAT);
923 }
924 GroupMap[Group] = SignatureSymbol;
925 }
926
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000927 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
928
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000929 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000930 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000931 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000932 const MCSectionELF &Section =
933 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000934 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000935 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000936 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000937 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
938 // FIXME: we could use the previous fragment
939 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000940 unsigned Index = SectionIndexMap.lookup(&Section);
941 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000942 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000943}
944
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000945void ELFObjectWriter::WriteSection(MCAssembler &Asm,
946 const SectionIndexMapTy &SectionIndexMap,
947 uint32_t GroupSymbolIndex,
948 uint64_t Offset, uint64_t Size,
949 uint64_t Alignment,
950 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000951 uint64_t sh_link = 0;
952 uint64_t sh_info = 0;
953
954 switch(Section.getType()) {
955 case ELF::SHT_DYNAMIC:
956 sh_link = SectionStringTableIndex[&Section];
957 sh_info = 0;
958 break;
959
960 case ELF::SHT_REL:
961 case ELF::SHT_RELA: {
962 const MCSectionELF *SymtabSection;
963 const MCSectionELF *InfoSection;
964 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
965 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000966 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000967 sh_link = SectionIndexMap.lookup(SymtabSection);
968 assert(sh_link && ".symtab not found");
969
970 // Remove ".rel" and ".rela" prefixes.
971 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
972 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
973
974 InfoSection = Asm.getContext().getELFSection(SectionName,
975 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000976 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000977 sh_info = SectionIndexMap.lookup(InfoSection);
978 break;
979 }
980
981 case ELF::SHT_SYMTAB:
982 case ELF::SHT_DYNSYM:
983 sh_link = StringTableIndex;
984 sh_info = LastLocalSymbolIndex;
985 break;
986
987 case ELF::SHT_SYMTAB_SHNDX:
988 sh_link = SymbolTableIndex;
989 break;
990
991 case ELF::SHT_PROGBITS:
992 case ELF::SHT_STRTAB:
993 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000994 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000995 case ELF::SHT_NULL:
996 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +0000997 case ELF::SHT_INIT_ARRAY:
998 case ELF::SHT_FINI_ARRAY:
999 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001000 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001001 // Nothing to do.
1002 break;
1003
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001004 case ELF::SHT_GROUP: {
1005 sh_link = SymbolTableIndex;
1006 sh_info = GroupSymbolIndex;
1007 break;
1008 }
1009
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001010 default:
1011 assert(0 && "FIXME: sh_type value not supported!");
1012 break;
1013 }
1014
1015 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1016 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1017 Alignment, Section.getEntrySize());
1018}
1019
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001020bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001021 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001022 !SD.getSection().isVirtualSection();
1023}
1024
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001025uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001026 uint64_t Ret = 0;
1027 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1028 ++i) {
1029 const MCFragment &F = *i;
1030 assert(F.getKind() == MCFragment::FT_Data);
1031 Ret += cast<MCDataFragment>(F).getContents().size();
1032 }
1033 return Ret;
1034}
1035
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001036uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1037 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001038 if (IsELFMetaDataSection(SD))
1039 return DataSectionSize(SD);
1040 return Layout.getSectionFileSize(&SD);
1041}
1042
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001043uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1044 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001045 if (IsELFMetaDataSection(SD))
1046 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001047 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001048}
1049
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001050void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1051 const MCAsmLayout &Layout,
1052 const MCSectionELF &Section) {
1053 uint64_t FileOff = OS.tell();
1054 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1055
1056 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1057 WriteZeros(Padding);
1058 FileOff += Padding;
1059
1060 FileOff += GetSectionFileSize(Layout, SD);
1061
1062 if (IsELFMetaDataSection(SD)) {
1063 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1064 ++i) {
1065 const MCFragment &F = *i;
1066 assert(F.getKind() == MCFragment::FT_Data);
1067 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1068 }
1069 } else {
1070 Asm.WriteSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001071 }
1072}
1073
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001074void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1075 const GroupMapTy &GroupMap,
1076 const MCAsmLayout &Layout,
1077 const SectionIndexMapTy &SectionIndexMap,
1078 const SectionOffsetMapTy &SectionOffsetMap) {
1079 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001080
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001081 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001082 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001083
1084 for (SectionIndexMapTy::const_iterator i=
1085 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1086 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001087 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001088 }
1089
Matt Fleming3565a062010-08-16 18:57:57 +00001090 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001091 uint64_t FirstSectionSize =
1092 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1093 uint32_t FirstSectionLink =
1094 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1095 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001096
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001097 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001098 const MCSectionELF &Section = *Sections[i];
1099 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1100 uint32_t GroupSymbolIndex;
1101 if (Section.getType() != ELF::SHT_GROUP)
1102 GroupSymbolIndex = 0;
1103 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001104 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1105 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001106
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001107 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001108
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001109 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001110 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001111 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001112 }
1113}
1114
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001115void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1116 std::vector<const MCSectionELF*> &Sections) {
1117 for (MCAssembler::iterator it = Asm.begin(),
1118 ie = Asm.end(); it != ie; ++it) {
1119 const MCSectionELF &Section =
1120 static_cast<const MCSectionELF &>(it->getSection());
1121 if (Section.getType() == ELF::SHT_GROUP)
1122 Sections.push_back(&Section);
1123 }
1124
1125 for (MCAssembler::iterator it = Asm.begin(),
1126 ie = Asm.end(); it != ie; ++it) {
1127 const MCSectionELF &Section =
1128 static_cast<const MCSectionELF &>(it->getSection());
1129 if (Section.getType() != ELF::SHT_GROUP &&
1130 Section.getType() != ELF::SHT_REL &&
1131 Section.getType() != ELF::SHT_RELA)
1132 Sections.push_back(&Section);
1133 }
1134
1135 for (MCAssembler::iterator it = Asm.begin(),
1136 ie = Asm.end(); it != ie; ++it) {
1137 const MCSectionELF &Section =
1138 static_cast<const MCSectionELF &>(it->getSection());
1139 if (Section.getType() == ELF::SHT_REL ||
1140 Section.getType() == ELF::SHT_RELA)
1141 Sections.push_back(&Section);
1142 }
1143}
1144
1145void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1146 const MCAsmLayout &Layout) {
1147 GroupMapTy GroupMap;
1148 RevGroupMapTy RevGroupMap;
1149 SectionIndexMapTy SectionIndexMap;
1150
1151 unsigned NumUserSections = Asm.size();
1152
1153 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1154 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1155
1156 const unsigned NumUserAndRelocSections = Asm.size();
1157 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1158 RevGroupMap, SectionIndexMap, RelMap);
1159 const unsigned AllSections = Asm.size();
1160 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1161
1162 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1163
1164 // Compute symbol table information.
1165 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1166
1167
1168 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1169
1170 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1171 const_cast<MCAsmLayout&>(Layout),
1172 SectionIndexMap,
1173 RelMap);
1174
1175 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1176 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1177 sizeof(ELF::Elf32_Ehdr);
1178 uint64_t FileOff = HeaderSize;
1179
1180 std::vector<const MCSectionELF*> Sections;
1181 ComputeSectionOrder(Asm, Sections);
1182 unsigned NumSections = Sections.size();
1183 SectionOffsetMapTy SectionOffsetMap;
1184 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1185 const MCSectionELF &Section = *Sections[i];
1186 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1187
1188 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1189
1190 // Remember the offset into the file for this section.
1191 SectionOffsetMap[&Section] = FileOff;
1192
1193 // Get the size of the section in the output file (including padding).
1194 FileOff += GetSectionFileSize(Layout, SD);
1195 }
1196
1197 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1198
1199 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1200
1201 uint64_t SectionHeaderEntrySize = is64Bit() ?
1202 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1203 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1204
1205 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1206 const MCSectionELF &Section = *Sections[i];
1207 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1208
1209 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1210
1211 // Remember the offset into the file for this section.
1212 SectionOffsetMap[&Section] = FileOff;
1213
1214 // Get the size of the section in the output file (including padding).
1215 FileOff += GetSectionFileSize(Layout, SD);
1216 }
1217
1218 // Write out the ELF header ...
1219 WriteHeader(SectionHeaderOffset, NumSections + 1);
1220
1221 // ... then the regular sections ...
1222 // + because of .shstrtab
1223 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1224 WriteDataSectionData(Asm, Layout, *Sections[i]);
1225
1226 FileOff = OS.tell();
1227 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1228 WriteZeros(Padding);
1229
1230 // ... then the section header table ...
1231 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1232 SectionOffsetMap);
1233
1234 FileOff = OS.tell();
1235
1236 // ... and then the remainting sections ...
1237 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1238 WriteDataSectionData(Asm, Layout, *Sections[i]);
1239}
1240
Eli Friedman78c1e172011-03-03 07:24:36 +00001241bool
1242ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1243 const MCSymbolData &DataA,
1244 const MCFragment &FB,
1245 bool InSet,
1246 bool IsPCRel) const {
1247 if (DataA.getFlags() & ELF_STB_Weak)
1248 return false;
1249 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1250 Asm, DataA, FB,InSet, IsPCRel);
1251}
1252
Rafael Espindola6024c972010-12-17 17:45:22 +00001253MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1254 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001255 bool IsLittleEndian) {
1256 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001257 case ELF::EM_386:
1258 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001259 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001260 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001261 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001262 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001263 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001264 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001265 }
1266}
1267
1268
1269/// START OF SUBCLASSES for ELFObjectWriter
1270//===- ARMELFObjectWriter -------------------------------------------===//
1271
Rafael Espindola31f35782010-12-17 18:01:31 +00001272ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001273 raw_ostream &_OS,
1274 bool IsLittleEndian)
1275 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001276{}
1277
1278ARMELFObjectWriter::~ARMELFObjectWriter()
1279{}
1280
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001281// FIXME: get the real EABI Version from the Triple.
1282void ARMELFObjectWriter::WriteEFlags() {
1283 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1284}
1285
Jason W Kim953a2a32011-02-07 01:11:15 +00001286// In ARM, _MergedGlobals and other most symbols get emitted directly.
1287// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001288// This code is an approximation of what ARM/gcc does.
1289
1290STATISTIC(PCRelCount, "Total number of PIC Relocations");
1291STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001292
1293const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1294 const MCValue &Target,
1295 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001296 const MCFixup &Fixup,
1297 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001298 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1299 bool EmitThisSym = false;
1300
Jason W Kime964d112011-05-11 22:53:06 +00001301 const MCSectionELF &Section =
1302 static_cast<const MCSectionELF&>(Symbol.getSection());
Jason W Kime964d112011-05-11 22:53:06 +00001303 bool InNormalSection = true;
1304 unsigned RelocType = 0;
1305 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1306
Matt Beaumont-Gay6dcd4132011-05-11 23:34:51 +00001307 DEBUG(
1308 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1309 MCSymbolRefExpr::VariantKind Kind2;
1310 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1311 MCSymbolRefExpr::VK_None;
1312 dbgs() << "considering symbol "
Jason W Kime964d112011-05-11 22:53:06 +00001313 << Section.getSectionName() << "/"
1314 << Symbol.getName() << "/"
1315 << " Rel:" << (unsigned)RelocType
1316 << " Kind: " << (int)Kind << "/" << (int)Kind2
1317 << " Tmp:"
1318 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1319 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1320 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1321
1322 if (IsPCRel || ForceARMElfPIC) { ++PCRelCount;
1323 switch (RelocType) {
1324 default:
1325 // Most relocation types are emitted as explicit symbols
1326 InNormalSection =
1327 StringSwitch<bool>(Section.getSectionName())
1328 .Case(".data.rel.ro.local", false)
1329 .Case(".data.rel", false)
1330 .Case(".bss", false)
1331 .Default(true);
1332 EmitThisSym = true;
1333 break;
1334 case ELF::R_ARM_ABS32:
1335 // But things get strange with R_ARM_ABS32
1336 // In this case, most things that go in .rodata show up
1337 // as section relative relocations
1338 InNormalSection =
1339 StringSwitch<bool>(Section.getSectionName())
1340 .Case(".data.rel.ro.local", false)
1341 .Case(".data.rel", false)
1342 .Case(".rodata", false)
1343 .Case(".bss", false)
1344 .Default(true);
1345 EmitThisSym = false;
1346 break;
1347 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001348 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001349 NonPCRelCount++;
1350 InNormalSection =
1351 StringSwitch<bool>(Section.getSectionName())
1352 .Case(".data.rel.ro.local", false)
1353 .Case(".rodata", false)
1354 .Case(".data.rel", false)
1355 .Case(".bss", false)
1356 .Default(true);
1357
1358 switch (RelocType) {
1359 default: EmitThisSym = true; break;
1360 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1361 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001362 }
Jason W Kime964d112011-05-11 22:53:06 +00001363
Jason W Kim953a2a32011-02-07 01:11:15 +00001364 if (EmitThisSym)
1365 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001366 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001367 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001368 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001369 return NULL;
1370}
1371
Jason W Kime964d112011-05-11 22:53:06 +00001372// Need to examine the Fixup when determining whether to
1373// emit the relocation as an explicit symbol or as a section relative
1374// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001375unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1376 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001377 bool IsPCRel,
1378 bool IsRelocWithSymbol,
1379 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001380 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1381 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1382
Jason W Kime964d112011-05-11 22:53:06 +00001383 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1384
1385 if (RelocNeedsGOT(Modifier))
1386 NeedsGOT = true;
1387
1388 return Type;
1389}
1390
1391unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1392 const MCFixup &Fixup,
1393 bool IsPCRel) const {
1394 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1395 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1396
Jason W Kima0871e72010-12-08 23:14:44 +00001397 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001398 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001399 switch ((unsigned)Fixup.getKind()) {
1400 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001401 case FK_Data_4:
1402 switch (Modifier) {
1403 default: llvm_unreachable("Unsupported Modifier");
1404 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001405 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001406 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001407 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001408 assert(0 && "unimplemented");
1409 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001410 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1411 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001412 break;
1413 }
1414 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001415 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001416 switch (Modifier) {
1417 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001418 Type = ELF::R_ARM_PLT32;
1419 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001420 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001421 Type = ELF::R_ARM_CALL;
1422 break;
1423 }
1424 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001425 case ARM::fixup_arm_condbranch:
1426 Type = ELF::R_ARM_JUMP24;
1427 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001428 case ARM::fixup_arm_movt_hi16:
1429 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001430 Type = ELF::R_ARM_MOVT_PREL;
1431 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001432 case ARM::fixup_arm_movw_lo16:
1433 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001434 Type = ELF::R_ARM_MOVW_PREL_NC;
1435 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001436 case ARM::fixup_t2_movt_hi16:
1437 case ARM::fixup_t2_movt_hi16_pcrel:
1438 Type = ELF::R_ARM_THM_MOVT_PREL;
1439 break;
1440 case ARM::fixup_t2_movw_lo16:
1441 case ARM::fixup_t2_movw_lo16_pcrel:
1442 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1443 break;
Rafael Espindola298c8e12011-05-20 20:01:01 +00001444 case ARM::fixup_arm_thumb_bl:
1445 case ARM::fixup_arm_thumb_blx:
1446 switch (Modifier) {
1447 case MCSymbolRefExpr::VK_ARM_PLT:
1448 Type = ELF::R_ARM_THM_CALL;
1449 break;
1450 default:
1451 Type = ELF::R_ARM_NONE;
1452 break;
1453 }
1454 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001455 }
1456 } else {
1457 switch ((unsigned)Fixup.getKind()) {
1458 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001459 case FK_Data_4:
1460 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001461 default: llvm_unreachable("Unsupported Modifier"); break;
1462 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001463 Type = ELF::R_ARM_GOT_BREL;
1464 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001465 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001466 Type = ELF::R_ARM_TLS_GD32;
1467 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001468 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001469 Type = ELF::R_ARM_TLS_LE32;
1470 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001471 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001472 Type = ELF::R_ARM_TLS_IE32;
1473 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001474 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001475 Type = ELF::R_ARM_ABS32;
1476 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001477 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001478 Type = ELF::R_ARM_GOTOFF32;
1479 break;
1480 }
1481 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001482 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001483 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001484 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001485 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001486 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001487 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001488 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001489 assert(0 && "Unimplemented");
1490 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001491 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001492 Type = ELF::R_ARM_CALL;
1493 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001494 case ARM::fixup_arm_condbranch:
1495 Type = ELF::R_ARM_JUMP24;
1496 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001497 case ARM::fixup_arm_movt_hi16:
1498 Type = ELF::R_ARM_MOVT_ABS;
1499 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001500 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001501 Type = ELF::R_ARM_MOVW_ABS_NC;
1502 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001503 case ARM::fixup_t2_movt_hi16:
1504 Type = ELF::R_ARM_THM_MOVT_ABS;
1505 break;
1506 case ARM::fixup_t2_movw_lo16:
1507 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1508 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001509 }
1510 }
1511
Jason W Kima0871e72010-12-08 23:14:44 +00001512 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001513}
1514
Wesley Peck4b047132010-11-21 22:06:28 +00001515//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001516
Rafael Espindola31f35782010-12-17 18:01:31 +00001517MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001518 raw_ostream &_OS,
1519 bool IsLittleEndian)
1520 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001521}
1522
1523MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1524}
1525
Jason W Kim56a39902010-12-06 21:57:34 +00001526unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001527 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001528 bool IsPCRel,
1529 bool IsRelocWithSymbol,
1530 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001531 // determine the type of the relocation
1532 unsigned Type;
1533 if (IsPCRel) {
1534 switch ((unsigned)Fixup.getKind()) {
1535 default:
1536 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001537 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001538 Type = ELF::R_MICROBLAZE_64_PCREL;
1539 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001540 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001541 Type = ELF::R_MICROBLAZE_32_PCREL;
1542 break;
1543 }
1544 } else {
1545 switch ((unsigned)Fixup.getKind()) {
1546 default: llvm_unreachable("invalid fixup kind!");
1547 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001548 Type = ((IsRelocWithSymbol || Addend !=0)
1549 ? ELF::R_MICROBLAZE_32
1550 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001551 break;
1552 case FK_Data_2:
1553 Type = ELF::R_MICROBLAZE_32;
1554 break;
1555 }
1556 }
Jason W Kim56a39902010-12-06 21:57:34 +00001557 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001558}
Jason W Kimd3443e92010-11-15 16:18:39 +00001559
1560//===- X86ELFObjectWriter -------------------------------------------===//
1561
1562
Rafael Espindola31f35782010-12-17 18:01:31 +00001563X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001564 raw_ostream &_OS,
1565 bool IsLittleEndian)
1566 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001567{}
1568
1569X86ELFObjectWriter::~X86ELFObjectWriter()
1570{}
1571
Jason W Kim56a39902010-12-06 21:57:34 +00001572unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1573 const MCFixup &Fixup,
1574 bool IsPCRel,
1575 bool IsRelocWithSymbol,
1576 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001577 // determine the type of the relocation
1578
Rafael Espindola12203cc2010-11-21 00:48:25 +00001579 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1580 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001581 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001582 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001583 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001584 switch ((unsigned)Fixup.getKind()) {
1585 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001586
1587 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1588 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1589 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1590
Rafael Espindola3a83c402010-12-27 00:36:05 +00001591 case FK_PCRel_8:
1592 assert(Modifier == MCSymbolRefExpr::VK_None);
1593 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001594 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001595 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001596 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001597 case X86::reloc_riprel_4byte:
1598 case FK_PCRel_4:
1599 switch (Modifier) {
1600 default:
1601 llvm_unreachable("Unimplemented");
1602 case MCSymbolRefExpr::VK_None:
1603 Type = ELF::R_X86_64_PC32;
1604 break;
1605 case MCSymbolRefExpr::VK_PLT:
1606 Type = ELF::R_X86_64_PLT32;
1607 break;
1608 case MCSymbolRefExpr::VK_GOTPCREL:
1609 Type = ELF::R_X86_64_GOTPCREL;
1610 break;
1611 case MCSymbolRefExpr::VK_GOTTPOFF:
1612 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001613 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001614 case MCSymbolRefExpr::VK_TLSGD:
1615 Type = ELF::R_X86_64_TLSGD;
1616 break;
1617 case MCSymbolRefExpr::VK_TLSLD:
1618 Type = ELF::R_X86_64_TLSLD;
1619 break;
1620 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001621 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001622 case FK_PCRel_2:
1623 assert(Modifier == MCSymbolRefExpr::VK_None);
1624 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001625 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001626 case FK_PCRel_1:
1627 assert(Modifier == MCSymbolRefExpr::VK_None);
1628 Type = ELF::R_X86_64_PC8;
1629 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001630 }
1631 } else {
1632 switch ((unsigned)Fixup.getKind()) {
1633 default: llvm_unreachable("invalid fixup kind!");
1634 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1635 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001636 assert(isInt<32>(Target.getConstant()));
1637 switch (Modifier) {
1638 default:
1639 llvm_unreachable("Unimplemented");
1640 case MCSymbolRefExpr::VK_None:
1641 Type = ELF::R_X86_64_32S;
1642 break;
1643 case MCSymbolRefExpr::VK_GOT:
1644 Type = ELF::R_X86_64_GOT32;
1645 break;
1646 case MCSymbolRefExpr::VK_GOTPCREL:
1647 Type = ELF::R_X86_64_GOTPCREL;
1648 break;
1649 case MCSymbolRefExpr::VK_TPOFF:
1650 Type = ELF::R_X86_64_TPOFF32;
1651 break;
1652 case MCSymbolRefExpr::VK_DTPOFF:
1653 Type = ELF::R_X86_64_DTPOFF32;
1654 break;
1655 }
1656 break;
1657 case FK_Data_4:
1658 Type = ELF::R_X86_64_32;
1659 break;
1660 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001661 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001662 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1663 }
1664 }
1665 } else {
1666 if (IsPCRel) {
1667 switch (Modifier) {
1668 default:
1669 llvm_unreachable("Unimplemented");
1670 case MCSymbolRefExpr::VK_None:
1671 Type = ELF::R_386_PC32;
1672 break;
1673 case MCSymbolRefExpr::VK_PLT:
1674 Type = ELF::R_386_PLT32;
1675 break;
1676 }
1677 } else {
1678 switch ((unsigned)Fixup.getKind()) {
1679 default: llvm_unreachable("invalid fixup kind!");
1680
1681 case X86::reloc_global_offset_table:
1682 Type = ELF::R_386_GOTPC;
1683 break;
1684
1685 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1686 // instead?
1687 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001688 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001689 case FK_Data_4:
1690 switch (Modifier) {
1691 default:
1692 llvm_unreachable("Unimplemented");
1693 case MCSymbolRefExpr::VK_None:
1694 Type = ELF::R_386_32;
1695 break;
1696 case MCSymbolRefExpr::VK_GOT:
1697 Type = ELF::R_386_GOT32;
1698 break;
1699 case MCSymbolRefExpr::VK_GOTOFF:
1700 Type = ELF::R_386_GOTOFF;
1701 break;
1702 case MCSymbolRefExpr::VK_TLSGD:
1703 Type = ELF::R_386_TLS_GD;
1704 break;
1705 case MCSymbolRefExpr::VK_TPOFF:
1706 Type = ELF::R_386_TLS_LE_32;
1707 break;
1708 case MCSymbolRefExpr::VK_INDNTPOFF:
1709 Type = ELF::R_386_TLS_IE;
1710 break;
1711 case MCSymbolRefExpr::VK_NTPOFF:
1712 Type = ELF::R_386_TLS_LE;
1713 break;
1714 case MCSymbolRefExpr::VK_GOTNTPOFF:
1715 Type = ELF::R_386_TLS_GOTIE;
1716 break;
1717 case MCSymbolRefExpr::VK_TLSLDM:
1718 Type = ELF::R_386_TLS_LDM;
1719 break;
1720 case MCSymbolRefExpr::VK_DTPOFF:
1721 Type = ELF::R_386_TLS_LDO_32;
1722 break;
1723 }
1724 break;
1725 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001726 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001727 case FK_Data_1: Type = ELF::R_386_8; break;
1728 }
1729 }
1730 }
1731
1732 if (RelocNeedsGOT(Modifier))
1733 NeedsGOT = true;
1734
Jason W Kim56a39902010-12-06 21:57:34 +00001735 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001736}