blob: 78588ae9acaf856d374564714a03c9e946d460c2 [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
Evan Cheng8c3fee52011-07-25 18:43:53 +000031#include "../Target/X86/MCTargetDesc/X86FixupKinds.h"
Evan Chengbe740292011-07-23 00:00:19 +000032#include "../Target/ARM/MCTargetDesc/ARMFixupKinds.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000033#include "../Target/PowerPC/MCTargetDesc/PPCFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000034
35#include <vector>
36using namespace llvm;
37
Jason W Kime964d112011-05-11 22:53:06 +000038#undef DEBUG_TYPE
39#define DEBUG_TYPE "reloc-info"
40
Jan Sjödin2ddfd952011-02-28 21:45:04 +000041bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
42 const MCFixupKindInfo &FKI =
43 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
44
45 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
46}
47
48bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
49 switch (Variant) {
50 default:
51 return false;
52 case MCSymbolRefExpr::VK_GOT:
53 case MCSymbolRefExpr::VK_PLT:
54 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +000055 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +000056 case MCSymbolRefExpr::VK_TPOFF:
57 case MCSymbolRefExpr::VK_TLSGD:
58 case MCSymbolRefExpr::VK_GOTTPOFF:
59 case MCSymbolRefExpr::VK_INDNTPOFF:
60 case MCSymbolRefExpr::VK_NTPOFF:
61 case MCSymbolRefExpr::VK_GOTNTPOFF:
62 case MCSymbolRefExpr::VK_TLSLDM:
63 case MCSymbolRefExpr::VK_DTPOFF:
64 case MCSymbolRefExpr::VK_TLSLD:
65 return true;
66 }
67}
68
Jason W Kimd3443e92010-11-15 16:18:39 +000069ELFObjectWriter::~ELFObjectWriter()
70{}
71
Matt Fleming3565a062010-08-16 18:57:57 +000072// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000073void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
74 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000075 // ELF Header
76 // ----------
77 //
78 // Note
79 // ----
80 // emitWord method behaves differently for ELF32 and ELF64, writing
81 // 4 bytes in the former and 8 in the latter.
82
83 Write8(0x7f); // e_ident[EI_MAG0]
84 Write8('E'); // e_ident[EI_MAG1]
85 Write8('L'); // e_ident[EI_MAG2]
86 Write8('F'); // e_ident[EI_MAG3]
87
Rafael Espindolabff66a82010-12-18 03:27:34 +000088 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000089
90 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000091 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +000092
93 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +000094 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +000095 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +000096 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
97 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
98 default: Write8(ELF::ELFOSABI_NONE); break;
99 }
Matt Fleming3565a062010-08-16 18:57:57 +0000100 Write8(0); // e_ident[EI_ABIVERSION]
101
102 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
103
104 Write16(ELF::ET_REL); // e_type
105
Rafael Espindolabff66a82010-12-18 03:27:34 +0000106 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000107
108 Write32(ELF::EV_CURRENT); // e_version
109 WriteWord(0); // e_entry, no entry point in .o file
110 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000111 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000112 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000113
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000114 // e_flags = whatever the target wants
115 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000116
117 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000118 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000119
120 Write16(0); // e_phentsize = prog header entry size
121 Write16(0); // e_phnum = # prog header entries = 0
122
123 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000124 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000125
126 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000127 if (NumberOfSections >= ELF::SHN_LORESERVE)
128 Write16(0);
129 else
130 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000131
132 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000133 if (NumberOfSections >= ELF::SHN_LORESERVE)
134 Write16(ELF::SHN_XINDEX);
135 else
136 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000137}
138
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000139void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
140 MCDataFragment *ShndxF,
141 uint64_t name,
142 uint8_t info, uint64_t value,
143 uint64_t size, uint8_t other,
144 uint32_t shndx,
145 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000146 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000147 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000148 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000149 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000150 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000151 }
152
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000153 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
154 uint16_t(ELF::SHN_XINDEX) : shndx;
155
Rafael Espindolabff66a82010-12-18 03:27:34 +0000156 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000157 String32(*SymtabF, name); // st_name
158 String8(*SymtabF, info); // st_info
159 String8(*SymtabF, other); // st_other
160 String16(*SymtabF, Index); // st_shndx
161 String64(*SymtabF, value); // st_value
162 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000163 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000164 String32(*SymtabF, name); // st_name
165 String32(*SymtabF, value); // st_value
166 String32(*SymtabF, size); // st_size
167 String8(*SymtabF, info); // st_info
168 String8(*SymtabF, other); // st_other
169 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000170 }
171}
172
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000173uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
174 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000175 if (Data.isCommon() && Data.isExternal())
176 return Data.getCommonAlignment();
177
178 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000179
180 if (Symbol.isAbsolute() && Symbol.isVariable()) {
181 if (const MCExpr *Value = Symbol.getVariableValue()) {
182 int64_t IntValue;
183 if (Value->EvaluateAsAbsolute(IntValue, Layout))
184 return (uint64_t)IntValue;
185 }
186 }
187
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000188 if (!Symbol.isInSection())
189 return 0;
190
Rafael Espindola64695402011-05-16 16:17:21 +0000191
192 if (Data.getFragment()) {
193 if (Data.getFlags() & ELF_Other_ThumbFunc)
194 return Layout.getSymbolOffset(&Data)+1;
195 else
196 return Layout.getSymbolOffset(&Data);
197 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000198
199 return 0;
200}
201
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000202void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
203 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000204 // The presence of symbol versions causes undefined symbols and
205 // versions declared with @@@ to be renamed.
206
207 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
208 ie = Asm.symbol_end(); it != ie; ++it) {
209 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000210 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000211 MCSymbolData &SD = Asm.getSymbolData(Symbol);
212
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000213 // Not an alias.
214 if (&Symbol == &Alias)
215 continue;
216
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000217 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000218 size_t Pos = AliasName.find('@');
219 if (Pos == StringRef::npos)
220 continue;
221
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000222 // Aliases defined with .symvar copy the binding from the symbol they alias.
223 // This is the first place we are able to copy this information.
224 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000225 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000226
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000227 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000228 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
229 continue;
230
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000231 // FIXME: produce a better error message.
232 if (Symbol.isUndefined() && Rest.startswith("@@") &&
233 !Rest.startswith("@@@"))
234 report_fatal_error("A @@ version cannot be undefined");
235
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000236 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000237 }
238}
239
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000240void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
241 MCDataFragment *ShndxF,
242 ELFSymbolData &MSD,
243 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000244 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000245 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000246 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000247
Rafael Espindola7be2c332010-10-31 00:16:26 +0000248 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
249 Data.getSymbol().isVariable();
250
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000251 uint8_t Binding = MCELF::GetBinding(OrigData);
252 uint8_t Visibility = MCELF::GetVisibility(OrigData);
253 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000254
255 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
256 uint8_t Other = Visibility;
257
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000258 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000259 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000260
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000261 assert(!(Data.isCommon() && !Data.isExternal()));
262
Rafael Espindolaf0121242010-12-22 16:03:00 +0000263 const MCExpr *ESize = Data.getSize();
264 if (ESize) {
265 int64_t Res;
266 if (!ESize->EvaluateAsAbsolute(Res, Layout))
267 report_fatal_error("Size expression must be absolute.");
268 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000269 }
270
271 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000272 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
273 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000274}
275
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000276void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
277 MCDataFragment *ShndxF,
278 const MCAssembler &Asm,
279 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000280 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000281 // The string table must be emitted first because we need the index
282 // into the string table for all the symbol names.
283 assert(StringTable.size() && "Missing string table");
284
285 // FIXME: Make sure the start of the symbol table is aligned.
286
287 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000288 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000289
290 // Write the symbol table entries.
291 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
292 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
293 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000294 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000295 }
296
Rafael Espindola71859c62010-09-16 19:46:31 +0000297 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000298 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
299 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000300 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000301 static_cast<const MCSectionELF&>(i->getSection());
302 if (Section.getType() == ELF::SHT_RELA ||
303 Section.getType() == ELF::SHT_REL ||
304 Section.getType() == ELF::SHT_STRTAB ||
305 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000306 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000307 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000308 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000309 LastLocalSymbolIndex++;
310 }
Matt Fleming3565a062010-08-16 18:57:57 +0000311
312 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
313 ELFSymbolData &MSD = ExternalSymbolData[i];
314 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000315 assert(((Data.getFlags() & ELF_STB_Global) ||
316 (Data.getFlags() & ELF_STB_Weak)) &&
317 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000318 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000319 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000320 LastLocalSymbolIndex++;
321 }
322
323 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
324 ELFSymbolData &MSD = UndefinedSymbolData[i];
325 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000326 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000327 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000328 LastLocalSymbolIndex++;
329 }
330}
331
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000332const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
333 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000334 const MCFragment &F,
335 const MCFixup &Fixup,
336 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000337 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000338 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
339 const MCSymbol *Renamed = Renames.lookup(&Symbol);
340 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000341
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000342 if (ASymbol.isUndefined()) {
343 if (Renamed)
344 return Renamed;
345 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000346 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000347
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000348 if (SD.isExternal()) {
349 if (Renamed)
350 return Renamed;
351 return &Symbol;
352 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000353
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000354 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000355 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000356 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000357
Rafael Espindola25958732010-11-24 21:57:39 +0000358 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000359 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000360
Rafael Espindola25958732010-11-24 21:57:39 +0000361 if (secKind.isThreadLocal()) {
362 if (Renamed)
363 return Renamed;
364 return &Symbol;
365 }
366
Rafael Espindola8cecf252010-10-06 16:23:36 +0000367 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000368 const MCSectionELF &Sec2 =
369 static_cast<const MCSectionELF&>(F.getParent()->getSection());
370
Rafael Espindola8cecf252010-10-06 16:23:36 +0000371 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000372 (Kind == MCSymbolRefExpr::VK_PLT ||
373 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000374 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000375 if (Renamed)
376 return Renamed;
377 return &Symbol;
378 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000379
Rafael Espindola1c130262011-01-23 04:43:11 +0000380 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000381 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000382 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000383 if (Renamed)
384 return Renamed;
385 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000386 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000387
Jason W Kime964d112011-05-11 22:53:06 +0000388 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
389
Rafael Espindola73ffea42010-09-25 05:42:19 +0000390}
391
Matt Fleming3565a062010-08-16 18:57:57 +0000392
Jason W Kim56a39902010-12-06 21:57:34 +0000393void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
394 const MCAsmLayout &Layout,
395 const MCFragment *Fragment,
396 const MCFixup &Fixup,
397 MCValue Target,
398 uint64_t &FixedValue) {
399 int64_t Addend = 0;
400 int Index = 0;
401 int64_t Value = Target.getConstant();
402 const MCSymbol *RelocSymbol = NULL;
403
Rafael Espindola127a6a42010-12-17 07:28:17 +0000404 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000405 if (!Target.isAbsolute()) {
406 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
407 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000408 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000409
410 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
411 const MCSymbol &SymbolB = RefB->getSymbol();
412 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
413 IsPCRel = true;
414
415 // Offset of the symbol in the section
416 int64_t a = Layout.getSymbolOffset(&SDB);
417
418 // Ofeset of the relocation in the section
419 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
420 Value += b - a;
421 }
422
423 if (!RelocSymbol) {
424 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
425 MCFragment *F = SD.getFragment();
426
427 Index = F->getParent()->getOrdinal() + 1;
428
429 // Offset of the symbol in the section
430 Value += Layout.getSymbolOffset(&SD);
431 } else {
432 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
433 WeakrefUsedInReloc.insert(RelocSymbol);
434 else
435 UsedInReloc.insert(RelocSymbol);
436 Index = -1;
437 }
438 Addend = Value;
439 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000440 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000441 Value = 0;
442 }
443
444 FixedValue = Value;
445 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
446 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000447
Jason W Kim56a39902010-12-06 21:57:34 +0000448 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
449 Fixup.getOffset();
Roman Divackyc9c0cc12011-08-02 16:15:32 +0000450#if 0
451 // TODO: This is necessary on PPC32 but it must be implemented
452 // in a different way.
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000453 switch ((unsigned)Fixup.getKind()) {
454 case PPC::fixup_ppc_ha16:
455 case PPC::fixup_ppc_lo16:
456 RelocOffset += 2;
457 break;
458 default:
459 break;
460 }
Roman Divackyc9c0cc12011-08-02 16:15:32 +0000461#endif
Jason W Kim56a39902010-12-06 21:57:34 +0000462
Rafael Espindolabff66a82010-12-18 03:27:34 +0000463 if (!hasRelocationAddend())
464 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000465 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
466 Relocations[Fragment->getParent()].push_back(ERE);
467}
468
469
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000470uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000471ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
472 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000473 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000474 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000475}
476
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000477bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
478 const MCSymbolData &Data,
479 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000480 if (Data.getFlags() & ELF_Other_Weakref)
481 return false;
482
Rafael Espindolabd701182010-10-19 19:31:37 +0000483 if (Used)
484 return true;
485
Rafael Espindola88182132010-10-27 15:18:17 +0000486 if (Renamed)
487 return false;
488
Rafael Espindola737cd212010-10-05 18:01:23 +0000489 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000490
Rafael Espindolad1798862010-10-29 23:09:31 +0000491 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
492 return true;
493
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000494 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000495 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
496 return false;
497
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000498 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000499 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000500 return false;
501
Rafael Espindola737cd212010-10-05 18:01:23 +0000502 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
503 return false;
504
Rafael Espindolabd701182010-10-19 19:31:37 +0000505 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000506 return false;
507
508 return true;
509}
510
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000511bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
512 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000513 if (Data.isExternal())
514 return false;
515
516 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000517 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000518
519 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
520 if (isSignature && !isUsedInReloc)
521 return true;
522
Rafael Espindola737cd212010-10-05 18:01:23 +0000523 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000524 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000525
526 return true;
527}
528
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000529void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000530 SectionIndexMapTy &SectionIndexMap,
531 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000532 unsigned Index = 1;
533 for (MCAssembler::iterator it = Asm.begin(),
534 ie = Asm.end(); it != ie; ++it) {
535 const MCSectionELF &Section =
536 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000537 if (Section.getType() != ELF::SHT_GROUP)
538 continue;
539 SectionIndexMap[&Section] = Index++;
540 }
541
542 for (MCAssembler::iterator it = Asm.begin(),
543 ie = Asm.end(); it != ie; ++it) {
544 const MCSectionELF &Section =
545 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000546 if (Section.getType() == ELF::SHT_GROUP ||
547 Section.getType() == ELF::SHT_REL ||
548 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000549 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000550 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000551 const MCSectionELF *RelSection = RelMap.lookup(&Section);
552 if (RelSection)
553 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000554 }
555}
556
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000557void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000558 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000559 RevGroupMapTy RevGroupMap,
560 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000561 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000562 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000563 if (NeedsGOT) {
564 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
565 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
566 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
567 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000568 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000569 }
570
Matt Fleming3565a062010-08-16 18:57:57 +0000571 // Index 0 is always the empty string.
572 StringMap<uint64_t> StringIndexMap;
573 StringTable += '\x00';
574
Rafael Espindolad5321da2011-04-07 23:21:52 +0000575 // FIXME: We could optimize suffixes in strtab in the same way we
576 // optimize them in shstrtab.
577
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000578 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000579 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
580 ie = Asm.symbol_end(); it != ie; ++it) {
581 const MCSymbol &Symbol = it->getSymbol();
582
Rafael Espindola484291c2010-11-01 14:28:48 +0000583 bool Used = UsedInReloc.count(&Symbol);
584 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000585 bool isSignature = RevGroupMap.count(&Symbol);
586
587 if (!isInSymtab(Asm, *it,
588 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000589 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000590 continue;
591
Matt Fleming3565a062010-08-16 18:57:57 +0000592 ELFSymbolData MSD;
593 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000594 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000595
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000596 // Undefined symbols are global, but this is the first place we
597 // are able to set it.
598 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000599 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000600 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000601 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
602 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000603 }
604
Rafael Espindola484291c2010-11-01 14:28:48 +0000605 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000606 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000607
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000608 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000609 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000610 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000611 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000612 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000613 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000614 if (isSignature && !Used)
615 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
616 else
617 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000618 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000619 const MCSectionELF &Section =
620 static_cast<const MCSectionELF&>(RefSymbol.getSection());
621 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000622 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
623 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000624 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000625 }
626
Rafael Espindola88182132010-10-27 15:18:17 +0000627 // The @@@ in symbol version is replaced with @ in undefined symbols and
628 // @@ in defined ones.
629 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000630 SmallString<32> Buf;
631
Rafael Espindola88182132010-10-27 15:18:17 +0000632 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000633 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000634 Buf += Name.substr(0, Pos);
635 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
636 Buf += Name.substr(Pos + Skip);
637 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000638 }
639
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000640 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000641 if (!Entry) {
642 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000643 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000644 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000645 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000646 MSD.StringIndex = Entry;
647 if (MSD.SectionIndex == ELF::SHN_UNDEF)
648 UndefinedSymbolData.push_back(MSD);
649 else if (Local)
650 LocalSymbolData.push_back(MSD);
651 else
652 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000653 }
654
655 // Symbols are required to be in lexicographic order.
656 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
657 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
658 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
659
660 // Set the symbol indices. Local symbols must come before all other
661 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000662 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000663 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
664 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000665
666 Index += NumRegularSections;
667
Matt Fleming3565a062010-08-16 18:57:57 +0000668 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
669 ExternalSymbolData[i].SymbolData->setIndex(Index++);
670 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
671 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
672}
673
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000674void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
675 MCAsmLayout &Layout,
676 RelMapTy &RelMap) {
677 for (MCAssembler::const_iterator it = Asm.begin(),
678 ie = Asm.end(); it != ie; ++it) {
679 const MCSectionData &SD = *it;
680 if (Relocations[&SD].empty())
681 continue;
682
Matt Fleming3565a062010-08-16 18:57:57 +0000683 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000684 const MCSectionELF &Section =
685 static_cast<const MCSectionELF&>(SD.getSection());
686
687 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000688 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000689 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000690
691 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000692 if (hasRelocationAddend())
693 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000694 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000695 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000696
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000697 const MCSectionELF *RelaSection =
698 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
699 ELF::SHT_RELA : ELF::SHT_REL, 0,
700 SectionKind::getReadOnly(),
701 EntrySize, "");
702 RelMap[&Section] = RelaSection;
703 Asm.getOrCreateSectionData(*RelaSection);
704 }
705}
Matt Fleming3565a062010-08-16 18:57:57 +0000706
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000707void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
708 const RelMapTy &RelMap) {
709 for (MCAssembler::const_iterator it = Asm.begin(),
710 ie = Asm.end(); it != ie; ++it) {
711 const MCSectionData &SD = *it;
712 const MCSectionELF &Section =
713 static_cast<const MCSectionELF&>(SD.getSection());
714
715 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
716 if (!RelaSection)
717 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000718 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000719 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000720
721 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000722 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000723 }
724}
725
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000726void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
727 uint64_t Flags, uint64_t Address,
728 uint64_t Offset, uint64_t Size,
729 uint32_t Link, uint32_t Info,
730 uint64_t Alignment,
731 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000732 Write32(Name); // sh_name: index into string table
733 Write32(Type); // sh_type
734 WriteWord(Flags); // sh_flags
735 WriteWord(Address); // sh_addr
736 WriteWord(Offset); // sh_offset
737 WriteWord(Size); // sh_size
738 Write32(Link); // sh_link
739 Write32(Info); // sh_info
740 WriteWord(Alignment); // sh_addralign
741 WriteWord(EntrySize); // sh_entsize
742}
743
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000744void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
745 MCDataFragment *F,
746 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000747 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
748 // sort by the r_offset just like gnu as does
749 array_pod_sort(Relocs.begin(), Relocs.end());
750
751 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
752 ELFRelocationEntry entry = Relocs[e - i - 1];
753
Rafael Espindola12203cc2010-11-21 00:48:25 +0000754 if (!entry.Index)
755 ;
756 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000757 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
758 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000759 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000760 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000761 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000762
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000763 struct ELF::Elf64_Rela ERE64;
764 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000765 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000766
Rafael Espindolabff66a82010-12-18 03:27:34 +0000767 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000768 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000769 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000770 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000771
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000772 struct ELF::Elf32_Rela ERE32;
773 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000774 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000775
Rafael Espindolabff66a82010-12-18 03:27:34 +0000776 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000777 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000778 }
Matt Fleming3565a062010-08-16 18:57:57 +0000779 }
780}
781
Rafael Espindolad5321da2011-04-07 23:21:52 +0000782static int compareBySuffix(const void *a, const void *b) {
783 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
784 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
785 const StringRef &NameA = secA->getSectionName();
786 const StringRef &NameB = secB->getSectionName();
787 const unsigned sizeA = NameA.size();
788 const unsigned sizeB = NameB.size();
789 const unsigned len = std::min(sizeA, sizeB);
790 for (unsigned int i = 0; i < len; ++i) {
791 char ca = NameA[sizeA - i - 1];
792 char cb = NameB[sizeB - i - 1];
793 if (ca != cb)
794 return cb - ca;
795 }
796
797 return sizeB - sizeA;
798}
799
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000800void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
801 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000802 SectionIndexMapTy &SectionIndexMap,
803 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000804 MCContext &Ctx = Asm.getContext();
805 MCDataFragment *F;
806
Rafael Espindolabff66a82010-12-18 03:27:34 +0000807 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000808
Rafael Espindola38738bf2010-09-22 19:04:41 +0000809 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000810 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000811 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000812 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000813 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
814 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000815
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000816 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000817 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
818 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000819 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000820 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000821 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000822
823 MCSectionData *SymtabShndxSD = NULL;
824
825 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000826 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000827 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000828 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000829 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
830 SymtabShndxSD->setAlignment(4);
831 }
Matt Fleming3565a062010-08-16 18:57:57 +0000832
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000833 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000834 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000835 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000836 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
837 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000838
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000839 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
840
841 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
842 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
843 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000844
845 // Symbol table
846 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000847 MCDataFragment *ShndxF = NULL;
848 if (NeedsSymtabShndx) {
849 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000850 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000851 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000852
Matt Fleming3565a062010-08-16 18:57:57 +0000853 F = new MCDataFragment(&StrtabSD);
854 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000855
Matt Fleming3565a062010-08-16 18:57:57 +0000856 F = new MCDataFragment(&ShstrtabSD);
857
Rafael Espindolad5321da2011-04-07 23:21:52 +0000858 std::vector<const MCSectionELF*> Sections;
859 for (MCAssembler::const_iterator it = Asm.begin(),
860 ie = Asm.end(); it != ie; ++it) {
861 const MCSectionELF &Section =
862 static_cast<const MCSectionELF&>(it->getSection());
863 Sections.push_back(&Section);
864 }
865 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
866
Matt Fleming3565a062010-08-16 18:57:57 +0000867 // Section header string table.
868 //
869 // The first entry of a string table holds a null character so skip
870 // section 0.
871 uint64_t Index = 1;
872 F->getContents() += '\x00';
873
Rafael Espindolad5321da2011-04-07 23:21:52 +0000874 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
875 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000876
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000877 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000878 if (I != 0) {
879 StringRef PreviousName = Sections[I - 1]->getSectionName();
880 if (PreviousName.endswith(Name)) {
881 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
882 continue;
883 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000884 }
Matt Fleming3565a062010-08-16 18:57:57 +0000885 // Remember the index into the string table so we can write it
886 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000887 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000888
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000889 Index += Name.size() + 1;
890 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000891 F->getContents() += '\x00';
892 }
Rafael Espindola70703872010-09-30 02:22:20 +0000893}
894
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000895void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
896 MCAsmLayout &Layout,
897 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000898 RevGroupMapTy &RevGroupMap,
899 SectionIndexMapTy &SectionIndexMap,
900 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000901 // Create the .note.GNU-stack section if needed.
902 MCContext &Ctx = Asm.getContext();
903 if (Asm.getNoExecStack()) {
904 const MCSectionELF *GnuStackSection =
905 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
906 SectionKind::getReadOnly());
907 Asm.getOrCreateSectionData(*GnuStackSection);
908 }
909
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000910 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000911 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
912 it != ie; ++it) {
913 const MCSectionELF &Section =
914 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000915 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000916 continue;
917
918 const MCSymbol *SignatureSymbol = Section.getGroup();
919 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000920 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000921 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000922 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000923 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
924 Data.setAlignment(4);
925 MCDataFragment *F = new MCDataFragment(&Data);
926 String32(*F, ELF::GRP_COMDAT);
927 }
928 GroupMap[Group] = SignatureSymbol;
929 }
930
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000931 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
932
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000933 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000934 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000935 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000936 const MCSectionELF &Section =
937 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000938 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000939 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000940 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000941 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
942 // FIXME: we could use the previous fragment
943 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000944 unsigned Index = SectionIndexMap.lookup(&Section);
945 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000946 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000947}
948
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000949void ELFObjectWriter::WriteSection(MCAssembler &Asm,
950 const SectionIndexMapTy &SectionIndexMap,
951 uint32_t GroupSymbolIndex,
952 uint64_t Offset, uint64_t Size,
953 uint64_t Alignment,
954 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000955 uint64_t sh_link = 0;
956 uint64_t sh_info = 0;
957
958 switch(Section.getType()) {
959 case ELF::SHT_DYNAMIC:
960 sh_link = SectionStringTableIndex[&Section];
961 sh_info = 0;
962 break;
963
964 case ELF::SHT_REL:
965 case ELF::SHT_RELA: {
966 const MCSectionELF *SymtabSection;
967 const MCSectionELF *InfoSection;
968 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
969 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000970 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000971 sh_link = SectionIndexMap.lookup(SymtabSection);
972 assert(sh_link && ".symtab not found");
973
974 // Remove ".rel" and ".rela" prefixes.
975 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
976 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
977
978 InfoSection = Asm.getContext().getELFSection(SectionName,
979 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000980 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000981 sh_info = SectionIndexMap.lookup(InfoSection);
982 break;
983 }
984
985 case ELF::SHT_SYMTAB:
986 case ELF::SHT_DYNSYM:
987 sh_link = StringTableIndex;
988 sh_info = LastLocalSymbolIndex;
989 break;
990
991 case ELF::SHT_SYMTAB_SHNDX:
992 sh_link = SymbolTableIndex;
993 break;
994
995 case ELF::SHT_PROGBITS:
996 case ELF::SHT_STRTAB:
997 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000998 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000999 case ELF::SHT_NULL:
1000 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001001 case ELF::SHT_INIT_ARRAY:
1002 case ELF::SHT_FINI_ARRAY:
1003 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001004 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001005 // Nothing to do.
1006 break;
1007
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001008 case ELF::SHT_GROUP: {
1009 sh_link = SymbolTableIndex;
1010 sh_info = GroupSymbolIndex;
1011 break;
1012 }
1013
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001014 default:
1015 assert(0 && "FIXME: sh_type value not supported!");
1016 break;
1017 }
1018
1019 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1020 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1021 Alignment, Section.getEntrySize());
1022}
1023
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001024bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001025 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001026 !SD.getSection().isVirtualSection();
1027}
1028
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001029uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001030 uint64_t Ret = 0;
1031 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1032 ++i) {
1033 const MCFragment &F = *i;
1034 assert(F.getKind() == MCFragment::FT_Data);
1035 Ret += cast<MCDataFragment>(F).getContents().size();
1036 }
1037 return Ret;
1038}
1039
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001040uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1041 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001042 if (IsELFMetaDataSection(SD))
1043 return DataSectionSize(SD);
1044 return Layout.getSectionFileSize(&SD);
1045}
1046
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001047uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1048 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001049 if (IsELFMetaDataSection(SD))
1050 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001051 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001052}
1053
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001054void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1055 const MCAsmLayout &Layout,
1056 const MCSectionELF &Section) {
1057 uint64_t FileOff = OS.tell();
1058 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1059
1060 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1061 WriteZeros(Padding);
1062 FileOff += Padding;
1063
1064 FileOff += GetSectionFileSize(Layout, SD);
1065
1066 if (IsELFMetaDataSection(SD)) {
1067 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1068 ++i) {
1069 const MCFragment &F = *i;
1070 assert(F.getKind() == MCFragment::FT_Data);
1071 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1072 }
1073 } else {
1074 Asm.WriteSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001075 }
1076}
1077
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001078void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1079 const GroupMapTy &GroupMap,
1080 const MCAsmLayout &Layout,
1081 const SectionIndexMapTy &SectionIndexMap,
1082 const SectionOffsetMapTy &SectionOffsetMap) {
1083 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001084
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001085 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001086 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001087
1088 for (SectionIndexMapTy::const_iterator i=
1089 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1090 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001091 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001092 }
1093
Matt Fleming3565a062010-08-16 18:57:57 +00001094 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001095 uint64_t FirstSectionSize =
1096 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1097 uint32_t FirstSectionLink =
1098 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1099 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001100
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001101 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001102 const MCSectionELF &Section = *Sections[i];
1103 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1104 uint32_t GroupSymbolIndex;
1105 if (Section.getType() != ELF::SHT_GROUP)
1106 GroupSymbolIndex = 0;
1107 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001108 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1109 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001110
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001111 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001112
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001113 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001114 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001115 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001116 }
1117}
1118
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001119void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1120 std::vector<const MCSectionELF*> &Sections) {
1121 for (MCAssembler::iterator it = Asm.begin(),
1122 ie = Asm.end(); it != ie; ++it) {
1123 const MCSectionELF &Section =
1124 static_cast<const MCSectionELF &>(it->getSection());
1125 if (Section.getType() == ELF::SHT_GROUP)
1126 Sections.push_back(&Section);
1127 }
1128
1129 for (MCAssembler::iterator it = Asm.begin(),
1130 ie = Asm.end(); it != ie; ++it) {
1131 const MCSectionELF &Section =
1132 static_cast<const MCSectionELF &>(it->getSection());
1133 if (Section.getType() != ELF::SHT_GROUP &&
1134 Section.getType() != ELF::SHT_REL &&
1135 Section.getType() != ELF::SHT_RELA)
1136 Sections.push_back(&Section);
1137 }
1138
1139 for (MCAssembler::iterator it = Asm.begin(),
1140 ie = Asm.end(); it != ie; ++it) {
1141 const MCSectionELF &Section =
1142 static_cast<const MCSectionELF &>(it->getSection());
1143 if (Section.getType() == ELF::SHT_REL ||
1144 Section.getType() == ELF::SHT_RELA)
1145 Sections.push_back(&Section);
1146 }
1147}
1148
1149void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1150 const MCAsmLayout &Layout) {
1151 GroupMapTy GroupMap;
1152 RevGroupMapTy RevGroupMap;
1153 SectionIndexMapTy SectionIndexMap;
1154
1155 unsigned NumUserSections = Asm.size();
1156
1157 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1158 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1159
1160 const unsigned NumUserAndRelocSections = Asm.size();
1161 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1162 RevGroupMap, SectionIndexMap, RelMap);
1163 const unsigned AllSections = Asm.size();
1164 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1165
1166 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1167
1168 // Compute symbol table information.
1169 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1170
1171
1172 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1173
1174 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1175 const_cast<MCAsmLayout&>(Layout),
1176 SectionIndexMap,
1177 RelMap);
1178
1179 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1180 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1181 sizeof(ELF::Elf32_Ehdr);
1182 uint64_t FileOff = HeaderSize;
1183
1184 std::vector<const MCSectionELF*> Sections;
1185 ComputeSectionOrder(Asm, Sections);
1186 unsigned NumSections = Sections.size();
1187 SectionOffsetMapTy SectionOffsetMap;
1188 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1189 const MCSectionELF &Section = *Sections[i];
1190 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1191
1192 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1193
1194 // Remember the offset into the file for this section.
1195 SectionOffsetMap[&Section] = FileOff;
1196
1197 // Get the size of the section in the output file (including padding).
1198 FileOff += GetSectionFileSize(Layout, SD);
1199 }
1200
1201 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1202
1203 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1204
1205 uint64_t SectionHeaderEntrySize = is64Bit() ?
1206 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1207 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1208
1209 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1210 const MCSectionELF &Section = *Sections[i];
1211 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1212
1213 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1214
1215 // Remember the offset into the file for this section.
1216 SectionOffsetMap[&Section] = FileOff;
1217
1218 // Get the size of the section in the output file (including padding).
1219 FileOff += GetSectionFileSize(Layout, SD);
1220 }
1221
1222 // Write out the ELF header ...
1223 WriteHeader(SectionHeaderOffset, NumSections + 1);
1224
1225 // ... then the regular sections ...
1226 // + because of .shstrtab
1227 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1228 WriteDataSectionData(Asm, Layout, *Sections[i]);
1229
1230 FileOff = OS.tell();
1231 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1232 WriteZeros(Padding);
1233
1234 // ... then the section header table ...
1235 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1236 SectionOffsetMap);
1237
1238 FileOff = OS.tell();
1239
1240 // ... and then the remainting sections ...
1241 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1242 WriteDataSectionData(Asm, Layout, *Sections[i]);
1243}
1244
Eli Friedman78c1e172011-03-03 07:24:36 +00001245bool
1246ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1247 const MCSymbolData &DataA,
1248 const MCFragment &FB,
1249 bool InSet,
1250 bool IsPCRel) const {
1251 if (DataA.getFlags() & ELF_STB_Weak)
1252 return false;
1253 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1254 Asm, DataA, FB,InSet, IsPCRel);
1255}
1256
Rafael Espindola6024c972010-12-17 17:45:22 +00001257MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1258 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001259 bool IsLittleEndian) {
1260 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001261 case ELF::EM_386:
1262 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001263 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001264 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001265 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001266 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001267 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001268 case ELF::EM_PPC:
1269 case ELF::EM_PPC64:
1270 return new PPCELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001271 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001272 }
1273}
1274
1275
1276/// START OF SUBCLASSES for ELFObjectWriter
1277//===- ARMELFObjectWriter -------------------------------------------===//
1278
Rafael Espindola31f35782010-12-17 18:01:31 +00001279ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001280 raw_ostream &_OS,
1281 bool IsLittleEndian)
1282 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001283{}
1284
1285ARMELFObjectWriter::~ARMELFObjectWriter()
1286{}
1287
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001288// FIXME: get the real EABI Version from the Triple.
1289void ARMELFObjectWriter::WriteEFlags() {
1290 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1291}
1292
Jason W Kim953a2a32011-02-07 01:11:15 +00001293// In ARM, _MergedGlobals and other most symbols get emitted directly.
1294// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001295// This code is an approximation of what ARM/gcc does.
1296
1297STATISTIC(PCRelCount, "Total number of PIC Relocations");
1298STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001299
1300const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1301 const MCValue &Target,
1302 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001303 const MCFixup &Fixup,
1304 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001305 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1306 bool EmitThisSym = false;
1307
Jason W Kime964d112011-05-11 22:53:06 +00001308 const MCSectionELF &Section =
1309 static_cast<const MCSectionELF&>(Symbol.getSection());
Jason W Kime964d112011-05-11 22:53:06 +00001310 bool InNormalSection = true;
1311 unsigned RelocType = 0;
1312 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1313
Matt Beaumont-Gay6dcd4132011-05-11 23:34:51 +00001314 DEBUG(
1315 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1316 MCSymbolRefExpr::VariantKind Kind2;
1317 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1318 MCSymbolRefExpr::VK_None;
1319 dbgs() << "considering symbol "
Jason W Kime964d112011-05-11 22:53:06 +00001320 << Section.getSectionName() << "/"
1321 << Symbol.getName() << "/"
1322 << " Rel:" << (unsigned)RelocType
1323 << " Kind: " << (int)Kind << "/" << (int)Kind2
1324 << " Tmp:"
1325 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1326 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1327 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1328
Jason W Kimbebd44a2011-06-09 19:13:45 +00001329 if (IsPCRel) { ++PCRelCount;
Jason W Kime964d112011-05-11 22:53:06 +00001330 switch (RelocType) {
1331 default:
1332 // Most relocation types are emitted as explicit symbols
1333 InNormalSection =
1334 StringSwitch<bool>(Section.getSectionName())
1335 .Case(".data.rel.ro.local", false)
1336 .Case(".data.rel", false)
1337 .Case(".bss", false)
1338 .Default(true);
1339 EmitThisSym = true;
1340 break;
1341 case ELF::R_ARM_ABS32:
1342 // But things get strange with R_ARM_ABS32
1343 // In this case, most things that go in .rodata show up
1344 // as section relative relocations
1345 InNormalSection =
1346 StringSwitch<bool>(Section.getSectionName())
1347 .Case(".data.rel.ro.local", false)
1348 .Case(".data.rel", false)
1349 .Case(".rodata", false)
1350 .Case(".bss", false)
1351 .Default(true);
1352 EmitThisSym = false;
1353 break;
1354 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001355 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001356 NonPCRelCount++;
1357 InNormalSection =
1358 StringSwitch<bool>(Section.getSectionName())
1359 .Case(".data.rel.ro.local", false)
1360 .Case(".rodata", false)
1361 .Case(".data.rel", false)
1362 .Case(".bss", false)
1363 .Default(true);
1364
1365 switch (RelocType) {
1366 default: EmitThisSym = true; break;
1367 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1368 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001369 }
Jason W Kime964d112011-05-11 22:53:06 +00001370
Jason W Kim953a2a32011-02-07 01:11:15 +00001371 if (EmitThisSym)
1372 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001373 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001374 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001375 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001376 return NULL;
1377}
1378
Jason W Kime964d112011-05-11 22:53:06 +00001379// Need to examine the Fixup when determining whether to
1380// emit the relocation as an explicit symbol or as a section relative
1381// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001382unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1383 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001384 bool IsPCRel,
1385 bool IsRelocWithSymbol,
1386 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001387 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1388 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1389
Jason W Kime964d112011-05-11 22:53:06 +00001390 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1391
1392 if (RelocNeedsGOT(Modifier))
1393 NeedsGOT = true;
1394
1395 return Type;
1396}
1397
1398unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1399 const MCFixup &Fixup,
1400 bool IsPCRel) const {
1401 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1402 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1403
Jason W Kima0871e72010-12-08 23:14:44 +00001404 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001405 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001406 switch ((unsigned)Fixup.getKind()) {
1407 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001408 case FK_Data_4:
1409 switch (Modifier) {
1410 default: llvm_unreachable("Unsupported Modifier");
1411 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001412 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001413 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001414 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001415 assert(0 && "unimplemented");
1416 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001417 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1418 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001419 break;
1420 }
1421 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001422 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001423 switch (Modifier) {
1424 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001425 Type = ELF::R_ARM_PLT32;
1426 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001427 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001428 Type = ELF::R_ARM_CALL;
1429 break;
1430 }
1431 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001432 case ARM::fixup_arm_condbranch:
1433 Type = ELF::R_ARM_JUMP24;
1434 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001435 case ARM::fixup_arm_movt_hi16:
1436 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001437 Type = ELF::R_ARM_MOVT_PREL;
1438 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001439 case ARM::fixup_arm_movw_lo16:
1440 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001441 Type = ELF::R_ARM_MOVW_PREL_NC;
1442 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001443 case ARM::fixup_t2_movt_hi16:
1444 case ARM::fixup_t2_movt_hi16_pcrel:
1445 Type = ELF::R_ARM_THM_MOVT_PREL;
1446 break;
1447 case ARM::fixup_t2_movw_lo16:
1448 case ARM::fixup_t2_movw_lo16_pcrel:
1449 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1450 break;
Rafael Espindola298c8e12011-05-20 20:01:01 +00001451 case ARM::fixup_arm_thumb_bl:
1452 case ARM::fixup_arm_thumb_blx:
1453 switch (Modifier) {
1454 case MCSymbolRefExpr::VK_ARM_PLT:
1455 Type = ELF::R_ARM_THM_CALL;
1456 break;
1457 default:
1458 Type = ELF::R_ARM_NONE;
1459 break;
1460 }
1461 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001462 }
1463 } else {
1464 switch ((unsigned)Fixup.getKind()) {
1465 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001466 case FK_Data_4:
1467 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001468 default: llvm_unreachable("Unsupported Modifier"); break;
1469 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001470 Type = ELF::R_ARM_GOT_BREL;
1471 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001472 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001473 Type = ELF::R_ARM_TLS_GD32;
1474 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001475 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001476 Type = ELF::R_ARM_TLS_LE32;
1477 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001478 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001479 Type = ELF::R_ARM_TLS_IE32;
1480 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001481 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001482 Type = ELF::R_ARM_ABS32;
1483 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001484 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001485 Type = ELF::R_ARM_GOTOFF32;
1486 break;
1487 }
1488 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001489 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001490 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001491 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001492 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001493 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001494 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001495 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001496 assert(0 && "Unimplemented");
1497 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001498 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001499 Type = ELF::R_ARM_CALL;
1500 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001501 case ARM::fixup_arm_condbranch:
1502 Type = ELF::R_ARM_JUMP24;
1503 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001504 case ARM::fixup_arm_movt_hi16:
1505 Type = ELF::R_ARM_MOVT_ABS;
1506 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001507 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001508 Type = ELF::R_ARM_MOVW_ABS_NC;
1509 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001510 case ARM::fixup_t2_movt_hi16:
1511 Type = ELF::R_ARM_THM_MOVT_ABS;
1512 break;
1513 case ARM::fixup_t2_movw_lo16:
1514 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1515 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001516 }
1517 }
1518
Jason W Kima0871e72010-12-08 23:14:44 +00001519 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001520}
1521
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001522//===- PPCELFObjectWriter -------------------------------------------===//
1523
1524PPCELFObjectWriter::PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1525 raw_ostream &_OS,
1526 bool IsLittleEndian)
1527 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1528}
1529
1530PPCELFObjectWriter::~PPCELFObjectWriter() {
1531}
1532
1533unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
1534 const MCFixup &Fixup,
1535 bool IsPCRel,
1536 bool IsRelocWithSymbol,
1537 int64_t Addend) {
1538 // determine the type of the relocation
1539 unsigned Type;
1540 if (IsPCRel) {
1541 switch ((unsigned)Fixup.getKind()) {
1542 default:
1543 llvm_unreachable("Unimplemented");
1544 case PPC::fixup_ppc_br24:
1545 Type = ELF::R_PPC_REL24;
1546 break;
1547 case FK_PCRel_4:
1548 Type = ELF::R_PPC_REL32;
1549 break;
1550 }
1551 } else {
1552 switch ((unsigned)Fixup.getKind()) {
1553 default: llvm_unreachable("invalid fixup kind!");
1554 case PPC::fixup_ppc_br24:
1555 Type = ELF::R_PPC_ADDR24;
1556 break;
1557 case PPC::fixup_ppc_brcond14:
1558 Type = ELF::R_PPC_ADDR14_BRTAKEN; // XXX: or BRNTAKEN?_
1559 break;
1560 case PPC::fixup_ppc_ha16:
1561 Type = ELF::R_PPC_ADDR16_HA;
1562 break;
1563 case PPC::fixup_ppc_lo16:
1564 Type = ELF::R_PPC_ADDR16_LO;
1565 break;
1566 case PPC::fixup_ppc_lo14:
1567 Type = ELF::R_PPC_ADDR14;
1568 break;
1569 case FK_Data_4:
1570 Type = ELF::R_PPC_ADDR32;
1571 break;
1572 case FK_Data_2:
1573 Type = ELF::R_PPC_ADDR16;
1574 break;
1575 }
1576 }
1577 return Type;
1578}
1579
Wesley Peck4b047132010-11-21 22:06:28 +00001580//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001581
Rafael Espindola31f35782010-12-17 18:01:31 +00001582MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001583 raw_ostream &_OS,
1584 bool IsLittleEndian)
1585 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001586}
1587
1588MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1589}
1590
Jason W Kim56a39902010-12-06 21:57:34 +00001591unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001592 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001593 bool IsPCRel,
1594 bool IsRelocWithSymbol,
1595 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001596 // determine the type of the relocation
1597 unsigned Type;
1598 if (IsPCRel) {
1599 switch ((unsigned)Fixup.getKind()) {
1600 default:
1601 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001602 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001603 Type = ELF::R_MICROBLAZE_64_PCREL;
1604 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001605 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001606 Type = ELF::R_MICROBLAZE_32_PCREL;
1607 break;
1608 }
1609 } else {
1610 switch ((unsigned)Fixup.getKind()) {
1611 default: llvm_unreachable("invalid fixup kind!");
1612 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001613 Type = ((IsRelocWithSymbol || Addend !=0)
1614 ? ELF::R_MICROBLAZE_32
1615 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001616 break;
1617 case FK_Data_2:
1618 Type = ELF::R_MICROBLAZE_32;
1619 break;
1620 }
1621 }
Jason W Kim56a39902010-12-06 21:57:34 +00001622 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001623}
Jason W Kimd3443e92010-11-15 16:18:39 +00001624
1625//===- X86ELFObjectWriter -------------------------------------------===//
1626
1627
Rafael Espindola31f35782010-12-17 18:01:31 +00001628X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001629 raw_ostream &_OS,
1630 bool IsLittleEndian)
1631 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001632{}
1633
1634X86ELFObjectWriter::~X86ELFObjectWriter()
1635{}
1636
Jason W Kim56a39902010-12-06 21:57:34 +00001637unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1638 const MCFixup &Fixup,
1639 bool IsPCRel,
1640 bool IsRelocWithSymbol,
1641 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001642 // determine the type of the relocation
1643
Rafael Espindola12203cc2010-11-21 00:48:25 +00001644 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1645 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001646 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001647 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001648 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001649 switch ((unsigned)Fixup.getKind()) {
1650 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001651
1652 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1653 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1654 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1655
Rafael Espindola3a83c402010-12-27 00:36:05 +00001656 case FK_PCRel_8:
1657 assert(Modifier == MCSymbolRefExpr::VK_None);
1658 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001659 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001660 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001661 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001662 case X86::reloc_riprel_4byte:
1663 case FK_PCRel_4:
1664 switch (Modifier) {
1665 default:
1666 llvm_unreachable("Unimplemented");
1667 case MCSymbolRefExpr::VK_None:
1668 Type = ELF::R_X86_64_PC32;
1669 break;
1670 case MCSymbolRefExpr::VK_PLT:
1671 Type = ELF::R_X86_64_PLT32;
1672 break;
1673 case MCSymbolRefExpr::VK_GOTPCREL:
1674 Type = ELF::R_X86_64_GOTPCREL;
1675 break;
1676 case MCSymbolRefExpr::VK_GOTTPOFF:
1677 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001678 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001679 case MCSymbolRefExpr::VK_TLSGD:
1680 Type = ELF::R_X86_64_TLSGD;
1681 break;
1682 case MCSymbolRefExpr::VK_TLSLD:
1683 Type = ELF::R_X86_64_TLSLD;
1684 break;
1685 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001686 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001687 case FK_PCRel_2:
1688 assert(Modifier == MCSymbolRefExpr::VK_None);
1689 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001690 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001691 case FK_PCRel_1:
1692 assert(Modifier == MCSymbolRefExpr::VK_None);
1693 Type = ELF::R_X86_64_PC8;
1694 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001695 }
1696 } else {
1697 switch ((unsigned)Fixup.getKind()) {
1698 default: llvm_unreachable("invalid fixup kind!");
1699 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1700 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001701 switch (Modifier) {
1702 default:
1703 llvm_unreachable("Unimplemented");
1704 case MCSymbolRefExpr::VK_None:
1705 Type = ELF::R_X86_64_32S;
1706 break;
1707 case MCSymbolRefExpr::VK_GOT:
1708 Type = ELF::R_X86_64_GOT32;
1709 break;
1710 case MCSymbolRefExpr::VK_GOTPCREL:
1711 Type = ELF::R_X86_64_GOTPCREL;
1712 break;
1713 case MCSymbolRefExpr::VK_TPOFF:
1714 Type = ELF::R_X86_64_TPOFF32;
1715 break;
1716 case MCSymbolRefExpr::VK_DTPOFF:
1717 Type = ELF::R_X86_64_DTPOFF32;
1718 break;
1719 }
1720 break;
1721 case FK_Data_4:
1722 Type = ELF::R_X86_64_32;
1723 break;
1724 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001725 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001726 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1727 }
1728 }
1729 } else {
1730 if (IsPCRel) {
1731 switch (Modifier) {
1732 default:
1733 llvm_unreachable("Unimplemented");
1734 case MCSymbolRefExpr::VK_None:
1735 Type = ELF::R_386_PC32;
1736 break;
1737 case MCSymbolRefExpr::VK_PLT:
1738 Type = ELF::R_386_PLT32;
1739 break;
1740 }
1741 } else {
1742 switch ((unsigned)Fixup.getKind()) {
1743 default: llvm_unreachable("invalid fixup kind!");
1744
1745 case X86::reloc_global_offset_table:
1746 Type = ELF::R_386_GOTPC;
1747 break;
1748
1749 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1750 // instead?
1751 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001752 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001753 case FK_Data_4:
1754 switch (Modifier) {
1755 default:
1756 llvm_unreachable("Unimplemented");
1757 case MCSymbolRefExpr::VK_None:
1758 Type = ELF::R_386_32;
1759 break;
1760 case MCSymbolRefExpr::VK_GOT:
1761 Type = ELF::R_386_GOT32;
1762 break;
1763 case MCSymbolRefExpr::VK_GOTOFF:
1764 Type = ELF::R_386_GOTOFF;
1765 break;
1766 case MCSymbolRefExpr::VK_TLSGD:
1767 Type = ELF::R_386_TLS_GD;
1768 break;
1769 case MCSymbolRefExpr::VK_TPOFF:
1770 Type = ELF::R_386_TLS_LE_32;
1771 break;
1772 case MCSymbolRefExpr::VK_INDNTPOFF:
1773 Type = ELF::R_386_TLS_IE;
1774 break;
1775 case MCSymbolRefExpr::VK_NTPOFF:
1776 Type = ELF::R_386_TLS_LE;
1777 break;
1778 case MCSymbolRefExpr::VK_GOTNTPOFF:
1779 Type = ELF::R_386_TLS_GOTIE;
1780 break;
1781 case MCSymbolRefExpr::VK_TLSLDM:
1782 Type = ELF::R_386_TLS_LDM;
1783 break;
1784 case MCSymbolRefExpr::VK_DTPOFF:
1785 Type = ELF::R_386_TLS_LDO_32;
1786 break;
Nick Lewyckye0b87032011-06-04 17:38:07 +00001787 case MCSymbolRefExpr::VK_GOTTPOFF:
1788 Type = ELF::R_386_TLS_IE_32;
1789 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001790 }
1791 break;
1792 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001793 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001794 case FK_Data_1: Type = ELF::R_386_8; break;
1795 }
1796 }
1797 }
1798
1799 if (RelocNeedsGOT(Modifier))
1800 NeedsGOT = true;
1801
Jason W Kim56a39902010-12-06 21:57:34 +00001802 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001803}