blob: ae29753582f108e69315531fa2866623d53f2c01 [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"
Evan Chenga7cfc082011-07-23 00:45:41 +000028#include "llvm/ADT/StringSwitch.h"
Matt Fleming3565a062010-08-16 18:57:57 +000029
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +000030#include "../Target/Mips/MCTargetDesc/MipsFixupKinds.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000031#include "../Target/PowerPC/MCTargetDesc/PPCFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000032
33#include <vector>
34using namespace llvm;
35
Jason W Kime964d112011-05-11 22:53:06 +000036#undef DEBUG_TYPE
37#define DEBUG_TYPE "reloc-info"
38
Jan Sjödin2ddfd952011-02-28 21:45:04 +000039bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
40 const MCFixupKindInfo &FKI =
41 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
42
43 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
44}
45
46bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
47 switch (Variant) {
48 default:
49 return false;
50 case MCSymbolRefExpr::VK_GOT:
51 case MCSymbolRefExpr::VK_PLT:
52 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +000053 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +000054 case MCSymbolRefExpr::VK_TPOFF:
55 case MCSymbolRefExpr::VK_TLSGD:
56 case MCSymbolRefExpr::VK_GOTTPOFF:
57 case MCSymbolRefExpr::VK_INDNTPOFF:
58 case MCSymbolRefExpr::VK_NTPOFF:
59 case MCSymbolRefExpr::VK_GOTNTPOFF:
60 case MCSymbolRefExpr::VK_TLSLDM:
61 case MCSymbolRefExpr::VK_DTPOFF:
62 case MCSymbolRefExpr::VK_TLSLD:
63 return true;
64 }
65}
66
Jason W Kimd3443e92010-11-15 16:18:39 +000067ELFObjectWriter::~ELFObjectWriter()
68{}
69
Matt Fleming3565a062010-08-16 18:57:57 +000070// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000071void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
72 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000073 // ELF Header
74 // ----------
75 //
76 // Note
77 // ----
78 // emitWord method behaves differently for ELF32 and ELF64, writing
79 // 4 bytes in the former and 8 in the latter.
80
81 Write8(0x7f); // e_ident[EI_MAG0]
82 Write8('E'); // e_ident[EI_MAG1]
83 Write8('L'); // e_ident[EI_MAG2]
84 Write8('F'); // e_ident[EI_MAG3]
85
Rafael Espindolabff66a82010-12-18 03:27:34 +000086 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000087
88 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000089 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +000090
91 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +000092 // e_ident[EI_OSABI]
Rafael Espindoladc9a8a32011-12-21 17:00:36 +000093 Write8(TargetObjectWriter->getOSABI());
Matt Fleming3565a062010-08-16 18:57:57 +000094 Write8(0); // e_ident[EI_ABIVERSION]
95
96 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
97
98 Write16(ELF::ET_REL); // e_type
99
Rafael Espindolabff66a82010-12-18 03:27:34 +0000100 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000101
102 Write32(ELF::EV_CURRENT); // e_version
103 WriteWord(0); // e_entry, no entry point in .o file
104 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000105 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000106 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000107
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000108 // e_flags = whatever the target wants
Rafael Espindolae8526d02011-12-21 20:09:46 +0000109 Write32(getEFlags());
Matt Fleming3565a062010-08-16 18:57:57 +0000110
111 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000112 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000113
114 Write16(0); // e_phentsize = prog header entry size
115 Write16(0); // e_phnum = # prog header entries = 0
116
117 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000118 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000119
120 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000121 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000122 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000123 else
124 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000125
126 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000127 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000128 Write16(ELF::SHN_XINDEX);
129 else
130 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000131}
132
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000133void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
134 MCDataFragment *ShndxF,
135 uint64_t name,
136 uint8_t info, uint64_t value,
137 uint64_t size, uint8_t other,
138 uint32_t shndx,
139 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000140 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000141 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000142 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000143 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000144 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000145 }
146
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000147 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
148 uint16_t(ELF::SHN_XINDEX) : shndx;
149
Rafael Espindolabff66a82010-12-18 03:27:34 +0000150 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000151 String32(*SymtabF, name); // st_name
152 String8(*SymtabF, info); // st_info
153 String8(*SymtabF, other); // st_other
154 String16(*SymtabF, Index); // st_shndx
155 String64(*SymtabF, value); // st_value
156 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000157 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000158 String32(*SymtabF, name); // st_name
159 String32(*SymtabF, value); // st_value
160 String32(*SymtabF, size); // st_size
161 String8(*SymtabF, info); // st_info
162 String8(*SymtabF, other); // st_other
163 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000164 }
165}
166
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000167uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
168 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000169 if (Data.isCommon() && Data.isExternal())
170 return Data.getCommonAlignment();
171
172 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000173
174 if (Symbol.isAbsolute() && Symbol.isVariable()) {
175 if (const MCExpr *Value = Symbol.getVariableValue()) {
176 int64_t IntValue;
177 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000178 return (uint64_t)IntValue;
Roman Divackyd1491862010-12-20 21:14:39 +0000179 }
180 }
181
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000182 if (!Symbol.isInSection())
183 return 0;
184
Rafael Espindola64695402011-05-16 16:17:21 +0000185
186 if (Data.getFragment()) {
187 if (Data.getFlags() & ELF_Other_ThumbFunc)
188 return Layout.getSymbolOffset(&Data)+1;
189 else
190 return Layout.getSymbolOffset(&Data);
191 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000192
193 return 0;
194}
195
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000196void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
197 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000198 // The presence of symbol versions causes undefined symbols and
199 // versions declared with @@@ to be renamed.
200
201 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
202 ie = Asm.symbol_end(); it != ie; ++it) {
203 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000204 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000205 MCSymbolData &SD = Asm.getSymbolData(Symbol);
206
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000207 // Not an alias.
208 if (&Symbol == &Alias)
209 continue;
210
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000211 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000212 size_t Pos = AliasName.find('@');
213 if (Pos == StringRef::npos)
214 continue;
215
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000216 // Aliases defined with .symvar copy the binding from the symbol they alias.
217 // This is the first place we are able to copy this information.
218 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000219 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000220
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000221 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000222 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
223 continue;
224
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000225 // FIXME: produce a better error message.
226 if (Symbol.isUndefined() && Rest.startswith("@@") &&
227 !Rest.startswith("@@@"))
228 report_fatal_error("A @@ version cannot be undefined");
229
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000230 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000231 }
232}
233
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000234void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
235 MCDataFragment *ShndxF,
236 ELFSymbolData &MSD,
237 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000238 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000239 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000240 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000241
Rafael Espindola7be2c332010-10-31 00:16:26 +0000242 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
243 Data.getSymbol().isVariable();
244
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000245 uint8_t Binding = MCELF::GetBinding(OrigData);
246 uint8_t Visibility = MCELF::GetVisibility(OrigData);
247 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000248
249 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
250 uint8_t Other = Visibility;
251
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000252 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000253 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000254
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000255 assert(!(Data.isCommon() && !Data.isExternal()));
256
Rafael Espindolaf0121242010-12-22 16:03:00 +0000257 const MCExpr *ESize = Data.getSize();
258 if (ESize) {
259 int64_t Res;
260 if (!ESize->EvaluateAsAbsolute(Res, Layout))
261 report_fatal_error("Size expression must be absolute.");
262 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000263 }
264
265 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000266 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
267 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000268}
269
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000270void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
271 MCDataFragment *ShndxF,
272 const MCAssembler &Asm,
273 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000274 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000275 // The string table must be emitted first because we need the index
276 // into the string table for all the symbol names.
277 assert(StringTable.size() && "Missing string table");
278
279 // FIXME: Make sure the start of the symbol table is aligned.
280
281 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000282 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000283
284 // Write the symbol table entries.
285 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
286 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
287 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000288 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000289 }
290
Rafael Espindola71859c62010-09-16 19:46:31 +0000291 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000292 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
293 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000294 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000295 static_cast<const MCSectionELF&>(i->getSection());
296 if (Section.getType() == ELF::SHT_RELA ||
297 Section.getType() == ELF::SHT_REL ||
298 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000299 Section.getType() == ELF::SHT_SYMTAB ||
300 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000301 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000302 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000303 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
304 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000305 LastLocalSymbolIndex++;
306 }
Matt Fleming3565a062010-08-16 18:57:57 +0000307
308 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
309 ELFSymbolData &MSD = ExternalSymbolData[i];
310 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000311 assert(((Data.getFlags() & ELF_STB_Global) ||
312 (Data.getFlags() & ELF_STB_Weak)) &&
313 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000314 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000315 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000316 LastLocalSymbolIndex++;
317 }
318
319 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
320 ELFSymbolData &MSD = UndefinedSymbolData[i];
321 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000322 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000323 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000324 LastLocalSymbolIndex++;
325 }
326}
327
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000328const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
329 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000330 const MCFragment &F,
331 const MCFixup &Fixup,
332 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000333 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000334 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
335 const MCSymbol *Renamed = Renames.lookup(&Symbol);
336 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000337
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000338 if (ASymbol.isUndefined()) {
339 if (Renamed)
340 return Renamed;
341 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000342 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000343
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000344 if (SD.isExternal()) {
345 if (Renamed)
346 return Renamed;
347 return &Symbol;
348 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000349
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000350 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000351 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000352 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000353
Rafael Espindola25958732010-11-24 21:57:39 +0000354 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000355 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000356
Rafael Espindola25958732010-11-24 21:57:39 +0000357 if (secKind.isThreadLocal()) {
358 if (Renamed)
359 return Renamed;
360 return &Symbol;
361 }
362
Rafael Espindola8cecf252010-10-06 16:23:36 +0000363 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000364 const MCSectionELF &Sec2 =
365 static_cast<const MCSectionELF&>(F.getParent()->getSection());
366
Rafael Espindola8cecf252010-10-06 16:23:36 +0000367 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000368 (Kind == MCSymbolRefExpr::VK_PLT ||
369 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000370 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000371 if (Renamed)
372 return Renamed;
373 return &Symbol;
374 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000375
Rafael Espindola1c130262011-01-23 04:43:11 +0000376 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000377 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000378 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000379 if (Renamed)
380 return Renamed;
381 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000382 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000383
Jason W Kime964d112011-05-11 22:53:06 +0000384 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
385
Rafael Espindola73ffea42010-09-25 05:42:19 +0000386}
387
Matt Fleming3565a062010-08-16 18:57:57 +0000388
Jason W Kim56a39902010-12-06 21:57:34 +0000389void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
390 const MCAsmLayout &Layout,
391 const MCFragment *Fragment,
392 const MCFixup &Fixup,
393 MCValue Target,
394 uint64_t &FixedValue) {
395 int64_t Addend = 0;
396 int Index = 0;
397 int64_t Value = Target.getConstant();
398 const MCSymbol *RelocSymbol = NULL;
399
Rafael Espindola127a6a42010-12-17 07:28:17 +0000400 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000401 if (!Target.isAbsolute()) {
402 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
403 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000404 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000405
406 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
407 const MCSymbol &SymbolB = RefB->getSymbol();
408 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
409 IsPCRel = true;
410
411 // Offset of the symbol in the section
412 int64_t a = Layout.getSymbolOffset(&SDB);
413
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000414 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000415 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
416 Value += b - a;
417 }
418
419 if (!RelocSymbol) {
420 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
421 MCFragment *F = SD.getFragment();
422
423 Index = F->getParent()->getOrdinal() + 1;
424
425 // Offset of the symbol in the section
426 Value += Layout.getSymbolOffset(&SD);
427 } else {
428 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
429 WeakrefUsedInReloc.insert(RelocSymbol);
430 else
431 UsedInReloc.insert(RelocSymbol);
432 Index = -1;
433 }
434 Addend = Value;
435 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000436 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000437 Value = 0;
438 }
439
440 FixedValue = Value;
441 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
442 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000443 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
444 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
445 if (RelocNeedsGOT(Modifier))
446 NeedsGOT = true;
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 Divacky2a66cea2011-08-04 19:08:19 +0000450
451 adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000452
Rafael Espindolabff66a82010-12-18 03:27:34 +0000453 if (!hasRelocationAddend())
454 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000455
456 if (is64Bit())
457 assert(isInt<64>(Addend));
458 else
459 assert(isInt<32>(Addend));
460
Jason W Kim56a39902010-12-06 21:57:34 +0000461 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
462 Relocations[Fragment->getParent()].push_back(ERE);
463}
464
465
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000466uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000467ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
468 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000469 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000470 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000471}
472
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000473bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
474 const MCSymbolData &Data,
475 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000476 if (Data.getFlags() & ELF_Other_Weakref)
477 return false;
478
Rafael Espindolabd701182010-10-19 19:31:37 +0000479 if (Used)
480 return true;
481
Rafael Espindola88182132010-10-27 15:18:17 +0000482 if (Renamed)
483 return false;
484
Rafael Espindola737cd212010-10-05 18:01:23 +0000485 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000486
Rafael Espindolad1798862010-10-29 23:09:31 +0000487 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
488 return true;
489
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000490 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000491 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
492 return false;
493
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000494 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000495 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000496 return false;
497
Rafael Espindola737cd212010-10-05 18:01:23 +0000498 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
499 return false;
500
Rafael Espindolabd701182010-10-19 19:31:37 +0000501 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000502 return false;
503
504 return true;
505}
506
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000507bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
508 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000509 if (Data.isExternal())
510 return false;
511
512 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000513 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000514
515 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
516 if (isSignature && !isUsedInReloc)
517 return true;
518
Rafael Espindola737cd212010-10-05 18:01:23 +0000519 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000520 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000521
522 return true;
523}
524
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000525void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000526 SectionIndexMapTy &SectionIndexMap,
527 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000528 unsigned Index = 1;
529 for (MCAssembler::iterator it = Asm.begin(),
530 ie = Asm.end(); it != ie; ++it) {
531 const MCSectionELF &Section =
532 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000533 if (Section.getType() != ELF::SHT_GROUP)
534 continue;
535 SectionIndexMap[&Section] = Index++;
536 }
537
538 for (MCAssembler::iterator it = Asm.begin(),
539 ie = Asm.end(); it != ie; ++it) {
540 const MCSectionELF &Section =
541 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000542 if (Section.getType() == ELF::SHT_GROUP ||
543 Section.getType() == ELF::SHT_REL ||
544 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000545 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000546 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000547 const MCSectionELF *RelSection = RelMap.lookup(&Section);
548 if (RelSection)
549 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000550 }
551}
552
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000553void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000554 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000555 RevGroupMapTy RevGroupMap,
556 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000557 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000558 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000559 if (NeedsGOT) {
560 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
561 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
562 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
563 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000564 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000565 }
566
Matt Fleming3565a062010-08-16 18:57:57 +0000567 // Index 0 is always the empty string.
568 StringMap<uint64_t> StringIndexMap;
569 StringTable += '\x00';
570
Rafael Espindolad5321da2011-04-07 23:21:52 +0000571 // FIXME: We could optimize suffixes in strtab in the same way we
572 // optimize them in shstrtab.
573
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000574 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000575 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
576 ie = Asm.symbol_end(); it != ie; ++it) {
577 const MCSymbol &Symbol = it->getSymbol();
578
Rafael Espindola484291c2010-11-01 14:28:48 +0000579 bool Used = UsedInReloc.count(&Symbol);
580 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000581 bool isSignature = RevGroupMap.count(&Symbol);
582
583 if (!isInSymtab(Asm, *it,
584 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000585 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000586 continue;
587
Matt Fleming3565a062010-08-16 18:57:57 +0000588 ELFSymbolData MSD;
589 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000590 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000591
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000592 // Undefined symbols are global, but this is the first place we
593 // are able to set it.
594 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000595 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000596 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000597 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
598 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000599 }
600
Rafael Espindola484291c2010-11-01 14:28:48 +0000601 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000602 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000603
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000604 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000605 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000606 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000607 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000608 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000609 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000610 if (isSignature && !Used)
611 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
612 else
613 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000614 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000615 const MCSectionELF &Section =
616 static_cast<const MCSectionELF&>(RefSymbol.getSection());
617 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000618 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
619 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000620 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000621 }
622
Rafael Espindola88182132010-10-27 15:18:17 +0000623 // The @@@ in symbol version is replaced with @ in undefined symbols and
624 // @@ in defined ones.
625 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000626 SmallString<32> Buf;
627
Rafael Espindola88182132010-10-27 15:18:17 +0000628 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000629 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000630 Buf += Name.substr(0, Pos);
631 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
632 Buf += Name.substr(Pos + Skip);
633 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000634 }
635
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000636 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000637 if (!Entry) {
638 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000639 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000640 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000641 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000642 MSD.StringIndex = Entry;
643 if (MSD.SectionIndex == ELF::SHN_UNDEF)
644 UndefinedSymbolData.push_back(MSD);
645 else if (Local)
646 LocalSymbolData.push_back(MSD);
647 else
648 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000649 }
650
651 // Symbols are required to be in lexicographic order.
652 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
653 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
654 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
655
656 // Set the symbol indices. Local symbols must come before all other
657 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000658 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000659 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
660 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000661
662 Index += NumRegularSections;
663
Matt Fleming3565a062010-08-16 18:57:57 +0000664 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
665 ExternalSymbolData[i].SymbolData->setIndex(Index++);
666 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
667 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000668
669 if (NumRegularSections > ELF::SHN_LORESERVE)
670 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000671}
672
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000673void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
674 MCAsmLayout &Layout,
675 RelMapTy &RelMap) {
676 for (MCAssembler::const_iterator it = Asm.begin(),
677 ie = Asm.end(); it != ie; ++it) {
678 const MCSectionData &SD = *it;
679 if (Relocations[&SD].empty())
680 continue;
681
Matt Fleming3565a062010-08-16 18:57:57 +0000682 MCContext &Ctx = Asm.getContext();
Matt Fleming3565a062010-08-16 18:57:57 +0000683 const MCSectionELF &Section =
684 static_cast<const MCSectionELF&>(SD.getSection());
685
686 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000687 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000688 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000689
690 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000691 if (hasRelocationAddend())
692 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000693 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000694 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000695
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000696 const MCSectionELF *RelaSection =
697 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
698 ELF::SHT_RELA : ELF::SHT_REL, 0,
699 SectionKind::getReadOnly(),
700 EntrySize, "");
701 RelMap[&Section] = RelaSection;
702 Asm.getOrCreateSectionData(*RelaSection);
703 }
704}
Matt Fleming3565a062010-08-16 18:57:57 +0000705
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000706void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
707 const RelMapTy &RelMap) {
708 for (MCAssembler::const_iterator it = Asm.begin(),
709 ie = Asm.end(); it != ie; ++it) {
710 const MCSectionData &SD = *it;
711 const MCSectionELF &Section =
712 static_cast<const MCSectionELF&>(SD.getSection());
713
714 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
715 if (!RelaSection)
716 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000717 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000718 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000719
720 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000721 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming3565a062010-08-16 18:57:57 +0000722 }
723}
724
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000725void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
726 uint64_t Flags, uint64_t Address,
727 uint64_t Offset, uint64_t Size,
728 uint32_t Link, uint32_t Info,
729 uint64_t Alignment,
730 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000731 Write32(Name); // sh_name: index into string table
732 Write32(Type); // sh_type
733 WriteWord(Flags); // sh_flags
734 WriteWord(Address); // sh_addr
735 WriteWord(Offset); // sh_offset
736 WriteWord(Size); // sh_size
737 Write32(Link); // sh_link
738 Write32(Info); // sh_info
739 WriteWord(Alignment); // sh_addralign
740 WriteWord(EntrySize); // sh_entsize
741}
742
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000743void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
744 MCDataFragment *F,
745 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000746 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
747 // sort by the r_offset just like gnu as does
748 array_pod_sort(Relocs.begin(), Relocs.end());
749
750 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
751 ELFRelocationEntry entry = Relocs[e - i - 1];
752
Rafael Espindola12203cc2010-11-21 00:48:25 +0000753 if (!entry.Index)
754 ;
755 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000756 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
757 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000758 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000759 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000760 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000761
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000762 struct ELF::Elf64_Rela ERE64;
763 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000764 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000765
Rafael Espindolabff66a82010-12-18 03:27:34 +0000766 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000767 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000768 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000769 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000770
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000771 struct ELF::Elf32_Rela ERE32;
772 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000773 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000774
Rafael Espindolabff66a82010-12-18 03:27:34 +0000775 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000776 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000777 }
Matt Fleming3565a062010-08-16 18:57:57 +0000778 }
779}
780
Rafael Espindolad5321da2011-04-07 23:21:52 +0000781static int compareBySuffix(const void *a, const void *b) {
782 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
783 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
784 const StringRef &NameA = secA->getSectionName();
785 const StringRef &NameB = secB->getSectionName();
786 const unsigned sizeA = NameA.size();
787 const unsigned sizeB = NameB.size();
788 const unsigned len = std::min(sizeA, sizeB);
789 for (unsigned int i = 0; i < len; ++i) {
790 char ca = NameA[sizeA - i - 1];
791 char cb = NameB[sizeB - i - 1];
792 if (ca != cb)
793 return cb - ca;
794 }
795
796 return sizeB - sizeA;
797}
798
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000799void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
800 MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000801 SectionIndexMapTy &SectionIndexMap,
802 const RelMapTy &RelMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000803 MCContext &Ctx = Asm.getContext();
804 MCDataFragment *F;
805
Rafael Espindolabff66a82010-12-18 03:27:34 +0000806 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +0000807
Rafael Espindola38738bf2010-09-22 19:04:41 +0000808 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000809 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000810 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000811 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000812 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
813 ShstrtabSD.setAlignment(1);
Rafael Espindola71859c62010-09-16 19:46:31 +0000814
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000815 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000816 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
817 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000818 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000819 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +0000820 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000821
822 MCSectionData *SymtabShndxSD = NULL;
823
824 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000825 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000826 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000827 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000828 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
829 SymtabShndxSD->setAlignment(4);
830 }
Matt Fleming3565a062010-08-16 18:57:57 +0000831
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000832 const MCSectionELF *StrtabSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000833 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000834 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +0000835 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
836 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000837
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000838 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
839
840 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
841 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
842 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola71859c62010-09-16 19:46:31 +0000843
844 // Symbol table
845 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000846 MCDataFragment *ShndxF = NULL;
847 if (NeedsSymtabShndx) {
848 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000849 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000850 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +0000851
Matt Fleming3565a062010-08-16 18:57:57 +0000852 F = new MCDataFragment(&StrtabSD);
853 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +0000854
Matt Fleming3565a062010-08-16 18:57:57 +0000855 F = new MCDataFragment(&ShstrtabSD);
856
Rafael Espindolad5321da2011-04-07 23:21:52 +0000857 std::vector<const MCSectionELF*> Sections;
858 for (MCAssembler::const_iterator it = Asm.begin(),
859 ie = Asm.end(); it != ie; ++it) {
860 const MCSectionELF &Section =
861 static_cast<const MCSectionELF&>(it->getSection());
862 Sections.push_back(&Section);
863 }
864 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
865
Matt Fleming3565a062010-08-16 18:57:57 +0000866 // Section header string table.
867 //
868 // The first entry of a string table holds a null character so skip
869 // section 0.
870 uint64_t Index = 1;
871 F->getContents() += '\x00';
872
Rafael Espindolad5321da2011-04-07 23:21:52 +0000873 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
874 const MCSectionELF &Section = *Sections[I];
Matt Fleming3565a062010-08-16 18:57:57 +0000875
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000876 StringRef Name = Section.getSectionName();
Rafael Espindolad5321da2011-04-07 23:21:52 +0000877 if (I != 0) {
878 StringRef PreviousName = Sections[I - 1]->getSectionName();
879 if (PreviousName.endswith(Name)) {
880 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
881 continue;
882 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000883 }
Matt Fleming3565a062010-08-16 18:57:57 +0000884 // Remember the index into the string table so we can write it
885 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000886 SectionStringTableIndex[&Section] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +0000887
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000888 Index += Name.size() + 1;
889 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +0000890 F->getContents() += '\x00';
891 }
Rafael Espindola70703872010-09-30 02:22:20 +0000892}
893
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000894void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
895 MCAsmLayout &Layout,
896 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000897 RevGroupMapTy &RevGroupMap,
898 SectionIndexMapTy &SectionIndexMap,
899 const RelMapTy &RelMap) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000900 // Create the .note.GNU-stack section if needed.
901 MCContext &Ctx = Asm.getContext();
902 if (Asm.getNoExecStack()) {
903 const MCSectionELF *GnuStackSection =
904 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
905 SectionKind::getReadOnly());
906 Asm.getOrCreateSectionData(*GnuStackSection);
907 }
908
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000909 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000910 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
911 it != ie; ++it) {
912 const MCSectionELF &Section =
913 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000914 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000915 continue;
916
917 const MCSymbol *SignatureSymbol = Section.getGroup();
918 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000919 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000920 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000921 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000922 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
923 Data.setAlignment(4);
924 MCDataFragment *F = new MCDataFragment(&Data);
925 String32(*F, ELF::GRP_COMDAT);
926 }
927 GroupMap[Group] = SignatureSymbol;
928 }
929
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000930 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
931
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000932 // Add sections to the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000933 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000934 it != ie; ++it) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000935 const MCSectionELF &Section =
936 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +0000937 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000938 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000939 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000940 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
941 // FIXME: we could use the previous fragment
942 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000943 unsigned Index = SectionIndexMap.lookup(&Section);
944 String32(*F, Index);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000945 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000946}
947
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000948void ELFObjectWriter::WriteSection(MCAssembler &Asm,
949 const SectionIndexMapTy &SectionIndexMap,
950 uint32_t GroupSymbolIndex,
951 uint64_t Offset, uint64_t Size,
952 uint64_t Alignment,
953 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000954 uint64_t sh_link = 0;
955 uint64_t sh_info = 0;
956
957 switch(Section.getType()) {
958 case ELF::SHT_DYNAMIC:
959 sh_link = SectionStringTableIndex[&Section];
960 sh_info = 0;
961 break;
962
963 case ELF::SHT_REL:
964 case ELF::SHT_RELA: {
965 const MCSectionELF *SymtabSection;
966 const MCSectionELF *InfoSection;
967 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
968 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000969 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000970 sh_link = SectionIndexMap.lookup(SymtabSection);
971 assert(sh_link && ".symtab not found");
972
973 // Remove ".rel" and ".rela" prefixes.
974 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
975 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
976
977 InfoSection = Asm.getContext().getELFSection(SectionName,
978 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000979 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000980 sh_info = SectionIndexMap.lookup(InfoSection);
981 break;
982 }
983
984 case ELF::SHT_SYMTAB:
985 case ELF::SHT_DYNSYM:
986 sh_link = StringTableIndex;
987 sh_info = LastLocalSymbolIndex;
988 break;
989
990 case ELF::SHT_SYMTAB_SHNDX:
991 sh_link = SymbolTableIndex;
992 break;
993
994 case ELF::SHT_PROGBITS:
995 case ELF::SHT_STRTAB:
996 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +0000997 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000998 case ELF::SHT_NULL:
999 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001000 case ELF::SHT_INIT_ARRAY:
1001 case ELF::SHT_FINI_ARRAY:
1002 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001003 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001004 // Nothing to do.
1005 break;
1006
Nick Lewycky4a8d43e2011-10-07 23:28:32 +00001007 case ELF::SHT_GROUP:
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001008 sh_link = SymbolTableIndex;
1009 sh_info = GroupSymbolIndex;
1010 break;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001011
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001012 default:
1013 assert(0 && "FIXME: sh_type value not supported!");
1014 break;
1015 }
1016
1017 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1018 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1019 Alignment, Section.getEntrySize());
1020}
1021
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001022bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001023 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001024 !SD.getSection().isVirtualSection();
1025}
1026
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001027uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001028 uint64_t Ret = 0;
1029 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1030 ++i) {
1031 const MCFragment &F = *i;
1032 assert(F.getKind() == MCFragment::FT_Data);
1033 Ret += cast<MCDataFragment>(F).getContents().size();
1034 }
1035 return Ret;
1036}
1037
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001038uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1039 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001040 if (IsELFMetaDataSection(SD))
1041 return DataSectionSize(SD);
1042 return Layout.getSectionFileSize(&SD);
1043}
1044
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001045uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1046 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001047 if (IsELFMetaDataSection(SD))
1048 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001049 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001050}
1051
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001052void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1053 const MCAsmLayout &Layout,
1054 const MCSectionELF &Section) {
1055 uint64_t FileOff = OS.tell();
1056 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1057
1058 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1059 WriteZeros(Padding);
1060 FileOff += Padding;
1061
1062 FileOff += GetSectionFileSize(Layout, SD);
1063
1064 if (IsELFMetaDataSection(SD)) {
1065 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1066 ++i) {
1067 const MCFragment &F = *i;
1068 assert(F.getKind() == MCFragment::FT_Data);
1069 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1070 }
1071 } else {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001072 Asm.writeSectionData(&SD, Layout);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001073 }
1074}
1075
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001076void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1077 const GroupMapTy &GroupMap,
1078 const MCAsmLayout &Layout,
1079 const SectionIndexMapTy &SectionIndexMap,
1080 const SectionOffsetMapTy &SectionOffsetMap) {
1081 const unsigned NumSections = Asm.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001082
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001083 std::vector<const MCSectionELF*> Sections;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001084 Sections.resize(NumSections - 1);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001085
1086 for (SectionIndexMapTy::const_iterator i=
1087 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1088 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001089 Sections[p.second - 1] = p.first;
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001090 }
1091
Matt Fleming3565a062010-08-16 18:57:57 +00001092 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001093 uint64_t FirstSectionSize =
1094 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1095 uint32_t FirstSectionLink =
1096 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1097 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001098
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001099 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001100 const MCSectionELF &Section = *Sections[i];
1101 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1102 uint32_t GroupSymbolIndex;
1103 if (Section.getType() != ELF::SHT_GROUP)
1104 GroupSymbolIndex = 0;
1105 else
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001106 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1107 GroupMap.lookup(&Section));
Matt Fleming3565a062010-08-16 18:57:57 +00001108
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001109 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001110
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001111 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001112 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001113 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001114 }
1115}
1116
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001117void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1118 std::vector<const MCSectionELF*> &Sections) {
1119 for (MCAssembler::iterator it = Asm.begin(),
1120 ie = Asm.end(); it != ie; ++it) {
1121 const MCSectionELF &Section =
1122 static_cast<const MCSectionELF &>(it->getSection());
1123 if (Section.getType() == ELF::SHT_GROUP)
1124 Sections.push_back(&Section);
1125 }
1126
1127 for (MCAssembler::iterator it = Asm.begin(),
1128 ie = Asm.end(); it != ie; ++it) {
1129 const MCSectionELF &Section =
1130 static_cast<const MCSectionELF &>(it->getSection());
1131 if (Section.getType() != ELF::SHT_GROUP &&
1132 Section.getType() != ELF::SHT_REL &&
1133 Section.getType() != ELF::SHT_RELA)
1134 Sections.push_back(&Section);
1135 }
1136
1137 for (MCAssembler::iterator it = Asm.begin(),
1138 ie = Asm.end(); it != ie; ++it) {
1139 const MCSectionELF &Section =
1140 static_cast<const MCSectionELF &>(it->getSection());
1141 if (Section.getType() == ELF::SHT_REL ||
1142 Section.getType() == ELF::SHT_RELA)
1143 Sections.push_back(&Section);
1144 }
1145}
1146
1147void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1148 const MCAsmLayout &Layout) {
1149 GroupMapTy GroupMap;
1150 RevGroupMapTy RevGroupMap;
1151 SectionIndexMapTy SectionIndexMap;
1152
1153 unsigned NumUserSections = Asm.size();
1154
1155 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1156 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1157
1158 const unsigned NumUserAndRelocSections = Asm.size();
1159 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1160 RevGroupMap, SectionIndexMap, RelMap);
1161 const unsigned AllSections = Asm.size();
1162 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1163
1164 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1165
1166 // Compute symbol table information.
1167 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1168
1169
1170 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1171
1172 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1173 const_cast<MCAsmLayout&>(Layout),
1174 SectionIndexMap,
1175 RelMap);
1176
1177 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1178 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1179 sizeof(ELF::Elf32_Ehdr);
1180 uint64_t FileOff = HeaderSize;
1181
1182 std::vector<const MCSectionELF*> Sections;
1183 ComputeSectionOrder(Asm, Sections);
1184 unsigned NumSections = Sections.size();
1185 SectionOffsetMapTy SectionOffsetMap;
1186 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1187 const MCSectionELF &Section = *Sections[i];
1188 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1189
1190 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1191
1192 // Remember the offset into the file for this section.
1193 SectionOffsetMap[&Section] = FileOff;
1194
1195 // Get the size of the section in the output file (including padding).
1196 FileOff += GetSectionFileSize(Layout, SD);
1197 }
1198
1199 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1200
1201 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1202
1203 uint64_t SectionHeaderEntrySize = is64Bit() ?
1204 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1205 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1206
1207 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1208 const MCSectionELF &Section = *Sections[i];
1209 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1210
1211 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1212
1213 // Remember the offset into the file for this section.
1214 SectionOffsetMap[&Section] = FileOff;
1215
1216 // Get the size of the section in the output file (including padding).
1217 FileOff += GetSectionFileSize(Layout, SD);
1218 }
1219
1220 // Write out the ELF header ...
1221 WriteHeader(SectionHeaderOffset, NumSections + 1);
1222
1223 // ... then the regular sections ...
1224 // + because of .shstrtab
1225 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1226 WriteDataSectionData(Asm, Layout, *Sections[i]);
1227
1228 FileOff = OS.tell();
1229 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1230 WriteZeros(Padding);
1231
1232 // ... then the section header table ...
1233 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1234 SectionOffsetMap);
1235
1236 FileOff = OS.tell();
1237
Nick Lewyckyaaae3f62011-10-07 20:56:23 +00001238 // ... and then the remaining sections ...
Rafael Espindola7c18fa82011-03-20 18:44:20 +00001239 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1240 WriteDataSectionData(Asm, Layout, *Sections[i]);
1241}
1242
Eli Friedman78c1e172011-03-03 07:24:36 +00001243bool
1244ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1245 const MCSymbolData &DataA,
1246 const MCFragment &FB,
1247 bool InSet,
1248 bool IsPCRel) const {
1249 if (DataA.getFlags() & ELF_STB_Weak)
1250 return false;
1251 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1252 Asm, DataA, FB,InSet, IsPCRel);
1253}
1254
Rafael Espindola6024c972010-12-17 17:45:22 +00001255MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1256 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001257 bool IsLittleEndian) {
1258 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001259 case ELF::EM_386:
1260 case ELF::EM_X86_64:
Jason W Kimd3443e92010-11-15 16:18:39 +00001261 case ELF::EM_ARM:
Rafael Espindola69bbda02011-12-22 00:37:50 +00001262 return new ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001263 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001264 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001265 case ELF::EM_PPC:
1266 case ELF::EM_PPC64:
1267 return new PPCELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Akira Hatanaka291512f2011-09-30 21:55:40 +00001268 case ELF::EM_MIPS:
1269 return new MipsELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001270 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001271 }
1272}
1273
Rafael Espindolaedae8e12011-12-21 17:30:17 +00001274unsigned ELFObjectWriter::GetRelocType(const MCValue &Target,
1275 const MCFixup &Fixup,
1276 bool IsPCRel,
1277 bool IsRelocWithSymbol,
1278 int64_t Addend) const {
1279 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
1280 IsRelocWithSymbol, Addend);
1281}
1282
Jason W Kimd3443e92010-11-15 16:18:39 +00001283/// START OF SUBCLASSES for ELFObjectWriter
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001284//===- PPCELFObjectWriter -------------------------------------------===//
1285
1286PPCELFObjectWriter::PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1287 raw_ostream &_OS,
1288 bool IsLittleEndian)
1289 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1290}
1291
1292PPCELFObjectWriter::~PPCELFObjectWriter() {
1293}
1294
1295unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
1296 const MCFixup &Fixup,
1297 bool IsPCRel,
1298 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001299 int64_t Addend) const {
Roman Divacky2c0d69f2011-08-02 15:51:38 +00001300 // determine the type of the relocation
1301 unsigned Type;
1302 if (IsPCRel) {
1303 switch ((unsigned)Fixup.getKind()) {
1304 default:
1305 llvm_unreachable("Unimplemented");
1306 case PPC::fixup_ppc_br24:
1307 Type = ELF::R_PPC_REL24;
1308 break;
1309 case FK_PCRel_4:
1310 Type = ELF::R_PPC_REL32;
1311 break;
1312 }
1313 } else {
1314 switch ((unsigned)Fixup.getKind()) {
1315 default: llvm_unreachable("invalid fixup kind!");
1316 case PPC::fixup_ppc_br24:
1317 Type = ELF::R_PPC_ADDR24;
1318 break;
1319 case PPC::fixup_ppc_brcond14:
1320 Type = ELF::R_PPC_ADDR14_BRTAKEN; // XXX: or BRNTAKEN?_
1321 break;
1322 case PPC::fixup_ppc_ha16:
1323 Type = ELF::R_PPC_ADDR16_HA;
1324 break;
1325 case PPC::fixup_ppc_lo16:
1326 Type = ELF::R_PPC_ADDR16_LO;
1327 break;
1328 case PPC::fixup_ppc_lo14:
1329 Type = ELF::R_PPC_ADDR14;
1330 break;
1331 case FK_Data_4:
1332 Type = ELF::R_PPC_ADDR32;
1333 break;
1334 case FK_Data_2:
1335 Type = ELF::R_PPC_ADDR16;
1336 break;
1337 }
1338 }
1339 return Type;
1340}
1341
Jim Grosbach946227d2011-11-15 16:46:22 +00001342void PPCELFObjectWriter::
1343adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
Roman Divacky2a66cea2011-08-04 19:08:19 +00001344 switch ((unsigned)Fixup.getKind()) {
1345 case PPC::fixup_ppc_ha16:
1346 case PPC::fixup_ppc_lo16:
1347 RelocOffset += 2;
1348 break;
1349 default:
1350 break;
1351 }
1352}
1353
Wesley Peck4b047132010-11-21 22:06:28 +00001354//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001355
Rafael Espindola31f35782010-12-17 18:01:31 +00001356MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001357 raw_ostream &_OS,
1358 bool IsLittleEndian)
1359 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001360}
1361
1362MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1363}
1364
Jason W Kim56a39902010-12-06 21:57:34 +00001365unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001366 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001367 bool IsPCRel,
1368 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001369 int64_t Addend) const {
Wesley Peck4b047132010-11-21 22:06:28 +00001370 // determine the type of the relocation
1371 unsigned Type;
1372 if (IsPCRel) {
1373 switch ((unsigned)Fixup.getKind()) {
1374 default:
1375 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001376 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001377 Type = ELF::R_MICROBLAZE_64_PCREL;
1378 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001379 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001380 Type = ELF::R_MICROBLAZE_32_PCREL;
1381 break;
1382 }
1383 } else {
1384 switch ((unsigned)Fixup.getKind()) {
1385 default: llvm_unreachable("invalid fixup kind!");
1386 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001387 Type = ((IsRelocWithSymbol || Addend !=0)
1388 ? ELF::R_MICROBLAZE_32
1389 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001390 break;
1391 case FK_Data_2:
1392 Type = ELF::R_MICROBLAZE_32;
1393 break;
1394 }
1395 }
Jason W Kim56a39902010-12-06 21:57:34 +00001396 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001397}
Jason W Kimd3443e92010-11-15 16:18:39 +00001398
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001399//===- MipsELFObjectWriter -------------------------------------------===//
1400
Akira Hatanaka291512f2011-09-30 21:55:40 +00001401MipsELFObjectWriter::MipsELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1402 raw_ostream &_OS,
1403 bool IsLittleEndian)
1404 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {}
1405
1406MipsELFObjectWriter::~MipsELFObjectWriter() {}
1407
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +00001408// FIXME: get the real EABI Version from the Triple.
Rafael Espindolae99183d2011-12-22 00:21:50 +00001409unsigned MipsELFObjectWriter::getEFlags() const {
Rafael Espindolae8526d02011-12-21 20:09:46 +00001410 return ELF::EF_MIPS_NOREORDER | ELF::EF_MIPS_ARCH_32R2;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +00001411}
1412
Bruno Cardoso Lopesa00a62a2011-12-06 03:34:42 +00001413const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1414 const MCValue &Target,
1415 const MCFragment &F,
1416 const MCFixup &Fixup,
1417 bool IsPCRel) const {
1418 assert(Target.getSymA() && "SymA cannot be 0.");
1419 const MCSymbol &Sym = Target.getSymA()->getSymbol();
1420
Akira Hatanakaf3315cf2011-12-13 02:27:40 +00001421 if (Sym.getSection().getKind().isMergeableCString() ||
1422 Sym.getSection().getKind().isMergeableConst())
Bruno Cardoso Lopesa00a62a2011-12-06 03:34:42 +00001423 return &Sym;
1424
1425 return NULL;
1426}
1427
Akira Hatanaka291512f2011-09-30 21:55:40 +00001428unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
1429 const MCFixup &Fixup,
1430 bool IsPCRel,
1431 bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +00001432 int64_t Addend) const {
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001433 // determine the type of the relocation
1434 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
1435 unsigned Kind = (unsigned)Fixup.getKind();
1436
1437 switch (Kind) {
1438 default:
1439 llvm_unreachable("invalid fixup kind!");
1440 case FK_Data_4:
1441 Type = ELF::R_MIPS_32;
1442 break;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +00001443 case FK_GPRel_4:
1444 Type = ELF::R_MIPS_GPREL32;
1445 break;
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001446 case Mips::fixup_Mips_GPREL16:
1447 Type = ELF::R_MIPS_GPREL16;
1448 break;
1449 case Mips::fixup_Mips_26:
1450 Type = ELF::R_MIPS_26;
1451 break;
1452 case Mips::fixup_Mips_CALL16:
1453 Type = ELF::R_MIPS_CALL16;
1454 break;
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001455 case Mips::fixup_Mips_GOT_Global:
1456 case Mips::fixup_Mips_GOT_Local:
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +00001457 Type = ELF::R_MIPS_GOT16;
1458 break;
1459 case Mips::fixup_Mips_HI16:
1460 Type = ELF::R_MIPS_HI16;
1461 break;
1462 case Mips::fixup_Mips_LO16:
1463 Type = ELF::R_MIPS_LO16;
1464 break;
1465 case Mips::fixup_Mips_TLSGD:
1466 Type = ELF::R_MIPS_TLS_GD;
1467 break;
1468 case Mips::fixup_Mips_GOTTPREL:
1469 Type = ELF::R_MIPS_TLS_GOTTPREL;
1470 break;
1471 case Mips::fixup_Mips_TPREL_HI:
1472 Type = ELF::R_MIPS_TLS_TPREL_HI16;
1473 break;
1474 case Mips::fixup_Mips_TPREL_LO:
1475 Type = ELF::R_MIPS_TLS_TPREL_LO16;
1476 break;
1477 case Mips::fixup_Mips_Branch_PCRel:
1478 case Mips::fixup_Mips_PC16:
1479 Type = ELF::R_MIPS_PC16;
1480 break;
1481 }
1482
1483 return Type;
Akira Hatanaka291512f2011-09-30 21:55:40 +00001484}