blob: e0ed86222cf9174fd43a8ec99eb8b8c2db0ec42e [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)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000128 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000129 else
130 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000131
132 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000133 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000134 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 ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000305 Section.getType() == ELF::SHT_SYMTAB ||
306 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000307 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000308 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000309 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000310 LastLocalSymbolIndex++;
311 }
Matt Fleming3565a062010-08-16 18:57:57 +0000312
313 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
314 ELFSymbolData &MSD = ExternalSymbolData[i];
315 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000316 assert(((Data.getFlags() & ELF_STB_Global) ||
317 (Data.getFlags() & ELF_STB_Weak)) &&
318 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000319 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000320 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000321 LastLocalSymbolIndex++;
322 }
323
324 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
325 ELFSymbolData &MSD = UndefinedSymbolData[i];
326 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000327 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000328 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000329 LastLocalSymbolIndex++;
330 }
331}
332
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000333const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
334 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000335 const MCFragment &F,
336 const MCFixup &Fixup,
337 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000338 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000339 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
340 const MCSymbol *Renamed = Renames.lookup(&Symbol);
341 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000342
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000343 if (ASymbol.isUndefined()) {
344 if (Renamed)
345 return Renamed;
346 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000347 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000348
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000349 if (SD.isExternal()) {
350 if (Renamed)
351 return Renamed;
352 return &Symbol;
353 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000354
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000355 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000356 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000357 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000358
Rafael Espindola25958732010-11-24 21:57:39 +0000359 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000360 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000361
Rafael Espindola25958732010-11-24 21:57:39 +0000362 if (secKind.isThreadLocal()) {
363 if (Renamed)
364 return Renamed;
365 return &Symbol;
366 }
367
Rafael Espindola8cecf252010-10-06 16:23:36 +0000368 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000369 const MCSectionELF &Sec2 =
370 static_cast<const MCSectionELF&>(F.getParent()->getSection());
371
Rafael Espindola8cecf252010-10-06 16:23:36 +0000372 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000373 (Kind == MCSymbolRefExpr::VK_PLT ||
374 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000375 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000376 if (Renamed)
377 return Renamed;
378 return &Symbol;
379 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000380
Rafael Espindola1c130262011-01-23 04:43:11 +0000381 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000382 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000383 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000384 if (Renamed)
385 return Renamed;
386 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000387 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000388
Jason W Kime964d112011-05-11 22:53:06 +0000389 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
390
Rafael Espindola73ffea42010-09-25 05:42:19 +0000391}
392
Matt Fleming3565a062010-08-16 18:57:57 +0000393
Jason W Kim56a39902010-12-06 21:57:34 +0000394void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
395 const MCAsmLayout &Layout,
396 const MCFragment *Fragment,
397 const MCFixup &Fixup,
398 MCValue Target,
399 uint64_t &FixedValue) {
400 int64_t Addend = 0;
401 int Index = 0;
402 int64_t Value = Target.getConstant();
403 const MCSymbol *RelocSymbol = NULL;
404
Rafael Espindola127a6a42010-12-17 07:28:17 +0000405 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000406 if (!Target.isAbsolute()) {
407 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
408 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000409 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000410
411 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
412 const MCSymbol &SymbolB = RefB->getSymbol();
413 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
414 IsPCRel = true;
415
416 // Offset of the symbol in the section
417 int64_t a = Layout.getSymbolOffset(&SDB);
418
419 // Ofeset of the relocation in the section
420 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
421 Value += b - a;
422 }
423
424 if (!RelocSymbol) {
425 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
426 MCFragment *F = SD.getFragment();
427
428 Index = F->getParent()->getOrdinal() + 1;
429
430 // Offset of the symbol in the section
431 Value += Layout.getSymbolOffset(&SD);
432 } else {
433 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
434 WeakrefUsedInReloc.insert(RelocSymbol);
435 else
436 UsedInReloc.insert(RelocSymbol);
437 Index = -1;
438 }
439 Addend = Value;
440 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000441 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000442 Value = 0;
443 }
444
445 FixedValue = Value;
446 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
447 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000448
Jason W Kim56a39902010-12-06 21:57:34 +0000449 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
450 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000451
452 adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000453
Rafael Espindolabff66a82010-12-18 03:27:34 +0000454 if (!hasRelocationAddend())
455 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000456
457 if (is64Bit())
458 assert(isInt<64>(Addend));
459 else
460 assert(isInt<32>(Addend));
461
Jason W Kim56a39902010-12-06 21:57:34 +0000462 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
463 Relocations[Fragment->getParent()].push_back(ERE);
464}
465
466
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000467uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000468ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
469 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000470 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000471 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000472}
473
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000474bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
475 const MCSymbolData &Data,
476 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000477 if (Data.getFlags() & ELF_Other_Weakref)
478 return false;
479
Rafael Espindolabd701182010-10-19 19:31:37 +0000480 if (Used)
481 return true;
482
Rafael Espindola88182132010-10-27 15:18:17 +0000483 if (Renamed)
484 return false;
485
Rafael Espindola737cd212010-10-05 18:01:23 +0000486 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000487
Rafael Espindolad1798862010-10-29 23:09:31 +0000488 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
489 return true;
490
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000491 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000492 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
493 return false;
494
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000495 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000496 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000497 return false;
498
Rafael Espindola737cd212010-10-05 18:01:23 +0000499 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
500 return false;
501
Rafael Espindolabd701182010-10-19 19:31:37 +0000502 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000503 return false;
504
505 return true;
506}
507
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000508bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
509 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000510 if (Data.isExternal())
511 return false;
512
513 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000514 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000515
516 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
517 if (isSignature && !isUsedInReloc)
518 return true;
519
Rafael Espindola737cd212010-10-05 18:01:23 +0000520 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000521 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000522
523 return true;
524}
525
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000526void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000527 SectionIndexMapTy &SectionIndexMap,
528 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000529 unsigned Index = 1;
530 for (MCAssembler::iterator it = Asm.begin(),
531 ie = Asm.end(); it != ie; ++it) {
532 const MCSectionELF &Section =
533 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000534 if (Section.getType() != ELF::SHT_GROUP)
535 continue;
536 SectionIndexMap[&Section] = Index++;
537 }
538
539 for (MCAssembler::iterator it = Asm.begin(),
540 ie = Asm.end(); it != ie; ++it) {
541 const MCSectionELF &Section =
542 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000543 if (Section.getType() == ELF::SHT_GROUP ||
544 Section.getType() == ELF::SHT_REL ||
545 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000546 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000547 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000548 const MCSectionELF *RelSection = RelMap.lookup(&Section);
549 if (RelSection)
550 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000551 }
552}
553
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000554void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000555 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000556 RevGroupMapTy RevGroupMap,
557 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000558 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000559 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000560 if (NeedsGOT) {
561 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
562 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
563 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
564 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000565 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000566 }
567
Matt Fleming3565a062010-08-16 18:57:57 +0000568 // Index 0 is always the empty string.
569 StringMap<uint64_t> StringIndexMap;
570 StringTable += '\x00';
571
Rafael Espindolad5321da2011-04-07 23:21:52 +0000572 // FIXME: We could optimize suffixes in strtab in the same way we
573 // optimize them in shstrtab.
574
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000575 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000576 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
577 ie = Asm.symbol_end(); it != ie; ++it) {
578 const MCSymbol &Symbol = it->getSymbol();
579
Rafael Espindola484291c2010-11-01 14:28:48 +0000580 bool Used = UsedInReloc.count(&Symbol);
581 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000582 bool isSignature = RevGroupMap.count(&Symbol);
583
584 if (!isInSymtab(Asm, *it,
585 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000586 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000587 continue;
588
Matt Fleming3565a062010-08-16 18:57:57 +0000589 ELFSymbolData MSD;
590 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000591 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000592
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000593 // Undefined symbols are global, but this is the first place we
594 // are able to set it.
595 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000596 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000597 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000598 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
599 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000600 }
601
Rafael Espindola484291c2010-11-01 14:28:48 +0000602 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000603 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000604
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000605 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000606 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000607 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000608 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000609 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000610 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000611 if (isSignature && !Used)
612 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
613 else
614 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000615 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000616 const MCSectionELF &Section =
617 static_cast<const MCSectionELF&>(RefSymbol.getSection());
618 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000619 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
620 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000621 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000622 }
623
Rafael Espindola88182132010-10-27 15:18:17 +0000624 // The @@@ in symbol version is replaced with @ in undefined symbols and
625 // @@ in defined ones.
626 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000627 SmallString<32> Buf;
628
Rafael Espindola88182132010-10-27 15:18:17 +0000629 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000630 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000631 Buf += Name.substr(0, Pos);
632 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
633 Buf += Name.substr(Pos + Skip);
634 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000635 }
636
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000637 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000638 if (!Entry) {
639 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000640 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000641 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000642 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000643 MSD.StringIndex = Entry;
644 if (MSD.SectionIndex == ELF::SHN_UNDEF)
645 UndefinedSymbolData.push_back(MSD);
646 else if (Local)
647 LocalSymbolData.push_back(MSD);
648 else
649 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000650 }
651
652 // Symbols are required to be in lexicographic order.
653 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
654 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
655 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
656
657 // Set the symbol indices. Local symbols must come before all other
658 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000659 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000660 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
661 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000662
663 Index += NumRegularSections;
664
Matt Fleming3565a062010-08-16 18:57:57 +0000665 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
666 ExternalSymbolData[i].SymbolData->setIndex(Index++);
667 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
668 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
669}
670
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000671void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
672 MCAsmLayout &Layout,
673 RelMapTy &RelMap) {
674 for (MCAssembler::const_iterator it = Asm.begin(),
675 ie = Asm.end(); it != ie; ++it) {
676 const MCSectionData &SD = *it;
677 if (Relocations[&SD].empty())
678 continue;
679
Matt Fleming3565a062010-08-16 18:57:57 +0000680 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000681 const MCSectionELF &Section =
682 static_cast<const MCSectionELF&>(SD.getSection());
683
684 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000685 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000686 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000687
688 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000689 if (hasRelocationAddend())
690 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000691 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000692 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000693
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000694 const MCSectionELF *RelaSection =
695 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
696 ELF::SHT_RELA : ELF::SHT_REL, 0,
697 SectionKind::getReadOnly(),
698 EntrySize, "");
699 RelMap[&Section] = RelaSection;
700 Asm.getOrCreateSectionData(*RelaSection);
701 }
702}
Matt Fleming3565a062010-08-16 18:57:57 +0000703
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000704void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
705 const RelMapTy &RelMap) {
706 for (MCAssembler::const_iterator it = Asm.begin(),
707 ie = Asm.end(); it != ie; ++it) {
708 const MCSectionData &SD = *it;
709 const MCSectionELF &Section =
710 static_cast<const MCSectionELF&>(SD.getSection());
711
712 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
713 if (!RelaSection)
714 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000715 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000716 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000717
718 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000719 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000720 }
721}
722
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000723void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
724 uint64_t Flags, uint64_t Address,
725 uint64_t Offset, uint64_t Size,
726 uint32_t Link, uint32_t Info,
727 uint64_t Alignment,
728 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000729 Write32(Name); // sh_name: index into string table
730 Write32(Type); // sh_type
731 WriteWord(Flags); // sh_flags
732 WriteWord(Address); // sh_addr
733 WriteWord(Offset); // sh_offset
734 WriteWord(Size); // sh_size
735 Write32(Link); // sh_link
736 Write32(Info); // sh_info
737 WriteWord(Alignment); // sh_addralign
738 WriteWord(EntrySize); // sh_entsize
739}
740
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000741void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
742 MCDataFragment *F,
743 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000744 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
745 // sort by the r_offset just like gnu as does
746 array_pod_sort(Relocs.begin(), Relocs.end());
747
748 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
749 ELFRelocationEntry entry = Relocs[e - i - 1];
750
Rafael Espindola12203cc2010-11-21 00:48:25 +0000751 if (!entry.Index)
752 ;
753 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000754 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
755 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000756 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000757 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000758 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000759
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000760 struct ELF::Elf64_Rela ERE64;
761 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000762 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000763
Rafael Espindolabff66a82010-12-18 03:27:34 +0000764 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000765 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000766 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000767 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000768
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000769 struct ELF::Elf32_Rela ERE32;
770 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000771 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000772
Rafael Espindolabff66a82010-12-18 03:27:34 +0000773 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000774 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000775 }
Matt Fleming3565a062010-08-16 18:57:57 +0000776 }
777}
778
Rafael Espindolad5321da2011-04-07 23:21:52 +0000779static int compareBySuffix(const void *a, const void *b) {
780 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
781 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
782 const StringRef &NameA = secA->getSectionName();
783 const StringRef &NameB = secB->getSectionName();
784 const unsigned sizeA = NameA.size();
785 const unsigned sizeB = NameB.size();
786 const unsigned len = std::min(sizeA, sizeB);
787 for (unsigned int i = 0; i < len; ++i) {
788 char ca = NameA[sizeA - i - 1];
789 char cb = NameB[sizeB - i - 1];
790 if (ca != cb)
791 return cb - ca;
792 }
793
794 return sizeB - sizeA;
795}
796
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000797void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
798 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000799 SectionIndexMapTy &SectionIndexMap,
800 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000801 MCContext &Ctx = Asm.getContext();
802 MCDataFragment *F;
803
Rafael Espindolabff66a82010-12-18 03:27:34 +0000804 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000805
Rafael Espindola38738bf2010-09-22 19:04:41 +0000806 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000807 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000808 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000809 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000810 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
811 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000812
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000813 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000814 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
815 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000816 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000817 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000818 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000819
820 MCSectionData *SymtabShndxSD = NULL;
821
822 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000823 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000824 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000825 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000826 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
827 SymtabShndxSD->setAlignment(4);
828 }
Matt Fleming3565a062010-08-16 18:57:57 +0000829
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000830 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000831 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000832 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000833 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
834 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000835
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000836 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
837
838 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
839 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
840 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000841
842 // Symbol table
843 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000844 MCDataFragment *ShndxF = NULL;
845 if (NeedsSymtabShndx) {
846 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000847 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000848 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000849
Matt Fleming3565a062010-08-16 18:57:57 +0000850 F = new MCDataFragment(&StrtabSD);
851 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000852
Matt Fleming3565a062010-08-16 18:57:57 +0000853 F = new MCDataFragment(&ShstrtabSD);
854
Rafael Espindolad5321da2011-04-07 23:21:52 +0000855 std::vector<const MCSectionELF*> Sections;
856 for (MCAssembler::const_iterator it = Asm.begin(),
857 ie = Asm.end(); it != ie; ++it) {
858 const MCSectionELF &Section =
859 static_cast<const MCSectionELF&>(it->getSection());
860 Sections.push_back(&Section);
861 }
862 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
863
Matt Fleming3565a062010-08-16 18:57:57 +0000864 // Section header string table.
865 //
866 // The first entry of a string table holds a null character so skip
867 // section 0.
868 uint64_t Index = 1;
869 F->getContents() += '\x00';
870
Rafael Espindolad5321da2011-04-07 23:21:52 +0000871 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
872 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000873
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000874 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000875 if (I != 0) {
876 StringRef PreviousName = Sections[I - 1]->getSectionName();
877 if (PreviousName.endswith(Name)) {
878 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
879 continue;
880 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000881 }
Matt Fleming3565a062010-08-16 18:57:57 +0000882 // Remember the index into the string table so we can write it
883 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000884 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000885
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000886 Index += Name.size() + 1;
887 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000888 F->getContents() += '\x00';
889 }
Rafael Espindola70703872010-09-30 02:22:20 +0000890}
891
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000892void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
893 MCAsmLayout &Layout,
894 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000895 RevGroupMapTy &RevGroupMap,
896 SectionIndexMapTy &SectionIndexMap,
897 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000898 // Create the .note.GNU-stack section if needed.
899 MCContext &Ctx = Asm.getContext();
900 if (Asm.getNoExecStack()) {
901 const MCSectionELF *GnuStackSection =
902 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
903 SectionKind::getReadOnly());
904 Asm.getOrCreateSectionData(*GnuStackSection);
905 }
906
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000907 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000908 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
909 it != ie; ++it) {
910 const MCSectionELF &Section =
911 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000912 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000913 continue;
914
915 const MCSymbol *SignatureSymbol = Section.getGroup();
916 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000917 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000918 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000919 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000920 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
921 Data.setAlignment(4);
922 MCDataFragment *F = new MCDataFragment(&Data);
923 String32(*F, ELF::GRP_COMDAT);
924 }
925 GroupMap[Group] = SignatureSymbol;
926 }
927
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000928 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
929
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000930 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000931 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000932 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000933 const MCSectionELF &Section =
934 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000935 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000936 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000937 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000938 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
939 // FIXME: we could use the previous fragment
940 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000941 unsigned Index = SectionIndexMap.lookup(&Section);
942 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000943 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000944}
945
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000946void ELFObjectWriter::WriteSection(MCAssembler &Asm,
947 const SectionIndexMapTy &SectionIndexMap,
948 uint32_t GroupSymbolIndex,
949 uint64_t Offset, uint64_t Size,
950 uint64_t Alignment,
951 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000952 uint64_t sh_link = 0;
953 uint64_t sh_info = 0;
954
955 switch(Section.getType()) {
956 case ELF::SHT_DYNAMIC:
957 sh_link = SectionStringTableIndex[&Section];
958 sh_info = 0;
959 break;
960
961 case ELF::SHT_REL:
962 case ELF::SHT_RELA: {
963 const MCSectionELF *SymtabSection;
964 const MCSectionELF *InfoSection;
965 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
966 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000967 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000968 sh_link = SectionIndexMap.lookup(SymtabSection);
969 assert(sh_link && ".symtab not found");
970
971 // Remove ".rel" and ".rela" prefixes.
972 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
973 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
974
975 InfoSection = Asm.getContext().getELFSection(SectionName,
976 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000977 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000978 sh_info = SectionIndexMap.lookup(InfoSection);
979 break;
980 }
981
982 case ELF::SHT_SYMTAB:
983 case ELF::SHT_DYNSYM:
984 sh_link = StringTableIndex;
985 sh_info = LastLocalSymbolIndex;
986 break;
987
988 case ELF::SHT_SYMTAB_SHNDX:
989 sh_link = SymbolTableIndex;
990 break;
991
992 case ELF::SHT_PROGBITS:
993 case ELF::SHT_STRTAB:
994 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000995 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000996 case ELF::SHT_NULL:
997 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +0000998 case ELF::SHT_INIT_ARRAY:
999 case ELF::SHT_FINI_ARRAY:
1000 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001001 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001002 // Nothing to do.
1003 break;
1004
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001005 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001006 sh_link = SymbolTableIndex;
1007 sh_info = GroupSymbolIndex;
1008 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001009
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001010 default:
1011 assert(0 && "FIXME: sh_type value not supported!");
1012 break;
1013 }
1014
1015 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1016 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1017 Alignment, Section.getEntrySize());
1018}
1019
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001020bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001021 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001022 !SD.getSection().isVirtualSection();
1023}
1024
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001025uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001026 uint64_t Ret = 0;
1027 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1028 ++i) {
1029 const MCFragment &F = *i;
1030 assert(F.getKind() == MCFragment::FT_Data);
1031 Ret += cast<MCDataFragment>(F).getContents().size();
1032 }
1033 return Ret;
1034}
1035
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001036uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1037 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001038 if (IsELFMetaDataSection(SD))
1039 return DataSectionSize(SD);
1040 return Layout.getSectionFileSize(&SD);
1041}
1042
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001043uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1044 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001045 if (IsELFMetaDataSection(SD))
1046 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001047 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001048}
1049
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001050void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1051 const MCAsmLayout &Layout,
1052 const MCSectionELF &Section) {
1053 uint64_t FileOff = OS.tell();
1054 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1055
1056 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1057 WriteZeros(Padding);
1058 FileOff += Padding;
1059
1060 FileOff += GetSectionFileSize(Layout, SD);
1061
1062 if (IsELFMetaDataSection(SD)) {
1063 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1064 ++i) {
1065 const MCFragment &F = *i;
1066 assert(F.getKind() == MCFragment::FT_Data);
1067 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1068 }
1069 } else {
1070 Asm.WriteSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001071 }
1072}
1073
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001074void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1075 const GroupMapTy &GroupMap,
1076 const MCAsmLayout &Layout,
1077 const SectionIndexMapTy &SectionIndexMap,
1078 const SectionOffsetMapTy &SectionOffsetMap) {
1079 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001080
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001081 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001082 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001083
1084 for (SectionIndexMapTy::const_iterator i=
1085 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1086 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001087 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001088 }
1089
Matt Fleming3565a062010-08-16 18:57:57 +00001090 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001091 uint64_t FirstSectionSize =
1092 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1093 uint32_t FirstSectionLink =
1094 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1095 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001096
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001097 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001098 const MCSectionELF &Section = *Sections[i];
1099 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1100 uint32_t GroupSymbolIndex;
1101 if (Section.getType() != ELF::SHT_GROUP)
1102 GroupSymbolIndex = 0;
1103 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001104 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1105 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001106
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001107 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001108
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001109 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001110 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001111 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001112 }
1113}
1114
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001115void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1116 std::vector<const MCSectionELF*> &Sections) {
1117 for (MCAssembler::iterator it = Asm.begin(),
1118 ie = Asm.end(); it != ie; ++it) {
1119 const MCSectionELF &Section =
1120 static_cast<const MCSectionELF &>(it->getSection());
1121 if (Section.getType() == ELF::SHT_GROUP)
1122 Sections.push_back(&Section);
1123 }
1124
1125 for (MCAssembler::iterator it = Asm.begin(),
1126 ie = Asm.end(); it != ie; ++it) {
1127 const MCSectionELF &Section =
1128 static_cast<const MCSectionELF &>(it->getSection());
1129 if (Section.getType() != ELF::SHT_GROUP &&
1130 Section.getType() != ELF::SHT_REL &&
1131 Section.getType() != ELF::SHT_RELA)
1132 Sections.push_back(&Section);
1133 }
1134
1135 for (MCAssembler::iterator it = Asm.begin(),
1136 ie = Asm.end(); it != ie; ++it) {
1137 const MCSectionELF &Section =
1138 static_cast<const MCSectionELF &>(it->getSection());
1139 if (Section.getType() == ELF::SHT_REL ||
1140 Section.getType() == ELF::SHT_RELA)
1141 Sections.push_back(&Section);
1142 }
1143}
1144
1145void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1146 const MCAsmLayout &Layout) {
1147 GroupMapTy GroupMap;
1148 RevGroupMapTy RevGroupMap;
1149 SectionIndexMapTy SectionIndexMap;
1150
1151 unsigned NumUserSections = Asm.size();
1152
1153 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1154 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1155
1156 const unsigned NumUserAndRelocSections = Asm.size();
1157 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1158 RevGroupMap, SectionIndexMap, RelMap);
1159 const unsigned AllSections = Asm.size();
1160 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1161
1162 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1163
1164 // Compute symbol table information.
1165 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1166
1167
1168 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1169
1170 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1171 const_cast<MCAsmLayout&>(Layout),
1172 SectionIndexMap,
1173 RelMap);
1174
1175 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1176 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1177 sizeof(ELF::Elf32_Ehdr);
1178 uint64_t FileOff = HeaderSize;
1179
1180 std::vector<const MCSectionELF*> Sections;
1181 ComputeSectionOrder(Asm, Sections);
1182 unsigned NumSections = Sections.size();
1183 SectionOffsetMapTy SectionOffsetMap;
1184 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1185 const MCSectionELF &Section = *Sections[i];
1186 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1187
1188 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1189
1190 // Remember the offset into the file for this section.
1191 SectionOffsetMap[&Section] = FileOff;
1192
1193 // Get the size of the section in the output file (including padding).
1194 FileOff += GetSectionFileSize(Layout, SD);
1195 }
1196
1197 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1198
1199 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1200
1201 uint64_t SectionHeaderEntrySize = is64Bit() ?
1202 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1203 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1204
1205 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1206 const MCSectionELF &Section = *Sections[i];
1207 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1208
1209 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1210
1211 // Remember the offset into the file for this section.
1212 SectionOffsetMap[&Section] = FileOff;
1213
1214 // Get the size of the section in the output file (including padding).
1215 FileOff += GetSectionFileSize(Layout, SD);
1216 }
1217
1218 // Write out the ELF header ...
1219 WriteHeader(SectionHeaderOffset, NumSections + 1);
1220
1221 // ... then the regular sections ...
1222 // + because of .shstrtab
1223 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1224 WriteDataSectionData(Asm, Layout, *Sections[i]);
1225
1226 FileOff = OS.tell();
1227 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1228 WriteZeros(Padding);
1229
1230 // ... then the section header table ...
1231 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1232 SectionOffsetMap);
1233
1234 FileOff = OS.tell();
1235
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001236 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001237 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1238 WriteDataSectionData(Asm, Layout, *Sections[i]);
1239}
1240
Eli Friedman78c1e172011-03-03 07:24:36 +00001241bool
1242ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1243 const MCSymbolData &DataA,
1244 const MCFragment &FB,
1245 bool InSet,
1246 bool IsPCRel) const {
1247 if (DataA.getFlags() & ELF_STB_Weak)
1248 return false;
1249 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1250 Asm, DataA, FB,InSet, IsPCRel);
1251}
1252
Rafael Espindola6024c972010-12-17 17:45:22 +00001253MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1254 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001255 bool IsLittleEndian) {
1256 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001257 case ELF::EM_386:
1258 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001259 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001260 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001261 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001262 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001263 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001264 case ELF::EM_PPC:
1265 case ELF::EM_PPC64:
1266 return new PPCELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Akira Hatanaka291512f2011-09-30 21:55:40 +00001267 case ELF::EM_MIPS:
1268 return new MipsELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001269 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001270 }
1271}
1272
1273
1274/// START OF SUBCLASSES for ELFObjectWriter
1275//===- ARMELFObjectWriter -------------------------------------------===//
1276
Rafael Espindola31f35782010-12-17 18:01:31 +00001277ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001278 raw_ostream &_OS,
1279 bool IsLittleEndian)
1280 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001281{}
1282
1283ARMELFObjectWriter::~ARMELFObjectWriter()
1284{}
1285
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001286// FIXME: get the real EABI Version from the Triple.
1287void ARMELFObjectWriter::WriteEFlags() {
1288 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1289}
1290
Jason W Kim953a2a32011-02-07 01:11:15 +00001291// In ARM, _MergedGlobals and other most symbols get emitted directly.
1292// I.e. not as an offset to a section symbol.
Jason W Kime964d112011-05-11 22:53:06 +00001293// This code is an approximation of what ARM/gcc does.
1294
1295STATISTIC(PCRelCount, "Total number of PIC Relocations");
1296STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
Jason W Kim953a2a32011-02-07 01:11:15 +00001297
1298const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1299 const MCValue &Target,
1300 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +00001301 const MCFixup &Fixup,
1302 bool IsPCRel) const {
Jason W Kim953a2a32011-02-07 01:11:15 +00001303 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1304 bool EmitThisSym = false;
1305
Jason W Kime964d112011-05-11 22:53:06 +00001306 const MCSectionELF &Section =
1307 static_cast<const MCSectionELF&>(Symbol.getSection());
Jason W Kime964d112011-05-11 22:53:06 +00001308 bool InNormalSection = true;
1309 unsigned RelocType = 0;
1310 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1311
Matt Beaumont-Gay6dcd4132011-05-11 23:34:51 +00001312 DEBUG(
1313 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1314 MCSymbolRefExpr::VariantKind Kind2;
1315 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1316 MCSymbolRefExpr::VK_None;
1317 dbgs() << "considering symbol "
Jason W Kime964d112011-05-11 22:53:06 +00001318 << Section.getSectionName() << "/"
1319 << Symbol.getName() << "/"
1320 << " Rel:" << (unsigned)RelocType
1321 << " Kind: " << (int)Kind << "/" << (int)Kind2
1322 << " Tmp:"
1323 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1324 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1325 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1326
Jason W Kimbebd44a2011-06-09 19:13:45 +00001327 if (IsPCRel) { ++PCRelCount;
Jason W Kime964d112011-05-11 22:53:06 +00001328 switch (RelocType) {
1329 default:
1330 // Most relocation types are emitted as explicit symbols
1331 InNormalSection =
1332 StringSwitch<bool>(Section.getSectionName())
1333 .Case(".data.rel.ro.local", false)
1334 .Case(".data.rel", false)
1335 .Case(".bss", false)
1336 .Default(true);
1337 EmitThisSym = true;
1338 break;
1339 case ELF::R_ARM_ABS32:
1340 // But things get strange with R_ARM_ABS32
1341 // In this case, most things that go in .rodata show up
1342 // as section relative relocations
1343 InNormalSection =
1344 StringSwitch<bool>(Section.getSectionName())
1345 .Case(".data.rel.ro.local", false)
1346 .Case(".data.rel", false)
1347 .Case(".rodata", false)
1348 .Case(".bss", false)
1349 .Default(true);
1350 EmitThisSym = false;
1351 break;
1352 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001353 } else {
Jason W Kime964d112011-05-11 22:53:06 +00001354 NonPCRelCount++;
1355 InNormalSection =
1356 StringSwitch<bool>(Section.getSectionName())
1357 .Case(".data.rel.ro.local", false)
1358 .Case(".rodata", false)
1359 .Case(".data.rel", false)
1360 .Case(".bss", false)
1361 .Default(true);
1362
1363 switch (RelocType) {
1364 default: EmitThisSym = true; break;
1365 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1366 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001367 }
Jason W Kime964d112011-05-11 22:53:06 +00001368
Jason W Kim953a2a32011-02-07 01:11:15 +00001369 if (EmitThisSym)
1370 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001371 if (! Symbol.isTemporary() && InNormalSection) {
Jason W Kim953a2a32011-02-07 01:11:15 +00001372 return &Symbol;
Jason W Kime964d112011-05-11 22:53:06 +00001373 }
Jason W Kim953a2a32011-02-07 01:11:15 +00001374 return NULL;
1375}
1376
Jason W Kime964d112011-05-11 22:53:06 +00001377// Need to examine the Fixup when determining whether to
1378// emit the relocation as an explicit symbol or as a section relative
1379// offset
Jason W Kim85fed5e2010-12-01 02:40:06 +00001380unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1381 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001382 bool IsPCRel,
1383 bool IsRelocWithSymbol,
1384 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001385 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1386 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1387
Jason W Kime964d112011-05-11 22:53:06 +00001388 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1389
1390 if (RelocNeedsGOT(Modifier))
1391 NeedsGOT = true;
1392
1393 return Type;
1394}
1395
1396unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1397 const MCFixup &Fixup,
1398 bool IsPCRel) const {
1399 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1400 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1401
Jason W Kima0871e72010-12-08 23:14:44 +00001402 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001403 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001404 switch ((unsigned)Fixup.getKind()) {
1405 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001406 case FK_Data_4:
1407 switch (Modifier) {
1408 default: llvm_unreachable("Unsupported Modifier");
1409 case MCSymbolRefExpr::VK_None:
Jason W Kime964d112011-05-11 22:53:06 +00001410 Type = ELF::R_ARM_REL32;
Jason W Kim1d866172011-01-13 00:07:51 +00001411 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001412 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001413 assert(0 && "unimplemented");
1414 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001415 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1416 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001417 break;
1418 }
1419 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001420 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001421 switch (Modifier) {
1422 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001423 Type = ELF::R_ARM_PLT32;
1424 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001425 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001426 Type = ELF::R_ARM_CALL;
1427 break;
1428 }
1429 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001430 case ARM::fixup_arm_condbranch:
1431 Type = ELF::R_ARM_JUMP24;
1432 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001433 case ARM::fixup_arm_movt_hi16:
1434 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001435 Type = ELF::R_ARM_MOVT_PREL;
1436 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001437 case ARM::fixup_arm_movw_lo16:
1438 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001439 Type = ELF::R_ARM_MOVW_PREL_NC;
1440 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001441 case ARM::fixup_t2_movt_hi16:
1442 case ARM::fixup_t2_movt_hi16_pcrel:
1443 Type = ELF::R_ARM_THM_MOVT_PREL;
1444 break;
1445 case ARM::fixup_t2_movw_lo16:
1446 case ARM::fixup_t2_movw_lo16_pcrel:
1447 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1448 break;
Rafael Espindola298c8e12011-05-20 20:01:01 +00001449 case ARM::fixup_arm_thumb_bl:
1450 case ARM::fixup_arm_thumb_blx:
1451 switch (Modifier) {
1452 case MCSymbolRefExpr::VK_ARM_PLT:
1453 Type = ELF::R_ARM_THM_CALL;
1454 break;
1455 default:
1456 Type = ELF::R_ARM_NONE;
1457 break;
1458 }
1459 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001460 }
1461 } else {
1462 switch ((unsigned)Fixup.getKind()) {
1463 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001464 case FK_Data_4:
1465 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001466 default: llvm_unreachable("Unsupported Modifier"); break;
1467 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001468 Type = ELF::R_ARM_GOT_BREL;
1469 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001470 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001471 Type = ELF::R_ARM_TLS_GD32;
1472 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001473 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001474 Type = ELF::R_ARM_TLS_LE32;
1475 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001476 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001477 Type = ELF::R_ARM_TLS_IE32;
1478 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001479 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001480 Type = ELF::R_ARM_ABS32;
1481 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001482 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001483 Type = ELF::R_ARM_GOTOFF32;
1484 break;
1485 }
1486 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001487 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001488 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001489 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001490 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001491 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001492 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001493 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001494 assert(0 && "Unimplemented");
1495 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001496 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001497 Type = ELF::R_ARM_CALL;
1498 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001499 case ARM::fixup_arm_condbranch:
1500 Type = ELF::R_ARM_JUMP24;
1501 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001502 case ARM::fixup_arm_movt_hi16:
1503 Type = ELF::R_ARM_MOVT_ABS;
1504 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001505 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001506 Type = ELF::R_ARM_MOVW_ABS_NC;
1507 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001508 case ARM::fixup_t2_movt_hi16:
1509 Type = ELF::R_ARM_THM_MOVT_ABS;
1510 break;
1511 case ARM::fixup_t2_movw_lo16:
1512 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1513 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001514 }
1515 }
1516
Jason W Kima0871e72010-12-08 23:14:44 +00001517 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001518}
1519
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001520//===- PPCELFObjectWriter -------------------------------------------===//
1521
1522PPCELFObjectWriter::PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1523 raw_ostream &_OS,
1524 bool IsLittleEndian)
1525 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1526}
1527
1528PPCELFObjectWriter::~PPCELFObjectWriter() {
1529}
1530
1531unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
1532 const MCFixup &Fixup,
1533 bool IsPCRel,
1534 bool IsRelocWithSymbol,
1535 int64_t Addend) {
1536 // determine the type of the relocation
1537 unsigned Type;
1538 if (IsPCRel) {
1539 switch ((unsigned)Fixup.getKind()) {
1540 default:
1541 llvm_unreachable("Unimplemented");
1542 case PPC::fixup_ppc_br24:
1543 Type = ELF::R_PPC_REL24;
1544 break;
1545 case FK_PCRel_4:
1546 Type = ELF::R_PPC_REL32;
1547 break;
1548 }
1549 } else {
1550 switch ((unsigned)Fixup.getKind()) {
1551 default: llvm_unreachable("invalid fixup kind!");
1552 case PPC::fixup_ppc_br24:
1553 Type = ELF::R_PPC_ADDR24;
1554 break;
1555 case PPC::fixup_ppc_brcond14:
1556 Type = ELF::R_PPC_ADDR14_BRTAKEN; // XXX: or BRNTAKEN?_
1557 break;
1558 case PPC::fixup_ppc_ha16:
1559 Type = ELF::R_PPC_ADDR16_HA;
1560 break;
1561 case PPC::fixup_ppc_lo16:
1562 Type = ELF::R_PPC_ADDR16_LO;
1563 break;
1564 case PPC::fixup_ppc_lo14:
1565 Type = ELF::R_PPC_ADDR14;
1566 break;
1567 case FK_Data_4:
1568 Type = ELF::R_PPC_ADDR32;
1569 break;
1570 case FK_Data_2:
1571 Type = ELF::R_PPC_ADDR16;
1572 break;
1573 }
1574 }
1575 return Type;
1576}
1577
Roman Divacky2a66cea2011-08-04 19:08:19 +00001578void
1579PPCELFObjectWriter::adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
1580 switch ((unsigned)Fixup.getKind()) {
1581 case PPC::fixup_ppc_ha16:
1582 case PPC::fixup_ppc_lo16:
1583 RelocOffset += 2;
1584 break;
1585 default:
1586 break;
1587 }
1588}
1589
Wesley Peck4b047132010-11-21 22:06:28 +00001590//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001591
Rafael Espindola31f35782010-12-17 18:01:31 +00001592MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001593 raw_ostream &_OS,
1594 bool IsLittleEndian)
1595 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001596}
1597
1598MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1599}
1600
Jason W Kim56a39902010-12-06 21:57:34 +00001601unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001602 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001603 bool IsPCRel,
1604 bool IsRelocWithSymbol,
1605 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001606 // determine the type of the relocation
1607 unsigned Type;
1608 if (IsPCRel) {
1609 switch ((unsigned)Fixup.getKind()) {
1610 default:
1611 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001612 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001613 Type = ELF::R_MICROBLAZE_64_PCREL;
1614 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001615 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001616 Type = ELF::R_MICROBLAZE_32_PCREL;
1617 break;
1618 }
1619 } else {
1620 switch ((unsigned)Fixup.getKind()) {
1621 default: llvm_unreachable("invalid fixup kind!");
1622 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001623 Type = ((IsRelocWithSymbol || Addend !=0)
1624 ? ELF::R_MICROBLAZE_32
1625 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001626 break;
1627 case FK_Data_2:
1628 Type = ELF::R_MICROBLAZE_32;
1629 break;
1630 }
1631 }
Jason W Kim56a39902010-12-06 21:57:34 +00001632 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001633}
Jason W Kimd3443e92010-11-15 16:18:39 +00001634
1635//===- X86ELFObjectWriter -------------------------------------------===//
1636
1637
Rafael Espindola31f35782010-12-17 18:01:31 +00001638X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001639 raw_ostream &_OS,
1640 bool IsLittleEndian)
1641 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001642{}
1643
1644X86ELFObjectWriter::~X86ELFObjectWriter()
1645{}
1646
Jason W Kim56a39902010-12-06 21:57:34 +00001647unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1648 const MCFixup &Fixup,
1649 bool IsPCRel,
1650 bool IsRelocWithSymbol,
1651 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001652 // determine the type of the relocation
1653
Rafael Espindola12203cc2010-11-21 00:48:25 +00001654 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1655 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001656 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001657 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001658 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001659 switch ((unsigned)Fixup.getKind()) {
1660 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindoladebd7e42011-05-01 03:50:49 +00001661
1662 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1663 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1664 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1665
Rafael Espindola3a83c402010-12-27 00:36:05 +00001666 case FK_PCRel_8:
1667 assert(Modifier == MCSymbolRefExpr::VK_None);
1668 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001669 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001670 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001671 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001672 case X86::reloc_riprel_4byte:
1673 case FK_PCRel_4:
1674 switch (Modifier) {
1675 default:
1676 llvm_unreachable("Unimplemented");
1677 case MCSymbolRefExpr::VK_None:
1678 Type = ELF::R_X86_64_PC32;
1679 break;
1680 case MCSymbolRefExpr::VK_PLT:
1681 Type = ELF::R_X86_64_PLT32;
1682 break;
1683 case MCSymbolRefExpr::VK_GOTPCREL:
1684 Type = ELF::R_X86_64_GOTPCREL;
1685 break;
1686 case MCSymbolRefExpr::VK_GOTTPOFF:
1687 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001688 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001689 case MCSymbolRefExpr::VK_TLSGD:
1690 Type = ELF::R_X86_64_TLSGD;
1691 break;
1692 case MCSymbolRefExpr::VK_TLSLD:
1693 Type = ELF::R_X86_64_TLSLD;
1694 break;
1695 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001696 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001697 case FK_PCRel_2:
1698 assert(Modifier == MCSymbolRefExpr::VK_None);
1699 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001700 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001701 case FK_PCRel_1:
1702 assert(Modifier == MCSymbolRefExpr::VK_None);
1703 Type = ELF::R_X86_64_PC8;
1704 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001705 }
1706 } else {
1707 switch ((unsigned)Fixup.getKind()) {
1708 default: llvm_unreachable("invalid fixup kind!");
1709 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1710 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001711 switch (Modifier) {
1712 default:
1713 llvm_unreachable("Unimplemented");
1714 case MCSymbolRefExpr::VK_None:
1715 Type = ELF::R_X86_64_32S;
1716 break;
1717 case MCSymbolRefExpr::VK_GOT:
1718 Type = ELF::R_X86_64_GOT32;
1719 break;
1720 case MCSymbolRefExpr::VK_GOTPCREL:
1721 Type = ELF::R_X86_64_GOTPCREL;
1722 break;
1723 case MCSymbolRefExpr::VK_TPOFF:
1724 Type = ELF::R_X86_64_TPOFF32;
1725 break;
1726 case MCSymbolRefExpr::VK_DTPOFF:
1727 Type = ELF::R_X86_64_DTPOFF32;
1728 break;
1729 }
1730 break;
1731 case FK_Data_4:
1732 Type = ELF::R_X86_64_32;
1733 break;
1734 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001735 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001736 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1737 }
1738 }
1739 } else {
1740 if (IsPCRel) {
1741 switch (Modifier) {
1742 default:
1743 llvm_unreachable("Unimplemented");
1744 case MCSymbolRefExpr::VK_None:
1745 Type = ELF::R_386_PC32;
1746 break;
1747 case MCSymbolRefExpr::VK_PLT:
1748 Type = ELF::R_386_PLT32;
1749 break;
1750 }
1751 } else {
1752 switch ((unsigned)Fixup.getKind()) {
1753 default: llvm_unreachable("invalid fixup kind!");
1754
1755 case X86::reloc_global_offset_table:
1756 Type = ELF::R_386_GOTPC;
1757 break;
1758
1759 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1760 // instead?
1761 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001762 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001763 case FK_Data_4:
1764 switch (Modifier) {
1765 default:
1766 llvm_unreachable("Unimplemented");
1767 case MCSymbolRefExpr::VK_None:
1768 Type = ELF::R_386_32;
1769 break;
1770 case MCSymbolRefExpr::VK_GOT:
1771 Type = ELF::R_386_GOT32;
1772 break;
1773 case MCSymbolRefExpr::VK_GOTOFF:
1774 Type = ELF::R_386_GOTOFF;
1775 break;
1776 case MCSymbolRefExpr::VK_TLSGD:
1777 Type = ELF::R_386_TLS_GD;
1778 break;
1779 case MCSymbolRefExpr::VK_TPOFF:
1780 Type = ELF::R_386_TLS_LE_32;
1781 break;
1782 case MCSymbolRefExpr::VK_INDNTPOFF:
1783 Type = ELF::R_386_TLS_IE;
1784 break;
1785 case MCSymbolRefExpr::VK_NTPOFF:
1786 Type = ELF::R_386_TLS_LE;
1787 break;
1788 case MCSymbolRefExpr::VK_GOTNTPOFF:
1789 Type = ELF::R_386_TLS_GOTIE;
1790 break;
1791 case MCSymbolRefExpr::VK_TLSLDM:
1792 Type = ELF::R_386_TLS_LDM;
1793 break;
1794 case MCSymbolRefExpr::VK_DTPOFF:
1795 Type = ELF::R_386_TLS_LDO_32;
1796 break;
Nick Lewyckye0b87032011-06-04 17:38:07 +00001797 case MCSymbolRefExpr::VK_GOTTPOFF:
1798 Type = ELF::R_386_TLS_IE_32;
1799 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001800 }
1801 break;
1802 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001803 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001804 case FK_Data_1: Type = ELF::R_386_8; break;
1805 }
1806 }
1807 }
1808
1809 if (RelocNeedsGOT(Modifier))
1810 NeedsGOT = true;
1811
Jason W Kim56a39902010-12-06 21:57:34 +00001812 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001813}
Akira Hatanaka291512f2011-09-30 21:55:40 +00001814
1815MipsELFObjectWriter::MipsELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1816 raw_ostream &_OS,
1817 bool IsLittleEndian)
1818 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {}
1819
1820MipsELFObjectWriter::~MipsELFObjectWriter() {}
1821
1822unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
1823 const MCFixup &Fixup,
1824 bool IsPCRel,
1825 bool IsRelocWithSymbol,
1826 int64_t Addend) {
1827 // tbd
1828 return 1;
1829}