blob: 5e1b0d0dfede9c052829d7ced37d859dbc594e91 [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 Espindoladc9a8a32011-12-21 17:00:36 +000096 Write8(TargetObjectWriter->getOSABI());
Matt Fleming3565a062010-08-16 18:57:57 +000097 Write8(0); // e_ident[EI_ABIVERSION]
98
99 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
100
101 Write16(ELF::ET_REL); // e_type
102
Rafael Espindolabff66a82010-12-18 03:27:34 +0000103 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000104
105 Write32(ELF::EV_CURRENT); // e_version
106 WriteWord(0); // e_entry, no entry point in .o file
107 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000108 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000109 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000110
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000111 // e_flags = whatever the target wants
112 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000113
114 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000115 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000116
117 Write16(0); // e_phentsize = prog header entry size
118 Write16(0); // e_phnum = # prog header entries = 0
119
120 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000121 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000122
123 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000124 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000125 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000126 else
127 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000128
129 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000130 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000131 Write16(ELF::SHN_XINDEX);
132 else
133 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000134}
135
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000136void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
137 MCDataFragment *ShndxF,
138 uint64_t name,
139 uint8_t info, uint64_t value,
140 uint64_t size, uint8_t other,
141 uint32_t shndx,
142 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000143 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000144 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000145 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000146 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000147 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000148 }
149
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000150 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
151 uint16_t(ELF::SHN_XINDEX) : shndx;
152
Rafael Espindolabff66a82010-12-18 03:27:34 +0000153 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000154 String32(*SymtabF, name); // st_name
155 String8(*SymtabF, info); // st_info
156 String8(*SymtabF, other); // st_other
157 String16(*SymtabF, Index); // st_shndx
158 String64(*SymtabF, value); // st_value
159 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000160 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000161 String32(*SymtabF, name); // st_name
162 String32(*SymtabF, value); // st_value
163 String32(*SymtabF, size); // st_size
164 String8(*SymtabF, info); // st_info
165 String8(*SymtabF, other); // st_other
166 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000167 }
168}
169
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000170uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
171 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000172 if (Data.isCommon() && Data.isExternal())
173 return Data.getCommonAlignment();
174
175 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000176
177 if (Symbol.isAbsolute() && Symbol.isVariable()) {
178 if (const MCExpr *Value = Symbol.getVariableValue()) {
179 int64_t IntValue;
180 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000181 return (uint64_t)IntValue;
Roman Divackyd1491862010-12-20 21:14:39 +0000182 }
183 }
184
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000185 if (!Symbol.isInSection())
186 return 0;
187
Rafael Espindola64695402011-05-16 16:17:21 +0000188
189 if (Data.getFragment()) {
190 if (Data.getFlags() & ELF_Other_ThumbFunc)
191 return Layout.getSymbolOffset(&Data)+1;
192 else
193 return Layout.getSymbolOffset(&Data);
194 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000195
196 return 0;
197}
198
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000199void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
200 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000201 // The presence of symbol versions causes undefined symbols and
202 // versions declared with @@@ to be renamed.
203
204 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
205 ie = Asm.symbol_end(); it != ie; ++it) {
206 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000207 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000208 MCSymbolData &SD = Asm.getSymbolData(Symbol);
209
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000210 // Not an alias.
211 if (&Symbol == &Alias)
212 continue;
213
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000214 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000215 size_t Pos = AliasName.find('@');
216 if (Pos == StringRef::npos)
217 continue;
218
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000219 // Aliases defined with .symvar copy the binding from the symbol they alias.
220 // This is the first place we are able to copy this information.
221 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000222 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000223
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000224 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000225 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
226 continue;
227
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000228 // FIXME: produce a better error message.
229 if (Symbol.isUndefined() && Rest.startswith("@@") &&
230 !Rest.startswith("@@@"))
231 report_fatal_error("A @@ version cannot be undefined");
232
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000233 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000234 }
235}
236
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000237void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
238 MCDataFragment *ShndxF,
239 ELFSymbolData &MSD,
240 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000241 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000242 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000243 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000244
Rafael Espindola7be2c332010-10-31 00:16:26 +0000245 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
246 Data.getSymbol().isVariable();
247
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000248 uint8_t Binding = MCELF::GetBinding(OrigData);
249 uint8_t Visibility = MCELF::GetVisibility(OrigData);
250 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000251
252 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
253 uint8_t Other = Visibility;
254
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000255 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000256 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000257
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000258 assert(!(Data.isCommon() && !Data.isExternal()));
259
Rafael Espindolaf0121242010-12-22 16:03:00 +0000260 const MCExpr *ESize = Data.getSize();
261 if (ESize) {
262 int64_t Res;
263 if (!ESize->EvaluateAsAbsolute(Res, Layout))
264 report_fatal_error("Size expression must be absolute.");
265 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000266 }
267
268 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000269 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
270 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000271}
272
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000273void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
274 MCDataFragment *ShndxF,
275 const MCAssembler &Asm,
276 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000277 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000278 // The string table must be emitted first because we need the index
279 // into the string table for all the symbol names.
280 assert(StringTable.size() && "Missing string table");
281
282 // FIXME: Make sure the start of the symbol table is aligned.
283
284 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000285 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000286
287 // Write the symbol table entries.
288 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
289 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
290 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000291 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000292 }
293
Rafael Espindola71859c62010-09-16 19:46:31 +0000294 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000295 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
296 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000297 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000298 static_cast<const MCSectionELF&>(i->getSection());
299 if (Section.getType() == ELF::SHT_RELA ||
300 Section.getType() == ELF::SHT_REL ||
301 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000302 Section.getType() == ELF::SHT_SYMTAB ||
303 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000304 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000305 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000306 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
307 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000308 LastLocalSymbolIndex++;
309 }
Matt Fleming3565a062010-08-16 18:57:57 +0000310
311 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
312 ELFSymbolData &MSD = ExternalSymbolData[i];
313 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000314 assert(((Data.getFlags() & ELF_STB_Global) ||
315 (Data.getFlags() & ELF_STB_Weak)) &&
316 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000317 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000318 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000319 LastLocalSymbolIndex++;
320 }
321
322 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
323 ELFSymbolData &MSD = UndefinedSymbolData[i];
324 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000325 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000326 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000327 LastLocalSymbolIndex++;
328 }
329}
330
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000331const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
332 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000333 const MCFragment &F,
334 const MCFixup &Fixup,
335 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000336 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000337 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
338 const MCSymbol *Renamed = Renames.lookup(&Symbol);
339 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000340
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000341 if (ASymbol.isUndefined()) {
342 if (Renamed)
343 return Renamed;
344 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000345 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000346
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000347 if (SD.isExternal()) {
348 if (Renamed)
349 return Renamed;
350 return &Symbol;
351 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000352
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000353 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000354 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000355 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000356
Rafael Espindola25958732010-11-24 21:57:39 +0000357 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000358 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000359
Rafael Espindola25958732010-11-24 21:57:39 +0000360 if (secKind.isThreadLocal()) {
361 if (Renamed)
362 return Renamed;
363 return &Symbol;
364 }
365
Rafael Espindola8cecf252010-10-06 16:23:36 +0000366 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000367 const MCSectionELF &Sec2 =
368 static_cast<const MCSectionELF&>(F.getParent()->getSection());
369
Rafael Espindola8cecf252010-10-06 16:23:36 +0000370 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000371 (Kind == MCSymbolRefExpr::VK_PLT ||
372 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000373 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000374 if (Renamed)
375 return Renamed;
376 return &Symbol;
377 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000378
Rafael Espindola1c130262011-01-23 04:43:11 +0000379 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000380 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000381 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000382 if (Renamed)
383 return Renamed;
384 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000385 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000386
Jason W Kime964d112011-05-11 22:53:06 +0000387 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
388
Rafael Espindola73ffea42010-09-25 05:42:19 +0000389}
390
Matt Fleming3565a062010-08-16 18:57:57 +0000391
Jason W Kim56a39902010-12-06 21:57:34 +0000392void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
393 const MCAsmLayout &Layout,
394 const MCFragment *Fragment,
395 const MCFixup &Fixup,
396 MCValue Target,
397 uint64_t &FixedValue) {
398 int64_t Addend = 0;
399 int Index = 0;
400 int64_t Value = Target.getConstant();
401 const MCSymbol *RelocSymbol = NULL;
402
Rafael Espindola127a6a42010-12-17 07:28:17 +0000403 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000404 if (!Target.isAbsolute()) {
405 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
406 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000407 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000408
409 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
410 const MCSymbol &SymbolB = RefB->getSymbol();
411 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
412 IsPCRel = true;
413
414 // Offset of the symbol in the section
415 int64_t a = Layout.getSymbolOffset(&SDB);
416
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000417 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000418 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
419 Value += b - a;
420 }
421
422 if (!RelocSymbol) {
423 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
424 MCFragment *F = SD.getFragment();
425
426 Index = F->getParent()->getOrdinal() + 1;
427
428 // Offset of the symbol in the section
429 Value += Layout.getSymbolOffset(&SD);
430 } else {
431 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
432 WeakrefUsedInReloc.insert(RelocSymbol);
433 else
434 UsedInReloc.insert(RelocSymbol);
435 Index = -1;
436 }
437 Addend = Value;
438 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000439 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000440 Value = 0;
441 }
442
443 FixedValue = Value;
444 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
445 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000446 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
447 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
448 if (RelocNeedsGOT(Modifier))
449 NeedsGOT = true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000450
Jason W Kim56a39902010-12-06 21:57:34 +0000451 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
452 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000453
454 adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000455
Rafael Espindolabff66a82010-12-18 03:27:34 +0000456 if (!hasRelocationAddend())
457 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000458
459 if (is64Bit())
460 assert(isInt<64>(Addend));
461 else
462 assert(isInt<32>(Addend));
463
Jason W Kim56a39902010-12-06 21:57:34 +0000464 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
465 Relocations[Fragment->getParent()].push_back(ERE);
466}
467
468
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000469uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000470ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
471 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000472 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000473 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000474}
475
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000476bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
477 const MCSymbolData &Data,
478 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000479 if (Data.getFlags() & ELF_Other_Weakref)
480 return false;
481
Rafael Espindolabd701182010-10-19 19:31:37 +0000482 if (Used)
483 return true;
484
Rafael Espindola88182132010-10-27 15:18:17 +0000485 if (Renamed)
486 return false;
487
Rafael Espindola737cd212010-10-05 18:01:23 +0000488 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000489
Rafael Espindolad1798862010-10-29 23:09:31 +0000490 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
491 return true;
492
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000493 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000494 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
495 return false;
496
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000497 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000498 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000499 return false;
500
Rafael Espindola737cd212010-10-05 18:01:23 +0000501 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
502 return false;
503
Rafael Espindolabd701182010-10-19 19:31:37 +0000504 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000505 return false;
506
507 return true;
508}
509
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000510bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
511 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000512 if (Data.isExternal())
513 return false;
514
515 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000516 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000517
518 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
519 if (isSignature && !isUsedInReloc)
520 return true;
521
Rafael Espindola737cd212010-10-05 18:01:23 +0000522 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000523 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000524
525 return true;
526}
527
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000528void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000529 SectionIndexMapTy &SectionIndexMap,
530 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000531 unsigned Index = 1;
532 for (MCAssembler::iterator it = Asm.begin(),
533 ie = Asm.end(); it != ie; ++it) {
534 const MCSectionELF &Section =
535 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000536 if (Section.getType() != ELF::SHT_GROUP)
537 continue;
538 SectionIndexMap[&Section] = Index++;
539 }
540
541 for (MCAssembler::iterator it = Asm.begin(),
542 ie = Asm.end(); it != ie; ++it) {
543 const MCSectionELF &Section =
544 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000545 if (Section.getType() == ELF::SHT_GROUP ||
546 Section.getType() == ELF::SHT_REL ||
547 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000548 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000549 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000550 const MCSectionELF *RelSection = RelMap.lookup(&Section);
551 if (RelSection)
552 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000553 }
554}
555
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000556void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000557 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000558 RevGroupMapTy RevGroupMap,
559 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000560 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000561 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000562 if (NeedsGOT) {
563 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
564 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
565 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
566 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000567 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000568 }
569
Matt Fleming3565a062010-08-16 18:57:57 +0000570 // Index 0 is always the empty string.
571 StringMap<uint64_t> StringIndexMap;
572 StringTable += '\x00';
573
Rafael Espindolad5321da2011-04-07 23:21:52 +0000574 // FIXME: We could optimize suffixes in strtab in the same way we
575 // optimize them in shstrtab.
576
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000577 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000578 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
579 ie = Asm.symbol_end(); it != ie; ++it) {
580 const MCSymbol &Symbol = it->getSymbol();
581
Rafael Espindola484291c2010-11-01 14:28:48 +0000582 bool Used = UsedInReloc.count(&Symbol);
583 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000584 bool isSignature = RevGroupMap.count(&Symbol);
585
586 if (!isInSymtab(Asm, *it,
587 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000588 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000589 continue;
590
Matt Fleming3565a062010-08-16 18:57:57 +0000591 ELFSymbolData MSD;
592 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000593 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000594
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000595 // Undefined symbols are global, but this is the first place we
596 // are able to set it.
597 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000598 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000599 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000600 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
601 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000602 }
603
Rafael Espindola484291c2010-11-01 14:28:48 +0000604 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000605 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000606
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000607 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000608 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000609 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000610 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000611 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000612 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000613 if (isSignature && !Used)
614 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
615 else
616 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000617 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000618 const MCSectionELF &Section =
619 static_cast<const MCSectionELF&>(RefSymbol.getSection());
620 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000621 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
622 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000623 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000624 }
625
Rafael Espindola88182132010-10-27 15:18:17 +0000626 // The @@@ in symbol version is replaced with @ in undefined symbols and
627 // @@ in defined ones.
628 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000629 SmallString<32> Buf;
630
Rafael Espindola88182132010-10-27 15:18:17 +0000631 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000632 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000633 Buf += Name.substr(0, Pos);
634 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
635 Buf += Name.substr(Pos + Skip);
636 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000637 }
638
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000639 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000640 if (!Entry) {
641 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000642 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000643 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000644 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000645 MSD.StringIndex = Entry;
646 if (MSD.SectionIndex == ELF::SHN_UNDEF)
647 UndefinedSymbolData.push_back(MSD);
648 else if (Local)
649 LocalSymbolData.push_back(MSD);
650 else
651 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000652 }
653
654 // Symbols are required to be in lexicographic order.
655 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
656 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
657 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
658
659 // Set the symbol indices. Local symbols must come before all other
660 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000661 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000662 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
663 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000664
665 Index += NumRegularSections;
666
Matt Fleming3565a062010-08-16 18:57:57 +0000667 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
668 ExternalSymbolData[i].SymbolData->setIndex(Index++);
669 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
670 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000671
672 if (NumRegularSections > ELF::SHN_LORESERVE)
673 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000674}
675
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000676void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
677 MCAsmLayout &Layout,
678 RelMapTy &RelMap) {
679 for (MCAssembler::const_iterator it = Asm.begin(),
680 ie = Asm.end(); it != ie; ++it) {
681 const MCSectionData &SD = *it;
682 if (Relocations[&SD].empty())
683 continue;
684
Matt Fleming3565a062010-08-16 18:57:57 +0000685 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000686 const MCSectionELF &Section =
687 static_cast<const MCSectionELF&>(SD.getSection());
688
689 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000690 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000691 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000692
693 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000694 if (hasRelocationAddend())
695 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000696 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000697 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000698
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000699 const MCSectionELF *RelaSection =
700 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
701 ELF::SHT_RELA : ELF::SHT_REL, 0,
702 SectionKind::getReadOnly(),
703 EntrySize, "");
704 RelMap[&Section] = RelaSection;
705 Asm.getOrCreateSectionData(*RelaSection);
706 }
707}
Matt Fleming3565a062010-08-16 18:57:57 +0000708
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000709void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
710 const RelMapTy &RelMap) {
711 for (MCAssembler::const_iterator it = Asm.begin(),
712 ie = Asm.end(); it != ie; ++it) {
713 const MCSectionData &SD = *it;
714 const MCSectionELF &Section =
715 static_cast<const MCSectionELF&>(SD.getSection());
716
717 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
718 if (!RelaSection)
719 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000720 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000721 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000722
723 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000724 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000725 }
726}
727
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000728void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
729 uint64_t Flags, uint64_t Address,
730 uint64_t Offset, uint64_t Size,
731 uint32_t Link, uint32_t Info,
732 uint64_t Alignment,
733 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000734 Write32(Name); // sh_name: index into string table
735 Write32(Type); // sh_type
736 WriteWord(Flags); // sh_flags
737 WriteWord(Address); // sh_addr
738 WriteWord(Offset); // sh_offset
739 WriteWord(Size); // sh_size
740 Write32(Link); // sh_link
741 Write32(Info); // sh_info
742 WriteWord(Alignment); // sh_addralign
743 WriteWord(EntrySize); // sh_entsize
744}
745
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000746void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
747 MCDataFragment *F,
748 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000749 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
750 // sort by the r_offset just like gnu as does
751 array_pod_sort(Relocs.begin(), Relocs.end());
752
753 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
754 ELFRelocationEntry entry = Relocs[e - i - 1];
755
Rafael Espindola12203cc2010-11-21 00:48:25 +0000756 if (!entry.Index)
757 ;
758 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000759 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
760 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000761 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000762 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000763 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000764
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000765 struct ELF::Elf64_Rela ERE64;
766 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000767 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000768
Rafael Espindolabff66a82010-12-18 03:27:34 +0000769 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000770 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000771 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000772 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000773
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000774 struct ELF::Elf32_Rela ERE32;
775 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000776 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000777
Rafael Espindolabff66a82010-12-18 03:27:34 +0000778 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000779 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000780 }
Matt Fleming3565a062010-08-16 18:57:57 +0000781 }
782}
783
Rafael Espindolad5321da2011-04-07 23:21:52 +0000784static int compareBySuffix(const void *a, const void *b) {
785 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
786 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
787 const StringRef &NameA = secA->getSectionName();
788 const StringRef &NameB = secB->getSectionName();
789 const unsigned sizeA = NameA.size();
790 const unsigned sizeB = NameB.size();
791 const unsigned len = std::min(sizeA, sizeB);
792 for (unsigned int i = 0; i < len; ++i) {
793 char ca = NameA[sizeA - i - 1];
794 char cb = NameB[sizeB - i - 1];
795 if (ca != cb)
796 return cb - ca;
797 }
798
799 return sizeB - sizeA;
800}
801
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000802void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
803 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000804 SectionIndexMapTy &SectionIndexMap,
805 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000806 MCContext &Ctx = Asm.getContext();
807 MCDataFragment *F;
808
Rafael Espindolabff66a82010-12-18 03:27:34 +0000809 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000810
Rafael Espindola38738bf2010-09-22 19:04:41 +0000811 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000812 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000813 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000814 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000815 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
816 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000817
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000818 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000819 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
820 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000821 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000822 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000823 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000824
825 MCSectionData *SymtabShndxSD = NULL;
826
827 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000828 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000829 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000830 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000831 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
832 SymtabShndxSD->setAlignment(4);
833 }
Matt Fleming3565a062010-08-16 18:57:57 +0000834
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000835 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000836 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000837 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000838 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
839 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000840
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000841 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
842
843 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
844 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
845 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000846
847 // Symbol table
848 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000849 MCDataFragment *ShndxF = NULL;
850 if (NeedsSymtabShndx) {
851 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000852 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000853 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000854
Matt Fleming3565a062010-08-16 18:57:57 +0000855 F = new MCDataFragment(&StrtabSD);
856 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000857
Matt Fleming3565a062010-08-16 18:57:57 +0000858 F = new MCDataFragment(&ShstrtabSD);
859
Rafael Espindolad5321da2011-04-07 23:21:52 +0000860 std::vector<const MCSectionELF*> Sections;
861 for (MCAssembler::const_iterator it = Asm.begin(),
862 ie = Asm.end(); it != ie; ++it) {
863 const MCSectionELF &Section =
864 static_cast<const MCSectionELF&>(it->getSection());
865 Sections.push_back(&Section);
866 }
867 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
868
Matt Fleming3565a062010-08-16 18:57:57 +0000869 // Section header string table.
870 //
871 // The first entry of a string table holds a null character so skip
872 // section 0.
873 uint64_t Index = 1;
874 F->getContents() += '\x00';
875
Rafael Espindolad5321da2011-04-07 23:21:52 +0000876 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
877 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000878
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000879 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000880 if (I != 0) {
881 StringRef PreviousName = Sections[I - 1]->getSectionName();
882 if (PreviousName.endswith(Name)) {
883 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
884 continue;
885 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000886 }
Matt Fleming3565a062010-08-16 18:57:57 +0000887 // Remember the index into the string table so we can write it
888 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000889 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000890
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000891 Index += Name.size() + 1;
892 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000893 F->getContents() += '\x00';
894 }
Rafael Espindola70703872010-09-30 02:22:20 +0000895}
896
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000897void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
898 MCAsmLayout &Layout,
899 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000900 RevGroupMapTy &RevGroupMap,
901 SectionIndexMapTy &SectionIndexMap,
902 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000903 // Create the .note.GNU-stack section if needed.
904 MCContext &Ctx = Asm.getContext();
905 if (Asm.getNoExecStack()) {
906 const MCSectionELF *GnuStackSection =
907 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
908 SectionKind::getReadOnly());
909 Asm.getOrCreateSectionData(*GnuStackSection);
910 }
911
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000912 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000913 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
914 it != ie; ++it) {
915 const MCSectionELF &Section =
916 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000917 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000918 continue;
919
920 const MCSymbol *SignatureSymbol = Section.getGroup();
921 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000922 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000923 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000924 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000925 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
926 Data.setAlignment(4);
927 MCDataFragment *F = new MCDataFragment(&Data);
928 String32(*F, ELF::GRP_COMDAT);
929 }
930 GroupMap[Group] = SignatureSymbol;
931 }
932
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000933 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
934
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000935 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000936 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000937 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000938 const MCSectionELF &Section =
939 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000940 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000941 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000942 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000943 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
944 // FIXME: we could use the previous fragment
945 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000946 unsigned Index = SectionIndexMap.lookup(&Section);
947 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000948 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000949}
950
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000951void ELFObjectWriter::WriteSection(MCAssembler &Asm,
952 const SectionIndexMapTy &SectionIndexMap,
953 uint32_t GroupSymbolIndex,
954 uint64_t Offset, uint64_t Size,
955 uint64_t Alignment,
956 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000957 uint64_t sh_link = 0;
958 uint64_t sh_info = 0;
959
960 switch(Section.getType()) {
961 case ELF::SHT_DYNAMIC:
962 sh_link = SectionStringTableIndex[&Section];
963 sh_info = 0;
964 break;
965
966 case ELF::SHT_REL:
967 case ELF::SHT_RELA: {
968 const MCSectionELF *SymtabSection;
969 const MCSectionELF *InfoSection;
970 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
971 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000972 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000973 sh_link = SectionIndexMap.lookup(SymtabSection);
974 assert(sh_link && ".symtab not found");
975
976 // Remove ".rel" and ".rela" prefixes.
977 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
978 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
979
980 InfoSection = Asm.getContext().getELFSection(SectionName,
981 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000982 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000983 sh_info = SectionIndexMap.lookup(InfoSection);
984 break;
985 }
986
987 case ELF::SHT_SYMTAB:
988 case ELF::SHT_DYNSYM:
989 sh_link = StringTableIndex;
990 sh_info = LastLocalSymbolIndex;
991 break;
992
993 case ELF::SHT_SYMTAB_SHNDX:
994 sh_link = SymbolTableIndex;
995 break;
996
997 case ELF::SHT_PROGBITS:
998 case ELF::SHT_STRTAB:
999 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001000 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001001 case ELF::SHT_NULL:
1002 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001003 case ELF::SHT_INIT_ARRAY:
1004 case ELF::SHT_FINI_ARRAY:
1005 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001006 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001007 // Nothing to do.
1008 break;
1009
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001010 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001011 sh_link = SymbolTableIndex;
1012 sh_info = GroupSymbolIndex;
1013 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001014
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001015 default:
1016 assert(0 && "FIXME: sh_type value not supported!");
1017 break;
1018 }
1019
1020 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1021 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1022 Alignment, Section.getEntrySize());
1023}
1024
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001025bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001026 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001027 !SD.getSection().isVirtualSection();
1028}
1029
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001030uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001031 uint64_t Ret = 0;
1032 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1033 ++i) {
1034 const MCFragment &F = *i;
1035 assert(F.getKind() == MCFragment::FT_Data);
1036 Ret += cast<MCDataFragment>(F).getContents().size();
1037 }
1038 return Ret;
1039}
1040
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001041uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1042 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001043 if (IsELFMetaDataSection(SD))
1044 return DataSectionSize(SD);
1045 return Layout.getSectionFileSize(&SD);
1046}
1047
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001048uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1049 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001050 if (IsELFMetaDataSection(SD))
1051 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001052 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001053}
1054
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001055void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1056 const MCAsmLayout &Layout,
1057 const MCSectionELF &Section) {
1058 uint64_t FileOff = OS.tell();
1059 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1060
1061 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1062 WriteZeros(Padding);
1063 FileOff += Padding;
1064
1065 FileOff += GetSectionFileSize(Layout, SD);
1066
1067 if (IsELFMetaDataSection(SD)) {
1068 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1069 ++i) {
1070 const MCFragment &F = *i;
1071 assert(F.getKind() == MCFragment::FT_Data);
1072 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1073 }
1074 } else {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001075 Asm.writeSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001076 }
1077}
1078
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001079void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1080 const GroupMapTy &GroupMap,
1081 const MCAsmLayout &Layout,
1082 const SectionIndexMapTy &SectionIndexMap,
1083 const SectionOffsetMapTy &SectionOffsetMap) {
1084 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001085
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001086 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001087 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001088
1089 for (SectionIndexMapTy::const_iterator i=
1090 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1091 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001092 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001093 }
1094
Matt Fleming3565a062010-08-16 18:57:57 +00001095 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001096 uint64_t FirstSectionSize =
1097 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1098 uint32_t FirstSectionLink =
1099 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1100 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001101
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001102 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001103 const MCSectionELF &Section = *Sections[i];
1104 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1105 uint32_t GroupSymbolIndex;
1106 if (Section.getType() != ELF::SHT_GROUP)
1107 GroupSymbolIndex = 0;
1108 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001109 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1110 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001111
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001112 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001113
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001114 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001115 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001116 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001117 }
1118}
1119
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001120void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1121 std::vector<const MCSectionELF*> &Sections) {
1122 for (MCAssembler::iterator it = Asm.begin(),
1123 ie = Asm.end(); it != ie; ++it) {
1124 const MCSectionELF &Section =
1125 static_cast<const MCSectionELF &>(it->getSection());
1126 if (Section.getType() == ELF::SHT_GROUP)
1127 Sections.push_back(&Section);
1128 }
1129
1130 for (MCAssembler::iterator it = Asm.begin(),
1131 ie = Asm.end(); it != ie; ++it) {
1132 const MCSectionELF &Section =
1133 static_cast<const MCSectionELF &>(it->getSection());
1134 if (Section.getType() != ELF::SHT_GROUP &&
1135 Section.getType() != ELF::SHT_REL &&
1136 Section.getType() != ELF::SHT_RELA)
1137 Sections.push_back(&Section);
1138 }
1139
1140 for (MCAssembler::iterator it = Asm.begin(),
1141 ie = Asm.end(); it != ie; ++it) {
1142 const MCSectionELF &Section =
1143 static_cast<const MCSectionELF &>(it->getSection());
1144 if (Section.getType() == ELF::SHT_REL ||
1145 Section.getType() == ELF::SHT_RELA)
1146 Sections.push_back(&Section);
1147 }
1148}
1149
1150void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1151 const MCAsmLayout &Layout) {
1152 GroupMapTy GroupMap;
1153 RevGroupMapTy RevGroupMap;
1154 SectionIndexMapTy SectionIndexMap;
1155
1156 unsigned NumUserSections = Asm.size();
1157
1158 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1159 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1160
1161 const unsigned NumUserAndRelocSections = Asm.size();
1162 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1163 RevGroupMap, SectionIndexMap, RelMap);
1164 const unsigned AllSections = Asm.size();
1165 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1166
1167 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1168
1169 // Compute symbol table information.
1170 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1171
1172
1173 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1174
1175 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1176 const_cast<MCAsmLayout&>(Layout),
1177 SectionIndexMap,
1178 RelMap);
1179
1180 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1181 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1182 sizeof(ELF::Elf32_Ehdr);
1183 uint64_t FileOff = HeaderSize;
1184
1185 std::vector<const MCSectionELF*> Sections;
1186 ComputeSectionOrder(Asm, Sections);
1187 unsigned NumSections = Sections.size();
1188 SectionOffsetMapTy SectionOffsetMap;
1189 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1190 const MCSectionELF &Section = *Sections[i];
1191 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1192
1193 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1194
1195 // Remember the offset into the file for this section.
1196 SectionOffsetMap[&Section] = FileOff;
1197
1198 // Get the size of the section in the output file (including padding).
1199 FileOff += GetSectionFileSize(Layout, SD);
1200 }
1201
1202 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1203
1204 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1205
1206 uint64_t SectionHeaderEntrySize = is64Bit() ?
1207 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1208 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1209
1210 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1211 const MCSectionELF &Section = *Sections[i];
1212 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1213
1214 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1215
1216 // Remember the offset into the file for this section.
1217 SectionOffsetMap[&Section] = FileOff;
1218
1219 // Get the size of the section in the output file (including padding).
1220 FileOff += GetSectionFileSize(Layout, SD);
1221 }
1222
1223 // Write out the ELF header ...
1224 WriteHeader(SectionHeaderOffset, NumSections + 1);
1225
1226 // ... then the regular sections ...
1227 // + because of .shstrtab
1228 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1229 WriteDataSectionData(Asm, Layout, *Sections[i]);
1230
1231 FileOff = OS.tell();
1232 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1233 WriteZeros(Padding);
1234
1235 // ... then the section header table ...
1236 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1237 SectionOffsetMap);
1238
1239 FileOff = OS.tell();
1240
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001241 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001242 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1243 WriteDataSectionData(Asm, Layout, *Sections[i]);
1244}
1245
Eli Friedman78c1e172011-03-03 07:24:36 +00001246bool
1247ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1248 const MCSymbolData &DataA,
1249 const MCFragment &FB,
1250 bool InSet,
1251 bool IsPCRel) const {
1252 if (DataA.getFlags() & ELF_STB_Weak)
1253 return false;
1254 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1255 Asm, DataA, FB,InSet, IsPCRel);
1256}
1257
Rafael Espindola6024c972010-12-17 17:45:22 +00001258MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1259 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001260 bool IsLittleEndian) {
1261 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001262 case ELF::EM_386:
1263 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001264 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001265 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001266 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001267 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001268 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001269 case ELF::EM_PPC:
1270 case ELF::EM_PPC64:
1271 return new PPCELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Akira Hatanaka291512f2011-09-30 21:55:40 +00001272 case ELF::EM_MIPS:
1273 return new MipsELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001274 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001275 }
1276}
1277
Jason W Kimd3443e92010-11-15 16:18:39 +00001278/// START OF SUBCLASSES for ELFObjectWriter
1279//===- ARMELFObjectWriter -------------------------------------------===//
1280
Rafael Espindola31f35782010-12-17 18:01:31 +00001281ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001282 raw_ostream &_OS,
1283 bool IsLittleEndian)
1284 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001285{}
1286
1287ARMELFObjectWriter::~ARMELFObjectWriter()
1288{}
1289
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001290// FIXME: get the real EABI Version from the Triple.
1291void ARMELFObjectWriter::WriteEFlags() {
1292 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1293}
1294
Jason W Kim953a2a32011-02-07 01:11:15 +00001295// In ARM, _MergedGlobals and other most symbols get emitted directly.
1296// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001297// This code is an approximation of what ARM/gcc does.
1298
1299STATISTIC(PCRelCount, "Total number of PIC Relocations");
1300STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001301
1302const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1303 const MCValue &Target,
1304 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001305 const MCFixup &Fixup,
1306 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001307 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1308 bool EmitThisSym = false;
1309
Jason W Kime964d112011-05-11 22:53:06 +00001310 const MCSectionELF &Section =
1311 static_cast<const MCSectionELF&>(Symbol.getSection());
Jason W Kime964d112011-05-11 22:53:06 +00001312 bool InNormalSection = true;
1313 unsigned RelocType = 0;
1314 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1315
Matt Beaumont-Gay6dcd4132011-05-11 23:34:51 +00001316 DEBUG(
1317 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1318 MCSymbolRefExpr::VariantKind Kind2;
1319 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1320 MCSymbolRefExpr::VK_None;
1321 dbgs() << "considering symbol "
Jason W Kime964d112011-05-11 22:53:06 +00001322 << Section.getSectionName() << "/"
1323 << Symbol.getName() << "/"
1324 << " Rel:" << (unsigned)RelocType
1325 << " Kind: " << (int)Kind << "/" << (int)Kind2
1326 << " Tmp:"
1327 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1328 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1329 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1330
Jason W Kimbebd44a2011-06-09 19:13:45 +00001331 if (IsPCRel) { ++PCRelCount;
Jason W Kime964d112011-05-11 22:53:06 +00001332 switch (RelocType) {
1333 default:
1334 // Most relocation types are emitted as explicit symbols
1335 InNormalSection =
1336 StringSwitch<bool>(Section.getSectionName())
1337 .Case(".data.rel.ro.local", false)
1338 .Case(".data.rel", false)
1339 .Case(".bss", false)
1340 .Default(true);
1341 EmitThisSym = true;
1342 break;
1343 case ELF::R_ARM_ABS32:
1344 // But things get strange with R_ARM_ABS32
1345 // In this case, most things that go in .rodata show up
1346 // as section relative relocations
1347 InNormalSection =
1348 StringSwitch<bool>(Section.getSectionName())
1349 .Case(".data.rel.ro.local", false)
1350 .Case(".data.rel", false)
1351 .Case(".rodata", false)
1352 .Case(".bss", false)
1353 .Default(true);
1354 EmitThisSym = false;
1355 break;
1356 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001357 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001358 NonPCRelCount++;
1359 InNormalSection =
1360 StringSwitch<bool>(Section.getSectionName())
1361 .Case(".data.rel.ro.local", false)
1362 .Case(".rodata", false)
1363 .Case(".data.rel", false)
1364 .Case(".bss", false)
1365 .Default(true);
1366
1367 switch (RelocType) {
1368 default: EmitThisSym = true; break;
1369 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1370 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001371 }
Jason W Kime964d112011-05-11 22:53:06 +00001372
Jason W Kim953a2a32011-02-07 01:11:15 +00001373 if (EmitThisSym)
1374 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001375 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001376 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001377 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001378 return NULL;
1379}
1380
Jason W Kime964d112011-05-11 22:53:06 +00001381// Need to examine the Fixup when determining whether to
1382// emit the relocation as an explicit symbol or as a section relative
1383// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001384unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1385 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001386 bool IsPCRel,
1387 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001388 int64_t Addend) const {
1389 return GetRelocTypeInner(Target, Fixup, IsPCRel);
Jason W Kime964d112011-05-11 22:53:06 +00001390}
1391
1392unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1393 const MCFixup &Fixup,
1394 bool IsPCRel) const {
1395 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1396 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1397
Jason W Kima0871e72010-12-08 23:14:44 +00001398 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001399 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001400 switch ((unsigned)Fixup.getKind()) {
1401 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001402 case FK_Data_4:
1403 switch (Modifier) {
1404 default: llvm_unreachable("Unsupported Modifier");
1405 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001406 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001407 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001408 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001409 assert(0 && "unimplemented");
1410 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001411 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1412 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001413 break;
1414 }
1415 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001416 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001417 switch (Modifier) {
1418 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001419 Type = ELF::R_ARM_PLT32;
1420 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001421 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001422 Type = ELF::R_ARM_CALL;
1423 break;
1424 }
1425 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001426 case ARM::fixup_arm_condbranch:
1427 Type = ELF::R_ARM_JUMP24;
1428 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001429 case ARM::fixup_arm_movt_hi16:
1430 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001431 Type = ELF::R_ARM_MOVT_PREL;
1432 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001433 case ARM::fixup_arm_movw_lo16:
1434 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001435 Type = ELF::R_ARM_MOVW_PREL_NC;
1436 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001437 case ARM::fixup_t2_movt_hi16:
1438 case ARM::fixup_t2_movt_hi16_pcrel:
1439 Type = ELF::R_ARM_THM_MOVT_PREL;
1440 break;
1441 case ARM::fixup_t2_movw_lo16:
1442 case ARM::fixup_t2_movw_lo16_pcrel:
1443 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1444 break;
Rafael Espindola298c8e12011-05-20 20:01:01 +00001445 case ARM::fixup_arm_thumb_bl:
1446 case ARM::fixup_arm_thumb_blx:
1447 switch (Modifier) {
1448 case MCSymbolRefExpr::VK_ARM_PLT:
1449 Type = ELF::R_ARM_THM_CALL;
1450 break;
1451 default:
1452 Type = ELF::R_ARM_NONE;
1453 break;
1454 }
1455 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001456 }
1457 } else {
1458 switch ((unsigned)Fixup.getKind()) {
1459 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001460 case FK_Data_4:
1461 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001462 default: llvm_unreachable("Unsupported Modifier"); break;
1463 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001464 Type = ELF::R_ARM_GOT_BREL;
1465 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001466 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001467 Type = ELF::R_ARM_TLS_GD32;
1468 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001469 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001470 Type = ELF::R_ARM_TLS_LE32;
1471 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001472 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001473 Type = ELF::R_ARM_TLS_IE32;
1474 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001475 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001476 Type = ELF::R_ARM_ABS32;
1477 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001478 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001479 Type = ELF::R_ARM_GOTOFF32;
1480 break;
1481 }
1482 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001483 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001484 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001485 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001486 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001487 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001488 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001489 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001490 assert(0 && "Unimplemented");
1491 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001492 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001493 Type = ELF::R_ARM_CALL;
1494 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001495 case ARM::fixup_arm_condbranch:
1496 Type = ELF::R_ARM_JUMP24;
1497 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001498 case ARM::fixup_arm_movt_hi16:
1499 Type = ELF::R_ARM_MOVT_ABS;
1500 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001501 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001502 Type = ELF::R_ARM_MOVW_ABS_NC;
1503 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001504 case ARM::fixup_t2_movt_hi16:
1505 Type = ELF::R_ARM_THM_MOVT_ABS;
1506 break;
1507 case ARM::fixup_t2_movw_lo16:
1508 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1509 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001510 }
1511 }
1512
Jason W Kima0871e72010-12-08 23:14:44 +00001513 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001514}
1515
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001516//===- PPCELFObjectWriter -------------------------------------------===//
1517
1518PPCELFObjectWriter::PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1519 raw_ostream &_OS,
1520 bool IsLittleEndian)
1521 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1522}
1523
1524PPCELFObjectWriter::~PPCELFObjectWriter() {
1525}
1526
1527unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
1528 const MCFixup &Fixup,
1529 bool IsPCRel,
1530 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001531 int64_t Addend) const {
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001532 // determine the type of the relocation
1533 unsigned Type;
1534 if (IsPCRel) {
1535 switch ((unsigned)Fixup.getKind()) {
1536 default:
1537 llvm_unreachable("Unimplemented");
1538 case PPC::fixup_ppc_br24:
1539 Type = ELF::R_PPC_REL24;
1540 break;
1541 case FK_PCRel_4:
1542 Type = ELF::R_PPC_REL32;
1543 break;
1544 }
1545 } else {
1546 switch ((unsigned)Fixup.getKind()) {
1547 default: llvm_unreachable("invalid fixup kind!");
1548 case PPC::fixup_ppc_br24:
1549 Type = ELF::R_PPC_ADDR24;
1550 break;
1551 case PPC::fixup_ppc_brcond14:
1552 Type = ELF::R_PPC_ADDR14_BRTAKEN; // XXX: or BRNTAKEN?_
1553 break;
1554 case PPC::fixup_ppc_ha16:
1555 Type = ELF::R_PPC_ADDR16_HA;
1556 break;
1557 case PPC::fixup_ppc_lo16:
1558 Type = ELF::R_PPC_ADDR16_LO;
1559 break;
1560 case PPC::fixup_ppc_lo14:
1561 Type = ELF::R_PPC_ADDR14;
1562 break;
1563 case FK_Data_4:
1564 Type = ELF::R_PPC_ADDR32;
1565 break;
1566 case FK_Data_2:
1567 Type = ELF::R_PPC_ADDR16;
1568 break;
1569 }
1570 }
1571 return Type;
1572}
1573
Jim Grosbach946227d2011-11-15 16:46:22 +00001574void PPCELFObjectWriter::
1575adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
Roman Divacky2a66cea2011-08-04 19:08:19 +00001576 switch ((unsigned)Fixup.getKind()) {
1577 case PPC::fixup_ppc_ha16:
1578 case PPC::fixup_ppc_lo16:
1579 RelocOffset += 2;
1580 break;
1581 default:
1582 break;
1583 }
1584}
1585
Wesley Peck4b047132010-11-21 22:06:28 +00001586//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001587
Rafael Espindola31f35782010-12-17 18:01:31 +00001588MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001589 raw_ostream &_OS,
1590 bool IsLittleEndian)
1591 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001592}
1593
1594MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1595}
1596
Jason W Kim56a39902010-12-06 21:57:34 +00001597unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001598 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001599 bool IsPCRel,
1600 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001601 int64_t Addend) const {
Wesley Peck4b047132010-11-21 22:06:28 +00001602 // determine the type of the relocation
1603 unsigned Type;
1604 if (IsPCRel) {
1605 switch ((unsigned)Fixup.getKind()) {
1606 default:
1607 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001608 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001609 Type = ELF::R_MICROBLAZE_64_PCREL;
1610 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001611 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001612 Type = ELF::R_MICROBLAZE_32_PCREL;
1613 break;
1614 }
1615 } else {
1616 switch ((unsigned)Fixup.getKind()) {
1617 default: llvm_unreachable("invalid fixup kind!");
1618 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001619 Type = ((IsRelocWithSymbol || Addend !=0)
1620 ? ELF::R_MICROBLAZE_32
1621 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001622 break;
1623 case FK_Data_2:
1624 Type = ELF::R_MICROBLAZE_32;
1625 break;
1626 }
1627 }
Jason W Kim56a39902010-12-06 21:57:34 +00001628 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001629}
Jason W Kimd3443e92010-11-15 16:18:39 +00001630
1631//===- X86ELFObjectWriter -------------------------------------------===//
1632
1633
Rafael Espindola31f35782010-12-17 18:01:31 +00001634X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001635 raw_ostream &_OS,
1636 bool IsLittleEndian)
1637 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001638{}
1639
1640X86ELFObjectWriter::~X86ELFObjectWriter()
1641{}
1642
Jason W Kim56a39902010-12-06 21:57:34 +00001643unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1644 const MCFixup &Fixup,
1645 bool IsPCRel,
1646 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001647 int64_t Addend) const {
Jason W Kimd3443e92010-11-15 16:18:39 +00001648 // determine the type of the relocation
1649
Rafael Espindola12203cc2010-11-21 00:48:25 +00001650 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1651 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001652 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001653 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001654 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001655 switch ((unsigned)Fixup.getKind()) {
1656 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001657
1658 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1659 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1660 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1661
Rafael Espindola3a83c402010-12-27 00:36:05 +00001662 case FK_PCRel_8:
1663 assert(Modifier == MCSymbolRefExpr::VK_None);
1664 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001665 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001666 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001667 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001668 case X86::reloc_riprel_4byte:
1669 case FK_PCRel_4:
1670 switch (Modifier) {
1671 default:
1672 llvm_unreachable("Unimplemented");
1673 case MCSymbolRefExpr::VK_None:
1674 Type = ELF::R_X86_64_PC32;
1675 break;
1676 case MCSymbolRefExpr::VK_PLT:
1677 Type = ELF::R_X86_64_PLT32;
1678 break;
1679 case MCSymbolRefExpr::VK_GOTPCREL:
1680 Type = ELF::R_X86_64_GOTPCREL;
1681 break;
1682 case MCSymbolRefExpr::VK_GOTTPOFF:
1683 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001684 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001685 case MCSymbolRefExpr::VK_TLSGD:
1686 Type = ELF::R_X86_64_TLSGD;
1687 break;
1688 case MCSymbolRefExpr::VK_TLSLD:
1689 Type = ELF::R_X86_64_TLSLD;
1690 break;
1691 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001692 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001693 case FK_PCRel_2:
1694 assert(Modifier == MCSymbolRefExpr::VK_None);
1695 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001696 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001697 case FK_PCRel_1:
1698 assert(Modifier == MCSymbolRefExpr::VK_None);
1699 Type = ELF::R_X86_64_PC8;
1700 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001701 }
1702 } else {
1703 switch ((unsigned)Fixup.getKind()) {
1704 default: llvm_unreachable("invalid fixup kind!");
1705 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1706 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001707 switch (Modifier) {
1708 default:
1709 llvm_unreachable("Unimplemented");
1710 case MCSymbolRefExpr::VK_None:
1711 Type = ELF::R_X86_64_32S;
1712 break;
1713 case MCSymbolRefExpr::VK_GOT:
1714 Type = ELF::R_X86_64_GOT32;
1715 break;
1716 case MCSymbolRefExpr::VK_GOTPCREL:
1717 Type = ELF::R_X86_64_GOTPCREL;
1718 break;
1719 case MCSymbolRefExpr::VK_TPOFF:
1720 Type = ELF::R_X86_64_TPOFF32;
1721 break;
1722 case MCSymbolRefExpr::VK_DTPOFF:
1723 Type = ELF::R_X86_64_DTPOFF32;
1724 break;
1725 }
1726 break;
1727 case FK_Data_4:
1728 Type = ELF::R_X86_64_32;
1729 break;
1730 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001731 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001732 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1733 }
1734 }
1735 } else {
1736 if (IsPCRel) {
Rafael Espindola1d5969d2011-12-09 03:03:58 +00001737 switch ((unsigned)Fixup.getKind()) {
1738 default: llvm_unreachable("invalid fixup kind!");
1739
1740 case X86::reloc_global_offset_table:
1741 Type = ELF::R_386_GOTPC;
Jason W Kimd3443e92010-11-15 16:18:39 +00001742 break;
Rafael Espindola1d5969d2011-12-09 03:03:58 +00001743
Rafael Espindola3c68acd2011-12-09 19:57:29 +00001744 case X86::reloc_signed_4byte:
Rafael Espindola1d5969d2011-12-09 03:03:58 +00001745 case FK_PCRel_4:
1746 case FK_Data_4:
1747 switch (Modifier) {
1748 default:
1749 llvm_unreachable("Unimplemented");
1750 case MCSymbolRefExpr::VK_None:
1751 Type = ELF::R_386_PC32;
1752 break;
1753 case MCSymbolRefExpr::VK_PLT:
1754 Type = ELF::R_386_PLT32;
1755 break;
1756 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001757 break;
1758 }
1759 } else {
1760 switch ((unsigned)Fixup.getKind()) {
1761 default: llvm_unreachable("invalid fixup kind!");
1762
1763 case X86::reloc_global_offset_table:
1764 Type = ELF::R_386_GOTPC;
1765 break;
1766
1767 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1768 // instead?
1769 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001770 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001771 case FK_Data_4:
1772 switch (Modifier) {
1773 default:
1774 llvm_unreachable("Unimplemented");
1775 case MCSymbolRefExpr::VK_None:
1776 Type = ELF::R_386_32;
1777 break;
1778 case MCSymbolRefExpr::VK_GOT:
1779 Type = ELF::R_386_GOT32;
1780 break;
1781 case MCSymbolRefExpr::VK_GOTOFF:
1782 Type = ELF::R_386_GOTOFF;
1783 break;
1784 case MCSymbolRefExpr::VK_TLSGD:
1785 Type = ELF::R_386_TLS_GD;
1786 break;
1787 case MCSymbolRefExpr::VK_TPOFF:
1788 Type = ELF::R_386_TLS_LE_32;
1789 break;
1790 case MCSymbolRefExpr::VK_INDNTPOFF:
1791 Type = ELF::R_386_TLS_IE;
1792 break;
1793 case MCSymbolRefExpr::VK_NTPOFF:
1794 Type = ELF::R_386_TLS_LE;
1795 break;
1796 case MCSymbolRefExpr::VK_GOTNTPOFF:
1797 Type = ELF::R_386_TLS_GOTIE;
1798 break;
1799 case MCSymbolRefExpr::VK_TLSLDM:
1800 Type = ELF::R_386_TLS_LDM;
1801 break;
1802 case MCSymbolRefExpr::VK_DTPOFF:
1803 Type = ELF::R_386_TLS_LDO_32;
1804 break;
Nick Lewyckye0b87032011-06-04 17:38:07 +00001805 case MCSymbolRefExpr::VK_GOTTPOFF:
1806 Type = ELF::R_386_TLS_IE_32;
1807 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001808 }
1809 break;
1810 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001811 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001812 case FK_Data_1: Type = ELF::R_386_8; break;
1813 }
1814 }
1815 }
1816
Jason W Kim56a39902010-12-06 21:57:34 +00001817 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001818}
Akira Hatanaka291512f2011-09-30 21:55:40 +00001819
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001820//===- MipsELFObjectWriter -------------------------------------------===//
1821
Akira Hatanaka291512f2011-09-30 21:55:40 +00001822MipsELFObjectWriter::MipsELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1823 raw_ostream &_OS,
1824 bool IsLittleEndian)
1825 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {}
1826
1827MipsELFObjectWriter::~MipsELFObjectWriter() {}
1828
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +00001829// FIXME: get the real EABI Version from the Triple.
1830void MipsELFObjectWriter::WriteEFlags() {
1831 Write32(ELF::EF_MIPS_NOREORDER |
1832 ELF::EF_MIPS_ARCH_32R2);
1833}
1834
Bruno Cardoso Lopesa00a62a2011-12-06 03:34:42 +00001835const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1836 const MCValue &Target,
1837 const MCFragment &F,
1838 const MCFixup &Fixup,
1839 bool IsPCRel) const {
1840 assert(Target.getSymA() && "SymA cannot be 0.");
1841 const MCSymbol &Sym = Target.getSymA()->getSymbol();
1842
Akira Hatanakaf3315cf2011-12-13 02:27:40 +00001843 if (Sym.getSection().getKind().isMergeableCString() ||
1844 Sym.getSection().getKind().isMergeableConst())
Bruno Cardoso Lopesa00a62a2011-12-06 03:34:42 +00001845 return &Sym;
1846
1847 return NULL;
1848}
1849
Akira Hatanaka291512f2011-09-30 21:55:40 +00001850unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
1851 const MCFixup &Fixup,
1852 bool IsPCRel,
1853 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001854 int64_t Addend) const {
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001855 // determine the type of the relocation
1856 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
1857 unsigned Kind = (unsigned)Fixup.getKind();
1858
1859 switch (Kind) {
1860 default:
1861 llvm_unreachable("invalid fixup kind!");
1862 case FK_Data_4:
1863 Type = ELF::R_MIPS_32;
1864 break;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +00001865 case FK_GPRel_4:
1866 Type = ELF::R_MIPS_GPREL32;
1867 break;
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001868 case Mips::fixup_Mips_GPREL16:
1869 Type = ELF::R_MIPS_GPREL16;
1870 break;
1871 case Mips::fixup_Mips_26:
1872 Type = ELF::R_MIPS_26;
1873 break;
1874 case Mips::fixup_Mips_CALL16:
1875 Type = ELF::R_MIPS_CALL16;
1876 break;
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001877 case Mips::fixup_Mips_GOT_Global:
1878 case Mips::fixup_Mips_GOT_Local:
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001879 Type = ELF::R_MIPS_GOT16;
1880 break;
1881 case Mips::fixup_Mips_HI16:
1882 Type = ELF::R_MIPS_HI16;
1883 break;
1884 case Mips::fixup_Mips_LO16:
1885 Type = ELF::R_MIPS_LO16;
1886 break;
1887 case Mips::fixup_Mips_TLSGD:
1888 Type = ELF::R_MIPS_TLS_GD;
1889 break;
1890 case Mips::fixup_Mips_GOTTPREL:
1891 Type = ELF::R_MIPS_TLS_GOTTPREL;
1892 break;
1893 case Mips::fixup_Mips_TPREL_HI:
1894 Type = ELF::R_MIPS_TLS_TPREL_HI16;
1895 break;
1896 case Mips::fixup_Mips_TPREL_LO:
1897 Type = ELF::R_MIPS_TLS_TPREL_LO16;
1898 break;
1899 case Mips::fixup_Mips_Branch_PCRel:
1900 case Mips::fixup_Mips_PC16:
1901 Type = ELF::R_MIPS_PC16;
1902 break;
1903 }
1904
1905 return Type;
Akira Hatanaka291512f2011-09-30 21:55:40 +00001906}