blob: 0aaf77084797346d214dbd4fae21227abb72b5b3 [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 Espindola64695402011-05-16 16:17:21 +0000196
197 if (Data.getFragment()) {
198 if (Data.getFlags() & ELF_Other_ThumbFunc)
199 return Layout.getSymbolOffset(&Data)+1;
200 else
201 return Layout.getSymbolOffset(&Data);
202 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000203
204 return 0;
205}
206
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000207void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
208 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000209 // The presence of symbol versions causes undefined symbols and
210 // versions declared with @@@ to be renamed.
211
212 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
213 ie = Asm.symbol_end(); it != ie; ++it) {
214 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000215 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000216 MCSymbolData &SD = Asm.getSymbolData(Symbol);
217
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000218 // Not an alias.
219 if (&Symbol == &Alias)
220 continue;
221
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000222 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000223 size_t Pos = AliasName.find('@');
224 if (Pos == StringRef::npos)
225 continue;
226
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000227 // Aliases defined with .symvar copy the binding from the symbol they alias.
228 // This is the first place we are able to copy this information.
229 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000230 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000231
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000232 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000233 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
234 continue;
235
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000236 // FIXME: produce a better error message.
237 if (Symbol.isUndefined() && Rest.startswith("@@") &&
238 !Rest.startswith("@@@"))
239 report_fatal_error("A @@ version cannot be undefined");
240
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000241 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000242 }
243}
244
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000245void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
246 MCDataFragment *ShndxF,
247 ELFSymbolData &MSD,
248 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000249 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000250 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000251 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000252
Rafael Espindola7be2c332010-10-31 00:16:26 +0000253 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
254 Data.getSymbol().isVariable();
255
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000256 uint8_t Binding = MCELF::GetBinding(OrigData);
257 uint8_t Visibility = MCELF::GetVisibility(OrigData);
258 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000259
260 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
261 uint8_t Other = Visibility;
262
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000263 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000264 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000265
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000266 assert(!(Data.isCommon() && !Data.isExternal()));
267
Rafael Espindolaf0121242010-12-22 16:03:00 +0000268 const MCExpr *ESize = Data.getSize();
269 if (ESize) {
270 int64_t Res;
271 if (!ESize->EvaluateAsAbsolute(Res, Layout))
272 report_fatal_error("Size expression must be absolute.");
273 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000274 }
275
276 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000277 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
278 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000279}
280
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000281void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
282 MCDataFragment *ShndxF,
283 const MCAssembler &Asm,
284 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000285 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000286 // The string table must be emitted first because we need the index
287 // into the string table for all the symbol names.
288 assert(StringTable.size() && "Missing string table");
289
290 // FIXME: Make sure the start of the symbol table is aligned.
291
292 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000293 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000294
295 // Write the symbol table entries.
296 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
297 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
298 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000299 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000300 }
301
Rafael Espindola71859c62010-09-16 19:46:31 +0000302 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000303 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
304 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000305 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000306 static_cast<const MCSectionELF&>(i->getSection());
307 if (Section.getType() == ELF::SHT_RELA ||
308 Section.getType() == ELF::SHT_REL ||
309 Section.getType() == ELF::SHT_STRTAB ||
310 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000311 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000312 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000313 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000314 LastLocalSymbolIndex++;
315 }
Matt Fleming3565a062010-08-16 18:57:57 +0000316
317 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
318 ELFSymbolData &MSD = ExternalSymbolData[i];
319 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000320 assert(((Data.getFlags() & ELF_STB_Global) ||
321 (Data.getFlags() & ELF_STB_Weak)) &&
322 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000323 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000324 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000325 LastLocalSymbolIndex++;
326 }
327
328 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
329 ELFSymbolData &MSD = UndefinedSymbolData[i];
330 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000331 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000332 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000333 LastLocalSymbolIndex++;
334 }
335}
336
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000337const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
338 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000339 const MCFragment &F,
340 const MCFixup &Fixup,
341 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000342 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000343 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
344 const MCSymbol *Renamed = Renames.lookup(&Symbol);
345 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000346
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000347 if (ASymbol.isUndefined()) {
348 if (Renamed)
349 return Renamed;
350 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000351 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000352
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000353 if (SD.isExternal()) {
354 if (Renamed)
355 return Renamed;
356 return &Symbol;
357 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000358
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000359 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000360 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000361 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000362
Rafael Espindola25958732010-11-24 21:57:39 +0000363 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000364 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000365
Rafael Espindola25958732010-11-24 21:57:39 +0000366 if (secKind.isThreadLocal()) {
367 if (Renamed)
368 return Renamed;
369 return &Symbol;
370 }
371
Rafael Espindola8cecf252010-10-06 16:23:36 +0000372 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000373 const MCSectionELF &Sec2 =
374 static_cast<const MCSectionELF&>(F.getParent()->getSection());
375
Rafael Espindola8cecf252010-10-06 16:23:36 +0000376 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000377 (Kind == MCSymbolRefExpr::VK_PLT ||
378 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000379 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000380 if (Renamed)
381 return Renamed;
382 return &Symbol;
383 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000384
Rafael Espindola1c130262011-01-23 04:43:11 +0000385 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000386 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000387 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000388 if (Renamed)
389 return Renamed;
390 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000391 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000392
Jason W Kime964d112011-05-11 22:53:06 +0000393 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
394
Rafael Espindola73ffea42010-09-25 05:42:19 +0000395}
396
Matt Fleming3565a062010-08-16 18:57:57 +0000397
Jason W Kim56a39902010-12-06 21:57:34 +0000398void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
399 const MCAsmLayout &Layout,
400 const MCFragment *Fragment,
401 const MCFixup &Fixup,
402 MCValue Target,
403 uint64_t &FixedValue) {
404 int64_t Addend = 0;
405 int Index = 0;
406 int64_t Value = Target.getConstant();
407 const MCSymbol *RelocSymbol = NULL;
408
Rafael Espindola127a6a42010-12-17 07:28:17 +0000409 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000410 if (!Target.isAbsolute()) {
411 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
412 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000413 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000414
415 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
416 const MCSymbol &SymbolB = RefB->getSymbol();
417 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
418 IsPCRel = true;
419
420 // Offset of the symbol in the section
421 int64_t a = Layout.getSymbolOffset(&SDB);
422
423 // Ofeset of the relocation in the section
424 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
425 Value += b - a;
426 }
427
428 if (!RelocSymbol) {
429 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
430 MCFragment *F = SD.getFragment();
431
432 Index = F->getParent()->getOrdinal() + 1;
433
434 // Offset of the symbol in the section
435 Value += Layout.getSymbolOffset(&SD);
436 } else {
437 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
438 WeakrefUsedInReloc.insert(RelocSymbol);
439 else
440 UsedInReloc.insert(RelocSymbol);
441 Index = -1;
442 }
443 Addend = Value;
444 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000445 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000446 Value = 0;
447 }
448
449 FixedValue = Value;
450 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
451 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000452
Jason W Kim56a39902010-12-06 21:57:34 +0000453 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
454 Fixup.getOffset();
455
Rafael Espindolabff66a82010-12-18 03:27:34 +0000456 if (!hasRelocationAddend())
457 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000458 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
459 Relocations[Fragment->getParent()].push_back(ERE);
460}
461
462
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000463uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000464ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
465 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000466 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000467 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000468}
469
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000470bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
471 const MCSymbolData &Data,
472 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000473 if (Data.getFlags() & ELF_Other_Weakref)
474 return false;
475
Rafael Espindolabd701182010-10-19 19:31:37 +0000476 if (Used)
477 return true;
478
Rafael Espindola88182132010-10-27 15:18:17 +0000479 if (Renamed)
480 return false;
481
Rafael Espindola737cd212010-10-05 18:01:23 +0000482 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000483
Rafael Espindolad1798862010-10-29 23:09:31 +0000484 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
485 return true;
486
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000487 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000488 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
489 return false;
490
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000491 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000492 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000493 return false;
494
Rafael Espindola737cd212010-10-05 18:01:23 +0000495 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
496 return false;
497
Rafael Espindolabd701182010-10-19 19:31:37 +0000498 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000499 return false;
500
501 return true;
502}
503
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000504bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
505 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000506 if (Data.isExternal())
507 return false;
508
509 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000510 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000511
512 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
513 if (isSignature && !isUsedInReloc)
514 return true;
515
Rafael Espindola737cd212010-10-05 18:01:23 +0000516 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000517 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000518
519 return true;
520}
521
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000522void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000523 SectionIndexMapTy &SectionIndexMap,
524 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000525 unsigned Index = 1;
526 for (MCAssembler::iterator it = Asm.begin(),
527 ie = Asm.end(); it != ie; ++it) {
528 const MCSectionELF &Section =
529 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000530 if (Section.getType() != ELF::SHT_GROUP)
531 continue;
532 SectionIndexMap[&Section] = Index++;
533 }
534
535 for (MCAssembler::iterator it = Asm.begin(),
536 ie = Asm.end(); it != ie; ++it) {
537 const MCSectionELF &Section =
538 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000539 if (Section.getType() == ELF::SHT_GROUP ||
540 Section.getType() == ELF::SHT_REL ||
541 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000542 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000543 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000544 const MCSectionELF *RelSection = RelMap.lookup(&Section);
545 if (RelSection)
546 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000547 }
548}
549
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000550void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000551 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000552 RevGroupMapTy RevGroupMap,
553 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000554 // FIXME: Is this the correct place to do this?
555 if (NeedsGOT) {
556 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
557 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
558 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
559 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000560 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000561 }
562
Matt Fleming3565a062010-08-16 18:57:57 +0000563 // Index 0 is always the empty string.
564 StringMap<uint64_t> StringIndexMap;
565 StringTable += '\x00';
566
Rafael Espindolad5321da2011-04-07 23:21:52 +0000567 // FIXME: We could optimize suffixes in strtab in the same way we
568 // optimize them in shstrtab.
569
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000570 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000571 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
572 ie = Asm.symbol_end(); it != ie; ++it) {
573 const MCSymbol &Symbol = it->getSymbol();
574
Rafael Espindola484291c2010-11-01 14:28:48 +0000575 bool Used = UsedInReloc.count(&Symbol);
576 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000577 bool isSignature = RevGroupMap.count(&Symbol);
578
579 if (!isInSymtab(Asm, *it,
580 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000581 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000582 continue;
583
Matt Fleming3565a062010-08-16 18:57:57 +0000584 ELFSymbolData MSD;
585 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000586 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000587
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000588 // Undefined symbols are global, but this is the first place we
589 // are able to set it.
590 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000591 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000592 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000593 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
594 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000595 }
596
Rafael Espindola484291c2010-11-01 14:28:48 +0000597 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000598 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000599
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000600 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000601 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000602 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000603 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000604 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000605 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000606 if (isSignature && !Used)
607 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
608 else
609 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000610 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000611 const MCSectionELF &Section =
612 static_cast<const MCSectionELF&>(RefSymbol.getSection());
613 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000614 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
615 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000616 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000617 }
618
Rafael Espindola88182132010-10-27 15:18:17 +0000619 // The @@@ in symbol version is replaced with @ in undefined symbols and
620 // @@ in defined ones.
621 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000622 SmallString<32> Buf;
623
Rafael Espindola88182132010-10-27 15:18:17 +0000624 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000625 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000626 Buf += Name.substr(0, Pos);
627 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
628 Buf += Name.substr(Pos + Skip);
629 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000630 }
631
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000632 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000633 if (!Entry) {
634 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000635 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000636 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000637 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000638 MSD.StringIndex = Entry;
639 if (MSD.SectionIndex == ELF::SHN_UNDEF)
640 UndefinedSymbolData.push_back(MSD);
641 else if (Local)
642 LocalSymbolData.push_back(MSD);
643 else
644 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000645 }
646
647 // Symbols are required to be in lexicographic order.
648 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
649 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
650 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
651
652 // Set the symbol indices. Local symbols must come before all other
653 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000654 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000655 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
656 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000657
658 Index += NumRegularSections;
659
Matt Fleming3565a062010-08-16 18:57:57 +0000660 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
661 ExternalSymbolData[i].SymbolData->setIndex(Index++);
662 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
663 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
664}
665
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000666void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
667 MCAsmLayout &Layout,
668 RelMapTy &RelMap) {
669 for (MCAssembler::const_iterator it = Asm.begin(),
670 ie = Asm.end(); it != ie; ++it) {
671 const MCSectionData &SD = *it;
672 if (Relocations[&SD].empty())
673 continue;
674
Matt Fleming3565a062010-08-16 18:57:57 +0000675 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000676 const MCSectionELF &Section =
677 static_cast<const MCSectionELF&>(SD.getSection());
678
679 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000680 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000681 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000682
683 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000684 if (hasRelocationAddend())
685 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000686 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000687 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000688
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000689 const MCSectionELF *RelaSection =
690 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
691 ELF::SHT_RELA : ELF::SHT_REL, 0,
692 SectionKind::getReadOnly(),
693 EntrySize, "");
694 RelMap[&Section] = RelaSection;
695 Asm.getOrCreateSectionData(*RelaSection);
696 }
697}
Matt Fleming3565a062010-08-16 18:57:57 +0000698
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000699void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
700 const RelMapTy &RelMap) {
701 for (MCAssembler::const_iterator it = Asm.begin(),
702 ie = Asm.end(); it != ie; ++it) {
703 const MCSectionData &SD = *it;
704 const MCSectionELF &Section =
705 static_cast<const MCSectionELF&>(SD.getSection());
706
707 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
708 if (!RelaSection)
709 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000710 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000711 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000712
713 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000714 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000715 }
716}
717
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000718void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
719 uint64_t Flags, uint64_t Address,
720 uint64_t Offset, uint64_t Size,
721 uint32_t Link, uint32_t Info,
722 uint64_t Alignment,
723 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000724 Write32(Name); // sh_name: index into string table
725 Write32(Type); // sh_type
726 WriteWord(Flags); // sh_flags
727 WriteWord(Address); // sh_addr
728 WriteWord(Offset); // sh_offset
729 WriteWord(Size); // sh_size
730 Write32(Link); // sh_link
731 Write32(Info); // sh_info
732 WriteWord(Alignment); // sh_addralign
733 WriteWord(EntrySize); // sh_entsize
734}
735
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000736void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
737 MCDataFragment *F,
738 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000739 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
740 // sort by the r_offset just like gnu as does
741 array_pod_sort(Relocs.begin(), Relocs.end());
742
743 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
744 ELFRelocationEntry entry = Relocs[e - i - 1];
745
Rafael Espindola12203cc2010-11-21 00:48:25 +0000746 if (!entry.Index)
747 ;
748 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000749 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
750 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000751 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000752 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000753 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000754
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000755 struct ELF::Elf64_Rela ERE64;
756 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000757 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000758
Rafael Espindolabff66a82010-12-18 03:27:34 +0000759 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000760 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000761 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000762 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000763
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000764 struct ELF::Elf32_Rela ERE32;
765 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000766 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000767
Rafael Espindolabff66a82010-12-18 03:27:34 +0000768 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000769 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000770 }
Matt Fleming3565a062010-08-16 18:57:57 +0000771 }
772}
773
Rafael Espindolad5321da2011-04-07 23:21:52 +0000774static int compareBySuffix(const void *a, const void *b) {
775 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
776 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
777 const StringRef &NameA = secA->getSectionName();
778 const StringRef &NameB = secB->getSectionName();
779 const unsigned sizeA = NameA.size();
780 const unsigned sizeB = NameB.size();
781 const unsigned len = std::min(sizeA, sizeB);
782 for (unsigned int i = 0; i < len; ++i) {
783 char ca = NameA[sizeA - i - 1];
784 char cb = NameB[sizeB - i - 1];
785 if (ca != cb)
786 return cb - ca;
787 }
788
789 return sizeB - sizeA;
790}
791
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000792void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
793 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000794 SectionIndexMapTy &SectionIndexMap,
795 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000796 MCContext &Ctx = Asm.getContext();
797 MCDataFragment *F;
798
Rafael Espindolabff66a82010-12-18 03:27:34 +0000799 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000800
Rafael Espindola38738bf2010-09-22 19:04:41 +0000801 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000802 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000803 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000804 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000805 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
806 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000807
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000808 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000809 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
810 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000811 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000812 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000813 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000814
815 MCSectionData *SymtabShndxSD = NULL;
816
817 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000818 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000819 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000820 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000821 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
822 SymtabShndxSD->setAlignment(4);
823 }
Matt Fleming3565a062010-08-16 18:57:57 +0000824
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000825 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000826 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000827 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000828 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
829 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000830
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000831 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
832
833 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
834 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
835 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000836
837 // Symbol table
838 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000839 MCDataFragment *ShndxF = NULL;
840 if (NeedsSymtabShndx) {
841 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000842 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000843 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000844
Matt Fleming3565a062010-08-16 18:57:57 +0000845 F = new MCDataFragment(&StrtabSD);
846 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000847
Matt Fleming3565a062010-08-16 18:57:57 +0000848 F = new MCDataFragment(&ShstrtabSD);
849
Rafael Espindolad5321da2011-04-07 23:21:52 +0000850 std::vector<const MCSectionELF*> Sections;
851 for (MCAssembler::const_iterator it = Asm.begin(),
852 ie = Asm.end(); it != ie; ++it) {
853 const MCSectionELF &Section =
854 static_cast<const MCSectionELF&>(it->getSection());
855 Sections.push_back(&Section);
856 }
857 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
858
Matt Fleming3565a062010-08-16 18:57:57 +0000859 // Section header string table.
860 //
861 // The first entry of a string table holds a null character so skip
862 // section 0.
863 uint64_t Index = 1;
864 F->getContents() += '\x00';
865
Rafael Espindolad5321da2011-04-07 23:21:52 +0000866 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
867 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000868
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000869 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000870 if (I != 0) {
871 StringRef PreviousName = Sections[I - 1]->getSectionName();
872 if (PreviousName.endswith(Name)) {
873 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
874 continue;
875 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000876 }
Matt Fleming3565a062010-08-16 18:57:57 +0000877 // Remember the index into the string table so we can write it
878 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000879 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000880
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000881 Index += Name.size() + 1;
882 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000883 F->getContents() += '\x00';
884 }
Rafael Espindola70703872010-09-30 02:22:20 +0000885}
886
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000887void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
888 MCAsmLayout &Layout,
889 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000890 RevGroupMapTy &RevGroupMap,
891 SectionIndexMapTy &SectionIndexMap,
892 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000893 // Create the .note.GNU-stack section if needed.
894 MCContext &Ctx = Asm.getContext();
895 if (Asm.getNoExecStack()) {
896 const MCSectionELF *GnuStackSection =
897 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
898 SectionKind::getReadOnly());
899 Asm.getOrCreateSectionData(*GnuStackSection);
900 }
901
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000902 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000903 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
904 it != ie; ++it) {
905 const MCSectionELF &Section =
906 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000907 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000908 continue;
909
910 const MCSymbol *SignatureSymbol = Section.getGroup();
911 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000912 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000913 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000914 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000915 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
916 Data.setAlignment(4);
917 MCDataFragment *F = new MCDataFragment(&Data);
918 String32(*F, ELF::GRP_COMDAT);
919 }
920 GroupMap[Group] = SignatureSymbol;
921 }
922
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000923 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
924
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000925 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000926 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000927 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000928 const MCSectionELF &Section =
929 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000930 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000931 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000932 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000933 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
934 // FIXME: we could use the previous fragment
935 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000936 unsigned Index = SectionIndexMap.lookup(&Section);
937 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000938 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000939}
940
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000941void ELFObjectWriter::WriteSection(MCAssembler &Asm,
942 const SectionIndexMapTy &SectionIndexMap,
943 uint32_t GroupSymbolIndex,
944 uint64_t Offset, uint64_t Size,
945 uint64_t Alignment,
946 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000947 uint64_t sh_link = 0;
948 uint64_t sh_info = 0;
949
950 switch(Section.getType()) {
951 case ELF::SHT_DYNAMIC:
952 sh_link = SectionStringTableIndex[&Section];
953 sh_info = 0;
954 break;
955
956 case ELF::SHT_REL:
957 case ELF::SHT_RELA: {
958 const MCSectionELF *SymtabSection;
959 const MCSectionELF *InfoSection;
960 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
961 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000962 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000963 sh_link = SectionIndexMap.lookup(SymtabSection);
964 assert(sh_link && ".symtab not found");
965
966 // Remove ".rel" and ".rela" prefixes.
967 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
968 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
969
970 InfoSection = Asm.getContext().getELFSection(SectionName,
971 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000972 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000973 sh_info = SectionIndexMap.lookup(InfoSection);
974 break;
975 }
976
977 case ELF::SHT_SYMTAB:
978 case ELF::SHT_DYNSYM:
979 sh_link = StringTableIndex;
980 sh_info = LastLocalSymbolIndex;
981 break;
982
983 case ELF::SHT_SYMTAB_SHNDX:
984 sh_link = SymbolTableIndex;
985 break;
986
987 case ELF::SHT_PROGBITS:
988 case ELF::SHT_STRTAB:
989 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000990 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000991 case ELF::SHT_NULL:
992 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +0000993 case ELF::SHT_INIT_ARRAY:
994 case ELF::SHT_FINI_ARRAY:
995 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000996 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000997 // Nothing to do.
998 break;
999
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001000 case ELF::SHT_GROUP: {
1001 sh_link = SymbolTableIndex;
1002 sh_info = GroupSymbolIndex;
1003 break;
1004 }
1005
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001006 default:
1007 assert(0 && "FIXME: sh_type value not supported!");
1008 break;
1009 }
1010
1011 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1012 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1013 Alignment, Section.getEntrySize());
1014}
1015
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001016bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001017 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001018 !SD.getSection().isVirtualSection();
1019}
1020
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001021uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001022 uint64_t Ret = 0;
1023 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1024 ++i) {
1025 const MCFragment &F = *i;
1026 assert(F.getKind() == MCFragment::FT_Data);
1027 Ret += cast<MCDataFragment>(F).getContents().size();
1028 }
1029 return Ret;
1030}
1031
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001032uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1033 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001034 if (IsELFMetaDataSection(SD))
1035 return DataSectionSize(SD);
1036 return Layout.getSectionFileSize(&SD);
1037}
1038
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001039uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1040 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001041 if (IsELFMetaDataSection(SD))
1042 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001043 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001044}
1045
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001046void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1047 const MCAsmLayout &Layout,
1048 const MCSectionELF &Section) {
1049 uint64_t FileOff = OS.tell();
1050 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1051
1052 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1053 WriteZeros(Padding);
1054 FileOff += Padding;
1055
1056 FileOff += GetSectionFileSize(Layout, SD);
1057
1058 if (IsELFMetaDataSection(SD)) {
1059 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1060 ++i) {
1061 const MCFragment &F = *i;
1062 assert(F.getKind() == MCFragment::FT_Data);
1063 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1064 }
1065 } else {
1066 Asm.WriteSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001067 }
1068}
1069
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001070void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1071 const GroupMapTy &GroupMap,
1072 const MCAsmLayout &Layout,
1073 const SectionIndexMapTy &SectionIndexMap,
1074 const SectionOffsetMapTy &SectionOffsetMap) {
1075 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001076
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001077 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001078 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001079
1080 for (SectionIndexMapTy::const_iterator i=
1081 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1082 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001083 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001084 }
1085
Matt Fleming3565a062010-08-16 18:57:57 +00001086 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001087 uint64_t FirstSectionSize =
1088 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1089 uint32_t FirstSectionLink =
1090 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1091 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001092
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001093 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001094 const MCSectionELF &Section = *Sections[i];
1095 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1096 uint32_t GroupSymbolIndex;
1097 if (Section.getType() != ELF::SHT_GROUP)
1098 GroupSymbolIndex = 0;
1099 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001100 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1101 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001102
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001103 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001104
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001105 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001106 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001107 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001108 }
1109}
1110
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001111void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1112 std::vector<const MCSectionELF*> &Sections) {
1113 for (MCAssembler::iterator it = Asm.begin(),
1114 ie = Asm.end(); it != ie; ++it) {
1115 const MCSectionELF &Section =
1116 static_cast<const MCSectionELF &>(it->getSection());
1117 if (Section.getType() == ELF::SHT_GROUP)
1118 Sections.push_back(&Section);
1119 }
1120
1121 for (MCAssembler::iterator it = Asm.begin(),
1122 ie = Asm.end(); it != ie; ++it) {
1123 const MCSectionELF &Section =
1124 static_cast<const MCSectionELF &>(it->getSection());
1125 if (Section.getType() != ELF::SHT_GROUP &&
1126 Section.getType() != ELF::SHT_REL &&
1127 Section.getType() != ELF::SHT_RELA)
1128 Sections.push_back(&Section);
1129 }
1130
1131 for (MCAssembler::iterator it = Asm.begin(),
1132 ie = Asm.end(); it != ie; ++it) {
1133 const MCSectionELF &Section =
1134 static_cast<const MCSectionELF &>(it->getSection());
1135 if (Section.getType() == ELF::SHT_REL ||
1136 Section.getType() == ELF::SHT_RELA)
1137 Sections.push_back(&Section);
1138 }
1139}
1140
1141void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1142 const MCAsmLayout &Layout) {
1143 GroupMapTy GroupMap;
1144 RevGroupMapTy RevGroupMap;
1145 SectionIndexMapTy SectionIndexMap;
1146
1147 unsigned NumUserSections = Asm.size();
1148
1149 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1150 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1151
1152 const unsigned NumUserAndRelocSections = Asm.size();
1153 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1154 RevGroupMap, SectionIndexMap, RelMap);
1155 const unsigned AllSections = Asm.size();
1156 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1157
1158 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1159
1160 // Compute symbol table information.
1161 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1162
1163
1164 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1165
1166 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1167 const_cast<MCAsmLayout&>(Layout),
1168 SectionIndexMap,
1169 RelMap);
1170
1171 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1172 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1173 sizeof(ELF::Elf32_Ehdr);
1174 uint64_t FileOff = HeaderSize;
1175
1176 std::vector<const MCSectionELF*> Sections;
1177 ComputeSectionOrder(Asm, Sections);
1178 unsigned NumSections = Sections.size();
1179 SectionOffsetMapTy SectionOffsetMap;
1180 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1181 const MCSectionELF &Section = *Sections[i];
1182 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1183
1184 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1185
1186 // Remember the offset into the file for this section.
1187 SectionOffsetMap[&Section] = FileOff;
1188
1189 // Get the size of the section in the output file (including padding).
1190 FileOff += GetSectionFileSize(Layout, SD);
1191 }
1192
1193 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1194
1195 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1196
1197 uint64_t SectionHeaderEntrySize = is64Bit() ?
1198 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1199 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1200
1201 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1202 const MCSectionELF &Section = *Sections[i];
1203 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1204
1205 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1206
1207 // Remember the offset into the file for this section.
1208 SectionOffsetMap[&Section] = FileOff;
1209
1210 // Get the size of the section in the output file (including padding).
1211 FileOff += GetSectionFileSize(Layout, SD);
1212 }
1213
1214 // Write out the ELF header ...
1215 WriteHeader(SectionHeaderOffset, NumSections + 1);
1216
1217 // ... then the regular sections ...
1218 // + because of .shstrtab
1219 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1220 WriteDataSectionData(Asm, Layout, *Sections[i]);
1221
1222 FileOff = OS.tell();
1223 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1224 WriteZeros(Padding);
1225
1226 // ... then the section header table ...
1227 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1228 SectionOffsetMap);
1229
1230 FileOff = OS.tell();
1231
1232 // ... and then the remainting sections ...
1233 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1234 WriteDataSectionData(Asm, Layout, *Sections[i]);
1235}
1236
Eli Friedman78c1e172011-03-03 07:24:36 +00001237bool
1238ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1239 const MCSymbolData &DataA,
1240 const MCFragment &FB,
1241 bool InSet,
1242 bool IsPCRel) const {
1243 if (DataA.getFlags() & ELF_STB_Weak)
1244 return false;
1245 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1246 Asm, DataA, FB,InSet, IsPCRel);
1247}
1248
Rafael Espindola6024c972010-12-17 17:45:22 +00001249MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1250 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001251 bool IsLittleEndian) {
1252 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001253 case ELF::EM_386:
1254 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001255 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001256 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001257 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001258 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001259 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001260 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001261 }
1262}
1263
1264
1265/// START OF SUBCLASSES for ELFObjectWriter
1266//===- ARMELFObjectWriter -------------------------------------------===//
1267
Rafael Espindola31f35782010-12-17 18:01:31 +00001268ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001269 raw_ostream &_OS,
1270 bool IsLittleEndian)
1271 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001272{}
1273
1274ARMELFObjectWriter::~ARMELFObjectWriter()
1275{}
1276
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001277// FIXME: get the real EABI Version from the Triple.
1278void ARMELFObjectWriter::WriteEFlags() {
1279 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1280}
1281
Jason W Kim953a2a32011-02-07 01:11:15 +00001282// In ARM, _MergedGlobals and other most symbols get emitted directly.
1283// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001284// This code is an approximation of what ARM/gcc does.
1285
1286STATISTIC(PCRelCount, "Total number of PIC Relocations");
1287STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001288
1289const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1290 const MCValue &Target,
1291 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001292 const MCFixup &Fixup,
1293 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001294 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1295 bool EmitThisSym = false;
1296
Jason W Kime964d112011-05-11 22:53:06 +00001297 const MCSectionELF &Section =
1298 static_cast<const MCSectionELF&>(Symbol.getSection());
Jason W Kime964d112011-05-11 22:53:06 +00001299 bool InNormalSection = true;
1300 unsigned RelocType = 0;
1301 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1302
Matt Beaumont-Gay6dcd4132011-05-11 23:34:51 +00001303 DEBUG(
1304 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1305 MCSymbolRefExpr::VariantKind Kind2;
1306 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1307 MCSymbolRefExpr::VK_None;
1308 dbgs() << "considering symbol "
Jason W Kime964d112011-05-11 22:53:06 +00001309 << Section.getSectionName() << "/"
1310 << Symbol.getName() << "/"
1311 << " Rel:" << (unsigned)RelocType
1312 << " Kind: " << (int)Kind << "/" << (int)Kind2
1313 << " Tmp:"
1314 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1315 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1316 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1317
1318 if (IsPCRel || ForceARMElfPIC) { ++PCRelCount;
1319 switch (RelocType) {
1320 default:
1321 // Most relocation types are emitted as explicit symbols
1322 InNormalSection =
1323 StringSwitch<bool>(Section.getSectionName())
1324 .Case(".data.rel.ro.local", false)
1325 .Case(".data.rel", false)
1326 .Case(".bss", false)
1327 .Default(true);
1328 EmitThisSym = true;
1329 break;
1330 case ELF::R_ARM_ABS32:
1331 // But things get strange with R_ARM_ABS32
1332 // In this case, most things that go in .rodata show up
1333 // as section relative relocations
1334 InNormalSection =
1335 StringSwitch<bool>(Section.getSectionName())
1336 .Case(".data.rel.ro.local", false)
1337 .Case(".data.rel", false)
1338 .Case(".rodata", false)
1339 .Case(".bss", false)
1340 .Default(true);
1341 EmitThisSym = false;
1342 break;
1343 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001344 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001345 NonPCRelCount++;
1346 InNormalSection =
1347 StringSwitch<bool>(Section.getSectionName())
1348 .Case(".data.rel.ro.local", false)
1349 .Case(".rodata", false)
1350 .Case(".data.rel", false)
1351 .Case(".bss", false)
1352 .Default(true);
1353
1354 switch (RelocType) {
1355 default: EmitThisSym = true; break;
1356 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1357 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001358 }
Jason W Kime964d112011-05-11 22:53:06 +00001359
Jason W Kim953a2a32011-02-07 01:11:15 +00001360 if (EmitThisSym)
1361 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001362 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001363 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001364 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001365 return NULL;
1366}
1367
Jason W Kime964d112011-05-11 22:53:06 +00001368// Need to examine the Fixup when determining whether to
1369// emit the relocation as an explicit symbol or as a section relative
1370// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001371unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1372 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001373 bool IsPCRel,
1374 bool IsRelocWithSymbol,
1375 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001376 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1377 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1378
Jason W Kime964d112011-05-11 22:53:06 +00001379 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1380
1381 if (RelocNeedsGOT(Modifier))
1382 NeedsGOT = true;
1383
1384 return Type;
1385}
1386
1387unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1388 const MCFixup &Fixup,
1389 bool IsPCRel) const {
1390 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1391 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1392
Jason W Kima0871e72010-12-08 23:14:44 +00001393 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001394 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001395 switch ((unsigned)Fixup.getKind()) {
1396 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001397 case FK_Data_4:
1398 switch (Modifier) {
1399 default: llvm_unreachable("Unsupported Modifier");
1400 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001401 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001402 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001403 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001404 assert(0 && "unimplemented");
1405 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001406 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1407 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001408 break;
1409 }
1410 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001411 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001412 switch (Modifier) {
1413 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001414 Type = ELF::R_ARM_PLT32;
1415 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001416 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001417 Type = ELF::R_ARM_CALL;
1418 break;
1419 }
1420 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001421 case ARM::fixup_arm_condbranch:
1422 Type = ELF::R_ARM_JUMP24;
1423 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001424 case ARM::fixup_arm_movt_hi16:
1425 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001426 Type = ELF::R_ARM_MOVT_PREL;
1427 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001428 case ARM::fixup_arm_movw_lo16:
1429 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001430 Type = ELF::R_ARM_MOVW_PREL_NC;
1431 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001432 case ARM::fixup_t2_movt_hi16:
1433 case ARM::fixup_t2_movt_hi16_pcrel:
1434 Type = ELF::R_ARM_THM_MOVT_PREL;
1435 break;
1436 case ARM::fixup_t2_movw_lo16:
1437 case ARM::fixup_t2_movw_lo16_pcrel:
1438 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1439 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001440 }
1441 } else {
1442 switch ((unsigned)Fixup.getKind()) {
1443 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001444 case FK_Data_4:
1445 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001446 default: llvm_unreachable("Unsupported Modifier"); break;
1447 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001448 Type = ELF::R_ARM_GOT_BREL;
1449 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001450 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001451 Type = ELF::R_ARM_TLS_GD32;
1452 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001453 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001454 Type = ELF::R_ARM_TLS_LE32;
1455 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001456 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001457 Type = ELF::R_ARM_TLS_IE32;
1458 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001459 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001460 Type = ELF::R_ARM_ABS32;
1461 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001462 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001463 Type = ELF::R_ARM_GOTOFF32;
1464 break;
1465 }
1466 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001467 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001468 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001469 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001470 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001471 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001472 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001473 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001474 assert(0 && "Unimplemented");
1475 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001476 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001477 Type = ELF::R_ARM_CALL;
1478 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001479 case ARM::fixup_arm_condbranch:
1480 Type = ELF::R_ARM_JUMP24;
1481 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001482 case ARM::fixup_arm_movt_hi16:
1483 Type = ELF::R_ARM_MOVT_ABS;
1484 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001485 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001486 Type = ELF::R_ARM_MOVW_ABS_NC;
1487 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001488 case ARM::fixup_t2_movt_hi16:
1489 Type = ELF::R_ARM_THM_MOVT_ABS;
1490 break;
1491 case ARM::fixup_t2_movw_lo16:
1492 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1493 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001494 }
1495 }
1496
Jason W Kima0871e72010-12-08 23:14:44 +00001497 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001498}
1499
Wesley Peck4b047132010-11-21 22:06:28 +00001500//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001501
Rafael Espindola31f35782010-12-17 18:01:31 +00001502MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001503 raw_ostream &_OS,
1504 bool IsLittleEndian)
1505 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001506}
1507
1508MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1509}
1510
Jason W Kim56a39902010-12-06 21:57:34 +00001511unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001512 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001513 bool IsPCRel,
1514 bool IsRelocWithSymbol,
1515 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001516 // determine the type of the relocation
1517 unsigned Type;
1518 if (IsPCRel) {
1519 switch ((unsigned)Fixup.getKind()) {
1520 default:
1521 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001522 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001523 Type = ELF::R_MICROBLAZE_64_PCREL;
1524 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001525 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001526 Type = ELF::R_MICROBLAZE_32_PCREL;
1527 break;
1528 }
1529 } else {
1530 switch ((unsigned)Fixup.getKind()) {
1531 default: llvm_unreachable("invalid fixup kind!");
1532 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001533 Type = ((IsRelocWithSymbol || Addend !=0)
1534 ? ELF::R_MICROBLAZE_32
1535 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001536 break;
1537 case FK_Data_2:
1538 Type = ELF::R_MICROBLAZE_32;
1539 break;
1540 }
1541 }
Jason W Kim56a39902010-12-06 21:57:34 +00001542 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001543}
Jason W Kimd3443e92010-11-15 16:18:39 +00001544
1545//===- X86ELFObjectWriter -------------------------------------------===//
1546
1547
Rafael Espindola31f35782010-12-17 18:01:31 +00001548X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001549 raw_ostream &_OS,
1550 bool IsLittleEndian)
1551 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001552{}
1553
1554X86ELFObjectWriter::~X86ELFObjectWriter()
1555{}
1556
Jason W Kim56a39902010-12-06 21:57:34 +00001557unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1558 const MCFixup &Fixup,
1559 bool IsPCRel,
1560 bool IsRelocWithSymbol,
1561 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001562 // determine the type of the relocation
1563
Rafael Espindola12203cc2010-11-21 00:48:25 +00001564 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1565 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001566 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001567 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001568 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001569 switch ((unsigned)Fixup.getKind()) {
1570 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001571
1572 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1573 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1574 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1575
Rafael Espindola3a83c402010-12-27 00:36:05 +00001576 case FK_PCRel_8:
1577 assert(Modifier == MCSymbolRefExpr::VK_None);
1578 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001579 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001580 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001581 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001582 case X86::reloc_riprel_4byte:
1583 case FK_PCRel_4:
1584 switch (Modifier) {
1585 default:
1586 llvm_unreachable("Unimplemented");
1587 case MCSymbolRefExpr::VK_None:
1588 Type = ELF::R_X86_64_PC32;
1589 break;
1590 case MCSymbolRefExpr::VK_PLT:
1591 Type = ELF::R_X86_64_PLT32;
1592 break;
1593 case MCSymbolRefExpr::VK_GOTPCREL:
1594 Type = ELF::R_X86_64_GOTPCREL;
1595 break;
1596 case MCSymbolRefExpr::VK_GOTTPOFF:
1597 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001598 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001599 case MCSymbolRefExpr::VK_TLSGD:
1600 Type = ELF::R_X86_64_TLSGD;
1601 break;
1602 case MCSymbolRefExpr::VK_TLSLD:
1603 Type = ELF::R_X86_64_TLSLD;
1604 break;
1605 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001606 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001607 case FK_PCRel_2:
1608 assert(Modifier == MCSymbolRefExpr::VK_None);
1609 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001610 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001611 case FK_PCRel_1:
1612 assert(Modifier == MCSymbolRefExpr::VK_None);
1613 Type = ELF::R_X86_64_PC8;
1614 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001615 }
1616 } else {
1617 switch ((unsigned)Fixup.getKind()) {
1618 default: llvm_unreachable("invalid fixup kind!");
1619 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1620 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001621 assert(isInt<32>(Target.getConstant()));
1622 switch (Modifier) {
1623 default:
1624 llvm_unreachable("Unimplemented");
1625 case MCSymbolRefExpr::VK_None:
1626 Type = ELF::R_X86_64_32S;
1627 break;
1628 case MCSymbolRefExpr::VK_GOT:
1629 Type = ELF::R_X86_64_GOT32;
1630 break;
1631 case MCSymbolRefExpr::VK_GOTPCREL:
1632 Type = ELF::R_X86_64_GOTPCREL;
1633 break;
1634 case MCSymbolRefExpr::VK_TPOFF:
1635 Type = ELF::R_X86_64_TPOFF32;
1636 break;
1637 case MCSymbolRefExpr::VK_DTPOFF:
1638 Type = ELF::R_X86_64_DTPOFF32;
1639 break;
1640 }
1641 break;
1642 case FK_Data_4:
1643 Type = ELF::R_X86_64_32;
1644 break;
1645 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001646 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001647 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1648 }
1649 }
1650 } else {
1651 if (IsPCRel) {
1652 switch (Modifier) {
1653 default:
1654 llvm_unreachable("Unimplemented");
1655 case MCSymbolRefExpr::VK_None:
1656 Type = ELF::R_386_PC32;
1657 break;
1658 case MCSymbolRefExpr::VK_PLT:
1659 Type = ELF::R_386_PLT32;
1660 break;
1661 }
1662 } else {
1663 switch ((unsigned)Fixup.getKind()) {
1664 default: llvm_unreachable("invalid fixup kind!");
1665
1666 case X86::reloc_global_offset_table:
1667 Type = ELF::R_386_GOTPC;
1668 break;
1669
1670 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1671 // instead?
1672 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001673 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001674 case FK_Data_4:
1675 switch (Modifier) {
1676 default:
1677 llvm_unreachable("Unimplemented");
1678 case MCSymbolRefExpr::VK_None:
1679 Type = ELF::R_386_32;
1680 break;
1681 case MCSymbolRefExpr::VK_GOT:
1682 Type = ELF::R_386_GOT32;
1683 break;
1684 case MCSymbolRefExpr::VK_GOTOFF:
1685 Type = ELF::R_386_GOTOFF;
1686 break;
1687 case MCSymbolRefExpr::VK_TLSGD:
1688 Type = ELF::R_386_TLS_GD;
1689 break;
1690 case MCSymbolRefExpr::VK_TPOFF:
1691 Type = ELF::R_386_TLS_LE_32;
1692 break;
1693 case MCSymbolRefExpr::VK_INDNTPOFF:
1694 Type = ELF::R_386_TLS_IE;
1695 break;
1696 case MCSymbolRefExpr::VK_NTPOFF:
1697 Type = ELF::R_386_TLS_LE;
1698 break;
1699 case MCSymbolRefExpr::VK_GOTNTPOFF:
1700 Type = ELF::R_386_TLS_GOTIE;
1701 break;
1702 case MCSymbolRefExpr::VK_TLSLDM:
1703 Type = ELF::R_386_TLS_LDM;
1704 break;
1705 case MCSymbolRefExpr::VK_DTPOFF:
1706 Type = ELF::R_386_TLS_LDO_32;
1707 break;
1708 }
1709 break;
1710 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001711 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001712 case FK_Data_1: Type = ELF::R_386_8; break;
1713 }
1714 }
1715 }
1716
1717 if (RelocNeedsGOT(Modifier))
1718 NeedsGOT = true;
1719
Jason W Kim56a39902010-12-06 21:57:34 +00001720 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001721}