blob: 2369d58b14074777274b8eb5982386c7391d6098 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
Jan Sjödin24b17c62011-03-03 14:52:12 +000014#include "ELFObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
Matt Fleming3565a062010-08-16 18:57:57 +000018#include "llvm/MC/MCAsmLayout.h"
19#include "llvm/MC/MCContext.h"
Matt Fleming3565a062010-08-16 18:57:57 +000020#include "llvm/MC/MCExpr.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCSectionELF.h"
Matt Fleming3565a062010-08-16 18:57:57 +000022#include "llvm/MC/MCValue.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/ELF.h"
26#include "llvm/Target/TargetAsmBackend.h"
Jason W Kim953a2a32011-02-07 01:11:15 +000027#include "llvm/ADT/StringSwitch.h"
Jason W Kime964d112011-05-11 22:53:06 +000028#include "llvm/Support/CommandLine.h"
29#include "llvm/ADT/Statistic.h"
Matt Fleming3565a062010-08-16 18:57:57 +000030
31#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000032#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000033
34#include <vector>
35using namespace llvm;
36
Jason W Kime964d112011-05-11 22:53:06 +000037#undef DEBUG_TYPE
38#define DEBUG_TYPE "reloc-info"
39
Jason W Kim97c07da2011-05-16 16:35:21 +000040// FIXME: This switch must be removed. Since GNU as does not
41// need a command line switch for doing its wierd thing with PIC,
42// LLVM should not need it either.
43// --
Jason W Kime964d112011-05-11 22:53:06 +000044// Emulate the wierd behavior of GNU-as for relocation types
45namespace llvm {
46cl::opt<bool>
47ForceARMElfPIC("arm-elf-force-pic", cl::Hidden, cl::init(false),
48 cl::desc("Force ELF emitter to emit PIC style relocations"));
49}
50
Jan Sjödin2ddfd952011-02-28 21:45:04 +000051bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
52 const MCFixupKindInfo &FKI =
53 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
54
55 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
56}
57
58bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
59 switch (Variant) {
60 default:
61 return false;
62 case MCSymbolRefExpr::VK_GOT:
63 case MCSymbolRefExpr::VK_PLT:
64 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +000065 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +000066 case MCSymbolRefExpr::VK_TPOFF:
67 case MCSymbolRefExpr::VK_TLSGD:
68 case MCSymbolRefExpr::VK_GOTTPOFF:
69 case MCSymbolRefExpr::VK_INDNTPOFF:
70 case MCSymbolRefExpr::VK_NTPOFF:
71 case MCSymbolRefExpr::VK_GOTNTPOFF:
72 case MCSymbolRefExpr::VK_TLSLDM:
73 case MCSymbolRefExpr::VK_DTPOFF:
74 case MCSymbolRefExpr::VK_TLSLD:
75 return true;
76 }
77}
78
Jason W Kimd3443e92010-11-15 16:18:39 +000079ELFObjectWriter::~ELFObjectWriter()
80{}
81
Matt Fleming3565a062010-08-16 18:57:57 +000082// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000083void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
84 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000085 // ELF Header
86 // ----------
87 //
88 // Note
89 // ----
90 // emitWord method behaves differently for ELF32 and ELF64, writing
91 // 4 bytes in the former and 8 in the latter.
92
93 Write8(0x7f); // e_ident[EI_MAG0]
94 Write8('E'); // e_ident[EI_MAG1]
95 Write8('L'); // e_ident[EI_MAG2]
96 Write8('F'); // e_ident[EI_MAG3]
97
Rafael Espindolabff66a82010-12-18 03:27:34 +000098 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000099
100 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000101 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000102
103 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000104 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000105 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000106 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
107 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
108 default: Write8(ELF::ELFOSABI_NONE); break;
109 }
Matt Fleming3565a062010-08-16 18:57:57 +0000110 Write8(0); // e_ident[EI_ABIVERSION]
111
112 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
113
114 Write16(ELF::ET_REL); // e_type
115
Rafael Espindolabff66a82010-12-18 03:27:34 +0000116 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000117
118 Write32(ELF::EV_CURRENT); // e_version
119 WriteWord(0); // e_entry, no entry point in .o file
120 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000121 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000122 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000123
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000124 // e_flags = whatever the target wants
125 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000126
127 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000128 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000129
130 Write16(0); // e_phentsize = prog header entry size
131 Write16(0); // e_phnum = # prog header entries = 0
132
133 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000134 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000135
136 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000137 if (NumberOfSections >= ELF::SHN_LORESERVE)
138 Write16(0);
139 else
140 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000141
142 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000143 if (NumberOfSections >= ELF::SHN_LORESERVE)
144 Write16(ELF::SHN_XINDEX);
145 else
146 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000147}
148
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000149void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
150 MCDataFragment *ShndxF,
151 uint64_t name,
152 uint8_t info, uint64_t value,
153 uint64_t size, uint8_t other,
154 uint32_t shndx,
155 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000156 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000157 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000158 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000159 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000160 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000161 }
162
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000163 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
164 uint16_t(ELF::SHN_XINDEX) : shndx;
165
Rafael Espindolabff66a82010-12-18 03:27:34 +0000166 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000167 String32(*SymtabF, name); // st_name
168 String8(*SymtabF, info); // st_info
169 String8(*SymtabF, other); // st_other
170 String16(*SymtabF, Index); // st_shndx
171 String64(*SymtabF, value); // st_value
172 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000173 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000174 String32(*SymtabF, name); // st_name
175 String32(*SymtabF, value); // st_value
176 String32(*SymtabF, size); // st_size
177 String8(*SymtabF, info); // st_info
178 String8(*SymtabF, other); // st_other
179 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000180 }
181}
182
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000183uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
184 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000185 if (Data.isCommon() && Data.isExternal())
186 return Data.getCommonAlignment();
187
188 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000189
190 if (Symbol.isAbsolute() && Symbol.isVariable()) {
191 if (const MCExpr *Value = Symbol.getVariableValue()) {
192 int64_t IntValue;
193 if (Value->EvaluateAsAbsolute(IntValue, Layout))
194 return (uint64_t)IntValue;
195 }
196 }
197
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000198 if (!Symbol.isInSection())
199 return 0;
200
Rafael Espindola64695402011-05-16 16:17:21 +0000201
202 if (Data.getFragment()) {
203 if (Data.getFlags() & ELF_Other_ThumbFunc)
204 return Layout.getSymbolOffset(&Data)+1;
205 else
206 return Layout.getSymbolOffset(&Data);
207 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000208
209 return 0;
210}
211
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000212void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
213 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000214 // The presence of symbol versions causes undefined symbols and
215 // versions declared with @@@ to be renamed.
216
217 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
218 ie = Asm.symbol_end(); it != ie; ++it) {
219 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000220 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000221 MCSymbolData &SD = Asm.getSymbolData(Symbol);
222
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000223 // Not an alias.
224 if (&Symbol == &Alias)
225 continue;
226
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000227 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000228 size_t Pos = AliasName.find('@');
229 if (Pos == StringRef::npos)
230 continue;
231
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000232 // Aliases defined with .symvar copy the binding from the symbol they alias.
233 // This is the first place we are able to copy this information.
234 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000235 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000236
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000237 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000238 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
239 continue;
240
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000241 // FIXME: produce a better error message.
242 if (Symbol.isUndefined() && Rest.startswith("@@") &&
243 !Rest.startswith("@@@"))
244 report_fatal_error("A @@ version cannot be undefined");
245
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000246 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000247 }
248}
249
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000250void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
251 MCDataFragment *ShndxF,
252 ELFSymbolData &MSD,
253 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000254 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000255 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000256 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000257
Rafael Espindola7be2c332010-10-31 00:16:26 +0000258 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
259 Data.getSymbol().isVariable();
260
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000261 uint8_t Binding = MCELF::GetBinding(OrigData);
262 uint8_t Visibility = MCELF::GetVisibility(OrigData);
263 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000264
265 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
266 uint8_t Other = Visibility;
267
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000268 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000269 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000270
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000271 assert(!(Data.isCommon() && !Data.isExternal()));
272
Rafael Espindolaf0121242010-12-22 16:03:00 +0000273 const MCExpr *ESize = Data.getSize();
274 if (ESize) {
275 int64_t Res;
276 if (!ESize->EvaluateAsAbsolute(Res, Layout))
277 report_fatal_error("Size expression must be absolute.");
278 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000279 }
280
281 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000282 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
283 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000284}
285
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000286void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
287 MCDataFragment *ShndxF,
288 const MCAssembler &Asm,
289 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000290 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000291 // The string table must be emitted first because we need the index
292 // into the string table for all the symbol names.
293 assert(StringTable.size() && "Missing string table");
294
295 // FIXME: Make sure the start of the symbol table is aligned.
296
297 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000298 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000299
300 // Write the symbol table entries.
301 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
302 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
303 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000304 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000305 }
306
Rafael Espindola71859c62010-09-16 19:46:31 +0000307 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000308 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
309 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000310 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000311 static_cast<const MCSectionELF&>(i->getSection());
312 if (Section.getType() == ELF::SHT_RELA ||
313 Section.getType() == ELF::SHT_REL ||
314 Section.getType() == ELF::SHT_STRTAB ||
315 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000316 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000317 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000318 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000319 LastLocalSymbolIndex++;
320 }
Matt Fleming3565a062010-08-16 18:57:57 +0000321
322 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
323 ELFSymbolData &MSD = ExternalSymbolData[i];
324 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000325 assert(((Data.getFlags() & ELF_STB_Global) ||
326 (Data.getFlags() & ELF_STB_Weak)) &&
327 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000328 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000329 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000330 LastLocalSymbolIndex++;
331 }
332
333 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
334 ELFSymbolData &MSD = UndefinedSymbolData[i];
335 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000336 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000337 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000338 LastLocalSymbolIndex++;
339 }
340}
341
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000342const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
343 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000344 const MCFragment &F,
345 const MCFixup &Fixup,
346 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000347 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000348 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
349 const MCSymbol *Renamed = Renames.lookup(&Symbol);
350 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000351
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000352 if (ASymbol.isUndefined()) {
353 if (Renamed)
354 return Renamed;
355 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000356 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000357
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000358 if (SD.isExternal()) {
359 if (Renamed)
360 return Renamed;
361 return &Symbol;
362 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000363
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000364 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000365 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000366 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000367
Rafael Espindola25958732010-11-24 21:57:39 +0000368 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000369 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000370
Rafael Espindola25958732010-11-24 21:57:39 +0000371 if (secKind.isThreadLocal()) {
372 if (Renamed)
373 return Renamed;
374 return &Symbol;
375 }
376
Rafael Espindola8cecf252010-10-06 16:23:36 +0000377 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000378 const MCSectionELF &Sec2 =
379 static_cast<const MCSectionELF&>(F.getParent()->getSection());
380
Rafael Espindola8cecf252010-10-06 16:23:36 +0000381 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000382 (Kind == MCSymbolRefExpr::VK_PLT ||
383 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000384 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000385 if (Renamed)
386 return Renamed;
387 return &Symbol;
388 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000389
Rafael Espindola1c130262011-01-23 04:43:11 +0000390 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000391 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000392 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000393 if (Renamed)
394 return Renamed;
395 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000396 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000397
Jason W Kime964d112011-05-11 22:53:06 +0000398 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
399
Rafael Espindola73ffea42010-09-25 05:42:19 +0000400}
401
Matt Fleming3565a062010-08-16 18:57:57 +0000402
Jason W Kim56a39902010-12-06 21:57:34 +0000403void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
404 const MCAsmLayout &Layout,
405 const MCFragment *Fragment,
406 const MCFixup &Fixup,
407 MCValue Target,
408 uint64_t &FixedValue) {
409 int64_t Addend = 0;
410 int Index = 0;
411 int64_t Value = Target.getConstant();
412 const MCSymbol *RelocSymbol = NULL;
413
Rafael Espindola127a6a42010-12-17 07:28:17 +0000414 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000415 if (!Target.isAbsolute()) {
416 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
417 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000418 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000419
420 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
421 const MCSymbol &SymbolB = RefB->getSymbol();
422 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
423 IsPCRel = true;
424
425 // Offset of the symbol in the section
426 int64_t a = Layout.getSymbolOffset(&SDB);
427
428 // Ofeset of the relocation in the section
429 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
430 Value += b - a;
431 }
432
433 if (!RelocSymbol) {
434 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
435 MCFragment *F = SD.getFragment();
436
437 Index = F->getParent()->getOrdinal() + 1;
438
439 // Offset of the symbol in the section
440 Value += Layout.getSymbolOffset(&SD);
441 } else {
442 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
443 WeakrefUsedInReloc.insert(RelocSymbol);
444 else
445 UsedInReloc.insert(RelocSymbol);
446 Index = -1;
447 }
448 Addend = Value;
449 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000450 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000451 Value = 0;
452 }
453
454 FixedValue = Value;
455 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
456 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000457
Jason W Kim56a39902010-12-06 21:57:34 +0000458 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
459 Fixup.getOffset();
460
Rafael Espindolabff66a82010-12-18 03:27:34 +0000461 if (!hasRelocationAddend())
462 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000463 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
464 Relocations[Fragment->getParent()].push_back(ERE);
465}
466
467
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000468uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000469ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
470 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000471 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000472 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000473}
474
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000475bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
476 const MCSymbolData &Data,
477 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000478 if (Data.getFlags() & ELF_Other_Weakref)
479 return false;
480
Rafael Espindolabd701182010-10-19 19:31:37 +0000481 if (Used)
482 return true;
483
Rafael Espindola88182132010-10-27 15:18:17 +0000484 if (Renamed)
485 return false;
486
Rafael Espindola737cd212010-10-05 18:01:23 +0000487 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000488
Rafael Espindolad1798862010-10-29 23:09:31 +0000489 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
490 return true;
491
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000492 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000493 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
494 return false;
495
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000496 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000497 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000498 return false;
499
Rafael Espindola737cd212010-10-05 18:01:23 +0000500 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
501 return false;
502
Rafael Espindolabd701182010-10-19 19:31:37 +0000503 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000504 return false;
505
506 return true;
507}
508
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000509bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
510 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000511 if (Data.isExternal())
512 return false;
513
514 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000515 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000516
517 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
518 if (isSignature && !isUsedInReloc)
519 return true;
520
Rafael Espindola737cd212010-10-05 18:01:23 +0000521 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000522 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000523
524 return true;
525}
526
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000527void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000528 SectionIndexMapTy &SectionIndexMap,
529 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000530 unsigned Index = 1;
531 for (MCAssembler::iterator it = Asm.begin(),
532 ie = Asm.end(); it != ie; ++it) {
533 const MCSectionELF &Section =
534 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000535 if (Section.getType() != ELF::SHT_GROUP)
536 continue;
537 SectionIndexMap[&Section] = Index++;
538 }
539
540 for (MCAssembler::iterator it = Asm.begin(),
541 ie = Asm.end(); it != ie; ++it) {
542 const MCSectionELF &Section =
543 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000544 if (Section.getType() == ELF::SHT_GROUP ||
545 Section.getType() == ELF::SHT_REL ||
546 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000547 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000548 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000549 const MCSectionELF *RelSection = RelMap.lookup(&Section);
550 if (RelSection)
551 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000552 }
553}
554
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000555void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000556 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000557 RevGroupMapTy RevGroupMap,
558 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000559 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000560 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000561 if (NeedsGOT) {
562 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
563 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
564 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
565 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000566 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000567 }
568
Matt Fleming3565a062010-08-16 18:57:57 +0000569 // Index 0 is always the empty string.
570 StringMap<uint64_t> StringIndexMap;
571 StringTable += '\x00';
572
Rafael Espindolad5321da2011-04-07 23:21:52 +0000573 // FIXME: We could optimize suffixes in strtab in the same way we
574 // optimize them in shstrtab.
575
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000576 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000577 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
578 ie = Asm.symbol_end(); it != ie; ++it) {
579 const MCSymbol &Symbol = it->getSymbol();
580
Rafael Espindola484291c2010-11-01 14:28:48 +0000581 bool Used = UsedInReloc.count(&Symbol);
582 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000583 bool isSignature = RevGroupMap.count(&Symbol);
584
585 if (!isInSymtab(Asm, *it,
586 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000587 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000588 continue;
589
Matt Fleming3565a062010-08-16 18:57:57 +0000590 ELFSymbolData MSD;
591 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000592 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000593
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000594 // Undefined symbols are global, but this is the first place we
595 // are able to set it.
596 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000597 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000598 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000599 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
600 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000601 }
602
Rafael Espindola484291c2010-11-01 14:28:48 +0000603 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000604 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000605
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000606 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000607 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000608 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000609 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000610 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000611 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000612 if (isSignature && !Used)
613 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
614 else
615 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000616 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000617 const MCSectionELF &Section =
618 static_cast<const MCSectionELF&>(RefSymbol.getSection());
619 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000620 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
621 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000622 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000623 }
624
Rafael Espindola88182132010-10-27 15:18:17 +0000625 // The @@@ in symbol version is replaced with @ in undefined symbols and
626 // @@ in defined ones.
627 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000628 SmallString<32> Buf;
629
Rafael Espindola88182132010-10-27 15:18:17 +0000630 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000631 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000632 Buf += Name.substr(0, Pos);
633 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
634 Buf += Name.substr(Pos + Skip);
635 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000636 }
637
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000638 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000639 if (!Entry) {
640 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000641 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000642 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000643 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000644 MSD.StringIndex = Entry;
645 if (MSD.SectionIndex == ELF::SHN_UNDEF)
646 UndefinedSymbolData.push_back(MSD);
647 else if (Local)
648 LocalSymbolData.push_back(MSD);
649 else
650 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000651 }
652
653 // Symbols are required to be in lexicographic order.
654 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
655 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
656 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
657
658 // Set the symbol indices. Local symbols must come before all other
659 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000660 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000661 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
662 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000663
664 Index += NumRegularSections;
665
Matt Fleming3565a062010-08-16 18:57:57 +0000666 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
667 ExternalSymbolData[i].SymbolData->setIndex(Index++);
668 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
669 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
670}
671
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000672void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
673 MCAsmLayout &Layout,
674 RelMapTy &RelMap) {
675 for (MCAssembler::const_iterator it = Asm.begin(),
676 ie = Asm.end(); it != ie; ++it) {
677 const MCSectionData &SD = *it;
678 if (Relocations[&SD].empty())
679 continue;
680
Matt Fleming3565a062010-08-16 18:57:57 +0000681 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000682 const MCSectionELF &Section =
683 static_cast<const MCSectionELF&>(SD.getSection());
684
685 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000686 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000687 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000688
689 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000690 if (hasRelocationAddend())
691 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000692 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000693 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000694
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000695 const MCSectionELF *RelaSection =
696 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
697 ELF::SHT_RELA : ELF::SHT_REL, 0,
698 SectionKind::getReadOnly(),
699 EntrySize, "");
700 RelMap[&Section] = RelaSection;
701 Asm.getOrCreateSectionData(*RelaSection);
702 }
703}
Matt Fleming3565a062010-08-16 18:57:57 +0000704
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000705void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
706 const RelMapTy &RelMap) {
707 for (MCAssembler::const_iterator it = Asm.begin(),
708 ie = Asm.end(); it != ie; ++it) {
709 const MCSectionData &SD = *it;
710 const MCSectionELF &Section =
711 static_cast<const MCSectionELF&>(SD.getSection());
712
713 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
714 if (!RelaSection)
715 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000716 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000717 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000718
719 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000720 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000721 }
722}
723
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000724void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
725 uint64_t Flags, uint64_t Address,
726 uint64_t Offset, uint64_t Size,
727 uint32_t Link, uint32_t Info,
728 uint64_t Alignment,
729 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000730 Write32(Name); // sh_name: index into string table
731 Write32(Type); // sh_type
732 WriteWord(Flags); // sh_flags
733 WriteWord(Address); // sh_addr
734 WriteWord(Offset); // sh_offset
735 WriteWord(Size); // sh_size
736 Write32(Link); // sh_link
737 Write32(Info); // sh_info
738 WriteWord(Alignment); // sh_addralign
739 WriteWord(EntrySize); // sh_entsize
740}
741
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000742void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
743 MCDataFragment *F,
744 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000745 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
746 // sort by the r_offset just like gnu as does
747 array_pod_sort(Relocs.begin(), Relocs.end());
748
749 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
750 ELFRelocationEntry entry = Relocs[e - i - 1];
751
Rafael Espindola12203cc2010-11-21 00:48:25 +0000752 if (!entry.Index)
753 ;
754 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000755 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
756 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000757 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000758 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000759 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000760
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000761 struct ELF::Elf64_Rela ERE64;
762 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000763 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000764
Rafael Espindolabff66a82010-12-18 03:27:34 +0000765 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000766 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000767 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000768 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000769
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000770 struct ELF::Elf32_Rela ERE32;
771 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000772 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000773
Rafael Espindolabff66a82010-12-18 03:27:34 +0000774 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000775 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000776 }
Matt Fleming3565a062010-08-16 18:57:57 +0000777 }
778}
779
Rafael Espindolad5321da2011-04-07 23:21:52 +0000780static int compareBySuffix(const void *a, const void *b) {
781 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
782 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
783 const StringRef &NameA = secA->getSectionName();
784 const StringRef &NameB = secB->getSectionName();
785 const unsigned sizeA = NameA.size();
786 const unsigned sizeB = NameB.size();
787 const unsigned len = std::min(sizeA, sizeB);
788 for (unsigned int i = 0; i < len; ++i) {
789 char ca = NameA[sizeA - i - 1];
790 char cb = NameB[sizeB - i - 1];
791 if (ca != cb)
792 return cb - ca;
793 }
794
795 return sizeB - sizeA;
796}
797
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000798void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
799 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000800 SectionIndexMapTy &SectionIndexMap,
801 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000802 MCContext &Ctx = Asm.getContext();
803 MCDataFragment *F;
804
Rafael Espindolabff66a82010-12-18 03:27:34 +0000805 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000806
Rafael Espindola38738bf2010-09-22 19:04:41 +0000807 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000808 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000809 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000810 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000811 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
812 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000813
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000814 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000815 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
816 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000817 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000818 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000819 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000820
821 MCSectionData *SymtabShndxSD = NULL;
822
823 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000824 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000825 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000826 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000827 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
828 SymtabShndxSD->setAlignment(4);
829 }
Matt Fleming3565a062010-08-16 18:57:57 +0000830
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000831 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000832 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000833 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000834 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
835 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000836
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000837 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
838
839 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
840 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
841 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000842
843 // Symbol table
844 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000845 MCDataFragment *ShndxF = NULL;
846 if (NeedsSymtabShndx) {
847 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000848 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000849 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000850
Matt Fleming3565a062010-08-16 18:57:57 +0000851 F = new MCDataFragment(&StrtabSD);
852 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000853
Matt Fleming3565a062010-08-16 18:57:57 +0000854 F = new MCDataFragment(&ShstrtabSD);
855
Rafael Espindolad5321da2011-04-07 23:21:52 +0000856 std::vector<const MCSectionELF*> Sections;
857 for (MCAssembler::const_iterator it = Asm.begin(),
858 ie = Asm.end(); it != ie; ++it) {
859 const MCSectionELF &Section =
860 static_cast<const MCSectionELF&>(it->getSection());
861 Sections.push_back(&Section);
862 }
863 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
864
Matt Fleming3565a062010-08-16 18:57:57 +0000865 // Section header string table.
866 //
867 // The first entry of a string table holds a null character so skip
868 // section 0.
869 uint64_t Index = 1;
870 F->getContents() += '\x00';
871
Rafael Espindolad5321da2011-04-07 23:21:52 +0000872 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
873 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000874
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000875 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000876 if (I != 0) {
877 StringRef PreviousName = Sections[I - 1]->getSectionName();
878 if (PreviousName.endswith(Name)) {
879 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
880 continue;
881 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000882 }
Matt Fleming3565a062010-08-16 18:57:57 +0000883 // Remember the index into the string table so we can write it
884 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000885 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000886
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000887 Index += Name.size() + 1;
888 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000889 F->getContents() += '\x00';
890 }
Rafael Espindola70703872010-09-30 02:22:20 +0000891}
892
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000893void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
894 MCAsmLayout &Layout,
895 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000896 RevGroupMapTy &RevGroupMap,
897 SectionIndexMapTy &SectionIndexMap,
898 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000899 // Create the .note.GNU-stack section if needed.
900 MCContext &Ctx = Asm.getContext();
901 if (Asm.getNoExecStack()) {
902 const MCSectionELF *GnuStackSection =
903 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
904 SectionKind::getReadOnly());
905 Asm.getOrCreateSectionData(*GnuStackSection);
906 }
907
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000908 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000909 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
910 it != ie; ++it) {
911 const MCSectionELF &Section =
912 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000913 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000914 continue;
915
916 const MCSymbol *SignatureSymbol = Section.getGroup();
917 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000918 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000919 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000920 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000921 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
922 Data.setAlignment(4);
923 MCDataFragment *F = new MCDataFragment(&Data);
924 String32(*F, ELF::GRP_COMDAT);
925 }
926 GroupMap[Group] = SignatureSymbol;
927 }
928
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000929 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
930
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000931 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000932 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000933 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000934 const MCSectionELF &Section =
935 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000936 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000937 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000938 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000939 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
940 // FIXME: we could use the previous fragment
941 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000942 unsigned Index = SectionIndexMap.lookup(&Section);
943 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000944 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000945}
946
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000947void ELFObjectWriter::WriteSection(MCAssembler &Asm,
948 const SectionIndexMapTy &SectionIndexMap,
949 uint32_t GroupSymbolIndex,
950 uint64_t Offset, uint64_t Size,
951 uint64_t Alignment,
952 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000953 uint64_t sh_link = 0;
954 uint64_t sh_info = 0;
955
956 switch(Section.getType()) {
957 case ELF::SHT_DYNAMIC:
958 sh_link = SectionStringTableIndex[&Section];
959 sh_info = 0;
960 break;
961
962 case ELF::SHT_REL:
963 case ELF::SHT_RELA: {
964 const MCSectionELF *SymtabSection;
965 const MCSectionELF *InfoSection;
966 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
967 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000968 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000969 sh_link = SectionIndexMap.lookup(SymtabSection);
970 assert(sh_link && ".symtab not found");
971
972 // Remove ".rel" and ".rela" prefixes.
973 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
974 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
975
976 InfoSection = Asm.getContext().getELFSection(SectionName,
977 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000978 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000979 sh_info = SectionIndexMap.lookup(InfoSection);
980 break;
981 }
982
983 case ELF::SHT_SYMTAB:
984 case ELF::SHT_DYNSYM:
985 sh_link = StringTableIndex;
986 sh_info = LastLocalSymbolIndex;
987 break;
988
989 case ELF::SHT_SYMTAB_SHNDX:
990 sh_link = SymbolTableIndex;
991 break;
992
993 case ELF::SHT_PROGBITS:
994 case ELF::SHT_STRTAB:
995 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000996 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000997 case ELF::SHT_NULL:
998 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +0000999 case ELF::SHT_INIT_ARRAY:
1000 case ELF::SHT_FINI_ARRAY:
1001 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001002 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001003 // Nothing to do.
1004 break;
1005
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001006 case ELF::SHT_GROUP: {
1007 sh_link = SymbolTableIndex;
1008 sh_info = GroupSymbolIndex;
1009 break;
1010 }
1011
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001012 default:
1013 assert(0 && "FIXME: sh_type value not supported!");
1014 break;
1015 }
1016
1017 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1018 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1019 Alignment, Section.getEntrySize());
1020}
1021
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001022bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001023 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001024 !SD.getSection().isVirtualSection();
1025}
1026
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001027uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001028 uint64_t Ret = 0;
1029 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1030 ++i) {
1031 const MCFragment &F = *i;
1032 assert(F.getKind() == MCFragment::FT_Data);
1033 Ret += cast<MCDataFragment>(F).getContents().size();
1034 }
1035 return Ret;
1036}
1037
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001038uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1039 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001040 if (IsELFMetaDataSection(SD))
1041 return DataSectionSize(SD);
1042 return Layout.getSectionFileSize(&SD);
1043}
1044
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001045uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1046 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001047 if (IsELFMetaDataSection(SD))
1048 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001049 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001050}
1051
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001052void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1053 const MCAsmLayout &Layout,
1054 const MCSectionELF &Section) {
1055 uint64_t FileOff = OS.tell();
1056 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1057
1058 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1059 WriteZeros(Padding);
1060 FileOff += Padding;
1061
1062 FileOff += GetSectionFileSize(Layout, SD);
1063
1064 if (IsELFMetaDataSection(SD)) {
1065 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1066 ++i) {
1067 const MCFragment &F = *i;
1068 assert(F.getKind() == MCFragment::FT_Data);
1069 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1070 }
1071 } else {
1072 Asm.WriteSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001073 }
1074}
1075
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001076void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1077 const GroupMapTy &GroupMap,
1078 const MCAsmLayout &Layout,
1079 const SectionIndexMapTy &SectionIndexMap,
1080 const SectionOffsetMapTy &SectionOffsetMap) {
1081 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001082
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001083 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001084 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001085
1086 for (SectionIndexMapTy::const_iterator i=
1087 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1088 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001089 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001090 }
1091
Matt Fleming3565a062010-08-16 18:57:57 +00001092 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001093 uint64_t FirstSectionSize =
1094 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1095 uint32_t FirstSectionLink =
1096 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1097 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001098
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001099 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001100 const MCSectionELF &Section = *Sections[i];
1101 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1102 uint32_t GroupSymbolIndex;
1103 if (Section.getType() != ELF::SHT_GROUP)
1104 GroupSymbolIndex = 0;
1105 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001106 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1107 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001108
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001109 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001110
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001111 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001112 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001113 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001114 }
1115}
1116
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001117void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1118 std::vector<const MCSectionELF*> &Sections) {
1119 for (MCAssembler::iterator it = Asm.begin(),
1120 ie = Asm.end(); it != ie; ++it) {
1121 const MCSectionELF &Section =
1122 static_cast<const MCSectionELF &>(it->getSection());
1123 if (Section.getType() == ELF::SHT_GROUP)
1124 Sections.push_back(&Section);
1125 }
1126
1127 for (MCAssembler::iterator it = Asm.begin(),
1128 ie = Asm.end(); it != ie; ++it) {
1129 const MCSectionELF &Section =
1130 static_cast<const MCSectionELF &>(it->getSection());
1131 if (Section.getType() != ELF::SHT_GROUP &&
1132 Section.getType() != ELF::SHT_REL &&
1133 Section.getType() != ELF::SHT_RELA)
1134 Sections.push_back(&Section);
1135 }
1136
1137 for (MCAssembler::iterator it = Asm.begin(),
1138 ie = Asm.end(); it != ie; ++it) {
1139 const MCSectionELF &Section =
1140 static_cast<const MCSectionELF &>(it->getSection());
1141 if (Section.getType() == ELF::SHT_REL ||
1142 Section.getType() == ELF::SHT_RELA)
1143 Sections.push_back(&Section);
1144 }
1145}
1146
1147void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1148 const MCAsmLayout &Layout) {
1149 GroupMapTy GroupMap;
1150 RevGroupMapTy RevGroupMap;
1151 SectionIndexMapTy SectionIndexMap;
1152
1153 unsigned NumUserSections = Asm.size();
1154
1155 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1156 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1157
1158 const unsigned NumUserAndRelocSections = Asm.size();
1159 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1160 RevGroupMap, SectionIndexMap, RelMap);
1161 const unsigned AllSections = Asm.size();
1162 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1163
1164 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1165
1166 // Compute symbol table information.
1167 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1168
1169
1170 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1171
1172 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1173 const_cast<MCAsmLayout&>(Layout),
1174 SectionIndexMap,
1175 RelMap);
1176
1177 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1178 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1179 sizeof(ELF::Elf32_Ehdr);
1180 uint64_t FileOff = HeaderSize;
1181
1182 std::vector<const MCSectionELF*> Sections;
1183 ComputeSectionOrder(Asm, Sections);
1184 unsigned NumSections = Sections.size();
1185 SectionOffsetMapTy SectionOffsetMap;
1186 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1187 const MCSectionELF &Section = *Sections[i];
1188 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1189
1190 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1191
1192 // Remember the offset into the file for this section.
1193 SectionOffsetMap[&Section] = FileOff;
1194
1195 // Get the size of the section in the output file (including padding).
1196 FileOff += GetSectionFileSize(Layout, SD);
1197 }
1198
1199 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1200
1201 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1202
1203 uint64_t SectionHeaderEntrySize = is64Bit() ?
1204 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1205 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1206
1207 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1208 const MCSectionELF &Section = *Sections[i];
1209 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1210
1211 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1212
1213 // Remember the offset into the file for this section.
1214 SectionOffsetMap[&Section] = FileOff;
1215
1216 // Get the size of the section in the output file (including padding).
1217 FileOff += GetSectionFileSize(Layout, SD);
1218 }
1219
1220 // Write out the ELF header ...
1221 WriteHeader(SectionHeaderOffset, NumSections + 1);
1222
1223 // ... then the regular sections ...
1224 // + because of .shstrtab
1225 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1226 WriteDataSectionData(Asm, Layout, *Sections[i]);
1227
1228 FileOff = OS.tell();
1229 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1230 WriteZeros(Padding);
1231
1232 // ... then the section header table ...
1233 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1234 SectionOffsetMap);
1235
1236 FileOff = OS.tell();
1237
1238 // ... and then the remainting sections ...
1239 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1240 WriteDataSectionData(Asm, Layout, *Sections[i]);
1241}
1242
Eli Friedman78c1e172011-03-03 07:24:36 +00001243bool
1244ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1245 const MCSymbolData &DataA,
1246 const MCFragment &FB,
1247 bool InSet,
1248 bool IsPCRel) const {
1249 if (DataA.getFlags() & ELF_STB_Weak)
1250 return false;
1251 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1252 Asm, DataA, FB,InSet, IsPCRel);
1253}
1254
Rafael Espindola6024c972010-12-17 17:45:22 +00001255MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1256 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001257 bool IsLittleEndian) {
1258 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001259 case ELF::EM_386:
1260 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001261 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001262 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001263 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001264 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001265 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001266 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001267 }
1268}
1269
1270
1271/// START OF SUBCLASSES for ELFObjectWriter
1272//===- ARMELFObjectWriter -------------------------------------------===//
1273
Rafael Espindola31f35782010-12-17 18:01:31 +00001274ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001275 raw_ostream &_OS,
1276 bool IsLittleEndian)
1277 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001278{}
1279
1280ARMELFObjectWriter::~ARMELFObjectWriter()
1281{}
1282
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001283// FIXME: get the real EABI Version from the Triple.
1284void ARMELFObjectWriter::WriteEFlags() {
1285 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1286}
1287
Jason W Kim953a2a32011-02-07 01:11:15 +00001288// In ARM, _MergedGlobals and other most symbols get emitted directly.
1289// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001290// This code is an approximation of what ARM/gcc does.
1291
1292STATISTIC(PCRelCount, "Total number of PIC Relocations");
1293STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001294
1295const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1296 const MCValue &Target,
1297 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001298 const MCFixup &Fixup,
1299 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001300 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1301 bool EmitThisSym = false;
1302
Jason W Kime964d112011-05-11 22:53:06 +00001303 const MCSectionELF &Section =
1304 static_cast<const MCSectionELF&>(Symbol.getSection());
Jason W Kime964d112011-05-11 22:53:06 +00001305 bool InNormalSection = true;
1306 unsigned RelocType = 0;
1307 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1308
Matt Beaumont-Gay6dcd4132011-05-11 23:34:51 +00001309 DEBUG(
1310 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1311 MCSymbolRefExpr::VariantKind Kind2;
1312 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1313 MCSymbolRefExpr::VK_None;
1314 dbgs() << "considering symbol "
Jason W Kime964d112011-05-11 22:53:06 +00001315 << Section.getSectionName() << "/"
1316 << Symbol.getName() << "/"
1317 << " Rel:" << (unsigned)RelocType
1318 << " Kind: " << (int)Kind << "/" << (int)Kind2
1319 << " Tmp:"
1320 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1321 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1322 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1323
1324 if (IsPCRel || ForceARMElfPIC) { ++PCRelCount;
1325 switch (RelocType) {
1326 default:
1327 // Most relocation types are emitted as explicit symbols
1328 InNormalSection =
1329 StringSwitch<bool>(Section.getSectionName())
1330 .Case(".data.rel.ro.local", false)
1331 .Case(".data.rel", false)
1332 .Case(".bss", false)
1333 .Default(true);
1334 EmitThisSym = true;
1335 break;
1336 case ELF::R_ARM_ABS32:
1337 // But things get strange with R_ARM_ABS32
1338 // In this case, most things that go in .rodata show up
1339 // as section relative relocations
1340 InNormalSection =
1341 StringSwitch<bool>(Section.getSectionName())
1342 .Case(".data.rel.ro.local", false)
1343 .Case(".data.rel", false)
1344 .Case(".rodata", false)
1345 .Case(".bss", false)
1346 .Default(true);
1347 EmitThisSym = false;
1348 break;
1349 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001350 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001351 NonPCRelCount++;
1352 InNormalSection =
1353 StringSwitch<bool>(Section.getSectionName())
1354 .Case(".data.rel.ro.local", false)
1355 .Case(".rodata", false)
1356 .Case(".data.rel", false)
1357 .Case(".bss", false)
1358 .Default(true);
1359
1360 switch (RelocType) {
1361 default: EmitThisSym = true; break;
1362 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1363 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001364 }
Jason W Kime964d112011-05-11 22:53:06 +00001365
Jason W Kim953a2a32011-02-07 01:11:15 +00001366 if (EmitThisSym)
1367 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001368 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001369 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001370 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001371 return NULL;
1372}
1373
Jason W Kime964d112011-05-11 22:53:06 +00001374// Need to examine the Fixup when determining whether to
1375// emit the relocation as an explicit symbol or as a section relative
1376// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001377unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1378 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001379 bool IsPCRel,
1380 bool IsRelocWithSymbol,
1381 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001382 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1383 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1384
Jason W Kime964d112011-05-11 22:53:06 +00001385 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1386
1387 if (RelocNeedsGOT(Modifier))
1388 NeedsGOT = true;
1389
1390 return Type;
1391}
1392
1393unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1394 const MCFixup &Fixup,
1395 bool IsPCRel) const {
1396 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1397 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1398
Jason W Kima0871e72010-12-08 23:14:44 +00001399 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001400 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001401 switch ((unsigned)Fixup.getKind()) {
1402 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001403 case FK_Data_4:
1404 switch (Modifier) {
1405 default: llvm_unreachable("Unsupported Modifier");
1406 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001407 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001408 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001409 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001410 assert(0 && "unimplemented");
1411 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001412 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1413 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001414 break;
1415 }
1416 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001417 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001418 switch (Modifier) {
1419 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001420 Type = ELF::R_ARM_PLT32;
1421 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001422 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001423 Type = ELF::R_ARM_CALL;
1424 break;
1425 }
1426 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001427 case ARM::fixup_arm_condbranch:
1428 Type = ELF::R_ARM_JUMP24;
1429 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001430 case ARM::fixup_arm_movt_hi16:
1431 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001432 Type = ELF::R_ARM_MOVT_PREL;
1433 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001434 case ARM::fixup_arm_movw_lo16:
1435 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001436 Type = ELF::R_ARM_MOVW_PREL_NC;
1437 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001438 case ARM::fixup_t2_movt_hi16:
1439 case ARM::fixup_t2_movt_hi16_pcrel:
1440 Type = ELF::R_ARM_THM_MOVT_PREL;
1441 break;
1442 case ARM::fixup_t2_movw_lo16:
1443 case ARM::fixup_t2_movw_lo16_pcrel:
1444 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1445 break;
Rafael Espindola298c8e12011-05-20 20:01:01 +00001446 case ARM::fixup_arm_thumb_bl:
1447 case ARM::fixup_arm_thumb_blx:
1448 switch (Modifier) {
1449 case MCSymbolRefExpr::VK_ARM_PLT:
1450 Type = ELF::R_ARM_THM_CALL;
1451 break;
1452 default:
1453 Type = ELF::R_ARM_NONE;
1454 break;
1455 }
1456 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001457 }
1458 } else {
1459 switch ((unsigned)Fixup.getKind()) {
1460 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001461 case FK_Data_4:
1462 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001463 default: llvm_unreachable("Unsupported Modifier"); break;
1464 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001465 Type = ELF::R_ARM_GOT_BREL;
1466 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001467 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001468 Type = ELF::R_ARM_TLS_GD32;
1469 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001470 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001471 Type = ELF::R_ARM_TLS_LE32;
1472 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001473 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001474 Type = ELF::R_ARM_TLS_IE32;
1475 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001476 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001477 Type = ELF::R_ARM_ABS32;
1478 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001479 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001480 Type = ELF::R_ARM_GOTOFF32;
1481 break;
1482 }
1483 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001484 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001485 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001486 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001487 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001488 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001489 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001490 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001491 assert(0 && "Unimplemented");
1492 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001493 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001494 Type = ELF::R_ARM_CALL;
1495 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001496 case ARM::fixup_arm_condbranch:
1497 Type = ELF::R_ARM_JUMP24;
1498 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001499 case ARM::fixup_arm_movt_hi16:
1500 Type = ELF::R_ARM_MOVT_ABS;
1501 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001502 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001503 Type = ELF::R_ARM_MOVW_ABS_NC;
1504 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001505 case ARM::fixup_t2_movt_hi16:
1506 Type = ELF::R_ARM_THM_MOVT_ABS;
1507 break;
1508 case ARM::fixup_t2_movw_lo16:
1509 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1510 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001511 }
1512 }
1513
Jason W Kima0871e72010-12-08 23:14:44 +00001514 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001515}
1516
Wesley Peck4b047132010-11-21 22:06:28 +00001517//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001518
Rafael Espindola31f35782010-12-17 18:01:31 +00001519MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001520 raw_ostream &_OS,
1521 bool IsLittleEndian)
1522 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001523}
1524
1525MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1526}
1527
Jason W Kim56a39902010-12-06 21:57:34 +00001528unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001529 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001530 bool IsPCRel,
1531 bool IsRelocWithSymbol,
1532 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001533 // determine the type of the relocation
1534 unsigned Type;
1535 if (IsPCRel) {
1536 switch ((unsigned)Fixup.getKind()) {
1537 default:
1538 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001539 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001540 Type = ELF::R_MICROBLAZE_64_PCREL;
1541 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001542 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001543 Type = ELF::R_MICROBLAZE_32_PCREL;
1544 break;
1545 }
1546 } else {
1547 switch ((unsigned)Fixup.getKind()) {
1548 default: llvm_unreachable("invalid fixup kind!");
1549 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001550 Type = ((IsRelocWithSymbol || Addend !=0)
1551 ? ELF::R_MICROBLAZE_32
1552 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001553 break;
1554 case FK_Data_2:
1555 Type = ELF::R_MICROBLAZE_32;
1556 break;
1557 }
1558 }
Jason W Kim56a39902010-12-06 21:57:34 +00001559 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001560}
Jason W Kimd3443e92010-11-15 16:18:39 +00001561
1562//===- X86ELFObjectWriter -------------------------------------------===//
1563
1564
Rafael Espindola31f35782010-12-17 18:01:31 +00001565X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001566 raw_ostream &_OS,
1567 bool IsLittleEndian)
1568 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001569{}
1570
1571X86ELFObjectWriter::~X86ELFObjectWriter()
1572{}
1573
Jason W Kim56a39902010-12-06 21:57:34 +00001574unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1575 const MCFixup &Fixup,
1576 bool IsPCRel,
1577 bool IsRelocWithSymbol,
1578 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001579 // determine the type of the relocation
1580
Rafael Espindola12203cc2010-11-21 00:48:25 +00001581 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1582 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001583 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001584 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001585 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001586 switch ((unsigned)Fixup.getKind()) {
1587 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001588
1589 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1590 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1591 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1592
Rafael Espindola3a83c402010-12-27 00:36:05 +00001593 case FK_PCRel_8:
1594 assert(Modifier == MCSymbolRefExpr::VK_None);
1595 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001596 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001597 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001598 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001599 case X86::reloc_riprel_4byte:
1600 case FK_PCRel_4:
1601 switch (Modifier) {
1602 default:
1603 llvm_unreachable("Unimplemented");
1604 case MCSymbolRefExpr::VK_None:
1605 Type = ELF::R_X86_64_PC32;
1606 break;
1607 case MCSymbolRefExpr::VK_PLT:
1608 Type = ELF::R_X86_64_PLT32;
1609 break;
1610 case MCSymbolRefExpr::VK_GOTPCREL:
1611 Type = ELF::R_X86_64_GOTPCREL;
1612 break;
1613 case MCSymbolRefExpr::VK_GOTTPOFF:
1614 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001615 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001616 case MCSymbolRefExpr::VK_TLSGD:
1617 Type = ELF::R_X86_64_TLSGD;
1618 break;
1619 case MCSymbolRefExpr::VK_TLSLD:
1620 Type = ELF::R_X86_64_TLSLD;
1621 break;
1622 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001623 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001624 case FK_PCRel_2:
1625 assert(Modifier == MCSymbolRefExpr::VK_None);
1626 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001627 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001628 case FK_PCRel_1:
1629 assert(Modifier == MCSymbolRefExpr::VK_None);
1630 Type = ELF::R_X86_64_PC8;
1631 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001632 }
1633 } else {
1634 switch ((unsigned)Fixup.getKind()) {
1635 default: llvm_unreachable("invalid fixup kind!");
1636 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1637 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001638 assert(isInt<32>(Target.getConstant()));
1639 switch (Modifier) {
1640 default:
1641 llvm_unreachable("Unimplemented");
1642 case MCSymbolRefExpr::VK_None:
1643 Type = ELF::R_X86_64_32S;
1644 break;
1645 case MCSymbolRefExpr::VK_GOT:
1646 Type = ELF::R_X86_64_GOT32;
1647 break;
1648 case MCSymbolRefExpr::VK_GOTPCREL:
1649 Type = ELF::R_X86_64_GOTPCREL;
1650 break;
1651 case MCSymbolRefExpr::VK_TPOFF:
1652 Type = ELF::R_X86_64_TPOFF32;
1653 break;
1654 case MCSymbolRefExpr::VK_DTPOFF:
1655 Type = ELF::R_X86_64_DTPOFF32;
1656 break;
1657 }
1658 break;
1659 case FK_Data_4:
1660 Type = ELF::R_X86_64_32;
1661 break;
1662 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001663 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001664 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1665 }
1666 }
1667 } else {
1668 if (IsPCRel) {
1669 switch (Modifier) {
1670 default:
1671 llvm_unreachable("Unimplemented");
1672 case MCSymbolRefExpr::VK_None:
1673 Type = ELF::R_386_PC32;
1674 break;
1675 case MCSymbolRefExpr::VK_PLT:
1676 Type = ELF::R_386_PLT32;
1677 break;
1678 }
1679 } else {
1680 switch ((unsigned)Fixup.getKind()) {
1681 default: llvm_unreachable("invalid fixup kind!");
1682
1683 case X86::reloc_global_offset_table:
1684 Type = ELF::R_386_GOTPC;
1685 break;
1686
1687 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1688 // instead?
1689 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001690 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001691 case FK_Data_4:
1692 switch (Modifier) {
1693 default:
1694 llvm_unreachable("Unimplemented");
1695 case MCSymbolRefExpr::VK_None:
1696 Type = ELF::R_386_32;
1697 break;
1698 case MCSymbolRefExpr::VK_GOT:
1699 Type = ELF::R_386_GOT32;
1700 break;
1701 case MCSymbolRefExpr::VK_GOTOFF:
1702 Type = ELF::R_386_GOTOFF;
1703 break;
1704 case MCSymbolRefExpr::VK_TLSGD:
1705 Type = ELF::R_386_TLS_GD;
1706 break;
1707 case MCSymbolRefExpr::VK_TPOFF:
1708 Type = ELF::R_386_TLS_LE_32;
1709 break;
1710 case MCSymbolRefExpr::VK_INDNTPOFF:
1711 Type = ELF::R_386_TLS_IE;
1712 break;
1713 case MCSymbolRefExpr::VK_NTPOFF:
1714 Type = ELF::R_386_TLS_LE;
1715 break;
1716 case MCSymbolRefExpr::VK_GOTNTPOFF:
1717 Type = ELF::R_386_TLS_GOTIE;
1718 break;
1719 case MCSymbolRefExpr::VK_TLSLDM:
1720 Type = ELF::R_386_TLS_LDM;
1721 break;
1722 case MCSymbolRefExpr::VK_DTPOFF:
1723 Type = ELF::R_386_TLS_LDO_32;
1724 break;
Nick Lewyckye0b87032011-06-04 17:38:07 +00001725 case MCSymbolRefExpr::VK_GOTTPOFF:
1726 Type = ELF::R_386_TLS_IE_32;
1727 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001728 }
1729 break;
1730 case FK_Data_2: Type = ELF::R_386_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_386_8; break;
1733 }
1734 }
1735 }
1736
1737 if (RelocNeedsGOT(Modifier))
1738 NeedsGOT = true;
1739
Jason W Kim56a39902010-12-06 21:57:34 +00001740 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001741}