blob: fa5321d3a741586817a4374a00b4f079092b8823 [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"
Evan Cheng78c10ee2011-07-25 23:24:55 +000018#include "llvm/MC/MCAsmBackend.h"
Matt Fleming3565a062010-08-16 18:57:57 +000019#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCContext.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCExpr.h"
Matt Fleming3565a062010-08-16 18:57:57 +000022#include "llvm/MC/MCSectionELF.h"
Matt Fleming3565a062010-08-16 18:57:57 +000023#include "llvm/MC/MCValue.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/ELF.h"
Jason W Kime964d112011-05-11 22:53:06 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/ADT/Statistic.h"
Evan Chenga7cfc082011-07-23 00:45:41 +000029#include "llvm/ADT/StringSwitch.h"
Matt Fleming3565a062010-08-16 18:57:57 +000030
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +000031#include "../Target/Mips/MCTargetDesc/MipsFixupKinds.h"
Evan Cheng8c3fee52011-07-25 18:43:53 +000032#include "../Target/X86/MCTargetDesc/X86FixupKinds.h"
Evan Chengbe740292011-07-23 00:00:19 +000033#include "../Target/ARM/MCTargetDesc/ARMFixupKinds.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000034#include "../Target/PowerPC/MCTargetDesc/PPCFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000035
36#include <vector>
37using namespace llvm;
38
Jason W Kime964d112011-05-11 22:53:06 +000039#undef DEBUG_TYPE
40#define DEBUG_TYPE "reloc-info"
41
Jan Sjödin2ddfd952011-02-28 21:45:04 +000042bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
43 const MCFixupKindInfo &FKI =
44 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
45
46 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
47}
48
49bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
50 switch (Variant) {
51 default:
52 return false;
53 case MCSymbolRefExpr::VK_GOT:
54 case MCSymbolRefExpr::VK_PLT:
55 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +000056 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +000057 case MCSymbolRefExpr::VK_TPOFF:
58 case MCSymbolRefExpr::VK_TLSGD:
59 case MCSymbolRefExpr::VK_GOTTPOFF:
60 case MCSymbolRefExpr::VK_INDNTPOFF:
61 case MCSymbolRefExpr::VK_NTPOFF:
62 case MCSymbolRefExpr::VK_GOTNTPOFF:
63 case MCSymbolRefExpr::VK_TLSLDM:
64 case MCSymbolRefExpr::VK_DTPOFF:
65 case MCSymbolRefExpr::VK_TLSLD:
66 return true;
67 }
68}
69
Jason W Kimd3443e92010-11-15 16:18:39 +000070ELFObjectWriter::~ELFObjectWriter()
71{}
72
Matt Fleming3565a062010-08-16 18:57:57 +000073// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000074void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
75 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000076 // ELF Header
77 // ----------
78 //
79 // Note
80 // ----
81 // emitWord method behaves differently for ELF32 and ELF64, writing
82 // 4 bytes in the former and 8 in the latter.
83
84 Write8(0x7f); // e_ident[EI_MAG0]
85 Write8('E'); // e_ident[EI_MAG1]
86 Write8('L'); // e_ident[EI_MAG2]
87 Write8('F'); // e_ident[EI_MAG3]
88
Rafael Espindolabff66a82010-12-18 03:27:34 +000089 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000090
91 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000092 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +000093
94 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +000095 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +000096 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +000097 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
98 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
99 default: Write8(ELF::ELFOSABI_NONE); break;
100 }
Matt Fleming3565a062010-08-16 18:57:57 +0000101 Write8(0); // e_ident[EI_ABIVERSION]
102
103 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
104
105 Write16(ELF::ET_REL); // e_type
106
Rafael Espindolabff66a82010-12-18 03:27:34 +0000107 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000108
109 Write32(ELF::EV_CURRENT); // e_version
110 WriteWord(0); // e_entry, no entry point in .o file
111 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000112 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000113 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000114
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000115 // e_flags = whatever the target wants
116 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000117
118 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000119 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000120
121 Write16(0); // e_phentsize = prog header entry size
122 Write16(0); // e_phnum = # prog header entries = 0
123
124 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000125 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000126
127 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000128 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000129 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000130 else
131 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000132
133 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000134 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000135 Write16(ELF::SHN_XINDEX);
136 else
137 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000138}
139
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000140void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
141 MCDataFragment *ShndxF,
142 uint64_t name,
143 uint8_t info, uint64_t value,
144 uint64_t size, uint8_t other,
145 uint32_t shndx,
146 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000147 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000148 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000149 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000150 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000151 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000152 }
153
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000154 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
155 uint16_t(ELF::SHN_XINDEX) : shndx;
156
Rafael Espindolabff66a82010-12-18 03:27:34 +0000157 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000158 String32(*SymtabF, name); // st_name
159 String8(*SymtabF, info); // st_info
160 String8(*SymtabF, other); // st_other
161 String16(*SymtabF, Index); // st_shndx
162 String64(*SymtabF, value); // st_value
163 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000164 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000165 String32(*SymtabF, name); // st_name
166 String32(*SymtabF, value); // st_value
167 String32(*SymtabF, size); // st_size
168 String8(*SymtabF, info); // st_info
169 String8(*SymtabF, other); // st_other
170 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000171 }
172}
173
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000174uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
175 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000176 if (Data.isCommon() && Data.isExternal())
177 return Data.getCommonAlignment();
178
179 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000180
181 if (Symbol.isAbsolute() && Symbol.isVariable()) {
182 if (const MCExpr *Value = Symbol.getVariableValue()) {
183 int64_t IntValue;
184 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000185 return (uint64_t)IntValue;
Roman Divackyd1491862010-12-20 21:14:39 +0000186 }
187 }
188
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000189 if (!Symbol.isInSection())
190 return 0;
191
Rafael Espindola64695402011-05-16 16:17:21 +0000192
193 if (Data.getFragment()) {
194 if (Data.getFlags() & ELF_Other_ThumbFunc)
195 return Layout.getSymbolOffset(&Data)+1;
196 else
197 return Layout.getSymbolOffset(&Data);
198 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000199
200 return 0;
201}
202
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000203void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
204 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000205 // The presence of symbol versions causes undefined symbols and
206 // versions declared with @@@ to be renamed.
207
208 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
209 ie = Asm.symbol_end(); it != ie; ++it) {
210 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000211 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000212 MCSymbolData &SD = Asm.getSymbolData(Symbol);
213
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000214 // Not an alias.
215 if (&Symbol == &Alias)
216 continue;
217
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000218 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000219 size_t Pos = AliasName.find('@');
220 if (Pos == StringRef::npos)
221 continue;
222
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000223 // Aliases defined with .symvar copy the binding from the symbol they alias.
224 // This is the first place we are able to copy this information.
225 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000226 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000227
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000228 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000229 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
230 continue;
231
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000232 // FIXME: produce a better error message.
233 if (Symbol.isUndefined() && Rest.startswith("@@") &&
234 !Rest.startswith("@@@"))
235 report_fatal_error("A @@ version cannot be undefined");
236
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000237 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000238 }
239}
240
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000241void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
242 MCDataFragment *ShndxF,
243 ELFSymbolData &MSD,
244 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000245 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000246 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000247 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000248
Rafael Espindola7be2c332010-10-31 00:16:26 +0000249 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
250 Data.getSymbol().isVariable();
251
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000252 uint8_t Binding = MCELF::GetBinding(OrigData);
253 uint8_t Visibility = MCELF::GetVisibility(OrigData);
254 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000255
256 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
257 uint8_t Other = Visibility;
258
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000259 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000260 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000261
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000262 assert(!(Data.isCommon() && !Data.isExternal()));
263
Rafael Espindolaf0121242010-12-22 16:03:00 +0000264 const MCExpr *ESize = Data.getSize();
265 if (ESize) {
266 int64_t Res;
267 if (!ESize->EvaluateAsAbsolute(Res, Layout))
268 report_fatal_error("Size expression must be absolute.");
269 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000270 }
271
272 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000273 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
274 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000275}
276
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000277void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
278 MCDataFragment *ShndxF,
279 const MCAssembler &Asm,
280 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000281 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000282 // The string table must be emitted first because we need the index
283 // into the string table for all the symbol names.
284 assert(StringTable.size() && "Missing string table");
285
286 // FIXME: Make sure the start of the symbol table is aligned.
287
288 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000289 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000290
291 // Write the symbol table entries.
292 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
293 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
294 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000295 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000296 }
297
Rafael Espindola71859c62010-09-16 19:46:31 +0000298 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000299 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
300 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000301 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000302 static_cast<const MCSectionELF&>(i->getSection());
303 if (Section.getType() == ELF::SHT_RELA ||
304 Section.getType() == ELF::SHT_REL ||
305 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000306 Section.getType() == ELF::SHT_SYMTAB ||
307 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000308 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000309 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000310 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
311 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000312 LastLocalSymbolIndex++;
313 }
Matt Fleming3565a062010-08-16 18:57:57 +0000314
315 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
316 ELFSymbolData &MSD = ExternalSymbolData[i];
317 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000318 assert(((Data.getFlags() & ELF_STB_Global) ||
319 (Data.getFlags() & ELF_STB_Weak)) &&
320 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000321 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000322 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000323 LastLocalSymbolIndex++;
324 }
325
326 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
327 ELFSymbolData &MSD = UndefinedSymbolData[i];
328 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000329 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000330 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000331 LastLocalSymbolIndex++;
332 }
333}
334
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000335const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
336 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000337 const MCFragment &F,
338 const MCFixup &Fixup,
339 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000340 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000341 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
342 const MCSymbol *Renamed = Renames.lookup(&Symbol);
343 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000344
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000345 if (ASymbol.isUndefined()) {
346 if (Renamed)
347 return Renamed;
348 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000349 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000350
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000351 if (SD.isExternal()) {
352 if (Renamed)
353 return Renamed;
354 return &Symbol;
355 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000356
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000357 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000358 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000359 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000360
Rafael Espindola25958732010-11-24 21:57:39 +0000361 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000362 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000363
Rafael Espindola25958732010-11-24 21:57:39 +0000364 if (secKind.isThreadLocal()) {
365 if (Renamed)
366 return Renamed;
367 return &Symbol;
368 }
369
Rafael Espindola8cecf252010-10-06 16:23:36 +0000370 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000371 const MCSectionELF &Sec2 =
372 static_cast<const MCSectionELF&>(F.getParent()->getSection());
373
Rafael Espindola8cecf252010-10-06 16:23:36 +0000374 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000375 (Kind == MCSymbolRefExpr::VK_PLT ||
376 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000377 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000378 if (Renamed)
379 return Renamed;
380 return &Symbol;
381 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000382
Rafael Espindola1c130262011-01-23 04:43:11 +0000383 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000384 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000385 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000386 if (Renamed)
387 return Renamed;
388 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000389 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000390
Jason W Kime964d112011-05-11 22:53:06 +0000391 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
392
Rafael Espindola73ffea42010-09-25 05:42:19 +0000393}
394
Matt Fleming3565a062010-08-16 18:57:57 +0000395
Jason W Kim56a39902010-12-06 21:57:34 +0000396void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
397 const MCAsmLayout &Layout,
398 const MCFragment *Fragment,
399 const MCFixup &Fixup,
400 MCValue Target,
401 uint64_t &FixedValue) {
402 int64_t Addend = 0;
403 int Index = 0;
404 int64_t Value = Target.getConstant();
405 const MCSymbol *RelocSymbol = NULL;
406
Rafael Espindola127a6a42010-12-17 07:28:17 +0000407 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000408 if (!Target.isAbsolute()) {
409 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
410 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000411 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000412
413 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
414 const MCSymbol &SymbolB = RefB->getSymbol();
415 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
416 IsPCRel = true;
417
418 // Offset of the symbol in the section
419 int64_t a = Layout.getSymbolOffset(&SDB);
420
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000421 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000422 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
423 Value += b - a;
424 }
425
426 if (!RelocSymbol) {
427 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
428 MCFragment *F = SD.getFragment();
429
430 Index = F->getParent()->getOrdinal() + 1;
431
432 // Offset of the symbol in the section
433 Value += Layout.getSymbolOffset(&SD);
434 } else {
435 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
436 WeakrefUsedInReloc.insert(RelocSymbol);
437 else
438 UsedInReloc.insert(RelocSymbol);
439 Index = -1;
440 }
441 Addend = Value;
442 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000443 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000444 Value = 0;
445 }
446
447 FixedValue = Value;
448 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
449 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000450 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
451 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
452 if (RelocNeedsGOT(Modifier))
453 NeedsGOT = true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000454
Jason W Kim56a39902010-12-06 21:57:34 +0000455 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
456 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000457
458 adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000459
Rafael Espindolabff66a82010-12-18 03:27:34 +0000460 if (!hasRelocationAddend())
461 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000462
463 if (is64Bit())
464 assert(isInt<64>(Addend));
465 else
466 assert(isInt<32>(Addend));
467
Jason W Kim56a39902010-12-06 21:57:34 +0000468 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
469 Relocations[Fragment->getParent()].push_back(ERE);
470}
471
472
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000473uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000474ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
475 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000476 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000477 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000478}
479
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000480bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
481 const MCSymbolData &Data,
482 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000483 if (Data.getFlags() & ELF_Other_Weakref)
484 return false;
485
Rafael Espindolabd701182010-10-19 19:31:37 +0000486 if (Used)
487 return true;
488
Rafael Espindola88182132010-10-27 15:18:17 +0000489 if (Renamed)
490 return false;
491
Rafael Espindola737cd212010-10-05 18:01:23 +0000492 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000493
Rafael Espindolad1798862010-10-29 23:09:31 +0000494 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
495 return true;
496
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000497 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000498 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
499 return false;
500
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000501 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000502 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000503 return false;
504
Rafael Espindola737cd212010-10-05 18:01:23 +0000505 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
506 return false;
507
Rafael Espindolabd701182010-10-19 19:31:37 +0000508 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000509 return false;
510
511 return true;
512}
513
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000514bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
515 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000516 if (Data.isExternal())
517 return false;
518
519 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000520 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000521
522 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
523 if (isSignature && !isUsedInReloc)
524 return true;
525
Rafael Espindola737cd212010-10-05 18:01:23 +0000526 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000527 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000528
529 return true;
530}
531
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000532void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000533 SectionIndexMapTy &SectionIndexMap,
534 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000535 unsigned Index = 1;
536 for (MCAssembler::iterator it = Asm.begin(),
537 ie = Asm.end(); it != ie; ++it) {
538 const MCSectionELF &Section =
539 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000540 if (Section.getType() != ELF::SHT_GROUP)
541 continue;
542 SectionIndexMap[&Section] = Index++;
543 }
544
545 for (MCAssembler::iterator it = Asm.begin(),
546 ie = Asm.end(); it != ie; ++it) {
547 const MCSectionELF &Section =
548 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000549 if (Section.getType() == ELF::SHT_GROUP ||
550 Section.getType() == ELF::SHT_REL ||
551 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000552 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000553 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000554 const MCSectionELF *RelSection = RelMap.lookup(&Section);
555 if (RelSection)
556 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000557 }
558}
559
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000560void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000561 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000562 RevGroupMapTy RevGroupMap,
563 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000564 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000565 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000566 if (NeedsGOT) {
567 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
568 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
569 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
570 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000571 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000572 }
573
Matt Fleming3565a062010-08-16 18:57:57 +0000574 // Index 0 is always the empty string.
575 StringMap<uint64_t> StringIndexMap;
576 StringTable += '\x00';
577
Rafael Espindolad5321da2011-04-07 23:21:52 +0000578 // FIXME: We could optimize suffixes in strtab in the same way we
579 // optimize them in shstrtab.
580
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000581 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000582 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
583 ie = Asm.symbol_end(); it != ie; ++it) {
584 const MCSymbol &Symbol = it->getSymbol();
585
Rafael Espindola484291c2010-11-01 14:28:48 +0000586 bool Used = UsedInReloc.count(&Symbol);
587 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000588 bool isSignature = RevGroupMap.count(&Symbol);
589
590 if (!isInSymtab(Asm, *it,
591 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000592 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000593 continue;
594
Matt Fleming3565a062010-08-16 18:57:57 +0000595 ELFSymbolData MSD;
596 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000597 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000598
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000599 // Undefined symbols are global, but this is the first place we
600 // are able to set it.
601 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000602 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000603 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000604 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
605 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000606 }
607
Rafael Espindola484291c2010-11-01 14:28:48 +0000608 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000609 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000610
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000611 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000612 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000613 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000614 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000615 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000616 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000617 if (isSignature && !Used)
618 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
619 else
620 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000621 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000622 const MCSectionELF &Section =
623 static_cast<const MCSectionELF&>(RefSymbol.getSection());
624 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000625 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
626 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000627 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000628 }
629
Rafael Espindola88182132010-10-27 15:18:17 +0000630 // The @@@ in symbol version is replaced with @ in undefined symbols and
631 // @@ in defined ones.
632 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000633 SmallString<32> Buf;
634
Rafael Espindola88182132010-10-27 15:18:17 +0000635 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000636 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000637 Buf += Name.substr(0, Pos);
638 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
639 Buf += Name.substr(Pos + Skip);
640 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000641 }
642
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000643 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000644 if (!Entry) {
645 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000646 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000647 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000648 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000649 MSD.StringIndex = Entry;
650 if (MSD.SectionIndex == ELF::SHN_UNDEF)
651 UndefinedSymbolData.push_back(MSD);
652 else if (Local)
653 LocalSymbolData.push_back(MSD);
654 else
655 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000656 }
657
658 // Symbols are required to be in lexicographic order.
659 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
660 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
661 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
662
663 // Set the symbol indices. Local symbols must come before all other
664 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000665 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000666 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
667 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000668
669 Index += NumRegularSections;
670
Matt Fleming3565a062010-08-16 18:57:57 +0000671 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
672 ExternalSymbolData[i].SymbolData->setIndex(Index++);
673 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
674 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000675
676 if (NumRegularSections > ELF::SHN_LORESERVE)
677 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000678}
679
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000680void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
681 MCAsmLayout &Layout,
682 RelMapTy &RelMap) {
683 for (MCAssembler::const_iterator it = Asm.begin(),
684 ie = Asm.end(); it != ie; ++it) {
685 const MCSectionData &SD = *it;
686 if (Relocations[&SD].empty())
687 continue;
688
Matt Fleming3565a062010-08-16 18:57:57 +0000689 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000690 const MCSectionELF &Section =
691 static_cast<const MCSectionELF&>(SD.getSection());
692
693 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000694 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000695 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000696
697 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000698 if (hasRelocationAddend())
699 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000700 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000701 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000702
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000703 const MCSectionELF *RelaSection =
704 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
705 ELF::SHT_RELA : ELF::SHT_REL, 0,
706 SectionKind::getReadOnly(),
707 EntrySize, "");
708 RelMap[&Section] = RelaSection;
709 Asm.getOrCreateSectionData(*RelaSection);
710 }
711}
Matt Fleming3565a062010-08-16 18:57:57 +0000712
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000713void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
714 const RelMapTy &RelMap) {
715 for (MCAssembler::const_iterator it = Asm.begin(),
716 ie = Asm.end(); it != ie; ++it) {
717 const MCSectionData &SD = *it;
718 const MCSectionELF &Section =
719 static_cast<const MCSectionELF&>(SD.getSection());
720
721 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
722 if (!RelaSection)
723 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000724 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000725 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000726
727 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000728 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000729 }
730}
731
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000732void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
733 uint64_t Flags, uint64_t Address,
734 uint64_t Offset, uint64_t Size,
735 uint32_t Link, uint32_t Info,
736 uint64_t Alignment,
737 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000738 Write32(Name); // sh_name: index into string table
739 Write32(Type); // sh_type
740 WriteWord(Flags); // sh_flags
741 WriteWord(Address); // sh_addr
742 WriteWord(Offset); // sh_offset
743 WriteWord(Size); // sh_size
744 Write32(Link); // sh_link
745 Write32(Info); // sh_info
746 WriteWord(Alignment); // sh_addralign
747 WriteWord(EntrySize); // sh_entsize
748}
749
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000750void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
751 MCDataFragment *F,
752 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000753 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
754 // sort by the r_offset just like gnu as does
755 array_pod_sort(Relocs.begin(), Relocs.end());
756
757 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
758 ELFRelocationEntry entry = Relocs[e - i - 1];
759
Rafael Espindola12203cc2010-11-21 00:48:25 +0000760 if (!entry.Index)
761 ;
762 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000763 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
764 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000765 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000766 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000767 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000768
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000769 struct ELF::Elf64_Rela ERE64;
770 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000771 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000772
Rafael Espindolabff66a82010-12-18 03:27:34 +0000773 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000774 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000775 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000776 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000777
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000778 struct ELF::Elf32_Rela ERE32;
779 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000780 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000781
Rafael Espindolabff66a82010-12-18 03:27:34 +0000782 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000783 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000784 }
Matt Fleming3565a062010-08-16 18:57:57 +0000785 }
786}
787
Rafael Espindolad5321da2011-04-07 23:21:52 +0000788static int compareBySuffix(const void *a, const void *b) {
789 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
790 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
791 const StringRef &NameA = secA->getSectionName();
792 const StringRef &NameB = secB->getSectionName();
793 const unsigned sizeA = NameA.size();
794 const unsigned sizeB = NameB.size();
795 const unsigned len = std::min(sizeA, sizeB);
796 for (unsigned int i = 0; i < len; ++i) {
797 char ca = NameA[sizeA - i - 1];
798 char cb = NameB[sizeB - i - 1];
799 if (ca != cb)
800 return cb - ca;
801 }
802
803 return sizeB - sizeA;
804}
805
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000806void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
807 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000808 SectionIndexMapTy &SectionIndexMap,
809 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000810 MCContext &Ctx = Asm.getContext();
811 MCDataFragment *F;
812
Rafael Espindolabff66a82010-12-18 03:27:34 +0000813 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000814
Rafael Espindola38738bf2010-09-22 19:04:41 +0000815 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000816 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000817 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000818 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000819 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
820 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000821
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000822 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000823 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
824 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000825 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000826 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000827 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000828
829 MCSectionData *SymtabShndxSD = NULL;
830
831 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000832 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000833 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000834 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000835 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
836 SymtabShndxSD->setAlignment(4);
837 }
Matt Fleming3565a062010-08-16 18:57:57 +0000838
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000839 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000840 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000841 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000842 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
843 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000844
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000845 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
846
847 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
848 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
849 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000850
851 // Symbol table
852 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000853 MCDataFragment *ShndxF = NULL;
854 if (NeedsSymtabShndx) {
855 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000856 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000857 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000858
Matt Fleming3565a062010-08-16 18:57:57 +0000859 F = new MCDataFragment(&StrtabSD);
860 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000861
Matt Fleming3565a062010-08-16 18:57:57 +0000862 F = new MCDataFragment(&ShstrtabSD);
863
Rafael Espindolad5321da2011-04-07 23:21:52 +0000864 std::vector<const MCSectionELF*> Sections;
865 for (MCAssembler::const_iterator it = Asm.begin(),
866 ie = Asm.end(); it != ie; ++it) {
867 const MCSectionELF &Section =
868 static_cast<const MCSectionELF&>(it->getSection());
869 Sections.push_back(&Section);
870 }
871 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
872
Matt Fleming3565a062010-08-16 18:57:57 +0000873 // Section header string table.
874 //
875 // The first entry of a string table holds a null character so skip
876 // section 0.
877 uint64_t Index = 1;
878 F->getContents() += '\x00';
879
Rafael Espindolad5321da2011-04-07 23:21:52 +0000880 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
881 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000882
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000883 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000884 if (I != 0) {
885 StringRef PreviousName = Sections[I - 1]->getSectionName();
886 if (PreviousName.endswith(Name)) {
887 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
888 continue;
889 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000890 }
Matt Fleming3565a062010-08-16 18:57:57 +0000891 // Remember the index into the string table so we can write it
892 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000893 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000894
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000895 Index += Name.size() + 1;
896 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000897 F->getContents() += '\x00';
898 }
Rafael Espindola70703872010-09-30 02:22:20 +0000899}
900
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000901void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
902 MCAsmLayout &Layout,
903 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000904 RevGroupMapTy &RevGroupMap,
905 SectionIndexMapTy &SectionIndexMap,
906 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000907 // Create the .note.GNU-stack section if needed.
908 MCContext &Ctx = Asm.getContext();
909 if (Asm.getNoExecStack()) {
910 const MCSectionELF *GnuStackSection =
911 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
912 SectionKind::getReadOnly());
913 Asm.getOrCreateSectionData(*GnuStackSection);
914 }
915
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000916 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000917 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
918 it != ie; ++it) {
919 const MCSectionELF &Section =
920 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000921 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000922 continue;
923
924 const MCSymbol *SignatureSymbol = Section.getGroup();
925 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000926 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000927 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000928 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000929 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
930 Data.setAlignment(4);
931 MCDataFragment *F = new MCDataFragment(&Data);
932 String32(*F, ELF::GRP_COMDAT);
933 }
934 GroupMap[Group] = SignatureSymbol;
935 }
936
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000937 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
938
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000939 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000940 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000941 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000942 const MCSectionELF &Section =
943 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000944 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000945 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000946 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000947 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
948 // FIXME: we could use the previous fragment
949 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000950 unsigned Index = SectionIndexMap.lookup(&Section);
951 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000952 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000953}
954
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000955void ELFObjectWriter::WriteSection(MCAssembler &Asm,
956 const SectionIndexMapTy &SectionIndexMap,
957 uint32_t GroupSymbolIndex,
958 uint64_t Offset, uint64_t Size,
959 uint64_t Alignment,
960 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000961 uint64_t sh_link = 0;
962 uint64_t sh_info = 0;
963
964 switch(Section.getType()) {
965 case ELF::SHT_DYNAMIC:
966 sh_link = SectionStringTableIndex[&Section];
967 sh_info = 0;
968 break;
969
970 case ELF::SHT_REL:
971 case ELF::SHT_RELA: {
972 const MCSectionELF *SymtabSection;
973 const MCSectionELF *InfoSection;
974 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
975 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000976 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000977 sh_link = SectionIndexMap.lookup(SymtabSection);
978 assert(sh_link && ".symtab not found");
979
980 // Remove ".rel" and ".rela" prefixes.
981 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
982 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
983
984 InfoSection = Asm.getContext().getELFSection(SectionName,
985 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000986 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000987 sh_info = SectionIndexMap.lookup(InfoSection);
988 break;
989 }
990
991 case ELF::SHT_SYMTAB:
992 case ELF::SHT_DYNSYM:
993 sh_link = StringTableIndex;
994 sh_info = LastLocalSymbolIndex;
995 break;
996
997 case ELF::SHT_SYMTAB_SHNDX:
998 sh_link = SymbolTableIndex;
999 break;
1000
1001 case ELF::SHT_PROGBITS:
1002 case ELF::SHT_STRTAB:
1003 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001004 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001005 case ELF::SHT_NULL:
1006 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001007 case ELF::SHT_INIT_ARRAY:
1008 case ELF::SHT_FINI_ARRAY:
1009 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001010 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001011 // Nothing to do.
1012 break;
1013
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001014 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001015 sh_link = SymbolTableIndex;
1016 sh_info = GroupSymbolIndex;
1017 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001018
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001019 default:
1020 assert(0 && "FIXME: sh_type value not supported!");
1021 break;
1022 }
1023
1024 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1025 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1026 Alignment, Section.getEntrySize());
1027}
1028
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001029bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001030 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001031 !SD.getSection().isVirtualSection();
1032}
1033
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001034uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001035 uint64_t Ret = 0;
1036 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1037 ++i) {
1038 const MCFragment &F = *i;
1039 assert(F.getKind() == MCFragment::FT_Data);
1040 Ret += cast<MCDataFragment>(F).getContents().size();
1041 }
1042 return Ret;
1043}
1044
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001045uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1046 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001047 if (IsELFMetaDataSection(SD))
1048 return DataSectionSize(SD);
1049 return Layout.getSectionFileSize(&SD);
1050}
1051
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001052uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1053 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001054 if (IsELFMetaDataSection(SD))
1055 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001056 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001057}
1058
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001059void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1060 const MCAsmLayout &Layout,
1061 const MCSectionELF &Section) {
1062 uint64_t FileOff = OS.tell();
1063 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1064
1065 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1066 WriteZeros(Padding);
1067 FileOff += Padding;
1068
1069 FileOff += GetSectionFileSize(Layout, SD);
1070
1071 if (IsELFMetaDataSection(SD)) {
1072 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1073 ++i) {
1074 const MCFragment &F = *i;
1075 assert(F.getKind() == MCFragment::FT_Data);
1076 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1077 }
1078 } else {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001079 Asm.writeSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001080 }
1081}
1082
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001083void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1084 const GroupMapTy &GroupMap,
1085 const MCAsmLayout &Layout,
1086 const SectionIndexMapTy &SectionIndexMap,
1087 const SectionOffsetMapTy &SectionOffsetMap) {
1088 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001089
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001090 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001091 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001092
1093 for (SectionIndexMapTy::const_iterator i=
1094 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1095 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001096 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001097 }
1098
Matt Fleming3565a062010-08-16 18:57:57 +00001099 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001100 uint64_t FirstSectionSize =
1101 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1102 uint32_t FirstSectionLink =
1103 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1104 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001105
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001106 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001107 const MCSectionELF &Section = *Sections[i];
1108 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1109 uint32_t GroupSymbolIndex;
1110 if (Section.getType() != ELF::SHT_GROUP)
1111 GroupSymbolIndex = 0;
1112 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001113 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1114 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001115
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001116 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001117
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001118 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001119 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001120 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001121 }
1122}
1123
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001124void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1125 std::vector<const MCSectionELF*> &Sections) {
1126 for (MCAssembler::iterator it = Asm.begin(),
1127 ie = Asm.end(); it != ie; ++it) {
1128 const MCSectionELF &Section =
1129 static_cast<const MCSectionELF &>(it->getSection());
1130 if (Section.getType() == ELF::SHT_GROUP)
1131 Sections.push_back(&Section);
1132 }
1133
1134 for (MCAssembler::iterator it = Asm.begin(),
1135 ie = Asm.end(); it != ie; ++it) {
1136 const MCSectionELF &Section =
1137 static_cast<const MCSectionELF &>(it->getSection());
1138 if (Section.getType() != ELF::SHT_GROUP &&
1139 Section.getType() != ELF::SHT_REL &&
1140 Section.getType() != ELF::SHT_RELA)
1141 Sections.push_back(&Section);
1142 }
1143
1144 for (MCAssembler::iterator it = Asm.begin(),
1145 ie = Asm.end(); it != ie; ++it) {
1146 const MCSectionELF &Section =
1147 static_cast<const MCSectionELF &>(it->getSection());
1148 if (Section.getType() == ELF::SHT_REL ||
1149 Section.getType() == ELF::SHT_RELA)
1150 Sections.push_back(&Section);
1151 }
1152}
1153
1154void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1155 const MCAsmLayout &Layout) {
1156 GroupMapTy GroupMap;
1157 RevGroupMapTy RevGroupMap;
1158 SectionIndexMapTy SectionIndexMap;
1159
1160 unsigned NumUserSections = Asm.size();
1161
1162 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1163 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1164
1165 const unsigned NumUserAndRelocSections = Asm.size();
1166 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1167 RevGroupMap, SectionIndexMap, RelMap);
1168 const unsigned AllSections = Asm.size();
1169 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1170
1171 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1172
1173 // Compute symbol table information.
1174 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1175
1176
1177 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1178
1179 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1180 const_cast<MCAsmLayout&>(Layout),
1181 SectionIndexMap,
1182 RelMap);
1183
1184 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1185 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1186 sizeof(ELF::Elf32_Ehdr);
1187 uint64_t FileOff = HeaderSize;
1188
1189 std::vector<const MCSectionELF*> Sections;
1190 ComputeSectionOrder(Asm, Sections);
1191 unsigned NumSections = Sections.size();
1192 SectionOffsetMapTy SectionOffsetMap;
1193 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1194 const MCSectionELF &Section = *Sections[i];
1195 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1196
1197 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1198
1199 // Remember the offset into the file for this section.
1200 SectionOffsetMap[&Section] = FileOff;
1201
1202 // Get the size of the section in the output file (including padding).
1203 FileOff += GetSectionFileSize(Layout, SD);
1204 }
1205
1206 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1207
1208 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1209
1210 uint64_t SectionHeaderEntrySize = is64Bit() ?
1211 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1212 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1213
1214 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1215 const MCSectionELF &Section = *Sections[i];
1216 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1217
1218 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1219
1220 // Remember the offset into the file for this section.
1221 SectionOffsetMap[&Section] = FileOff;
1222
1223 // Get the size of the section in the output file (including padding).
1224 FileOff += GetSectionFileSize(Layout, SD);
1225 }
1226
1227 // Write out the ELF header ...
1228 WriteHeader(SectionHeaderOffset, NumSections + 1);
1229
1230 // ... then the regular sections ...
1231 // + because of .shstrtab
1232 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1233 WriteDataSectionData(Asm, Layout, *Sections[i]);
1234
1235 FileOff = OS.tell();
1236 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1237 WriteZeros(Padding);
1238
1239 // ... then the section header table ...
1240 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1241 SectionOffsetMap);
1242
1243 FileOff = OS.tell();
1244
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001245 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001246 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1247 WriteDataSectionData(Asm, Layout, *Sections[i]);
1248}
1249
Eli Friedman78c1e172011-03-03 07:24:36 +00001250bool
1251ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1252 const MCSymbolData &DataA,
1253 const MCFragment &FB,
1254 bool InSet,
1255 bool IsPCRel) const {
1256 if (DataA.getFlags() & ELF_STB_Weak)
1257 return false;
1258 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1259 Asm, DataA, FB,InSet, IsPCRel);
1260}
1261
Rafael Espindola6024c972010-12-17 17:45:22 +00001262MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1263 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001264 bool IsLittleEndian) {
1265 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001266 case ELF::EM_386:
1267 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001268 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001269 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001270 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001271 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001272 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001273 case ELF::EM_PPC:
1274 case ELF::EM_PPC64:
1275 return new PPCELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Akira Hatanaka291512f2011-09-30 21:55:40 +00001276 case ELF::EM_MIPS:
1277 return new MipsELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001278 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001279 }
1280}
1281
Jason W Kimd3443e92010-11-15 16:18:39 +00001282/// START OF SUBCLASSES for ELFObjectWriter
1283//===- ARMELFObjectWriter -------------------------------------------===//
1284
Rafael Espindola31f35782010-12-17 18:01:31 +00001285ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001286 raw_ostream &_OS,
1287 bool IsLittleEndian)
1288 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001289{}
1290
1291ARMELFObjectWriter::~ARMELFObjectWriter()
1292{}
1293
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001294// FIXME: get the real EABI Version from the Triple.
1295void ARMELFObjectWriter::WriteEFlags() {
1296 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1297}
1298
Jason W Kim953a2a32011-02-07 01:11:15 +00001299// In ARM, _MergedGlobals and other most symbols get emitted directly.
1300// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001301// This code is an approximation of what ARM/gcc does.
1302
1303STATISTIC(PCRelCount, "Total number of PIC Relocations");
1304STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001305
1306const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1307 const MCValue &Target,
1308 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001309 const MCFixup &Fixup,
1310 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001311 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1312 bool EmitThisSym = false;
1313
Jason W Kime964d112011-05-11 22:53:06 +00001314 const MCSectionELF &Section =
1315 static_cast<const MCSectionELF&>(Symbol.getSection());
Jason W Kime964d112011-05-11 22:53:06 +00001316 bool InNormalSection = true;
1317 unsigned RelocType = 0;
1318 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1319
Matt Beaumont-Gay6dcd4132011-05-11 23:34:51 +00001320 DEBUG(
1321 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1322 MCSymbolRefExpr::VariantKind Kind2;
1323 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1324 MCSymbolRefExpr::VK_None;
1325 dbgs() << "considering symbol "
Jason W Kime964d112011-05-11 22:53:06 +00001326 << Section.getSectionName() << "/"
1327 << Symbol.getName() << "/"
1328 << " Rel:" << (unsigned)RelocType
1329 << " Kind: " << (int)Kind << "/" << (int)Kind2
1330 << " Tmp:"
1331 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1332 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1333 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1334
Jason W Kimbebd44a2011-06-09 19:13:45 +00001335 if (IsPCRel) { ++PCRelCount;
Jason W Kime964d112011-05-11 22:53:06 +00001336 switch (RelocType) {
1337 default:
1338 // Most relocation types are emitted as explicit symbols
1339 InNormalSection =
1340 StringSwitch<bool>(Section.getSectionName())
1341 .Case(".data.rel.ro.local", false)
1342 .Case(".data.rel", false)
1343 .Case(".bss", false)
1344 .Default(true);
1345 EmitThisSym = true;
1346 break;
1347 case ELF::R_ARM_ABS32:
1348 // But things get strange with R_ARM_ABS32
1349 // In this case, most things that go in .rodata show up
1350 // as section relative relocations
1351 InNormalSection =
1352 StringSwitch<bool>(Section.getSectionName())
1353 .Case(".data.rel.ro.local", false)
1354 .Case(".data.rel", false)
1355 .Case(".rodata", false)
1356 .Case(".bss", false)
1357 .Default(true);
1358 EmitThisSym = false;
1359 break;
1360 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001361 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001362 NonPCRelCount++;
1363 InNormalSection =
1364 StringSwitch<bool>(Section.getSectionName())
1365 .Case(".data.rel.ro.local", false)
1366 .Case(".rodata", false)
1367 .Case(".data.rel", false)
1368 .Case(".bss", false)
1369 .Default(true);
1370
1371 switch (RelocType) {
1372 default: EmitThisSym = true; break;
1373 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1374 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001375 }
Jason W Kime964d112011-05-11 22:53:06 +00001376
Jason W Kim953a2a32011-02-07 01:11:15 +00001377 if (EmitThisSym)
1378 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001379 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001380 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001381 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001382 return NULL;
1383}
1384
Jason W Kime964d112011-05-11 22:53:06 +00001385// Need to examine the Fixup when determining whether to
1386// emit the relocation as an explicit symbol or as a section relative
1387// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001388unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1389 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001390 bool IsPCRel,
1391 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001392 int64_t Addend) const {
1393 return GetRelocTypeInner(Target, Fixup, IsPCRel);
Jason W Kime964d112011-05-11 22:53:06 +00001394}
1395
1396unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1397 const MCFixup &Fixup,
1398 bool IsPCRel) const {
1399 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1400 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1401
Jason W Kima0871e72010-12-08 23:14:44 +00001402 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001403 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001404 switch ((unsigned)Fixup.getKind()) {
1405 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001406 case FK_Data_4:
1407 switch (Modifier) {
1408 default: llvm_unreachable("Unsupported Modifier");
1409 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001410 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001411 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001412 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001413 assert(0 && "unimplemented");
1414 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001415 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1416 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001417 break;
1418 }
1419 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001420 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001421 switch (Modifier) {
1422 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001423 Type = ELF::R_ARM_PLT32;
1424 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001425 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001426 Type = ELF::R_ARM_CALL;
1427 break;
1428 }
1429 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001430 case ARM::fixup_arm_condbranch:
1431 Type = ELF::R_ARM_JUMP24;
1432 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001433 case ARM::fixup_arm_movt_hi16:
1434 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001435 Type = ELF::R_ARM_MOVT_PREL;
1436 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001437 case ARM::fixup_arm_movw_lo16:
1438 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001439 Type = ELF::R_ARM_MOVW_PREL_NC;
1440 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001441 case ARM::fixup_t2_movt_hi16:
1442 case ARM::fixup_t2_movt_hi16_pcrel:
1443 Type = ELF::R_ARM_THM_MOVT_PREL;
1444 break;
1445 case ARM::fixup_t2_movw_lo16:
1446 case ARM::fixup_t2_movw_lo16_pcrel:
1447 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1448 break;
Rafael Espindola298c8e12011-05-20 20:01:01 +00001449 case ARM::fixup_arm_thumb_bl:
1450 case ARM::fixup_arm_thumb_blx:
1451 switch (Modifier) {
1452 case MCSymbolRefExpr::VK_ARM_PLT:
1453 Type = ELF::R_ARM_THM_CALL;
1454 break;
1455 default:
1456 Type = ELF::R_ARM_NONE;
1457 break;
1458 }
1459 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001460 }
1461 } else {
1462 switch ((unsigned)Fixup.getKind()) {
1463 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001464 case FK_Data_4:
1465 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001466 default: llvm_unreachable("Unsupported Modifier"); break;
1467 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001468 Type = ELF::R_ARM_GOT_BREL;
1469 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001470 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001471 Type = ELF::R_ARM_TLS_GD32;
1472 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001473 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001474 Type = ELF::R_ARM_TLS_LE32;
1475 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001476 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001477 Type = ELF::R_ARM_TLS_IE32;
1478 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001479 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001480 Type = ELF::R_ARM_ABS32;
1481 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001482 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001483 Type = ELF::R_ARM_GOTOFF32;
1484 break;
1485 }
1486 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001487 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001488 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001489 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001490 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001491 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001492 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001493 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001494 assert(0 && "Unimplemented");
1495 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001496 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001497 Type = ELF::R_ARM_CALL;
1498 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001499 case ARM::fixup_arm_condbranch:
1500 Type = ELF::R_ARM_JUMP24;
1501 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001502 case ARM::fixup_arm_movt_hi16:
1503 Type = ELF::R_ARM_MOVT_ABS;
1504 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001505 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001506 Type = ELF::R_ARM_MOVW_ABS_NC;
1507 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001508 case ARM::fixup_t2_movt_hi16:
1509 Type = ELF::R_ARM_THM_MOVT_ABS;
1510 break;
1511 case ARM::fixup_t2_movw_lo16:
1512 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1513 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001514 }
1515 }
1516
Jason W Kima0871e72010-12-08 23:14:44 +00001517 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001518}
1519
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001520//===- PPCELFObjectWriter -------------------------------------------===//
1521
1522PPCELFObjectWriter::PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1523 raw_ostream &_OS,
1524 bool IsLittleEndian)
1525 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1526}
1527
1528PPCELFObjectWriter::~PPCELFObjectWriter() {
1529}
1530
1531unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
1532 const MCFixup &Fixup,
1533 bool IsPCRel,
1534 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001535 int64_t Addend) const {
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001536 // determine the type of the relocation
1537 unsigned Type;
1538 if (IsPCRel) {
1539 switch ((unsigned)Fixup.getKind()) {
1540 default:
1541 llvm_unreachable("Unimplemented");
1542 case PPC::fixup_ppc_br24:
1543 Type = ELF::R_PPC_REL24;
1544 break;
1545 case FK_PCRel_4:
1546 Type = ELF::R_PPC_REL32;
1547 break;
1548 }
1549 } else {
1550 switch ((unsigned)Fixup.getKind()) {
1551 default: llvm_unreachable("invalid fixup kind!");
1552 case PPC::fixup_ppc_br24:
1553 Type = ELF::R_PPC_ADDR24;
1554 break;
1555 case PPC::fixup_ppc_brcond14:
1556 Type = ELF::R_PPC_ADDR14_BRTAKEN; // XXX: or BRNTAKEN?_
1557 break;
1558 case PPC::fixup_ppc_ha16:
1559 Type = ELF::R_PPC_ADDR16_HA;
1560 break;
1561 case PPC::fixup_ppc_lo16:
1562 Type = ELF::R_PPC_ADDR16_LO;
1563 break;
1564 case PPC::fixup_ppc_lo14:
1565 Type = ELF::R_PPC_ADDR14;
1566 break;
1567 case FK_Data_4:
1568 Type = ELF::R_PPC_ADDR32;
1569 break;
1570 case FK_Data_2:
1571 Type = ELF::R_PPC_ADDR16;
1572 break;
1573 }
1574 }
1575 return Type;
1576}
1577
Jim Grosbach946227d2011-11-15 16:46:22 +00001578void PPCELFObjectWriter::
1579adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
Roman Divacky2a66cea2011-08-04 19:08:19 +00001580 switch ((unsigned)Fixup.getKind()) {
1581 case PPC::fixup_ppc_ha16:
1582 case PPC::fixup_ppc_lo16:
1583 RelocOffset += 2;
1584 break;
1585 default:
1586 break;
1587 }
1588}
1589
Wesley Peck4b047132010-11-21 22:06:28 +00001590//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001591
Rafael Espindola31f35782010-12-17 18:01:31 +00001592MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001593 raw_ostream &_OS,
1594 bool IsLittleEndian)
1595 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001596}
1597
1598MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1599}
1600
Jason W Kim56a39902010-12-06 21:57:34 +00001601unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001602 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001603 bool IsPCRel,
1604 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001605 int64_t Addend) const {
Wesley Peck4b047132010-11-21 22:06:28 +00001606 // determine the type of the relocation
1607 unsigned Type;
1608 if (IsPCRel) {
1609 switch ((unsigned)Fixup.getKind()) {
1610 default:
1611 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001612 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001613 Type = ELF::R_MICROBLAZE_64_PCREL;
1614 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001615 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001616 Type = ELF::R_MICROBLAZE_32_PCREL;
1617 break;
1618 }
1619 } else {
1620 switch ((unsigned)Fixup.getKind()) {
1621 default: llvm_unreachable("invalid fixup kind!");
1622 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001623 Type = ((IsRelocWithSymbol || Addend !=0)
1624 ? ELF::R_MICROBLAZE_32
1625 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001626 break;
1627 case FK_Data_2:
1628 Type = ELF::R_MICROBLAZE_32;
1629 break;
1630 }
1631 }
Jason W Kim56a39902010-12-06 21:57:34 +00001632 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001633}
Jason W Kimd3443e92010-11-15 16:18:39 +00001634
1635//===- X86ELFObjectWriter -------------------------------------------===//
1636
1637
Rafael Espindola31f35782010-12-17 18:01:31 +00001638X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001639 raw_ostream &_OS,
1640 bool IsLittleEndian)
1641 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001642{}
1643
1644X86ELFObjectWriter::~X86ELFObjectWriter()
1645{}
1646
Jason W Kim56a39902010-12-06 21:57:34 +00001647unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1648 const MCFixup &Fixup,
1649 bool IsPCRel,
1650 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001651 int64_t Addend) const {
Jason W Kimd3443e92010-11-15 16:18:39 +00001652 // determine the type of the relocation
1653
Rafael Espindola12203cc2010-11-21 00:48:25 +00001654 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1655 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001656 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001657 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001658 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001659 switch ((unsigned)Fixup.getKind()) {
1660 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001661
1662 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1663 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1664 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1665
Rafael Espindola3a83c402010-12-27 00:36:05 +00001666 case FK_PCRel_8:
1667 assert(Modifier == MCSymbolRefExpr::VK_None);
1668 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001669 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001670 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001671 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001672 case X86::reloc_riprel_4byte:
1673 case FK_PCRel_4:
1674 switch (Modifier) {
1675 default:
1676 llvm_unreachable("Unimplemented");
1677 case MCSymbolRefExpr::VK_None:
1678 Type = ELF::R_X86_64_PC32;
1679 break;
1680 case MCSymbolRefExpr::VK_PLT:
1681 Type = ELF::R_X86_64_PLT32;
1682 break;
1683 case MCSymbolRefExpr::VK_GOTPCREL:
1684 Type = ELF::R_X86_64_GOTPCREL;
1685 break;
1686 case MCSymbolRefExpr::VK_GOTTPOFF:
1687 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001688 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001689 case MCSymbolRefExpr::VK_TLSGD:
1690 Type = ELF::R_X86_64_TLSGD;
1691 break;
1692 case MCSymbolRefExpr::VK_TLSLD:
1693 Type = ELF::R_X86_64_TLSLD;
1694 break;
1695 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001696 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001697 case FK_PCRel_2:
1698 assert(Modifier == MCSymbolRefExpr::VK_None);
1699 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001700 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001701 case FK_PCRel_1:
1702 assert(Modifier == MCSymbolRefExpr::VK_None);
1703 Type = ELF::R_X86_64_PC8;
1704 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001705 }
1706 } else {
1707 switch ((unsigned)Fixup.getKind()) {
1708 default: llvm_unreachable("invalid fixup kind!");
1709 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1710 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001711 switch (Modifier) {
1712 default:
1713 llvm_unreachable("Unimplemented");
1714 case MCSymbolRefExpr::VK_None:
1715 Type = ELF::R_X86_64_32S;
1716 break;
1717 case MCSymbolRefExpr::VK_GOT:
1718 Type = ELF::R_X86_64_GOT32;
1719 break;
1720 case MCSymbolRefExpr::VK_GOTPCREL:
1721 Type = ELF::R_X86_64_GOTPCREL;
1722 break;
1723 case MCSymbolRefExpr::VK_TPOFF:
1724 Type = ELF::R_X86_64_TPOFF32;
1725 break;
1726 case MCSymbolRefExpr::VK_DTPOFF:
1727 Type = ELF::R_X86_64_DTPOFF32;
1728 break;
1729 }
1730 break;
1731 case FK_Data_4:
1732 Type = ELF::R_X86_64_32;
1733 break;
1734 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001735 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001736 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1737 }
1738 }
1739 } else {
1740 if (IsPCRel) {
Rafael Espindola1d5969d2011-12-09 03:03:58 +00001741 switch ((unsigned)Fixup.getKind()) {
1742 default: llvm_unreachable("invalid fixup kind!");
1743
1744 case X86::reloc_global_offset_table:
1745 Type = ELF::R_386_GOTPC;
Jason W Kimd3443e92010-11-15 16:18:39 +00001746 break;
Rafael Espindola1d5969d2011-12-09 03:03:58 +00001747
Rafael Espindola3c68acd2011-12-09 19:57:29 +00001748 case X86::reloc_signed_4byte:
Rafael Espindola1d5969d2011-12-09 03:03:58 +00001749 case FK_PCRel_4:
1750 case FK_Data_4:
1751 switch (Modifier) {
1752 default:
1753 llvm_unreachable("Unimplemented");
1754 case MCSymbolRefExpr::VK_None:
1755 Type = ELF::R_386_PC32;
1756 break;
1757 case MCSymbolRefExpr::VK_PLT:
1758 Type = ELF::R_386_PLT32;
1759 break;
1760 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001761 break;
1762 }
1763 } else {
1764 switch ((unsigned)Fixup.getKind()) {
1765 default: llvm_unreachable("invalid fixup kind!");
1766
1767 case X86::reloc_global_offset_table:
1768 Type = ELF::R_386_GOTPC;
1769 break;
1770
1771 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1772 // instead?
1773 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001774 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001775 case FK_Data_4:
1776 switch (Modifier) {
1777 default:
1778 llvm_unreachable("Unimplemented");
1779 case MCSymbolRefExpr::VK_None:
1780 Type = ELF::R_386_32;
1781 break;
1782 case MCSymbolRefExpr::VK_GOT:
1783 Type = ELF::R_386_GOT32;
1784 break;
1785 case MCSymbolRefExpr::VK_GOTOFF:
1786 Type = ELF::R_386_GOTOFF;
1787 break;
1788 case MCSymbolRefExpr::VK_TLSGD:
1789 Type = ELF::R_386_TLS_GD;
1790 break;
1791 case MCSymbolRefExpr::VK_TPOFF:
1792 Type = ELF::R_386_TLS_LE_32;
1793 break;
1794 case MCSymbolRefExpr::VK_INDNTPOFF:
1795 Type = ELF::R_386_TLS_IE;
1796 break;
1797 case MCSymbolRefExpr::VK_NTPOFF:
1798 Type = ELF::R_386_TLS_LE;
1799 break;
1800 case MCSymbolRefExpr::VK_GOTNTPOFF:
1801 Type = ELF::R_386_TLS_GOTIE;
1802 break;
1803 case MCSymbolRefExpr::VK_TLSLDM:
1804 Type = ELF::R_386_TLS_LDM;
1805 break;
1806 case MCSymbolRefExpr::VK_DTPOFF:
1807 Type = ELF::R_386_TLS_LDO_32;
1808 break;
Nick Lewyckye0b87032011-06-04 17:38:07 +00001809 case MCSymbolRefExpr::VK_GOTTPOFF:
1810 Type = ELF::R_386_TLS_IE_32;
1811 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001812 }
1813 break;
1814 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001815 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001816 case FK_Data_1: Type = ELF::R_386_8; break;
1817 }
1818 }
1819 }
1820
Jason W Kim56a39902010-12-06 21:57:34 +00001821 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001822}
Akira Hatanaka291512f2011-09-30 21:55:40 +00001823
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001824//===- MipsELFObjectWriter -------------------------------------------===//
1825
Akira Hatanaka291512f2011-09-30 21:55:40 +00001826MipsELFObjectWriter::MipsELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1827 raw_ostream &_OS,
1828 bool IsLittleEndian)
1829 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {}
1830
1831MipsELFObjectWriter::~MipsELFObjectWriter() {}
1832
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +00001833// FIXME: get the real EABI Version from the Triple.
1834void MipsELFObjectWriter::WriteEFlags() {
1835 Write32(ELF::EF_MIPS_NOREORDER |
1836 ELF::EF_MIPS_ARCH_32R2);
1837}
1838
Bruno Cardoso Lopesa00a62a2011-12-06 03:34:42 +00001839const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1840 const MCValue &Target,
1841 const MCFragment &F,
1842 const MCFixup &Fixup,
1843 bool IsPCRel) const {
1844 assert(Target.getSymA() && "SymA cannot be 0.");
1845 const MCSymbol &Sym = Target.getSymA()->getSymbol();
1846
Akira Hatanakaf3315cf2011-12-13 02:27:40 +00001847 if (Sym.getSection().getKind().isMergeableCString() ||
1848 Sym.getSection().getKind().isMergeableConst())
Bruno Cardoso Lopesa00a62a2011-12-06 03:34:42 +00001849 return &Sym;
1850
1851 return NULL;
1852}
1853
Akira Hatanaka291512f2011-09-30 21:55:40 +00001854unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
1855 const MCFixup &Fixup,
1856 bool IsPCRel,
1857 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001858 int64_t Addend) const {
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001859 // determine the type of the relocation
1860 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
1861 unsigned Kind = (unsigned)Fixup.getKind();
1862
1863 switch (Kind) {
1864 default:
1865 llvm_unreachable("invalid fixup kind!");
1866 case FK_Data_4:
1867 Type = ELF::R_MIPS_32;
1868 break;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +00001869 case FK_GPRel_4:
1870 Type = ELF::R_MIPS_GPREL32;
1871 break;
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001872 case Mips::fixup_Mips_GPREL16:
1873 Type = ELF::R_MIPS_GPREL16;
1874 break;
1875 case Mips::fixup_Mips_26:
1876 Type = ELF::R_MIPS_26;
1877 break;
1878 case Mips::fixup_Mips_CALL16:
1879 Type = ELF::R_MIPS_CALL16;
1880 break;
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001881 case Mips::fixup_Mips_GOT_Global:
1882 case Mips::fixup_Mips_GOT_Local:
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001883 Type = ELF::R_MIPS_GOT16;
1884 break;
1885 case Mips::fixup_Mips_HI16:
1886 Type = ELF::R_MIPS_HI16;
1887 break;
1888 case Mips::fixup_Mips_LO16:
1889 Type = ELF::R_MIPS_LO16;
1890 break;
1891 case Mips::fixup_Mips_TLSGD:
1892 Type = ELF::R_MIPS_TLS_GD;
1893 break;
1894 case Mips::fixup_Mips_GOTTPREL:
1895 Type = ELF::R_MIPS_TLS_GOTTPREL;
1896 break;
1897 case Mips::fixup_Mips_TPREL_HI:
1898 Type = ELF::R_MIPS_TLS_TPREL_HI16;
1899 break;
1900 case Mips::fixup_Mips_TPREL_LO:
1901 Type = ELF::R_MIPS_TLS_TPREL_LO16;
1902 break;
1903 case Mips::fixup_Mips_Branch_PCRel:
1904 case Mips::fixup_Mips_PC16:
1905 Type = ELF::R_MIPS_PC16;
1906 break;
1907 }
1908
1909 return Type;
Akira Hatanaka291512f2011-09-30 21:55:40 +00001910}