blob: ce227f00c2b47277f600fdd88e17d512f5066acf [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
40// Emulate the wierd behavior of GNU-as for relocation types
41namespace llvm {
42cl::opt<bool>
43ForceARMElfPIC("arm-elf-force-pic", cl::Hidden, cl::init(false),
44 cl::desc("Force ELF emitter to emit PIC style relocations"));
45}
46
Jan Sjödin2ddfd952011-02-28 21:45:04 +000047bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
48 const MCFixupKindInfo &FKI =
49 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
50
51 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
52}
53
54bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
55 switch (Variant) {
56 default:
57 return false;
58 case MCSymbolRefExpr::VK_GOT:
59 case MCSymbolRefExpr::VK_PLT:
60 case MCSymbolRefExpr::VK_GOTPCREL:
61 case MCSymbolRefExpr::VK_TPOFF:
62 case MCSymbolRefExpr::VK_TLSGD:
63 case MCSymbolRefExpr::VK_GOTTPOFF:
64 case MCSymbolRefExpr::VK_INDNTPOFF:
65 case MCSymbolRefExpr::VK_NTPOFF:
66 case MCSymbolRefExpr::VK_GOTNTPOFF:
67 case MCSymbolRefExpr::VK_TLSLDM:
68 case MCSymbolRefExpr::VK_DTPOFF:
69 case MCSymbolRefExpr::VK_TLSLD:
70 return true;
71 }
72}
73
Jason W Kimd3443e92010-11-15 16:18:39 +000074ELFObjectWriter::~ELFObjectWriter()
75{}
76
Matt Fleming3565a062010-08-16 18:57:57 +000077// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000078void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
79 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000080 // ELF Header
81 // ----------
82 //
83 // Note
84 // ----
85 // emitWord method behaves differently for ELF32 and ELF64, writing
86 // 4 bytes in the former and 8 in the latter.
87
88 Write8(0x7f); // e_ident[EI_MAG0]
89 Write8('E'); // e_ident[EI_MAG1]
90 Write8('L'); // e_ident[EI_MAG2]
91 Write8('F'); // e_ident[EI_MAG3]
92
Rafael Espindolabff66a82010-12-18 03:27:34 +000093 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000094
95 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000096 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +000097
98 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +000099 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000100 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000101 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
102 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
103 default: Write8(ELF::ELFOSABI_NONE); break;
104 }
Matt Fleming3565a062010-08-16 18:57:57 +0000105 Write8(0); // e_ident[EI_ABIVERSION]
106
107 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
108
109 Write16(ELF::ET_REL); // e_type
110
Rafael Espindolabff66a82010-12-18 03:27:34 +0000111 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000112
113 Write32(ELF::EV_CURRENT); // e_version
114 WriteWord(0); // e_entry, no entry point in .o file
115 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000116 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000117 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000118
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000119 // e_flags = whatever the target wants
120 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000121
122 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000123 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000124
125 Write16(0); // e_phentsize = prog header entry size
126 Write16(0); // e_phnum = # prog header entries = 0
127
128 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000129 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000130
131 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000132 if (NumberOfSections >= ELF::SHN_LORESERVE)
133 Write16(0);
134 else
135 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000136
137 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000138 if (NumberOfSections >= ELF::SHN_LORESERVE)
139 Write16(ELF::SHN_XINDEX);
140 else
141 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000142}
143
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000144void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
145 MCDataFragment *ShndxF,
146 uint64_t name,
147 uint8_t info, uint64_t value,
148 uint64_t size, uint8_t other,
149 uint32_t shndx,
150 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000151 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000152 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000153 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000154 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000155 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000156 }
157
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000158 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
159 uint16_t(ELF::SHN_XINDEX) : shndx;
160
Rafael Espindolabff66a82010-12-18 03:27:34 +0000161 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000162 String32(*SymtabF, name); // st_name
163 String8(*SymtabF, info); // st_info
164 String8(*SymtabF, other); // st_other
165 String16(*SymtabF, Index); // st_shndx
166 String64(*SymtabF, value); // st_value
167 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000168 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000169 String32(*SymtabF, name); // st_name
170 String32(*SymtabF, value); // st_value
171 String32(*SymtabF, size); // st_size
172 String8(*SymtabF, info); // st_info
173 String8(*SymtabF, other); // st_other
174 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000175 }
176}
177
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000178uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
179 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000180 if (Data.isCommon() && Data.isExternal())
181 return Data.getCommonAlignment();
182
183 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000184
185 if (Symbol.isAbsolute() && Symbol.isVariable()) {
186 if (const MCExpr *Value = Symbol.getVariableValue()) {
187 int64_t IntValue;
188 if (Value->EvaluateAsAbsolute(IntValue, Layout))
189 return (uint64_t)IntValue;
190 }
191 }
192
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000193 if (!Symbol.isInSection())
194 return 0;
195
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000196 if (Data.getFragment())
197 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000198
199 return 0;
200}
201
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000202void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
203 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000204 // The presence of symbol versions causes undefined symbols and
205 // versions declared with @@@ to be renamed.
206
207 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
208 ie = Asm.symbol_end(); it != ie; ++it) {
209 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000210 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000211 MCSymbolData &SD = Asm.getSymbolData(Symbol);
212
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000213 // Not an alias.
214 if (&Symbol == &Alias)
215 continue;
216
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000217 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000218 size_t Pos = AliasName.find('@');
219 if (Pos == StringRef::npos)
220 continue;
221
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000222 // Aliases defined with .symvar copy the binding from the symbol they alias.
223 // This is the first place we are able to copy this information.
224 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000225 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000226
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000227 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000228 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
229 continue;
230
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000231 // FIXME: produce a better error message.
232 if (Symbol.isUndefined() && Rest.startswith("@@") &&
233 !Rest.startswith("@@@"))
234 report_fatal_error("A @@ version cannot be undefined");
235
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000236 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000237 }
238}
239
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000240void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
241 MCDataFragment *ShndxF,
242 ELFSymbolData &MSD,
243 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000244 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000245 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000246 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000247
Rafael Espindola7be2c332010-10-31 00:16:26 +0000248 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
249 Data.getSymbol().isVariable();
250
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000251 uint8_t Binding = MCELF::GetBinding(OrigData);
252 uint8_t Visibility = MCELF::GetVisibility(OrigData);
253 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000254
255 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
256 uint8_t Other = Visibility;
257
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000258 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000259 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000260
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000261 assert(!(Data.isCommon() && !Data.isExternal()));
262
Rafael Espindolaf0121242010-12-22 16:03:00 +0000263 const MCExpr *ESize = Data.getSize();
264 if (ESize) {
265 int64_t Res;
266 if (!ESize->EvaluateAsAbsolute(Res, Layout))
267 report_fatal_error("Size expression must be absolute.");
268 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000269 }
270
271 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000272 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
273 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000274}
275
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000276void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
277 MCDataFragment *ShndxF,
278 const MCAssembler &Asm,
279 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000280 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000281 // The string table must be emitted first because we need the index
282 // into the string table for all the symbol names.
283 assert(StringTable.size() && "Missing string table");
284
285 // FIXME: Make sure the start of the symbol table is aligned.
286
287 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000288 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000289
290 // Write the symbol table entries.
291 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
292 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
293 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000294 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000295 }
296
Rafael Espindola71859c62010-09-16 19:46:31 +0000297 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000298 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
299 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000300 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000301 static_cast<const MCSectionELF&>(i->getSection());
302 if (Section.getType() == ELF::SHT_RELA ||
303 Section.getType() == ELF::SHT_REL ||
304 Section.getType() == ELF::SHT_STRTAB ||
305 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000306 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000307 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000308 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000309 LastLocalSymbolIndex++;
310 }
Matt Fleming3565a062010-08-16 18:57:57 +0000311
312 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
313 ELFSymbolData &MSD = ExternalSymbolData[i];
314 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000315 assert(((Data.getFlags() & ELF_STB_Global) ||
316 (Data.getFlags() & ELF_STB_Weak)) &&
317 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000318 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000319 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000320 LastLocalSymbolIndex++;
321 }
322
323 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
324 ELFSymbolData &MSD = UndefinedSymbolData[i];
325 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000326 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000327 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000328 LastLocalSymbolIndex++;
329 }
330}
331
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000332const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
333 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000334 const MCFragment &F,
335 const MCFixup &Fixup,
336 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000337 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000338 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
339 const MCSymbol *Renamed = Renames.lookup(&Symbol);
340 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000341
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000342 if (ASymbol.isUndefined()) {
343 if (Renamed)
344 return Renamed;
345 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000346 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000347
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000348 if (SD.isExternal()) {
349 if (Renamed)
350 return Renamed;
351 return &Symbol;
352 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000353
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000354 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000355 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000356 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000357
Rafael Espindola25958732010-11-24 21:57:39 +0000358 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000359 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000360
Rafael Espindola25958732010-11-24 21:57:39 +0000361 if (secKind.isThreadLocal()) {
362 if (Renamed)
363 return Renamed;
364 return &Symbol;
365 }
366
Rafael Espindola8cecf252010-10-06 16:23:36 +0000367 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000368 const MCSectionELF &Sec2 =
369 static_cast<const MCSectionELF&>(F.getParent()->getSection());
370
Rafael Espindola8cecf252010-10-06 16:23:36 +0000371 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000372 (Kind == MCSymbolRefExpr::VK_PLT ||
373 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000374 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000375 if (Renamed)
376 return Renamed;
377 return &Symbol;
378 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000379
Rafael Espindola1c130262011-01-23 04:43:11 +0000380 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000381 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000382 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000383 if (Renamed)
384 return Renamed;
385 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000386 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000387
Jason W Kime964d112011-05-11 22:53:06 +0000388 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
389
Rafael Espindola73ffea42010-09-25 05:42:19 +0000390}
391
Matt Fleming3565a062010-08-16 18:57:57 +0000392
Jason W Kim56a39902010-12-06 21:57:34 +0000393void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
394 const MCAsmLayout &Layout,
395 const MCFragment *Fragment,
396 const MCFixup &Fixup,
397 MCValue Target,
398 uint64_t &FixedValue) {
399 int64_t Addend = 0;
400 int Index = 0;
401 int64_t Value = Target.getConstant();
402 const MCSymbol *RelocSymbol = NULL;
403
Rafael Espindola127a6a42010-12-17 07:28:17 +0000404 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000405 if (!Target.isAbsolute()) {
406 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
407 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000408 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000409
410 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
411 const MCSymbol &SymbolB = RefB->getSymbol();
412 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
413 IsPCRel = true;
414
415 // Offset of the symbol in the section
416 int64_t a = Layout.getSymbolOffset(&SDB);
417
418 // Ofeset of the relocation in the section
419 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
420 Value += b - a;
421 }
422
423 if (!RelocSymbol) {
424 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
425 MCFragment *F = SD.getFragment();
426
427 Index = F->getParent()->getOrdinal() + 1;
428
429 // Offset of the symbol in the section
430 Value += Layout.getSymbolOffset(&SD);
431 } else {
432 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
433 WeakrefUsedInReloc.insert(RelocSymbol);
434 else
435 UsedInReloc.insert(RelocSymbol);
436 Index = -1;
437 }
438 Addend = Value;
439 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000440 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000441 Value = 0;
442 }
443
444 FixedValue = Value;
445 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
446 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000447
Jason W Kim56a39902010-12-06 21:57:34 +0000448 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
449 Fixup.getOffset();
450
Rafael Espindolabff66a82010-12-18 03:27:34 +0000451 if (!hasRelocationAddend())
452 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000453 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
454 Relocations[Fragment->getParent()].push_back(ERE);
455}
456
457
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000458uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000459ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
460 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000461 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000462 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000463}
464
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000465bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
466 const MCSymbolData &Data,
467 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000468 if (Data.getFlags() & ELF_Other_Weakref)
469 return false;
470
Rafael Espindolabd701182010-10-19 19:31:37 +0000471 if (Used)
472 return true;
473
Rafael Espindola88182132010-10-27 15:18:17 +0000474 if (Renamed)
475 return false;
476
Rafael Espindola737cd212010-10-05 18:01:23 +0000477 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000478
Rafael Espindolad1798862010-10-29 23:09:31 +0000479 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
480 return true;
481
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000482 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000483 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
484 return false;
485
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000486 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000487 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000488 return false;
489
Rafael Espindola737cd212010-10-05 18:01:23 +0000490 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
491 return false;
492
Rafael Espindolabd701182010-10-19 19:31:37 +0000493 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000494 return false;
495
496 return true;
497}
498
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000499bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
500 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000501 if (Data.isExternal())
502 return false;
503
504 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000505 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000506
507 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
508 if (isSignature && !isUsedInReloc)
509 return true;
510
Rafael Espindola737cd212010-10-05 18:01:23 +0000511 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000512 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000513
514 return true;
515}
516
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000517void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000518 SectionIndexMapTy &SectionIndexMap,
519 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000520 unsigned Index = 1;
521 for (MCAssembler::iterator it = Asm.begin(),
522 ie = Asm.end(); it != ie; ++it) {
523 const MCSectionELF &Section =
524 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000525 if (Section.getType() != ELF::SHT_GROUP)
526 continue;
527 SectionIndexMap[&Section] = Index++;
528 }
529
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 Espindola7c18fa82011-03-20 18:44:20 +0000534 if (Section.getType() == ELF::SHT_GROUP ||
535 Section.getType() == ELF::SHT_REL ||
536 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000537 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000538 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000539 const MCSectionELF *RelSection = RelMap.lookup(&Section);
540 if (RelSection)
541 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000542 }
543}
544
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000545void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000546 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000547 RevGroupMapTy RevGroupMap,
548 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000549 // FIXME: Is this the correct place to do this?
550 if (NeedsGOT) {
551 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
552 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
553 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
554 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000555 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000556 }
557
Matt Fleming3565a062010-08-16 18:57:57 +0000558 // Index 0 is always the empty string.
559 StringMap<uint64_t> StringIndexMap;
560 StringTable += '\x00';
561
Rafael Espindolad5321da2011-04-07 23:21:52 +0000562 // FIXME: We could optimize suffixes in strtab in the same way we
563 // optimize them in shstrtab.
564
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000565 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000566 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
567 ie = Asm.symbol_end(); it != ie; ++it) {
568 const MCSymbol &Symbol = it->getSymbol();
569
Rafael Espindola484291c2010-11-01 14:28:48 +0000570 bool Used = UsedInReloc.count(&Symbol);
571 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000572 bool isSignature = RevGroupMap.count(&Symbol);
573
574 if (!isInSymtab(Asm, *it,
575 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000576 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000577 continue;
578
Matt Fleming3565a062010-08-16 18:57:57 +0000579 ELFSymbolData MSD;
580 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000581 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000582
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000583 // Undefined symbols are global, but this is the first place we
584 // are able to set it.
585 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000586 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000587 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000588 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
589 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000590 }
591
Rafael Espindola484291c2010-11-01 14:28:48 +0000592 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000593 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000594
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000595 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000596 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000597 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000598 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000599 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000600 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000601 if (isSignature && !Used)
602 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
603 else
604 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000605 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000606 const MCSectionELF &Section =
607 static_cast<const MCSectionELF&>(RefSymbol.getSection());
608 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000609 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
610 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000611 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000612 }
613
Rafael Espindola88182132010-10-27 15:18:17 +0000614 // The @@@ in symbol version is replaced with @ in undefined symbols and
615 // @@ in defined ones.
616 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000617 SmallString<32> Buf;
618
Rafael Espindola88182132010-10-27 15:18:17 +0000619 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000620 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000621 Buf += Name.substr(0, Pos);
622 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
623 Buf += Name.substr(Pos + Skip);
624 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000625 }
626
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000627 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000628 if (!Entry) {
629 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000630 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000631 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000632 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000633 MSD.StringIndex = Entry;
634 if (MSD.SectionIndex == ELF::SHN_UNDEF)
635 UndefinedSymbolData.push_back(MSD);
636 else if (Local)
637 LocalSymbolData.push_back(MSD);
638 else
639 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000640 }
641
642 // Symbols are required to be in lexicographic order.
643 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
644 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
645 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
646
647 // Set the symbol indices. Local symbols must come before all other
648 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000649 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000650 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
651 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000652
653 Index += NumRegularSections;
654
Matt Fleming3565a062010-08-16 18:57:57 +0000655 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
656 ExternalSymbolData[i].SymbolData->setIndex(Index++);
657 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
658 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
659}
660
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000661void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
662 MCAsmLayout &Layout,
663 RelMapTy &RelMap) {
664 for (MCAssembler::const_iterator it = Asm.begin(),
665 ie = Asm.end(); it != ie; ++it) {
666 const MCSectionData &SD = *it;
667 if (Relocations[&SD].empty())
668 continue;
669
Matt Fleming3565a062010-08-16 18:57:57 +0000670 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000671 const MCSectionELF &Section =
672 static_cast<const MCSectionELF&>(SD.getSection());
673
674 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000675 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000676 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000677
678 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000679 if (hasRelocationAddend())
680 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000681 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000682 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000683
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000684 const MCSectionELF *RelaSection =
685 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
686 ELF::SHT_RELA : ELF::SHT_REL, 0,
687 SectionKind::getReadOnly(),
688 EntrySize, "");
689 RelMap[&Section] = RelaSection;
690 Asm.getOrCreateSectionData(*RelaSection);
691 }
692}
Matt Fleming3565a062010-08-16 18:57:57 +0000693
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000694void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
695 const RelMapTy &RelMap) {
696 for (MCAssembler::const_iterator it = Asm.begin(),
697 ie = Asm.end(); it != ie; ++it) {
698 const MCSectionData &SD = *it;
699 const MCSectionELF &Section =
700 static_cast<const MCSectionELF&>(SD.getSection());
701
702 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
703 if (!RelaSection)
704 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000705 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000706 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000707
708 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000709 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000710 }
711}
712
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000713void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
714 uint64_t Flags, uint64_t Address,
715 uint64_t Offset, uint64_t Size,
716 uint32_t Link, uint32_t Info,
717 uint64_t Alignment,
718 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000719 Write32(Name); // sh_name: index into string table
720 Write32(Type); // sh_type
721 WriteWord(Flags); // sh_flags
722 WriteWord(Address); // sh_addr
723 WriteWord(Offset); // sh_offset
724 WriteWord(Size); // sh_size
725 Write32(Link); // sh_link
726 Write32(Info); // sh_info
727 WriteWord(Alignment); // sh_addralign
728 WriteWord(EntrySize); // sh_entsize
729}
730
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000731void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
732 MCDataFragment *F,
733 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000734 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
735 // sort by the r_offset just like gnu as does
736 array_pod_sort(Relocs.begin(), Relocs.end());
737
738 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
739 ELFRelocationEntry entry = Relocs[e - i - 1];
740
Rafael Espindola12203cc2010-11-21 00:48:25 +0000741 if (!entry.Index)
742 ;
743 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000744 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
745 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000746 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000747 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000748 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000749
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000750 struct ELF::Elf64_Rela ERE64;
751 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000752 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000753
Rafael Espindolabff66a82010-12-18 03:27:34 +0000754 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000755 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000756 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000757 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000758
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000759 struct ELF::Elf32_Rela ERE32;
760 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000761 String32(*F, ERE32.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 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000765 }
Matt Fleming3565a062010-08-16 18:57:57 +0000766 }
767}
768
Rafael Espindolad5321da2011-04-07 23:21:52 +0000769static int compareBySuffix(const void *a, const void *b) {
770 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
771 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
772 const StringRef &NameA = secA->getSectionName();
773 const StringRef &NameB = secB->getSectionName();
774 const unsigned sizeA = NameA.size();
775 const unsigned sizeB = NameB.size();
776 const unsigned len = std::min(sizeA, sizeB);
777 for (unsigned int i = 0; i < len; ++i) {
778 char ca = NameA[sizeA - i - 1];
779 char cb = NameB[sizeB - i - 1];
780 if (ca != cb)
781 return cb - ca;
782 }
783
784 return sizeB - sizeA;
785}
786
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000787void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
788 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000789 SectionIndexMapTy &SectionIndexMap,
790 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000791 MCContext &Ctx = Asm.getContext();
792 MCDataFragment *F;
793
Rafael Espindolabff66a82010-12-18 03:27:34 +0000794 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000795
Rafael Espindola38738bf2010-09-22 19:04:41 +0000796 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000797 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000798 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000799 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000800 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
801 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000802
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000803 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000804 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
805 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000806 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000807 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000808 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000809
810 MCSectionData *SymtabShndxSD = NULL;
811
812 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000813 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000814 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000815 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000816 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
817 SymtabShndxSD->setAlignment(4);
818 }
Matt Fleming3565a062010-08-16 18:57:57 +0000819
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000820 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000821 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000822 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000823 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
824 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000825
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000826 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
827
828 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
829 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
830 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000831
832 // Symbol table
833 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000834 MCDataFragment *ShndxF = NULL;
835 if (NeedsSymtabShndx) {
836 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000837 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000838 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000839
Matt Fleming3565a062010-08-16 18:57:57 +0000840 F = new MCDataFragment(&StrtabSD);
841 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000842
Matt Fleming3565a062010-08-16 18:57:57 +0000843 F = new MCDataFragment(&ShstrtabSD);
844
Rafael Espindolad5321da2011-04-07 23:21:52 +0000845 std::vector<const MCSectionELF*> Sections;
846 for (MCAssembler::const_iterator it = Asm.begin(),
847 ie = Asm.end(); it != ie; ++it) {
848 const MCSectionELF &Section =
849 static_cast<const MCSectionELF&>(it->getSection());
850 Sections.push_back(&Section);
851 }
852 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
853
Matt Fleming3565a062010-08-16 18:57:57 +0000854 // Section header string table.
855 //
856 // The first entry of a string table holds a null character so skip
857 // section 0.
858 uint64_t Index = 1;
859 F->getContents() += '\x00';
860
Rafael Espindolad5321da2011-04-07 23:21:52 +0000861 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
862 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000863
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000864 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000865 if (I != 0) {
866 StringRef PreviousName = Sections[I - 1]->getSectionName();
867 if (PreviousName.endswith(Name)) {
868 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
869 continue;
870 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000871 }
Matt Fleming3565a062010-08-16 18:57:57 +0000872 // Remember the index into the string table so we can write it
873 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000874 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000875
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000876 Index += Name.size() + 1;
877 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000878 F->getContents() += '\x00';
879 }
Rafael Espindola70703872010-09-30 02:22:20 +0000880}
881
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000882void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
883 MCAsmLayout &Layout,
884 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000885 RevGroupMapTy &RevGroupMap,
886 SectionIndexMapTy &SectionIndexMap,
887 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000888 // Create the .note.GNU-stack section if needed.
889 MCContext &Ctx = Asm.getContext();
890 if (Asm.getNoExecStack()) {
891 const MCSectionELF *GnuStackSection =
892 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
893 SectionKind::getReadOnly());
894 Asm.getOrCreateSectionData(*GnuStackSection);
895 }
896
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000897 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000898 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
899 it != ie; ++it) {
900 const MCSectionELF &Section =
901 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000902 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000903 continue;
904
905 const MCSymbol *SignatureSymbol = Section.getGroup();
906 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000907 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000908 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000909 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000910 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
911 Data.setAlignment(4);
912 MCDataFragment *F = new MCDataFragment(&Data);
913 String32(*F, ELF::GRP_COMDAT);
914 }
915 GroupMap[Group] = SignatureSymbol;
916 }
917
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000918 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
919
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000920 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000921 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000922 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000923 const MCSectionELF &Section =
924 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000925 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000926 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000927 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000928 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
929 // FIXME: we could use the previous fragment
930 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000931 unsigned Index = SectionIndexMap.lookup(&Section);
932 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000933 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000934}
935
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000936void ELFObjectWriter::WriteSection(MCAssembler &Asm,
937 const SectionIndexMapTy &SectionIndexMap,
938 uint32_t GroupSymbolIndex,
939 uint64_t Offset, uint64_t Size,
940 uint64_t Alignment,
941 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000942 uint64_t sh_link = 0;
943 uint64_t sh_info = 0;
944
945 switch(Section.getType()) {
946 case ELF::SHT_DYNAMIC:
947 sh_link = SectionStringTableIndex[&Section];
948 sh_info = 0;
949 break;
950
951 case ELF::SHT_REL:
952 case ELF::SHT_RELA: {
953 const MCSectionELF *SymtabSection;
954 const MCSectionELF *InfoSection;
955 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
956 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000957 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000958 sh_link = SectionIndexMap.lookup(SymtabSection);
959 assert(sh_link && ".symtab not found");
960
961 // Remove ".rel" and ".rela" prefixes.
962 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
963 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
964
965 InfoSection = Asm.getContext().getELFSection(SectionName,
966 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000967 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000968 sh_info = SectionIndexMap.lookup(InfoSection);
969 break;
970 }
971
972 case ELF::SHT_SYMTAB:
973 case ELF::SHT_DYNSYM:
974 sh_link = StringTableIndex;
975 sh_info = LastLocalSymbolIndex;
976 break;
977
978 case ELF::SHT_SYMTAB_SHNDX:
979 sh_link = SymbolTableIndex;
980 break;
981
982 case ELF::SHT_PROGBITS:
983 case ELF::SHT_STRTAB:
984 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000985 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000986 case ELF::SHT_NULL:
987 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +0000988 case ELF::SHT_INIT_ARRAY:
989 case ELF::SHT_FINI_ARRAY:
990 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000991 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000992 // Nothing to do.
993 break;
994
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000995 case ELF::SHT_GROUP: {
996 sh_link = SymbolTableIndex;
997 sh_info = GroupSymbolIndex;
998 break;
999 }
1000
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001001 default:
1002 assert(0 && "FIXME: sh_type value not supported!");
1003 break;
1004 }
1005
1006 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1007 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1008 Alignment, Section.getEntrySize());
1009}
1010
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001011bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001012 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001013 !SD.getSection().isVirtualSection();
1014}
1015
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001016uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001017 uint64_t Ret = 0;
1018 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1019 ++i) {
1020 const MCFragment &F = *i;
1021 assert(F.getKind() == MCFragment::FT_Data);
1022 Ret += cast<MCDataFragment>(F).getContents().size();
1023 }
1024 return Ret;
1025}
1026
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001027uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1028 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001029 if (IsELFMetaDataSection(SD))
1030 return DataSectionSize(SD);
1031 return Layout.getSectionFileSize(&SD);
1032}
1033
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001034uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1035 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001036 if (IsELFMetaDataSection(SD))
1037 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001038 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001039}
1040
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001041void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1042 const MCAsmLayout &Layout,
1043 const MCSectionELF &Section) {
1044 uint64_t FileOff = OS.tell();
1045 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1046
1047 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1048 WriteZeros(Padding);
1049 FileOff += Padding;
1050
1051 FileOff += GetSectionFileSize(Layout, SD);
1052
1053 if (IsELFMetaDataSection(SD)) {
1054 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1055 ++i) {
1056 const MCFragment &F = *i;
1057 assert(F.getKind() == MCFragment::FT_Data);
1058 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1059 }
1060 } else {
1061 Asm.WriteSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001062 }
1063}
1064
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001065void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1066 const GroupMapTy &GroupMap,
1067 const MCAsmLayout &Layout,
1068 const SectionIndexMapTy &SectionIndexMap,
1069 const SectionOffsetMapTy &SectionOffsetMap) {
1070 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001071
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001072 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001073 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001074
1075 for (SectionIndexMapTy::const_iterator i=
1076 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1077 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001078 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001079 }
1080
Matt Fleming3565a062010-08-16 18:57:57 +00001081 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001082 uint64_t FirstSectionSize =
1083 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1084 uint32_t FirstSectionLink =
1085 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1086 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001087
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001088 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001089 const MCSectionELF &Section = *Sections[i];
1090 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1091 uint32_t GroupSymbolIndex;
1092 if (Section.getType() != ELF::SHT_GROUP)
1093 GroupSymbolIndex = 0;
1094 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001095 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1096 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001097
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001098 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001099
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001100 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001101 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001102 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001103 }
1104}
1105
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001106void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1107 std::vector<const MCSectionELF*> &Sections) {
1108 for (MCAssembler::iterator it = Asm.begin(),
1109 ie = Asm.end(); it != ie; ++it) {
1110 const MCSectionELF &Section =
1111 static_cast<const MCSectionELF &>(it->getSection());
1112 if (Section.getType() == ELF::SHT_GROUP)
1113 Sections.push_back(&Section);
1114 }
1115
1116 for (MCAssembler::iterator it = Asm.begin(),
1117 ie = Asm.end(); it != ie; ++it) {
1118 const MCSectionELF &Section =
1119 static_cast<const MCSectionELF &>(it->getSection());
1120 if (Section.getType() != ELF::SHT_GROUP &&
1121 Section.getType() != ELF::SHT_REL &&
1122 Section.getType() != ELF::SHT_RELA)
1123 Sections.push_back(&Section);
1124 }
1125
1126 for (MCAssembler::iterator it = Asm.begin(),
1127 ie = Asm.end(); it != ie; ++it) {
1128 const MCSectionELF &Section =
1129 static_cast<const MCSectionELF &>(it->getSection());
1130 if (Section.getType() == ELF::SHT_REL ||
1131 Section.getType() == ELF::SHT_RELA)
1132 Sections.push_back(&Section);
1133 }
1134}
1135
1136void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1137 const MCAsmLayout &Layout) {
1138 GroupMapTy GroupMap;
1139 RevGroupMapTy RevGroupMap;
1140 SectionIndexMapTy SectionIndexMap;
1141
1142 unsigned NumUserSections = Asm.size();
1143
1144 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1145 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1146
1147 const unsigned NumUserAndRelocSections = Asm.size();
1148 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1149 RevGroupMap, SectionIndexMap, RelMap);
1150 const unsigned AllSections = Asm.size();
1151 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1152
1153 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1154
1155 // Compute symbol table information.
1156 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1157
1158
1159 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1160
1161 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1162 const_cast<MCAsmLayout&>(Layout),
1163 SectionIndexMap,
1164 RelMap);
1165
1166 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1167 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1168 sizeof(ELF::Elf32_Ehdr);
1169 uint64_t FileOff = HeaderSize;
1170
1171 std::vector<const MCSectionELF*> Sections;
1172 ComputeSectionOrder(Asm, Sections);
1173 unsigned NumSections = Sections.size();
1174 SectionOffsetMapTy SectionOffsetMap;
1175 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1176 const MCSectionELF &Section = *Sections[i];
1177 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1178
1179 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1180
1181 // Remember the offset into the file for this section.
1182 SectionOffsetMap[&Section] = FileOff;
1183
1184 // Get the size of the section in the output file (including padding).
1185 FileOff += GetSectionFileSize(Layout, SD);
1186 }
1187
1188 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1189
1190 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1191
1192 uint64_t SectionHeaderEntrySize = is64Bit() ?
1193 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1194 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1195
1196 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1197 const MCSectionELF &Section = *Sections[i];
1198 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1199
1200 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1201
1202 // Remember the offset into the file for this section.
1203 SectionOffsetMap[&Section] = FileOff;
1204
1205 // Get the size of the section in the output file (including padding).
1206 FileOff += GetSectionFileSize(Layout, SD);
1207 }
1208
1209 // Write out the ELF header ...
1210 WriteHeader(SectionHeaderOffset, NumSections + 1);
1211
1212 // ... then the regular sections ...
1213 // + because of .shstrtab
1214 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1215 WriteDataSectionData(Asm, Layout, *Sections[i]);
1216
1217 FileOff = OS.tell();
1218 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1219 WriteZeros(Padding);
1220
1221 // ... then the section header table ...
1222 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1223 SectionOffsetMap);
1224
1225 FileOff = OS.tell();
1226
1227 // ... and then the remainting sections ...
1228 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1229 WriteDataSectionData(Asm, Layout, *Sections[i]);
1230}
1231
Eli Friedman78c1e172011-03-03 07:24:36 +00001232bool
1233ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1234 const MCSymbolData &DataA,
1235 const MCFragment &FB,
1236 bool InSet,
1237 bool IsPCRel) const {
1238 if (DataA.getFlags() & ELF_STB_Weak)
1239 return false;
1240 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1241 Asm, DataA, FB,InSet, IsPCRel);
1242}
1243
Rafael Espindola6024c972010-12-17 17:45:22 +00001244MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1245 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001246 bool IsLittleEndian) {
1247 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001248 case ELF::EM_386:
1249 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001250 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001251 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001252 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001253 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001254 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001255 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001256 }
1257}
1258
1259
1260/// START OF SUBCLASSES for ELFObjectWriter
1261//===- ARMELFObjectWriter -------------------------------------------===//
1262
Rafael Espindola31f35782010-12-17 18:01:31 +00001263ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001264 raw_ostream &_OS,
1265 bool IsLittleEndian)
1266 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001267{}
1268
1269ARMELFObjectWriter::~ARMELFObjectWriter()
1270{}
1271
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001272// FIXME: get the real EABI Version from the Triple.
1273void ARMELFObjectWriter::WriteEFlags() {
1274 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1275}
1276
Jason W Kim953a2a32011-02-07 01:11:15 +00001277// In ARM, _MergedGlobals and other most symbols get emitted directly.
1278// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001279// This code is an approximation of what ARM/gcc does.
1280
1281STATISTIC(PCRelCount, "Total number of PIC Relocations");
1282STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001283
1284const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1285 const MCValue &Target,
1286 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001287 const MCFixup &Fixup,
1288 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001289 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1290 bool EmitThisSym = false;
1291
Jason W Kime964d112011-05-11 22:53:06 +00001292 const MCSectionELF &Section =
1293 static_cast<const MCSectionELF&>(Symbol.getSection());
1294 const SectionKind secKind = Section.getKind();
1295 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1296 MCSymbolRefExpr::VariantKind Kind2;
1297 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1298 MCSymbolRefExpr::VK_None;
1299 bool InNormalSection = true;
1300 unsigned RelocType = 0;
1301 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1302
1303 DEBUG(dbgs() << "considering symbol "
1304 << Section.getSectionName() << "/"
1305 << Symbol.getName() << "/"
1306 << " Rel:" << (unsigned)RelocType
1307 << " Kind: " << (int)Kind << "/" << (int)Kind2
1308 << " Tmp:"
1309 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1310 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1311 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1312
1313 if (IsPCRel || ForceARMElfPIC) { ++PCRelCount;
1314 switch (RelocType) {
1315 default:
1316 // Most relocation types are emitted as explicit symbols
1317 InNormalSection =
1318 StringSwitch<bool>(Section.getSectionName())
1319 .Case(".data.rel.ro.local", false)
1320 .Case(".data.rel", false)
1321 .Case(".bss", false)
1322 .Default(true);
1323 EmitThisSym = true;
1324 break;
1325 case ELF::R_ARM_ABS32:
1326 // But things get strange with R_ARM_ABS32
1327 // In this case, most things that go in .rodata show up
1328 // as section relative relocations
1329 InNormalSection =
1330 StringSwitch<bool>(Section.getSectionName())
1331 .Case(".data.rel.ro.local", false)
1332 .Case(".data.rel", false)
1333 .Case(".rodata", false)
1334 .Case(".bss", false)
1335 .Default(true);
1336 EmitThisSym = false;
1337 break;
1338 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001339 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001340 NonPCRelCount++;
1341 InNormalSection =
1342 StringSwitch<bool>(Section.getSectionName())
1343 .Case(".data.rel.ro.local", false)
1344 .Case(".rodata", false)
1345 .Case(".data.rel", false)
1346 .Case(".bss", false)
1347 .Default(true);
1348
1349 switch (RelocType) {
1350 default: EmitThisSym = true; break;
1351 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1352 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001353 }
Jason W Kime964d112011-05-11 22:53:06 +00001354
Jason W Kim953a2a32011-02-07 01:11:15 +00001355 if (EmitThisSym)
1356 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001357 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001358 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001359 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001360 return NULL;
1361}
1362
Jason W Kime964d112011-05-11 22:53:06 +00001363// Need to examine the Fixup when determining whether to
1364// emit the relocation as an explicit symbol or as a section relative
1365// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001366unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1367 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001368 bool IsPCRel,
1369 bool IsRelocWithSymbol,
1370 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001371 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1372 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1373
Jason W Kime964d112011-05-11 22:53:06 +00001374 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1375
1376 if (RelocNeedsGOT(Modifier))
1377 NeedsGOT = true;
1378
1379 return Type;
1380}
1381
1382unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1383 const MCFixup &Fixup,
1384 bool IsPCRel) const {
1385 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1386 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1387
Jason W Kima0871e72010-12-08 23:14:44 +00001388 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001389 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001390 switch ((unsigned)Fixup.getKind()) {
1391 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001392 case FK_Data_4:
1393 switch (Modifier) {
1394 default: llvm_unreachable("Unsupported Modifier");
1395 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001396 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001397 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001398 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001399 assert(0 && "unimplemented");
1400 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001401 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1402 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001403 break;
1404 }
1405 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001406 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001407 switch (Modifier) {
1408 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001409 Type = ELF::R_ARM_PLT32;
1410 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001411 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001412 Type = ELF::R_ARM_CALL;
1413 break;
1414 }
1415 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001416 case ARM::fixup_arm_condbranch:
1417 Type = ELF::R_ARM_JUMP24;
1418 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001419 case ARM::fixup_arm_movt_hi16:
1420 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001421 Type = ELF::R_ARM_MOVT_PREL;
1422 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001423 case ARM::fixup_arm_movw_lo16:
1424 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001425 Type = ELF::R_ARM_MOVW_PREL_NC;
1426 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001427 case ARM::fixup_t2_movt_hi16:
1428 case ARM::fixup_t2_movt_hi16_pcrel:
1429 Type = ELF::R_ARM_THM_MOVT_PREL;
1430 break;
1431 case ARM::fixup_t2_movw_lo16:
1432 case ARM::fixup_t2_movw_lo16_pcrel:
1433 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1434 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001435 }
1436 } else {
1437 switch ((unsigned)Fixup.getKind()) {
1438 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001439 case FK_Data_4:
1440 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001441 default: llvm_unreachable("Unsupported Modifier"); break;
1442 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001443 Type = ELF::R_ARM_GOT_BREL;
1444 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001445 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001446 Type = ELF::R_ARM_TLS_GD32;
1447 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001448 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001449 Type = ELF::R_ARM_TLS_LE32;
1450 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001451 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001452 Type = ELF::R_ARM_TLS_IE32;
1453 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001454 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001455 Type = ELF::R_ARM_ABS32;
1456 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001457 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001458 Type = ELF::R_ARM_GOTOFF32;
1459 break;
1460 }
1461 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001462 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001463 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001464 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001465 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001466 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001467 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001468 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001469 assert(0 && "Unimplemented");
1470 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001471 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001472 Type = ELF::R_ARM_CALL;
1473 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001474 case ARM::fixup_arm_condbranch:
1475 Type = ELF::R_ARM_JUMP24;
1476 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001477 case ARM::fixup_arm_movt_hi16:
1478 Type = ELF::R_ARM_MOVT_ABS;
1479 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001480 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001481 Type = ELF::R_ARM_MOVW_ABS_NC;
1482 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001483 case ARM::fixup_t2_movt_hi16:
1484 Type = ELF::R_ARM_THM_MOVT_ABS;
1485 break;
1486 case ARM::fixup_t2_movw_lo16:
1487 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1488 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001489 }
1490 }
1491
Jason W Kima0871e72010-12-08 23:14:44 +00001492 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001493}
1494
Wesley Peck4b047132010-11-21 22:06:28 +00001495//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001496
Rafael Espindola31f35782010-12-17 18:01:31 +00001497MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001498 raw_ostream &_OS,
1499 bool IsLittleEndian)
1500 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001501}
1502
1503MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1504}
1505
Jason W Kim56a39902010-12-06 21:57:34 +00001506unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001507 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001508 bool IsPCRel,
1509 bool IsRelocWithSymbol,
1510 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001511 // determine the type of the relocation
1512 unsigned Type;
1513 if (IsPCRel) {
1514 switch ((unsigned)Fixup.getKind()) {
1515 default:
1516 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001517 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001518 Type = ELF::R_MICROBLAZE_64_PCREL;
1519 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001520 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001521 Type = ELF::R_MICROBLAZE_32_PCREL;
1522 break;
1523 }
1524 } else {
1525 switch ((unsigned)Fixup.getKind()) {
1526 default: llvm_unreachable("invalid fixup kind!");
1527 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001528 Type = ((IsRelocWithSymbol || Addend !=0)
1529 ? ELF::R_MICROBLAZE_32
1530 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001531 break;
1532 case FK_Data_2:
1533 Type = ELF::R_MICROBLAZE_32;
1534 break;
1535 }
1536 }
Jason W Kim56a39902010-12-06 21:57:34 +00001537 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001538}
Jason W Kimd3443e92010-11-15 16:18:39 +00001539
1540//===- X86ELFObjectWriter -------------------------------------------===//
1541
1542
Rafael Espindola31f35782010-12-17 18:01:31 +00001543X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001544 raw_ostream &_OS,
1545 bool IsLittleEndian)
1546 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001547{}
1548
1549X86ELFObjectWriter::~X86ELFObjectWriter()
1550{}
1551
Jason W Kim56a39902010-12-06 21:57:34 +00001552unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1553 const MCFixup &Fixup,
1554 bool IsPCRel,
1555 bool IsRelocWithSymbol,
1556 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001557 // determine the type of the relocation
1558
Rafael Espindola12203cc2010-11-21 00:48:25 +00001559 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1560 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001561 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001562 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001563 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001564 switch ((unsigned)Fixup.getKind()) {
1565 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001566
1567 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1568 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1569 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1570
Rafael Espindola3a83c402010-12-27 00:36:05 +00001571 case FK_PCRel_8:
1572 assert(Modifier == MCSymbolRefExpr::VK_None);
1573 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001574 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001575 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001576 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001577 case X86::reloc_riprel_4byte:
1578 case FK_PCRel_4:
1579 switch (Modifier) {
1580 default:
1581 llvm_unreachable("Unimplemented");
1582 case MCSymbolRefExpr::VK_None:
1583 Type = ELF::R_X86_64_PC32;
1584 break;
1585 case MCSymbolRefExpr::VK_PLT:
1586 Type = ELF::R_X86_64_PLT32;
1587 break;
1588 case MCSymbolRefExpr::VK_GOTPCREL:
1589 Type = ELF::R_X86_64_GOTPCREL;
1590 break;
1591 case MCSymbolRefExpr::VK_GOTTPOFF:
1592 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001593 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001594 case MCSymbolRefExpr::VK_TLSGD:
1595 Type = ELF::R_X86_64_TLSGD;
1596 break;
1597 case MCSymbolRefExpr::VK_TLSLD:
1598 Type = ELF::R_X86_64_TLSLD;
1599 break;
1600 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001601 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001602 case FK_PCRel_2:
1603 assert(Modifier == MCSymbolRefExpr::VK_None);
1604 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001605 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001606 case FK_PCRel_1:
1607 assert(Modifier == MCSymbolRefExpr::VK_None);
1608 Type = ELF::R_X86_64_PC8;
1609 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001610 }
1611 } else {
1612 switch ((unsigned)Fixup.getKind()) {
1613 default: llvm_unreachable("invalid fixup kind!");
1614 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1615 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001616 assert(isInt<32>(Target.getConstant()));
1617 switch (Modifier) {
1618 default:
1619 llvm_unreachable("Unimplemented");
1620 case MCSymbolRefExpr::VK_None:
1621 Type = ELF::R_X86_64_32S;
1622 break;
1623 case MCSymbolRefExpr::VK_GOT:
1624 Type = ELF::R_X86_64_GOT32;
1625 break;
1626 case MCSymbolRefExpr::VK_GOTPCREL:
1627 Type = ELF::R_X86_64_GOTPCREL;
1628 break;
1629 case MCSymbolRefExpr::VK_TPOFF:
1630 Type = ELF::R_X86_64_TPOFF32;
1631 break;
1632 case MCSymbolRefExpr::VK_DTPOFF:
1633 Type = ELF::R_X86_64_DTPOFF32;
1634 break;
1635 }
1636 break;
1637 case FK_Data_4:
1638 Type = ELF::R_X86_64_32;
1639 break;
1640 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001641 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001642 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1643 }
1644 }
1645 } else {
1646 if (IsPCRel) {
1647 switch (Modifier) {
1648 default:
1649 llvm_unreachable("Unimplemented");
1650 case MCSymbolRefExpr::VK_None:
1651 Type = ELF::R_386_PC32;
1652 break;
1653 case MCSymbolRefExpr::VK_PLT:
1654 Type = ELF::R_386_PLT32;
1655 break;
1656 }
1657 } else {
1658 switch ((unsigned)Fixup.getKind()) {
1659 default: llvm_unreachable("invalid fixup kind!");
1660
1661 case X86::reloc_global_offset_table:
1662 Type = ELF::R_386_GOTPC;
1663 break;
1664
1665 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1666 // instead?
1667 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001668 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001669 case FK_Data_4:
1670 switch (Modifier) {
1671 default:
1672 llvm_unreachable("Unimplemented");
1673 case MCSymbolRefExpr::VK_None:
1674 Type = ELF::R_386_32;
1675 break;
1676 case MCSymbolRefExpr::VK_GOT:
1677 Type = ELF::R_386_GOT32;
1678 break;
1679 case MCSymbolRefExpr::VK_GOTOFF:
1680 Type = ELF::R_386_GOTOFF;
1681 break;
1682 case MCSymbolRefExpr::VK_TLSGD:
1683 Type = ELF::R_386_TLS_GD;
1684 break;
1685 case MCSymbolRefExpr::VK_TPOFF:
1686 Type = ELF::R_386_TLS_LE_32;
1687 break;
1688 case MCSymbolRefExpr::VK_INDNTPOFF:
1689 Type = ELF::R_386_TLS_IE;
1690 break;
1691 case MCSymbolRefExpr::VK_NTPOFF:
1692 Type = ELF::R_386_TLS_LE;
1693 break;
1694 case MCSymbolRefExpr::VK_GOTNTPOFF:
1695 Type = ELF::R_386_TLS_GOTIE;
1696 break;
1697 case MCSymbolRefExpr::VK_TLSLDM:
1698 Type = ELF::R_386_TLS_LDM;
1699 break;
1700 case MCSymbolRefExpr::VK_DTPOFF:
1701 Type = ELF::R_386_TLS_LDO_32;
1702 break;
1703 }
1704 break;
1705 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001706 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001707 case FK_Data_1: Type = ELF::R_386_8; break;
1708 }
1709 }
1710 }
1711
1712 if (RelocNeedsGOT(Modifier))
1713 NeedsGOT = true;
1714
Jason W Kim56a39902010-12-06 21:57:34 +00001715 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001716}