blob: e44d8c53447caef6a3f39ffd8cdbbe6411054fa5 [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"
Matt Fleming3565a062010-08-16 18:57:57 +000028
29#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000030#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000031
32#include <vector>
33using namespace llvm;
34
Jan Sjödin2ddfd952011-02-28 21:45:04 +000035bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
36 const MCFixupKindInfo &FKI =
37 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
38
39 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
40}
41
42bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
43 switch (Variant) {
44 default:
45 return false;
46 case MCSymbolRefExpr::VK_GOT:
47 case MCSymbolRefExpr::VK_PLT:
48 case MCSymbolRefExpr::VK_GOTPCREL:
49 case MCSymbolRefExpr::VK_TPOFF:
50 case MCSymbolRefExpr::VK_TLSGD:
51 case MCSymbolRefExpr::VK_GOTTPOFF:
52 case MCSymbolRefExpr::VK_INDNTPOFF:
53 case MCSymbolRefExpr::VK_NTPOFF:
54 case MCSymbolRefExpr::VK_GOTNTPOFF:
55 case MCSymbolRefExpr::VK_TLSLDM:
56 case MCSymbolRefExpr::VK_DTPOFF:
57 case MCSymbolRefExpr::VK_TLSLD:
58 return true;
59 }
60}
61
Jason W Kimd3443e92010-11-15 16:18:39 +000062ELFObjectWriter::~ELFObjectWriter()
63{}
64
Matt Fleming3565a062010-08-16 18:57:57 +000065// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000066void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
67 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000068 // ELF Header
69 // ----------
70 //
71 // Note
72 // ----
73 // emitWord method behaves differently for ELF32 and ELF64, writing
74 // 4 bytes in the former and 8 in the latter.
75
76 Write8(0x7f); // e_ident[EI_MAG0]
77 Write8('E'); // e_ident[EI_MAG1]
78 Write8('L'); // e_ident[EI_MAG2]
79 Write8('F'); // e_ident[EI_MAG3]
80
Rafael Espindolabff66a82010-12-18 03:27:34 +000081 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000082
83 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000084 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +000085
86 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +000087 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +000088 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +000089 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
90 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
91 default: Write8(ELF::ELFOSABI_NONE); break;
92 }
Matt Fleming3565a062010-08-16 18:57:57 +000093 Write8(0); // e_ident[EI_ABIVERSION]
94
95 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
96
97 Write16(ELF::ET_REL); // e_type
98
Rafael Espindolabff66a82010-12-18 03:27:34 +000099 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000100
101 Write32(ELF::EV_CURRENT); // e_version
102 WriteWord(0); // e_entry, no entry point in .o file
103 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000104 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000105 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000106
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000107 // e_flags = whatever the target wants
108 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000109
110 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000111 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000112
113 Write16(0); // e_phentsize = prog header entry size
114 Write16(0); // e_phnum = # prog header entries = 0
115
116 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000117 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000118
119 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000120 if (NumberOfSections >= ELF::SHN_LORESERVE)
121 Write16(0);
122 else
123 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000124
125 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000126 if (NumberOfSections >= ELF::SHN_LORESERVE)
127 Write16(ELF::SHN_XINDEX);
128 else
129 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000130}
131
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000132void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
133 MCDataFragment *ShndxF,
134 uint64_t name,
135 uint8_t info, uint64_t value,
136 uint64_t size, uint8_t other,
137 uint32_t shndx,
138 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000139 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000140 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000141 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000142 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000143 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000144 }
145
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000146 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
147 uint16_t(ELF::SHN_XINDEX) : shndx;
148
Rafael Espindolabff66a82010-12-18 03:27:34 +0000149 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000150 String32(*SymtabF, name); // st_name
151 String8(*SymtabF, info); // st_info
152 String8(*SymtabF, other); // st_other
153 String16(*SymtabF, Index); // st_shndx
154 String64(*SymtabF, value); // st_value
155 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000156 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000157 String32(*SymtabF, name); // st_name
158 String32(*SymtabF, value); // st_value
159 String32(*SymtabF, size); // st_size
160 String8(*SymtabF, info); // st_info
161 String8(*SymtabF, other); // st_other
162 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000163 }
164}
165
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000166uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
167 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000168 if (Data.isCommon() && Data.isExternal())
169 return Data.getCommonAlignment();
170
171 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000172
173 if (Symbol.isAbsolute() && Symbol.isVariable()) {
174 if (const MCExpr *Value = Symbol.getVariableValue()) {
175 int64_t IntValue;
176 if (Value->EvaluateAsAbsolute(IntValue, Layout))
177 return (uint64_t)IntValue;
178 }
179 }
180
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000181 if (!Symbol.isInSection())
182 return 0;
183
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000184 if (Data.getFragment())
185 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000186
187 return 0;
188}
189
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000190void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
191 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000192 // The presence of symbol versions causes undefined symbols and
193 // versions declared with @@@ to be renamed.
194
195 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
196 ie = Asm.symbol_end(); it != ie; ++it) {
197 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000198 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000199 MCSymbolData &SD = Asm.getSymbolData(Symbol);
200
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000201 // Not an alias.
202 if (&Symbol == &Alias)
203 continue;
204
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000205 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000206 size_t Pos = AliasName.find('@');
207 if (Pos == StringRef::npos)
208 continue;
209
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000210 // Aliases defined with .symvar copy the binding from the symbol they alias.
211 // This is the first place we are able to copy this information.
212 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000213 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000214
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000215 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000216 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
217 continue;
218
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000219 // FIXME: produce a better error message.
220 if (Symbol.isUndefined() && Rest.startswith("@@") &&
221 !Rest.startswith("@@@"))
222 report_fatal_error("A @@ version cannot be undefined");
223
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000224 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000225 }
226}
227
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000228void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
229 MCDataFragment *ShndxF,
230 ELFSymbolData &MSD,
231 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000232 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000233 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000234 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000235
Rafael Espindola7be2c332010-10-31 00:16:26 +0000236 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
237 Data.getSymbol().isVariable();
238
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000239 uint8_t Binding = MCELF::GetBinding(OrigData);
240 uint8_t Visibility = MCELF::GetVisibility(OrigData);
241 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000242
243 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
244 uint8_t Other = Visibility;
245
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000246 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000247 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000248
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000249 assert(!(Data.isCommon() && !Data.isExternal()));
250
Rafael Espindolaf0121242010-12-22 16:03:00 +0000251 const MCExpr *ESize = Data.getSize();
252 if (ESize) {
253 int64_t Res;
254 if (!ESize->EvaluateAsAbsolute(Res, Layout))
255 report_fatal_error("Size expression must be absolute.");
256 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000257 }
258
259 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000260 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
261 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000262}
263
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000264void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
265 MCDataFragment *ShndxF,
266 const MCAssembler &Asm,
267 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000268 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000269 // The string table must be emitted first because we need the index
270 // into the string table for all the symbol names.
271 assert(StringTable.size() && "Missing string table");
272
273 // FIXME: Make sure the start of the symbol table is aligned.
274
275 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000276 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000277
278 // Write the symbol table entries.
279 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
280 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
281 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000282 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000283 }
284
Rafael Espindola71859c62010-09-16 19:46:31 +0000285 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000286 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
287 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000288 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000289 static_cast<const MCSectionELF&>(i->getSection());
290 if (Section.getType() == ELF::SHT_RELA ||
291 Section.getType() == ELF::SHT_REL ||
292 Section.getType() == ELF::SHT_STRTAB ||
293 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000294 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000295 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000296 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000297 LastLocalSymbolIndex++;
298 }
Matt Fleming3565a062010-08-16 18:57:57 +0000299
300 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
301 ELFSymbolData &MSD = ExternalSymbolData[i];
302 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000303 assert(((Data.getFlags() & ELF_STB_Global) ||
304 (Data.getFlags() & ELF_STB_Weak)) &&
305 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000306 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000307 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000308 LastLocalSymbolIndex++;
309 }
310
311 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
312 ELFSymbolData &MSD = UndefinedSymbolData[i];
313 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000314 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000315 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000316 LastLocalSymbolIndex++;
317 }
318}
319
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000320const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
321 const MCValue &Target,
322 const MCFragment &F) const {
323 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000324 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
325 const MCSymbol *Renamed = Renames.lookup(&Symbol);
326 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000327
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000328 if (ASymbol.isUndefined()) {
329 if (Renamed)
330 return Renamed;
331 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000332 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000333
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000334 if (SD.isExternal()) {
335 if (Renamed)
336 return Renamed;
337 return &Symbol;
338 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000339
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000340 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000341 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000342 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000343
Rafael Espindola25958732010-11-24 21:57:39 +0000344 if (secKind.isBSS())
Jason W Kim953a2a32011-02-07 01:11:15 +0000345 return ExplicitRelSym(Asm, Target, F, true);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000346
Rafael Espindola25958732010-11-24 21:57:39 +0000347 if (secKind.isThreadLocal()) {
348 if (Renamed)
349 return Renamed;
350 return &Symbol;
351 }
352
Rafael Espindola8cecf252010-10-06 16:23:36 +0000353 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000354 const MCSectionELF &Sec2 =
355 static_cast<const MCSectionELF&>(F.getParent()->getSection());
356
Rafael Espindola8cecf252010-10-06 16:23:36 +0000357 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000358 (Kind == MCSymbolRefExpr::VK_PLT ||
359 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000360 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000361 if (Renamed)
362 return Renamed;
363 return &Symbol;
364 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000365
Rafael Espindola1c130262011-01-23 04:43:11 +0000366 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000367 if (Target.getConstant() == 0)
368 return NULL;
369 if (Renamed)
370 return Renamed;
371 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000372 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000373
Jason W Kim953a2a32011-02-07 01:11:15 +0000374 return ExplicitRelSym(Asm, Target, F, false);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000375}
376
Matt Fleming3565a062010-08-16 18:57:57 +0000377
Jason W Kim56a39902010-12-06 21:57:34 +0000378void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
379 const MCAsmLayout &Layout,
380 const MCFragment *Fragment,
381 const MCFixup &Fixup,
382 MCValue Target,
383 uint64_t &FixedValue) {
384 int64_t Addend = 0;
385 int Index = 0;
386 int64_t Value = Target.getConstant();
387 const MCSymbol *RelocSymbol = NULL;
388
Rafael Espindola127a6a42010-12-17 07:28:17 +0000389 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000390 if (!Target.isAbsolute()) {
391 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
392 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
393 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
394
395 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
396 const MCSymbol &SymbolB = RefB->getSymbol();
397 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
398 IsPCRel = true;
399
400 // Offset of the symbol in the section
401 int64_t a = Layout.getSymbolOffset(&SDB);
402
403 // Ofeset of the relocation in the section
404 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
405 Value += b - a;
406 }
407
408 if (!RelocSymbol) {
409 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
410 MCFragment *F = SD.getFragment();
411
412 Index = F->getParent()->getOrdinal() + 1;
413
414 // Offset of the symbol in the section
415 Value += Layout.getSymbolOffset(&SD);
416 } else {
417 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
418 WeakrefUsedInReloc.insert(RelocSymbol);
419 else
420 UsedInReloc.insert(RelocSymbol);
421 Index = -1;
422 }
423 Addend = Value;
424 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000425 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000426 Value = 0;
427 }
428
429 FixedValue = Value;
430 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
431 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000432
Jason W Kim56a39902010-12-06 21:57:34 +0000433 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
434 Fixup.getOffset();
435
Rafael Espindolabff66a82010-12-18 03:27:34 +0000436 if (!hasRelocationAddend())
437 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000438 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
439 Relocations[Fragment->getParent()].push_back(ERE);
440}
441
442
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000443uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000444ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
445 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000446 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000447 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000448}
449
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000450bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
451 const MCSymbolData &Data,
452 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000453 if (Data.getFlags() & ELF_Other_Weakref)
454 return false;
455
Rafael Espindolabd701182010-10-19 19:31:37 +0000456 if (Used)
457 return true;
458
Rafael Espindola88182132010-10-27 15:18:17 +0000459 if (Renamed)
460 return false;
461
Rafael Espindola737cd212010-10-05 18:01:23 +0000462 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000463
Rafael Espindolad1798862010-10-29 23:09:31 +0000464 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
465 return true;
466
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000467 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000468 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
469 return false;
470
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000471 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000472 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000473 return false;
474
Rafael Espindola737cd212010-10-05 18:01:23 +0000475 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
476 return false;
477
Rafael Espindolabd701182010-10-19 19:31:37 +0000478 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000479 return false;
480
481 return true;
482}
483
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000484bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
485 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000486 if (Data.isExternal())
487 return false;
488
489 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000490 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000491
492 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
493 if (isSignature && !isUsedInReloc)
494 return true;
495
Rafael Espindola737cd212010-10-05 18:01:23 +0000496 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000497 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000498
499 return true;
500}
501
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000502void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000503 SectionIndexMapTy &SectionIndexMap,
504 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000505 unsigned Index = 1;
506 for (MCAssembler::iterator it = Asm.begin(),
507 ie = Asm.end(); it != ie; ++it) {
508 const MCSectionELF &Section =
509 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000510 if (Section.getType() != ELF::SHT_GROUP)
511 continue;
512 SectionIndexMap[&Section] = Index++;
513 }
514
515 for (MCAssembler::iterator it = Asm.begin(),
516 ie = Asm.end(); it != ie; ++it) {
517 const MCSectionELF &Section =
518 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000519 if (Section.getType() == ELF::SHT_GROUP ||
520 Section.getType() == ELF::SHT_REL ||
521 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000522 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000523 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000524 const MCSectionELF *RelSection = RelMap.lookup(&Section);
525 if (RelSection)
526 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000527 }
528}
529
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000530void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000531 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000532 RevGroupMapTy RevGroupMap,
533 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000534 // FIXME: Is this the correct place to do this?
535 if (NeedsGOT) {
536 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
537 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
538 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
539 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000540 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000541 }
542
Matt Fleming3565a062010-08-16 18:57:57 +0000543 // Index 0 is always the empty string.
544 StringMap<uint64_t> StringIndexMap;
545 StringTable += '\x00';
546
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000547 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000548 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
549 ie = Asm.symbol_end(); it != ie; ++it) {
550 const MCSymbol &Symbol = it->getSymbol();
551
Rafael Espindola484291c2010-11-01 14:28:48 +0000552 bool Used = UsedInReloc.count(&Symbol);
553 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000554 bool isSignature = RevGroupMap.count(&Symbol);
555
556 if (!isInSymtab(Asm, *it,
557 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000558 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000559 continue;
560
Matt Fleming3565a062010-08-16 18:57:57 +0000561 ELFSymbolData MSD;
562 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000563 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000564
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000565 // Undefined symbols are global, but this is the first place we
566 // are able to set it.
567 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000568 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000569 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000570 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
571 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000572 }
573
Rafael Espindola484291c2010-11-01 14:28:48 +0000574 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000575 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000576
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000577 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000578 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000579 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000580 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000581 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000582 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000583 if (isSignature && !Used)
584 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
585 else
586 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000587 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000588 const MCSectionELF &Section =
589 static_cast<const MCSectionELF&>(RefSymbol.getSection());
590 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000591 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
592 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000593 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000594 }
595
Rafael Espindola88182132010-10-27 15:18:17 +0000596 // The @@@ in symbol version is replaced with @ in undefined symbols and
597 // @@ in defined ones.
598 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000599 SmallString<32> Buf;
600
Rafael Espindola88182132010-10-27 15:18:17 +0000601 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000602 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000603 Buf += Name.substr(0, Pos);
604 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
605 Buf += Name.substr(Pos + Skip);
606 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000607 }
608
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000609 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000610 if (!Entry) {
611 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000612 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000613 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000614 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000615 MSD.StringIndex = Entry;
616 if (MSD.SectionIndex == ELF::SHN_UNDEF)
617 UndefinedSymbolData.push_back(MSD);
618 else if (Local)
619 LocalSymbolData.push_back(MSD);
620 else
621 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000622 }
623
624 // Symbols are required to be in lexicographic order.
625 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
626 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
627 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
628
629 // Set the symbol indices. Local symbols must come before all other
630 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000631 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000632 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
633 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000634
635 Index += NumRegularSections;
636
Matt Fleming3565a062010-08-16 18:57:57 +0000637 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
638 ExternalSymbolData[i].SymbolData->setIndex(Index++);
639 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
640 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
641}
642
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000643void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
644 MCAsmLayout &Layout,
645 RelMapTy &RelMap) {
646 for (MCAssembler::const_iterator it = Asm.begin(),
647 ie = Asm.end(); it != ie; ++it) {
648 const MCSectionData &SD = *it;
649 if (Relocations[&SD].empty())
650 continue;
651
Matt Fleming3565a062010-08-16 18:57:57 +0000652 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000653 const MCSectionELF &Section =
654 static_cast<const MCSectionELF&>(SD.getSection());
655
656 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000657 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000658 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000659
660 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000661 if (hasRelocationAddend())
662 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000663 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000664 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000665
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000666 const MCSectionELF *RelaSection =
667 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
668 ELF::SHT_RELA : ELF::SHT_REL, 0,
669 SectionKind::getReadOnly(),
670 EntrySize, "");
671 RelMap[&Section] = RelaSection;
672 Asm.getOrCreateSectionData(*RelaSection);
673 }
674}
Matt Fleming3565a062010-08-16 18:57:57 +0000675
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000676void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
677 const RelMapTy &RelMap) {
678 for (MCAssembler::const_iterator it = Asm.begin(),
679 ie = Asm.end(); it != ie; ++it) {
680 const MCSectionData &SD = *it;
681 const MCSectionELF &Section =
682 static_cast<const MCSectionELF&>(SD.getSection());
683
684 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
685 if (!RelaSection)
686 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000687 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000688 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000689
690 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000691 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000692 }
693}
694
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000695void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
696 uint64_t Flags, uint64_t Address,
697 uint64_t Offset, uint64_t Size,
698 uint32_t Link, uint32_t Info,
699 uint64_t Alignment,
700 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000701 Write32(Name); // sh_name: index into string table
702 Write32(Type); // sh_type
703 WriteWord(Flags); // sh_flags
704 WriteWord(Address); // sh_addr
705 WriteWord(Offset); // sh_offset
706 WriteWord(Size); // sh_size
707 Write32(Link); // sh_link
708 Write32(Info); // sh_info
709 WriteWord(Alignment); // sh_addralign
710 WriteWord(EntrySize); // sh_entsize
711}
712
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000713void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
714 MCDataFragment *F,
715 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000716 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
717 // sort by the r_offset just like gnu as does
718 array_pod_sort(Relocs.begin(), Relocs.end());
719
720 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
721 ELFRelocationEntry entry = Relocs[e - i - 1];
722
Rafael Espindola12203cc2010-11-21 00:48:25 +0000723 if (!entry.Index)
724 ;
725 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000726 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
727 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000728 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000729 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000730 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000731
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000732 struct ELF::Elf64_Rela ERE64;
733 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000734 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000735
Rafael Espindolabff66a82010-12-18 03:27:34 +0000736 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000737 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000738 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000739 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000740
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000741 struct ELF::Elf32_Rela ERE32;
742 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000743 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000744
Rafael Espindolabff66a82010-12-18 03:27:34 +0000745 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000746 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000747 }
Matt Fleming3565a062010-08-16 18:57:57 +0000748 }
749}
750
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000751void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
752 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000753 SectionIndexMapTy &SectionIndexMap,
754 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000755 MCContext &Ctx = Asm.getContext();
756 MCDataFragment *F;
757
Rafael Espindolabff66a82010-12-18 03:27:34 +0000758 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000759
Rafael Espindola38738bf2010-09-22 19:04:41 +0000760 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000761 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000762 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000763 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000764 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
765 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000766
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000767 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000768 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
769 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000770 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000771 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000772 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000773
774 MCSectionData *SymtabShndxSD = NULL;
775
776 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000777 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000778 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000779 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000780 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
781 SymtabShndxSD->setAlignment(4);
782 }
Matt Fleming3565a062010-08-16 18:57:57 +0000783
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000784 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000785 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000786 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000787 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
788 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000789
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000790 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
791
792 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
793 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
794 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000795
796 // Symbol table
797 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000798 MCDataFragment *ShndxF = NULL;
799 if (NeedsSymtabShndx) {
800 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000801 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000802 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000803
Matt Fleming3565a062010-08-16 18:57:57 +0000804 F = new MCDataFragment(&StrtabSD);
805 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000806
Matt Fleming3565a062010-08-16 18:57:57 +0000807 F = new MCDataFragment(&ShstrtabSD);
808
Matt Fleming3565a062010-08-16 18:57:57 +0000809 // Section header string table.
810 //
811 // The first entry of a string table holds a null character so skip
812 // section 0.
813 uint64_t Index = 1;
814 F->getContents() += '\x00';
815
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000816 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000817 for (MCAssembler::const_iterator it = Asm.begin(),
818 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +0000819 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000820 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +0000821 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +0000822
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000823 StringRef Name = Section.getSectionName();
824 if (SecStringMap.count(Name)) {
825 SectionStringTableIndex[&Section] = SecStringMap[Name];
826 continue;
827 }
Matt Fleming3565a062010-08-16 18:57:57 +0000828 // Remember the index into the string table so we can write it
829 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000830 SectionStringTableIndex[&Section] = Index;
831 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000832
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000833 Index += Name.size() + 1;
834 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000835 F->getContents() += '\x00';
836 }
Rafael Espindola70703872010-09-30 02:22:20 +0000837}
838
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000839void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
840 MCAsmLayout &Layout,
841 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000842 RevGroupMapTy &RevGroupMap,
843 SectionIndexMapTy &SectionIndexMap,
844 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000845 // Create the .note.GNU-stack section if needed.
846 MCContext &Ctx = Asm.getContext();
847 if (Asm.getNoExecStack()) {
848 const MCSectionELF *GnuStackSection =
849 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
850 SectionKind::getReadOnly());
851 Asm.getOrCreateSectionData(*GnuStackSection);
852 }
853
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000854 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000855 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
856 it != ie; ++it) {
857 const MCSectionELF &Section =
858 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000859 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000860 continue;
861
862 const MCSymbol *SignatureSymbol = Section.getGroup();
863 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000864 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000865 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000866 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000867 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
868 Data.setAlignment(4);
869 MCDataFragment *F = new MCDataFragment(&Data);
870 String32(*F, ELF::GRP_COMDAT);
871 }
872 GroupMap[Group] = SignatureSymbol;
873 }
874
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000875 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
876
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000877 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000878 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000879 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000880 const MCSectionELF &Section =
881 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000882 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000883 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000884 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000885 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
886 // FIXME: we could use the previous fragment
887 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000888 unsigned Index = SectionIndexMap.lookup(&Section);
889 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000890 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000891}
892
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000893void ELFObjectWriter::WriteSection(MCAssembler &Asm,
894 const SectionIndexMapTy &SectionIndexMap,
895 uint32_t GroupSymbolIndex,
896 uint64_t Offset, uint64_t Size,
897 uint64_t Alignment,
898 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000899 uint64_t sh_link = 0;
900 uint64_t sh_info = 0;
901
902 switch(Section.getType()) {
903 case ELF::SHT_DYNAMIC:
904 sh_link = SectionStringTableIndex[&Section];
905 sh_info = 0;
906 break;
907
908 case ELF::SHT_REL:
909 case ELF::SHT_RELA: {
910 const MCSectionELF *SymtabSection;
911 const MCSectionELF *InfoSection;
912 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
913 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000914 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000915 sh_link = SectionIndexMap.lookup(SymtabSection);
916 assert(sh_link && ".symtab not found");
917
918 // Remove ".rel" and ".rela" prefixes.
919 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
920 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
921
922 InfoSection = Asm.getContext().getELFSection(SectionName,
923 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000924 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000925 sh_info = SectionIndexMap.lookup(InfoSection);
926 break;
927 }
928
929 case ELF::SHT_SYMTAB:
930 case ELF::SHT_DYNSYM:
931 sh_link = StringTableIndex;
932 sh_info = LastLocalSymbolIndex;
933 break;
934
935 case ELF::SHT_SYMTAB_SHNDX:
936 sh_link = SymbolTableIndex;
937 break;
938
939 case ELF::SHT_PROGBITS:
940 case ELF::SHT_STRTAB:
941 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000942 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000943 case ELF::SHT_NULL:
944 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +0000945 case ELF::SHT_INIT_ARRAY:
946 case ELF::SHT_FINI_ARRAY:
947 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000948 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000949 // Nothing to do.
950 break;
951
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000952 case ELF::SHT_GROUP: {
953 sh_link = SymbolTableIndex;
954 sh_info = GroupSymbolIndex;
955 break;
956 }
957
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000958 default:
959 assert(0 && "FIXME: sh_type value not supported!");
960 break;
961 }
962
963 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
964 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
965 Alignment, Section.getEntrySize());
966}
967
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000968bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +0000969 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000970 !SD.getSection().isVirtualSection();
971}
972
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000973uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000974 uint64_t Ret = 0;
975 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
976 ++i) {
977 const MCFragment &F = *i;
978 assert(F.getKind() == MCFragment::FT_Data);
979 Ret += cast<MCDataFragment>(F).getContents().size();
980 }
981 return Ret;
982}
983
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000984uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
985 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000986 if (IsELFMetaDataSection(SD))
987 return DataSectionSize(SD);
988 return Layout.getSectionFileSize(&SD);
989}
990
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000991uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
992 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000993 if (IsELFMetaDataSection(SD))
994 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000995 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000996}
997
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000998void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
999 const MCAsmLayout &Layout,
1000 const MCSectionELF &Section) {
1001 uint64_t FileOff = OS.tell();
1002 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1003
1004 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1005 WriteZeros(Padding);
1006 FileOff += Padding;
1007
1008 FileOff += GetSectionFileSize(Layout, SD);
1009
1010 if (IsELFMetaDataSection(SD)) {
1011 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1012 ++i) {
1013 const MCFragment &F = *i;
1014 assert(F.getKind() == MCFragment::FT_Data);
1015 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1016 }
1017 } else {
1018 Asm.WriteSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001019 }
1020}
1021
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001022void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1023 const GroupMapTy &GroupMap,
1024 const MCAsmLayout &Layout,
1025 const SectionIndexMapTy &SectionIndexMap,
1026 const SectionOffsetMapTy &SectionOffsetMap) {
1027 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001028
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001029 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001030 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001031
1032 for (SectionIndexMapTy::const_iterator i=
1033 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1034 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001035 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001036 }
1037
Matt Fleming3565a062010-08-16 18:57:57 +00001038 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001039 uint64_t FirstSectionSize =
1040 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1041 uint32_t FirstSectionLink =
1042 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1043 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001044
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001045 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001046 const MCSectionELF &Section = *Sections[i];
1047 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1048 uint32_t GroupSymbolIndex;
1049 if (Section.getType() != ELF::SHT_GROUP)
1050 GroupSymbolIndex = 0;
1051 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001052 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1053 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001054
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001055 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001056
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001057 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001058 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001059 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001060 }
1061}
1062
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001063void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1064 std::vector<const MCSectionELF*> &Sections) {
1065 for (MCAssembler::iterator it = Asm.begin(),
1066 ie = Asm.end(); it != ie; ++it) {
1067 const MCSectionELF &Section =
1068 static_cast<const MCSectionELF &>(it->getSection());
1069 if (Section.getType() == ELF::SHT_GROUP)
1070 Sections.push_back(&Section);
1071 }
1072
1073 for (MCAssembler::iterator it = Asm.begin(),
1074 ie = Asm.end(); it != ie; ++it) {
1075 const MCSectionELF &Section =
1076 static_cast<const MCSectionELF &>(it->getSection());
1077 if (Section.getType() != ELF::SHT_GROUP &&
1078 Section.getType() != ELF::SHT_REL &&
1079 Section.getType() != ELF::SHT_RELA)
1080 Sections.push_back(&Section);
1081 }
1082
1083 for (MCAssembler::iterator it = Asm.begin(),
1084 ie = Asm.end(); it != ie; ++it) {
1085 const MCSectionELF &Section =
1086 static_cast<const MCSectionELF &>(it->getSection());
1087 if (Section.getType() == ELF::SHT_REL ||
1088 Section.getType() == ELF::SHT_RELA)
1089 Sections.push_back(&Section);
1090 }
1091}
1092
1093void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1094 const MCAsmLayout &Layout) {
1095 GroupMapTy GroupMap;
1096 RevGroupMapTy RevGroupMap;
1097 SectionIndexMapTy SectionIndexMap;
1098
1099 unsigned NumUserSections = Asm.size();
1100
1101 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1102 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1103
1104 const unsigned NumUserAndRelocSections = Asm.size();
1105 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1106 RevGroupMap, SectionIndexMap, RelMap);
1107 const unsigned AllSections = Asm.size();
1108 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1109
1110 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1111
1112 // Compute symbol table information.
1113 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1114
1115
1116 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1117
1118 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1119 const_cast<MCAsmLayout&>(Layout),
1120 SectionIndexMap,
1121 RelMap);
1122
1123 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1124 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1125 sizeof(ELF::Elf32_Ehdr);
1126 uint64_t FileOff = HeaderSize;
1127
1128 std::vector<const MCSectionELF*> Sections;
1129 ComputeSectionOrder(Asm, Sections);
1130 unsigned NumSections = Sections.size();
1131 SectionOffsetMapTy SectionOffsetMap;
1132 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1133 const MCSectionELF &Section = *Sections[i];
1134 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1135
1136 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1137
1138 // Remember the offset into the file for this section.
1139 SectionOffsetMap[&Section] = FileOff;
1140
1141 // Get the size of the section in the output file (including padding).
1142 FileOff += GetSectionFileSize(Layout, SD);
1143 }
1144
1145 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1146
1147 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1148
1149 uint64_t SectionHeaderEntrySize = is64Bit() ?
1150 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1151 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1152
1153 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1154 const MCSectionELF &Section = *Sections[i];
1155 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1156
1157 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1158
1159 // Remember the offset into the file for this section.
1160 SectionOffsetMap[&Section] = FileOff;
1161
1162 // Get the size of the section in the output file (including padding).
1163 FileOff += GetSectionFileSize(Layout, SD);
1164 }
1165
1166 // Write out the ELF header ...
1167 WriteHeader(SectionHeaderOffset, NumSections + 1);
1168
1169 // ... then the regular sections ...
1170 // + because of .shstrtab
1171 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1172 WriteDataSectionData(Asm, Layout, *Sections[i]);
1173
1174 FileOff = OS.tell();
1175 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1176 WriteZeros(Padding);
1177
1178 // ... then the section header table ...
1179 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1180 SectionOffsetMap);
1181
1182 FileOff = OS.tell();
1183
1184 // ... and then the remainting sections ...
1185 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1186 WriteDataSectionData(Asm, Layout, *Sections[i]);
1187}
1188
Eli Friedman78c1e172011-03-03 07:24:36 +00001189bool
1190ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1191 const MCSymbolData &DataA,
1192 const MCFragment &FB,
1193 bool InSet,
1194 bool IsPCRel) const {
1195 if (DataA.getFlags() & ELF_STB_Weak)
1196 return false;
1197 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1198 Asm, DataA, FB,InSet, IsPCRel);
1199}
1200
Rafael Espindola6024c972010-12-17 17:45:22 +00001201MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1202 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001203 bool IsLittleEndian) {
1204 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001205 case ELF::EM_386:
1206 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001207 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001208 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001209 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001210 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001211 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001212 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001213 }
1214}
1215
1216
1217/// START OF SUBCLASSES for ELFObjectWriter
1218//===- ARMELFObjectWriter -------------------------------------------===//
1219
Rafael Espindola31f35782010-12-17 18:01:31 +00001220ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001221 raw_ostream &_OS,
1222 bool IsLittleEndian)
1223 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001224{}
1225
1226ARMELFObjectWriter::~ARMELFObjectWriter()
1227{}
1228
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001229// FIXME: get the real EABI Version from the Triple.
1230void ARMELFObjectWriter::WriteEFlags() {
1231 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1232}
1233
Jason W Kim953a2a32011-02-07 01:11:15 +00001234// In ARM, _MergedGlobals and other most symbols get emitted directly.
1235// I.e. not as an offset to a section symbol.
1236// This code is a first-cut approximation of what ARM/gcc does.
1237
1238const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1239 const MCValue &Target,
1240 const MCFragment &F,
1241 bool IsBSS) const {
1242 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1243 bool EmitThisSym = false;
1244
1245 if (IsBSS) {
1246 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1247 .Case("_MergedGlobals", true)
1248 .Default(false);
1249 } else {
1250 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1251 .Case("_MergedGlobals", true)
1252 .StartsWith(".L.str", true)
1253 .Default(false);
1254 }
1255 if (EmitThisSym)
1256 return &Symbol;
1257 if (! Symbol.isTemporary())
1258 return &Symbol;
1259 return NULL;
1260}
1261
Jason W Kim85fed5e2010-12-01 02:40:06 +00001262unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1263 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001264 bool IsPCRel,
1265 bool IsRelocWithSymbol,
1266 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001267 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1268 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1269
Jason W Kima0871e72010-12-08 23:14:44 +00001270 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001271 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001272 switch ((unsigned)Fixup.getKind()) {
1273 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001274 case FK_Data_4:
1275 switch (Modifier) {
1276 default: llvm_unreachable("Unsupported Modifier");
1277 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001278 Type = ELF::R_ARM_BASE_PREL;
1279 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001280 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001281 assert(0 && "unimplemented");
1282 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001283 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1284 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001285 break;
1286 }
1287 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001288 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001289 switch (Modifier) {
1290 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001291 Type = ELF::R_ARM_PLT32;
1292 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001293 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001294 Type = ELF::R_ARM_CALL;
1295 break;
1296 }
1297 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001298 case ARM::fixup_arm_condbranch:
1299 Type = ELF::R_ARM_JUMP24;
1300 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001301 case ARM::fixup_arm_movt_hi16:
1302 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001303 Type = ELF::R_ARM_MOVT_PREL;
1304 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001305 case ARM::fixup_arm_movw_lo16:
1306 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001307 Type = ELF::R_ARM_MOVW_PREL_NC;
1308 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001309 case ARM::fixup_t2_movt_hi16:
1310 case ARM::fixup_t2_movt_hi16_pcrel:
1311 Type = ELF::R_ARM_THM_MOVT_PREL;
1312 break;
1313 case ARM::fixup_t2_movw_lo16:
1314 case ARM::fixup_t2_movw_lo16_pcrel:
1315 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1316 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001317 }
1318 } else {
1319 switch ((unsigned)Fixup.getKind()) {
1320 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001321 case FK_Data_4:
1322 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001323 default: llvm_unreachable("Unsupported Modifier"); break;
1324 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001325 Type = ELF::R_ARM_GOT_BREL;
1326 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001327 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001328 Type = ELF::R_ARM_TLS_GD32;
1329 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001330 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001331 Type = ELF::R_ARM_TLS_LE32;
1332 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001333 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001334 Type = ELF::R_ARM_TLS_IE32;
1335 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001336 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001337 Type = ELF::R_ARM_ABS32;
1338 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001339 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001340 Type = ELF::R_ARM_GOTOFF32;
1341 break;
1342 }
1343 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001344 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001345 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001346 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001347 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001348 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001349 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001350 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001351 assert(0 && "Unimplemented");
1352 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001353 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001354 Type = ELF::R_ARM_CALL;
1355 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001356 case ARM::fixup_arm_condbranch:
1357 Type = ELF::R_ARM_JUMP24;
1358 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001359 case ARM::fixup_arm_movt_hi16:
1360 Type = ELF::R_ARM_MOVT_ABS;
1361 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001362 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001363 Type = ELF::R_ARM_MOVW_ABS_NC;
1364 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001365 case ARM::fixup_t2_movt_hi16:
1366 Type = ELF::R_ARM_THM_MOVT_ABS;
1367 break;
1368 case ARM::fixup_t2_movw_lo16:
1369 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1370 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001371 }
1372 }
1373
1374 if (RelocNeedsGOT(Modifier))
1375 NeedsGOT = true;
Jason W Kim953a2a32011-02-07 01:11:15 +00001376
Jason W Kima0871e72010-12-08 23:14:44 +00001377 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001378}
1379
Wesley Peck4b047132010-11-21 22:06:28 +00001380//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001381
Rafael Espindola31f35782010-12-17 18:01:31 +00001382MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001383 raw_ostream &_OS,
1384 bool IsLittleEndian)
1385 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001386}
1387
1388MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1389}
1390
Jason W Kim56a39902010-12-06 21:57:34 +00001391unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001392 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001393 bool IsPCRel,
1394 bool IsRelocWithSymbol,
1395 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001396 // determine the type of the relocation
1397 unsigned Type;
1398 if (IsPCRel) {
1399 switch ((unsigned)Fixup.getKind()) {
1400 default:
1401 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001402 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001403 Type = ELF::R_MICROBLAZE_64_PCREL;
1404 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001405 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001406 Type = ELF::R_MICROBLAZE_32_PCREL;
1407 break;
1408 }
1409 } else {
1410 switch ((unsigned)Fixup.getKind()) {
1411 default: llvm_unreachable("invalid fixup kind!");
1412 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001413 Type = ((IsRelocWithSymbol || Addend !=0)
1414 ? ELF::R_MICROBLAZE_32
1415 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001416 break;
1417 case FK_Data_2:
1418 Type = ELF::R_MICROBLAZE_32;
1419 break;
1420 }
1421 }
Jason W Kim56a39902010-12-06 21:57:34 +00001422 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001423}
Jason W Kimd3443e92010-11-15 16:18:39 +00001424
1425//===- X86ELFObjectWriter -------------------------------------------===//
1426
1427
Rafael Espindola31f35782010-12-17 18:01:31 +00001428X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001429 raw_ostream &_OS,
1430 bool IsLittleEndian)
1431 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001432{}
1433
1434X86ELFObjectWriter::~X86ELFObjectWriter()
1435{}
1436
Jason W Kim56a39902010-12-06 21:57:34 +00001437unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1438 const MCFixup &Fixup,
1439 bool IsPCRel,
1440 bool IsRelocWithSymbol,
1441 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001442 // determine the type of the relocation
1443
Rafael Espindola12203cc2010-11-21 00:48:25 +00001444 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1445 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001446 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001447 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001448 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001449 switch ((unsigned)Fixup.getKind()) {
1450 default: llvm_unreachable("invalid fixup kind!");
1451 case FK_PCRel_8:
1452 assert(Modifier == MCSymbolRefExpr::VK_None);
1453 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001454 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001455 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001456 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001457 case FK_Data_4: // FIXME?
1458 case X86::reloc_riprel_4byte:
1459 case FK_PCRel_4:
1460 switch (Modifier) {
1461 default:
1462 llvm_unreachable("Unimplemented");
1463 case MCSymbolRefExpr::VK_None:
1464 Type = ELF::R_X86_64_PC32;
1465 break;
1466 case MCSymbolRefExpr::VK_PLT:
1467 Type = ELF::R_X86_64_PLT32;
1468 break;
1469 case MCSymbolRefExpr::VK_GOTPCREL:
1470 Type = ELF::R_X86_64_GOTPCREL;
1471 break;
1472 case MCSymbolRefExpr::VK_GOTTPOFF:
1473 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001474 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001475 case MCSymbolRefExpr::VK_TLSGD:
1476 Type = ELF::R_X86_64_TLSGD;
1477 break;
1478 case MCSymbolRefExpr::VK_TLSLD:
1479 Type = ELF::R_X86_64_TLSLD;
1480 break;
1481 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001482 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001483 case FK_PCRel_2:
1484 assert(Modifier == MCSymbolRefExpr::VK_None);
1485 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001486 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001487 case FK_PCRel_1:
1488 assert(Modifier == MCSymbolRefExpr::VK_None);
1489 Type = ELF::R_X86_64_PC8;
1490 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001491 }
1492 } else {
1493 switch ((unsigned)Fixup.getKind()) {
1494 default: llvm_unreachable("invalid fixup kind!");
1495 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1496 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001497 assert(isInt<32>(Target.getConstant()));
1498 switch (Modifier) {
1499 default:
1500 llvm_unreachable("Unimplemented");
1501 case MCSymbolRefExpr::VK_None:
1502 Type = ELF::R_X86_64_32S;
1503 break;
1504 case MCSymbolRefExpr::VK_GOT:
1505 Type = ELF::R_X86_64_GOT32;
1506 break;
1507 case MCSymbolRefExpr::VK_GOTPCREL:
1508 Type = ELF::R_X86_64_GOTPCREL;
1509 break;
1510 case MCSymbolRefExpr::VK_TPOFF:
1511 Type = ELF::R_X86_64_TPOFF32;
1512 break;
1513 case MCSymbolRefExpr::VK_DTPOFF:
1514 Type = ELF::R_X86_64_DTPOFF32;
1515 break;
1516 }
1517 break;
1518 case FK_Data_4:
1519 Type = ELF::R_X86_64_32;
1520 break;
1521 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001522 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001523 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1524 }
1525 }
1526 } else {
1527 if (IsPCRel) {
1528 switch (Modifier) {
1529 default:
1530 llvm_unreachable("Unimplemented");
1531 case MCSymbolRefExpr::VK_None:
1532 Type = ELF::R_386_PC32;
1533 break;
1534 case MCSymbolRefExpr::VK_PLT:
1535 Type = ELF::R_386_PLT32;
1536 break;
1537 }
1538 } else {
1539 switch ((unsigned)Fixup.getKind()) {
1540 default: llvm_unreachable("invalid fixup kind!");
1541
1542 case X86::reloc_global_offset_table:
1543 Type = ELF::R_386_GOTPC;
1544 break;
1545
1546 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1547 // instead?
1548 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001549 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001550 case FK_Data_4:
1551 switch (Modifier) {
1552 default:
1553 llvm_unreachable("Unimplemented");
1554 case MCSymbolRefExpr::VK_None:
1555 Type = ELF::R_386_32;
1556 break;
1557 case MCSymbolRefExpr::VK_GOT:
1558 Type = ELF::R_386_GOT32;
1559 break;
1560 case MCSymbolRefExpr::VK_GOTOFF:
1561 Type = ELF::R_386_GOTOFF;
1562 break;
1563 case MCSymbolRefExpr::VK_TLSGD:
1564 Type = ELF::R_386_TLS_GD;
1565 break;
1566 case MCSymbolRefExpr::VK_TPOFF:
1567 Type = ELF::R_386_TLS_LE_32;
1568 break;
1569 case MCSymbolRefExpr::VK_INDNTPOFF:
1570 Type = ELF::R_386_TLS_IE;
1571 break;
1572 case MCSymbolRefExpr::VK_NTPOFF:
1573 Type = ELF::R_386_TLS_LE;
1574 break;
1575 case MCSymbolRefExpr::VK_GOTNTPOFF:
1576 Type = ELF::R_386_TLS_GOTIE;
1577 break;
1578 case MCSymbolRefExpr::VK_TLSLDM:
1579 Type = ELF::R_386_TLS_LDM;
1580 break;
1581 case MCSymbolRefExpr::VK_DTPOFF:
1582 Type = ELF::R_386_TLS_LDO_32;
1583 break;
1584 }
1585 break;
1586 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001587 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001588 case FK_Data_1: Type = ELF::R_386_8; break;
1589 }
1590 }
1591 }
1592
1593 if (RelocNeedsGOT(Modifier))
1594 NeedsGOT = true;
1595
Jason W Kim56a39902010-12-06 21:57:34 +00001596 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001597}