blob: 3492cbc55d66a1a8beb8dbea32e0f3eb48318957 [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,
503 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000504 unsigned Index = 1;
505 for (MCAssembler::iterator it = Asm.begin(),
506 ie = Asm.end(); it != ie; ++it) {
507 const MCSectionELF &Section =
508 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000509 if (Section.getType() != ELF::SHT_GROUP)
510 continue;
511 SectionIndexMap[&Section] = Index++;
512 }
513
514 for (MCAssembler::iterator it = Asm.begin(),
515 ie = Asm.end(); it != ie; ++it) {
516 const MCSectionELF &Section =
517 static_cast<const MCSectionELF &>(it->getSection());
518 if (Section.getType() == ELF::SHT_GROUP)
519 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000520 SectionIndexMap[&Section] = Index++;
521 }
522}
523
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000524void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000525 const SectionIndexMapTy &SectionIndexMap,
526 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000527 // FIXME: Is this the correct place to do this?
528 if (NeedsGOT) {
529 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
530 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
531 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
532 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000533 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000534 }
535
Matt Fleming3565a062010-08-16 18:57:57 +0000536 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000537 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000538
539 // Index 0 is always the empty string.
540 StringMap<uint64_t> StringIndexMap;
541 StringTable += '\x00';
542
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000543 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000544 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
545 ie = Asm.symbol_end(); it != ie; ++it) {
546 const MCSymbol &Symbol = it->getSymbol();
547
Rafael Espindola484291c2010-11-01 14:28:48 +0000548 bool Used = UsedInReloc.count(&Symbol);
549 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000550 bool isSignature = RevGroupMap.count(&Symbol);
551
552 if (!isInSymtab(Asm, *it,
553 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000554 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000555 continue;
556
Matt Fleming3565a062010-08-16 18:57:57 +0000557 ELFSymbolData MSD;
558 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000559 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000560
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000561 // Undefined symbols are global, but this is the first place we
562 // are able to set it.
563 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000564 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000565 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000566 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
567 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000568 }
569
Rafael Espindola484291c2010-11-01 14:28:48 +0000570 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000571 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000572
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000573 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000574 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000575 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000576 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000577 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000578 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000579 if (isSignature && !Used)
580 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
581 else
582 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000583 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000584 const MCSectionELF &Section =
585 static_cast<const MCSectionELF&>(RefSymbol.getSection());
586 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000587 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
588 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000589 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000590 }
591
Rafael Espindola88182132010-10-27 15:18:17 +0000592 // The @@@ in symbol version is replaced with @ in undefined symbols and
593 // @@ in defined ones.
594 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000595 SmallString<32> Buf;
596
Rafael Espindola88182132010-10-27 15:18:17 +0000597 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000598 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000599 Buf += Name.substr(0, Pos);
600 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
601 Buf += Name.substr(Pos + Skip);
602 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000603 }
604
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000605 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000606 if (!Entry) {
607 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000608 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000609 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000610 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000611 MSD.StringIndex = Entry;
612 if (MSD.SectionIndex == ELF::SHN_UNDEF)
613 UndefinedSymbolData.push_back(MSD);
614 else if (Local)
615 LocalSymbolData.push_back(MSD);
616 else
617 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000618 }
619
620 // Symbols are required to be in lexicographic order.
621 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
622 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
623 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
624
625 // Set the symbol indices. Local symbols must come before all other
626 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000627 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000628 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
629 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000630
631 Index += NumRegularSections;
632
Matt Fleming3565a062010-08-16 18:57:57 +0000633 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
634 ExternalSymbolData[i].SymbolData->setIndex(Index++);
635 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
636 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
637}
638
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000639void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
640 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000641 if (!Relocations[&SD].empty()) {
642 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000643 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000644 const MCSectionELF &Section =
645 static_cast<const MCSectionELF&>(SD.getSection());
646
647 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000648 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000649 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000650
651 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000652 if (hasRelocationAddend())
653 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000654 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000655 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000656
Rafael Espindolabff66a82010-12-18 03:27:34 +0000657 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +0000658 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000659 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000660 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000661
662 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000663 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000664
665 MCDataFragment *F = new MCDataFragment(&RelaSD);
666
667 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +0000668 }
669}
670
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000671void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
672 uint64_t Flags, uint64_t Address,
673 uint64_t Offset, uint64_t Size,
674 uint32_t Link, uint32_t Info,
675 uint64_t Alignment,
676 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000677 Write32(Name); // sh_name: index into string table
678 Write32(Type); // sh_type
679 WriteWord(Flags); // sh_flags
680 WriteWord(Address); // sh_addr
681 WriteWord(Offset); // sh_offset
682 WriteWord(Size); // sh_size
683 Write32(Link); // sh_link
684 Write32(Info); // sh_info
685 WriteWord(Alignment); // sh_addralign
686 WriteWord(EntrySize); // sh_entsize
687}
688
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000689void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
690 MCDataFragment *F,
691 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000692 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
693 // sort by the r_offset just like gnu as does
694 array_pod_sort(Relocs.begin(), Relocs.end());
695
696 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
697 ELFRelocationEntry entry = Relocs[e - i - 1];
698
Rafael Espindola12203cc2010-11-21 00:48:25 +0000699 if (!entry.Index)
700 ;
701 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000702 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
703 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000704 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000705 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000706 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000707
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000708 struct ELF::Elf64_Rela ERE64;
709 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000710 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000711
Rafael Espindolabff66a82010-12-18 03:27:34 +0000712 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000713 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000714 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000715 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000716
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000717 struct ELF::Elf32_Rela ERE32;
718 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000719 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000720
Rafael Espindolabff66a82010-12-18 03:27:34 +0000721 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000722 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000723 }
Matt Fleming3565a062010-08-16 18:57:57 +0000724 }
725}
726
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000727void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
728 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000729 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000730 MCContext &Ctx = Asm.getContext();
731 MCDataFragment *F;
732
Rafael Espindolabff66a82010-12-18 03:27:34 +0000733 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000734
Rafael Espindola38738bf2010-09-22 19:04:41 +0000735 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000736 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000737 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000738 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000739 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
740 ShstrtabSD.setAlignment(1);
741 ShstrtabIndex = Asm.size();
742
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000743 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000744 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
745 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000746 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000747 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000748 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000749 SymbolTableIndex = Asm.size();
750
751 MCSectionData *SymtabShndxSD = NULL;
752
753 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000754 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000755 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000756 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000757 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
758 SymtabShndxSD->setAlignment(4);
759 }
Matt Fleming3565a062010-08-16 18:57:57 +0000760
Matt Fleming3565a062010-08-16 18:57:57 +0000761 const MCSection *StrtabSection;
762 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000763 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000764 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
765 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000766 StringTableIndex = Asm.size();
767
Rafael Espindolac3c413f2010-09-27 22:04:54 +0000768 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +0000769
770 // Symbol table
771 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000772 MCDataFragment *ShndxF = NULL;
773 if (NeedsSymtabShndx) {
774 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000775 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000776 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000777
Matt Fleming3565a062010-08-16 18:57:57 +0000778 F = new MCDataFragment(&StrtabSD);
779 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000780
Matt Fleming3565a062010-08-16 18:57:57 +0000781 F = new MCDataFragment(&ShstrtabSD);
782
Matt Fleming3565a062010-08-16 18:57:57 +0000783 // Section header string table.
784 //
785 // The first entry of a string table holds a null character so skip
786 // section 0.
787 uint64_t Index = 1;
788 F->getContents() += '\x00';
789
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000790 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000791 for (MCAssembler::const_iterator it = Asm.begin(),
792 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +0000793 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000794 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +0000795 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +0000796
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000797 StringRef Name = Section.getSectionName();
798 if (SecStringMap.count(Name)) {
799 SectionStringTableIndex[&Section] = SecStringMap[Name];
800 continue;
801 }
Matt Fleming3565a062010-08-16 18:57:57 +0000802 // Remember the index into the string table so we can write it
803 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000804 SectionStringTableIndex[&Section] = Index;
805 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000806
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000807 Index += Name.size() + 1;
808 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000809 F->getContents() += '\x00';
810 }
Rafael Espindola70703872010-09-30 02:22:20 +0000811}
812
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000813void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
814 MCAsmLayout &Layout,
815 GroupMapTy &GroupMap,
816 RevGroupMapTy &RevGroupMap) {
817 // Create the .note.GNU-stack section if needed.
818 MCContext &Ctx = Asm.getContext();
819 if (Asm.getNoExecStack()) {
820 const MCSectionELF *GnuStackSection =
821 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
822 SectionKind::getReadOnly());
823 Asm.getOrCreateSectionData(*GnuStackSection);
824 }
825
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000826 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000827 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
828 it != ie; ++it) {
829 const MCSectionELF &Section =
830 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000831 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000832 continue;
833
834 const MCSymbol *SignatureSymbol = Section.getGroup();
835 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000836 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000837 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000838 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000839 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
840 Data.setAlignment(4);
841 MCDataFragment *F = new MCDataFragment(&Data);
842 String32(*F, ELF::GRP_COMDAT);
843 }
844 GroupMap[Group] = SignatureSymbol;
845 }
846
847 // Add sections to the groups
848 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000849 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000850 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
851 it != ie; ++it, ++Index) {
852 const MCSectionELF &Section =
853 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000854 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000855 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000856 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000857 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
858 // FIXME: we could use the previous fragment
859 MCDataFragment *F = new MCDataFragment(&Data);
860 String32(*F, NumGroups + Index);
861 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000862}
863
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000864void ELFObjectWriter::WriteSection(MCAssembler &Asm,
865 const SectionIndexMapTy &SectionIndexMap,
866 uint32_t GroupSymbolIndex,
867 uint64_t Offset, uint64_t Size,
868 uint64_t Alignment,
869 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000870 uint64_t sh_link = 0;
871 uint64_t sh_info = 0;
872
873 switch(Section.getType()) {
874 case ELF::SHT_DYNAMIC:
875 sh_link = SectionStringTableIndex[&Section];
876 sh_info = 0;
877 break;
878
879 case ELF::SHT_REL:
880 case ELF::SHT_RELA: {
881 const MCSectionELF *SymtabSection;
882 const MCSectionELF *InfoSection;
883 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
884 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000885 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000886 sh_link = SectionIndexMap.lookup(SymtabSection);
887 assert(sh_link && ".symtab not found");
888
889 // Remove ".rel" and ".rela" prefixes.
890 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
891 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
892
893 InfoSection = Asm.getContext().getELFSection(SectionName,
894 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000895 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000896 sh_info = SectionIndexMap.lookup(InfoSection);
897 break;
898 }
899
900 case ELF::SHT_SYMTAB:
901 case ELF::SHT_DYNSYM:
902 sh_link = StringTableIndex;
903 sh_info = LastLocalSymbolIndex;
904 break;
905
906 case ELF::SHT_SYMTAB_SHNDX:
907 sh_link = SymbolTableIndex;
908 break;
909
910 case ELF::SHT_PROGBITS:
911 case ELF::SHT_STRTAB:
912 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000913 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000914 case ELF::SHT_NULL:
915 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +0000916 case ELF::SHT_INIT_ARRAY:
917 case ELF::SHT_FINI_ARRAY:
918 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000919 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000920 // Nothing to do.
921 break;
922
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000923 case ELF::SHT_GROUP: {
924 sh_link = SymbolTableIndex;
925 sh_info = GroupSymbolIndex;
926 break;
927 }
928
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000929 default:
930 assert(0 && "FIXME: sh_type value not supported!");
931 break;
932 }
933
934 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
935 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
936 Alignment, Section.getEntrySize());
937}
938
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000939bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +0000940 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000941 !SD.getSection().isVirtualSection();
942}
943
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000944uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000945 uint64_t Ret = 0;
946 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
947 ++i) {
948 const MCFragment &F = *i;
949 assert(F.getKind() == MCFragment::FT_Data);
950 Ret += cast<MCDataFragment>(F).getContents().size();
951 }
952 return Ret;
953}
954
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000955uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
956 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000957 if (IsELFMetaDataSection(SD))
958 return DataSectionSize(SD);
959 return Layout.getSectionFileSize(&SD);
960}
961
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000962uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
963 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000964 if (IsELFMetaDataSection(SD))
965 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000966 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000967}
968
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000969void ELFObjectWriter::WriteDataSectionData(ELFObjectWriter *W,
970 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +0000971 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
972 ++i) {
973 const MCFragment &F = *i;
974 assert(F.getKind() == MCFragment::FT_Data);
975 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
976 }
977}
978
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000979void ELFObjectWriter::WriteObject(MCAssembler &Asm,
980 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000981 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000982 RevGroupMapTy RevGroupMap;
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000983 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
984 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000985
Rafael Espindolabab2a802010-11-10 21:51:05 +0000986 SectionIndexMapTy SectionIndexMap;
987
988 ComputeIndexMap(Asm, SectionIndexMap);
989
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000990 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000991 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000992
Matt Fleming3565a062010-08-16 18:57:57 +0000993 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000994 const_cast<MCAsmLayout&>(Layout),
995 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000996
Rafael Espindola1d739a02010-11-10 22:34:07 +0000997 // Update to include the metadata sections.
998 ComputeIndexMap(Asm, SectionIndexMap);
999
Matt Fleming3565a062010-08-16 18:57:57 +00001000 // Add 1 for the null section.
1001 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001002 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1003 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1004 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001005 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001006
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001007 std::vector<const MCSectionELF*> Sections;
1008 Sections.resize(NumSections);
1009
1010 for (SectionIndexMapTy::const_iterator i=
1011 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1012 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1013 Sections[p.second] = p.first;
1014 }
1015
1016 for (unsigned i = 1; i < NumSections; ++i) {
1017 const MCSectionELF &Section = *Sections[i];
1018 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001019
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001020 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1021
Matt Fleming3565a062010-08-16 18:57:57 +00001022 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001023 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001024 }
1025
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001026 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1027
Matt Fleming3565a062010-08-16 18:57:57 +00001028 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001029 WriteHeader(FileOff - HeaderSize, NumSections);
1030
1031 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001032
1033 // ... then all of the sections ...
1034 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1035
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001036 for (unsigned i = 1; i < NumSections; ++i) {
1037 const MCSectionELF &Section = *Sections[i];
1038 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001039
1040 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1041 WriteZeros(Padding);
1042 FileOff += Padding;
1043
Matt Fleming3565a062010-08-16 18:57:57 +00001044 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001045 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001046
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001047 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001048
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001049 if (IsELFMetaDataSection(SD))
1050 WriteDataSectionData(this, SD);
1051 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001052 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001053 }
1054
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001055 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1056 WriteZeros(Padding);
1057 FileOff += Padding;
1058
Matt Fleming3565a062010-08-16 18:57:57 +00001059 // ... and then the section header table.
1060 // Should we align the section header table?
1061 //
1062 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001063 uint64_t FirstSectionSize =
1064 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1065 uint32_t FirstSectionLink =
1066 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1067 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001068
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001069 for (unsigned i = 1; i < NumSections; ++i) {
1070 const MCSectionELF &Section = *Sections[i];
1071 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1072 uint32_t GroupSymbolIndex;
1073 if (Section.getType() != ELF::SHT_GROUP)
1074 GroupSymbolIndex = 0;
1075 else
1076 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001077
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001078 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001079
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001080 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001081 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001082 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001083 }
1084}
1085
Eli Friedman78c1e172011-03-03 07:24:36 +00001086bool
1087ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1088 const MCSymbolData &DataA,
1089 const MCFragment &FB,
1090 bool InSet,
1091 bool IsPCRel) const {
1092 if (DataA.getFlags() & ELF_STB_Weak)
1093 return false;
1094 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1095 Asm, DataA, FB,InSet, IsPCRel);
1096}
1097
Rafael Espindola6024c972010-12-17 17:45:22 +00001098MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1099 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001100 bool IsLittleEndian) {
1101 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001102 case ELF::EM_386:
1103 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001104 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001105 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001106 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001107 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001108 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001109 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001110 }
1111}
1112
1113
1114/// START OF SUBCLASSES for ELFObjectWriter
1115//===- ARMELFObjectWriter -------------------------------------------===//
1116
Rafael Espindola31f35782010-12-17 18:01:31 +00001117ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001118 raw_ostream &_OS,
1119 bool IsLittleEndian)
1120 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001121{}
1122
1123ARMELFObjectWriter::~ARMELFObjectWriter()
1124{}
1125
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001126// FIXME: get the real EABI Version from the Triple.
1127void ARMELFObjectWriter::WriteEFlags() {
1128 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1129}
1130
Jason W Kim953a2a32011-02-07 01:11:15 +00001131// In ARM, _MergedGlobals and other most symbols get emitted directly.
1132// I.e. not as an offset to a section symbol.
1133// This code is a first-cut approximation of what ARM/gcc does.
1134
1135const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1136 const MCValue &Target,
1137 const MCFragment &F,
1138 bool IsBSS) const {
1139 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1140 bool EmitThisSym = false;
1141
1142 if (IsBSS) {
1143 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1144 .Case("_MergedGlobals", true)
1145 .Default(false);
1146 } else {
1147 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1148 .Case("_MergedGlobals", true)
1149 .StartsWith(".L.str", true)
1150 .Default(false);
1151 }
1152 if (EmitThisSym)
1153 return &Symbol;
1154 if (! Symbol.isTemporary())
1155 return &Symbol;
1156 return NULL;
1157}
1158
Jason W Kim85fed5e2010-12-01 02:40:06 +00001159unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1160 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001161 bool IsPCRel,
1162 bool IsRelocWithSymbol,
1163 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001164 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1165 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1166
Jason W Kima0871e72010-12-08 23:14:44 +00001167 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001168 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001169 switch ((unsigned)Fixup.getKind()) {
1170 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001171 case FK_Data_4:
1172 switch (Modifier) {
1173 default: llvm_unreachable("Unsupported Modifier");
1174 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001175 Type = ELF::R_ARM_BASE_PREL;
1176 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001177 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001178 assert(0 && "unimplemented");
1179 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001180 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1181 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001182 break;
1183 }
1184 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001185 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001186 switch (Modifier) {
1187 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001188 Type = ELF::R_ARM_PLT32;
1189 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001190 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001191 Type = ELF::R_ARM_CALL;
1192 break;
1193 }
1194 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001195 case ARM::fixup_arm_condbranch:
1196 Type = ELF::R_ARM_JUMP24;
1197 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001198 case ARM::fixup_arm_movt_hi16:
1199 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001200 Type = ELF::R_ARM_MOVT_PREL;
1201 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001202 case ARM::fixup_arm_movw_lo16:
1203 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001204 Type = ELF::R_ARM_MOVW_PREL_NC;
1205 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001206 case ARM::fixup_t2_movt_hi16:
1207 case ARM::fixup_t2_movt_hi16_pcrel:
1208 Type = ELF::R_ARM_THM_MOVT_PREL;
1209 break;
1210 case ARM::fixup_t2_movw_lo16:
1211 case ARM::fixup_t2_movw_lo16_pcrel:
1212 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1213 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001214 }
1215 } else {
1216 switch ((unsigned)Fixup.getKind()) {
1217 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001218 case FK_Data_4:
1219 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001220 default: llvm_unreachable("Unsupported Modifier"); break;
1221 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001222 Type = ELF::R_ARM_GOT_BREL;
1223 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001224 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001225 Type = ELF::R_ARM_TLS_GD32;
1226 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001227 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001228 Type = ELF::R_ARM_TLS_LE32;
1229 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001230 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001231 Type = ELF::R_ARM_TLS_IE32;
1232 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001233 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001234 Type = ELF::R_ARM_ABS32;
1235 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001236 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001237 Type = ELF::R_ARM_GOTOFF32;
1238 break;
1239 }
1240 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001241 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001242 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001243 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001244 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001245 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001246 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001247 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001248 assert(0 && "Unimplemented");
1249 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001250 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001251 Type = ELF::R_ARM_CALL;
1252 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001253 case ARM::fixup_arm_condbranch:
1254 Type = ELF::R_ARM_JUMP24;
1255 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001256 case ARM::fixup_arm_movt_hi16:
1257 Type = ELF::R_ARM_MOVT_ABS;
1258 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001259 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001260 Type = ELF::R_ARM_MOVW_ABS_NC;
1261 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001262 case ARM::fixup_t2_movt_hi16:
1263 Type = ELF::R_ARM_THM_MOVT_ABS;
1264 break;
1265 case ARM::fixup_t2_movw_lo16:
1266 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1267 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001268 }
1269 }
1270
1271 if (RelocNeedsGOT(Modifier))
1272 NeedsGOT = true;
Jason W Kim953a2a32011-02-07 01:11:15 +00001273
Jason W Kima0871e72010-12-08 23:14:44 +00001274 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001275}
1276
Wesley Peck4b047132010-11-21 22:06:28 +00001277//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001278
Rafael Espindola31f35782010-12-17 18:01:31 +00001279MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001280 raw_ostream &_OS,
1281 bool IsLittleEndian)
1282 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001283}
1284
1285MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1286}
1287
Jason W Kim56a39902010-12-06 21:57:34 +00001288unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001289 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001290 bool IsPCRel,
1291 bool IsRelocWithSymbol,
1292 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001293 // determine the type of the relocation
1294 unsigned Type;
1295 if (IsPCRel) {
1296 switch ((unsigned)Fixup.getKind()) {
1297 default:
1298 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001299 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001300 Type = ELF::R_MICROBLAZE_64_PCREL;
1301 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001302 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001303 Type = ELF::R_MICROBLAZE_32_PCREL;
1304 break;
1305 }
1306 } else {
1307 switch ((unsigned)Fixup.getKind()) {
1308 default: llvm_unreachable("invalid fixup kind!");
1309 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001310 Type = ((IsRelocWithSymbol || Addend !=0)
1311 ? ELF::R_MICROBLAZE_32
1312 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001313 break;
1314 case FK_Data_2:
1315 Type = ELF::R_MICROBLAZE_32;
1316 break;
1317 }
1318 }
Jason W Kim56a39902010-12-06 21:57:34 +00001319 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001320}
Jason W Kimd3443e92010-11-15 16:18:39 +00001321
1322//===- X86ELFObjectWriter -------------------------------------------===//
1323
1324
Rafael Espindola31f35782010-12-17 18:01:31 +00001325X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001326 raw_ostream &_OS,
1327 bool IsLittleEndian)
1328 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001329{}
1330
1331X86ELFObjectWriter::~X86ELFObjectWriter()
1332{}
1333
Jason W Kim56a39902010-12-06 21:57:34 +00001334unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1335 const MCFixup &Fixup,
1336 bool IsPCRel,
1337 bool IsRelocWithSymbol,
1338 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001339 // determine the type of the relocation
1340
Rafael Espindola12203cc2010-11-21 00:48:25 +00001341 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1342 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001343 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001344 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001345 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001346 switch ((unsigned)Fixup.getKind()) {
1347 default: llvm_unreachable("invalid fixup kind!");
1348 case FK_PCRel_8:
1349 assert(Modifier == MCSymbolRefExpr::VK_None);
1350 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001351 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001352 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001353 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001354 case FK_Data_4: // FIXME?
1355 case X86::reloc_riprel_4byte:
1356 case FK_PCRel_4:
1357 switch (Modifier) {
1358 default:
1359 llvm_unreachable("Unimplemented");
1360 case MCSymbolRefExpr::VK_None:
1361 Type = ELF::R_X86_64_PC32;
1362 break;
1363 case MCSymbolRefExpr::VK_PLT:
1364 Type = ELF::R_X86_64_PLT32;
1365 break;
1366 case MCSymbolRefExpr::VK_GOTPCREL:
1367 Type = ELF::R_X86_64_GOTPCREL;
1368 break;
1369 case MCSymbolRefExpr::VK_GOTTPOFF:
1370 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001371 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001372 case MCSymbolRefExpr::VK_TLSGD:
1373 Type = ELF::R_X86_64_TLSGD;
1374 break;
1375 case MCSymbolRefExpr::VK_TLSLD:
1376 Type = ELF::R_X86_64_TLSLD;
1377 break;
1378 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001379 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001380 case FK_PCRel_2:
1381 assert(Modifier == MCSymbolRefExpr::VK_None);
1382 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001383 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001384 case FK_PCRel_1:
1385 assert(Modifier == MCSymbolRefExpr::VK_None);
1386 Type = ELF::R_X86_64_PC8;
1387 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001388 }
1389 } else {
1390 switch ((unsigned)Fixup.getKind()) {
1391 default: llvm_unreachable("invalid fixup kind!");
1392 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1393 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001394 assert(isInt<32>(Target.getConstant()));
1395 switch (Modifier) {
1396 default:
1397 llvm_unreachable("Unimplemented");
1398 case MCSymbolRefExpr::VK_None:
1399 Type = ELF::R_X86_64_32S;
1400 break;
1401 case MCSymbolRefExpr::VK_GOT:
1402 Type = ELF::R_X86_64_GOT32;
1403 break;
1404 case MCSymbolRefExpr::VK_GOTPCREL:
1405 Type = ELF::R_X86_64_GOTPCREL;
1406 break;
1407 case MCSymbolRefExpr::VK_TPOFF:
1408 Type = ELF::R_X86_64_TPOFF32;
1409 break;
1410 case MCSymbolRefExpr::VK_DTPOFF:
1411 Type = ELF::R_X86_64_DTPOFF32;
1412 break;
1413 }
1414 break;
1415 case FK_Data_4:
1416 Type = ELF::R_X86_64_32;
1417 break;
1418 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001419 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001420 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1421 }
1422 }
1423 } else {
1424 if (IsPCRel) {
1425 switch (Modifier) {
1426 default:
1427 llvm_unreachable("Unimplemented");
1428 case MCSymbolRefExpr::VK_None:
1429 Type = ELF::R_386_PC32;
1430 break;
1431 case MCSymbolRefExpr::VK_PLT:
1432 Type = ELF::R_386_PLT32;
1433 break;
1434 }
1435 } else {
1436 switch ((unsigned)Fixup.getKind()) {
1437 default: llvm_unreachable("invalid fixup kind!");
1438
1439 case X86::reloc_global_offset_table:
1440 Type = ELF::R_386_GOTPC;
1441 break;
1442
1443 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1444 // instead?
1445 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001446 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001447 case FK_Data_4:
1448 switch (Modifier) {
1449 default:
1450 llvm_unreachable("Unimplemented");
1451 case MCSymbolRefExpr::VK_None:
1452 Type = ELF::R_386_32;
1453 break;
1454 case MCSymbolRefExpr::VK_GOT:
1455 Type = ELF::R_386_GOT32;
1456 break;
1457 case MCSymbolRefExpr::VK_GOTOFF:
1458 Type = ELF::R_386_GOTOFF;
1459 break;
1460 case MCSymbolRefExpr::VK_TLSGD:
1461 Type = ELF::R_386_TLS_GD;
1462 break;
1463 case MCSymbolRefExpr::VK_TPOFF:
1464 Type = ELF::R_386_TLS_LE_32;
1465 break;
1466 case MCSymbolRefExpr::VK_INDNTPOFF:
1467 Type = ELF::R_386_TLS_IE;
1468 break;
1469 case MCSymbolRefExpr::VK_NTPOFF:
1470 Type = ELF::R_386_TLS_LE;
1471 break;
1472 case MCSymbolRefExpr::VK_GOTNTPOFF:
1473 Type = ELF::R_386_TLS_GOTIE;
1474 break;
1475 case MCSymbolRefExpr::VK_TLSLDM:
1476 Type = ELF::R_386_TLS_LDM;
1477 break;
1478 case MCSymbolRefExpr::VK_DTPOFF:
1479 Type = ELF::R_386_TLS_LDO_32;
1480 break;
1481 }
1482 break;
1483 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001484 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001485 case FK_Data_1: Type = ELF::R_386_8; break;
1486 }
1487 }
1488 }
1489
1490 if (RelocNeedsGOT(Modifier))
1491 NeedsGOT = true;
1492
Jason W Kim56a39902010-12-06 21:57:34 +00001493 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001494}