blob: e342429797b195be6d9e5d46dd1327fdbce87666 [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
Matt Fleming3565a062010-08-16 18:57:57 +000030#include <vector>
31using namespace llvm;
32
Jason W Kime964d112011-05-11 22:53:06 +000033#undef DEBUG_TYPE
34#define DEBUG_TYPE "reloc-info"
35
Jan Sjödin2ddfd952011-02-28 21:45:04 +000036bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
37 const MCFixupKindInfo &FKI =
38 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
39
40 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
41}
42
43bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
44 switch (Variant) {
45 default:
46 return false;
47 case MCSymbolRefExpr::VK_GOT:
48 case MCSymbolRefExpr::VK_PLT:
49 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola378e0ec2011-06-05 01:20:06 +000050 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin2ddfd952011-02-28 21:45:04 +000051 case MCSymbolRefExpr::VK_TPOFF:
52 case MCSymbolRefExpr::VK_TLSGD:
53 case MCSymbolRefExpr::VK_GOTTPOFF:
54 case MCSymbolRefExpr::VK_INDNTPOFF:
55 case MCSymbolRefExpr::VK_NTPOFF:
56 case MCSymbolRefExpr::VK_GOTNTPOFF:
57 case MCSymbolRefExpr::VK_TLSLDM:
58 case MCSymbolRefExpr::VK_DTPOFF:
59 case MCSymbolRefExpr::VK_TLSLD:
60 return true;
61 }
62}
63
Jason W Kimd3443e92010-11-15 16:18:39 +000064ELFObjectWriter::~ELFObjectWriter()
65{}
66
Matt Fleming3565a062010-08-16 18:57:57 +000067// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000068void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
69 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +000070 // ELF Header
71 // ----------
72 //
73 // Note
74 // ----
75 // emitWord method behaves differently for ELF32 and ELF64, writing
76 // 4 bytes in the former and 8 in the latter.
77
78 Write8(0x7f); // e_ident[EI_MAG0]
79 Write8('E'); // e_ident[EI_MAG1]
80 Write8('L'); // e_ident[EI_MAG2]
81 Write8('F'); // e_ident[EI_MAG3]
82
Rafael Espindolabff66a82010-12-18 03:27:34 +000083 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +000084
85 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000086 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +000087
88 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +000089 // e_ident[EI_OSABI]
Rafael Espindoladc9a8a32011-12-21 17:00:36 +000090 Write8(TargetObjectWriter->getOSABI());
Matt Fleming3565a062010-08-16 18:57:57 +000091 Write8(0); // e_ident[EI_ABIVERSION]
92
93 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
94
95 Write16(ELF::ET_REL); // e_type
96
Rafael Espindolabff66a82010-12-18 03:27:34 +000097 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +000098
99 Write32(ELF::EV_CURRENT); // e_version
100 WriteWord(0); // e_entry, no entry point in .o file
101 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000102 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000103 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000104
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000105 // e_flags = whatever the target wants
Rafael Espindolae8526d02011-12-21 20:09:46 +0000106 Write32(getEFlags());
Matt Fleming3565a062010-08-16 18:57:57 +0000107
108 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000109 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000110
111 Write16(0); // e_phentsize = prog header entry size
112 Write16(0); // e_phnum = # prog header entries = 0
113
114 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000115 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000116
117 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000118 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewyckyaaae3f62011-10-07 20:56:23 +0000119 Write16(ELF::SHN_UNDEF);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000120 else
121 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000122
123 // e_shstrndx = Section # of '.shstrtab'
Nick Lewyckyb3429d32011-10-07 20:58:24 +0000124 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000125 Write16(ELF::SHN_XINDEX);
126 else
127 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000128}
129
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000130void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
131 MCDataFragment *ShndxF,
132 uint64_t name,
133 uint8_t info, uint64_t value,
134 uint64_t size, uint8_t other,
135 uint32_t shndx,
136 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000137 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000138 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000139 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000140 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000141 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000142 }
143
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000144 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
145 uint16_t(ELF::SHN_XINDEX) : shndx;
146
Rafael Espindolabff66a82010-12-18 03:27:34 +0000147 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000148 String32(*SymtabF, name); // st_name
149 String8(*SymtabF, info); // st_info
150 String8(*SymtabF, other); // st_other
151 String16(*SymtabF, Index); // st_shndx
152 String64(*SymtabF, value); // st_value
153 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000154 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000155 String32(*SymtabF, name); // st_name
156 String32(*SymtabF, value); // st_value
157 String32(*SymtabF, size); // st_size
158 String8(*SymtabF, info); // st_info
159 String8(*SymtabF, other); // st_other
160 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000161 }
162}
163
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000164uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
165 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000166 if (Data.isCommon() && Data.isExternal())
167 return Data.getCommonAlignment();
168
169 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000170
171 if (Symbol.isAbsolute() && Symbol.isVariable()) {
172 if (const MCExpr *Value = Symbol.getVariableValue()) {
173 int64_t IntValue;
174 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000175 return (uint64_t)IntValue;
Roman Divackyd1491862010-12-20 21:14:39 +0000176 }
177 }
178
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000179 if (!Symbol.isInSection())
180 return 0;
181
Rafael Espindola64695402011-05-16 16:17:21 +0000182
183 if (Data.getFragment()) {
184 if (Data.getFlags() & ELF_Other_ThumbFunc)
185 return Layout.getSymbolOffset(&Data)+1;
186 else
187 return Layout.getSymbolOffset(&Data);
188 }
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000189
190 return 0;
191}
192
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000193void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
194 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000195 // The presence of symbol versions causes undefined symbols and
196 // versions declared with @@@ to be renamed.
197
198 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
199 ie = Asm.symbol_end(); it != ie; ++it) {
200 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000201 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000202 MCSymbolData &SD = Asm.getSymbolData(Symbol);
203
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000204 // Not an alias.
205 if (&Symbol == &Alias)
206 continue;
207
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000208 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000209 size_t Pos = AliasName.find('@');
210 if (Pos == StringRef::npos)
211 continue;
212
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000213 // Aliases defined with .symvar copy the binding from the symbol they alias.
214 // This is the first place we are able to copy this information.
215 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000216 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000217
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000218 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000219 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
220 continue;
221
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000222 // FIXME: produce a better error message.
223 if (Symbol.isUndefined() && Rest.startswith("@@") &&
224 !Rest.startswith("@@@"))
225 report_fatal_error("A @@ version cannot be undefined");
226
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000227 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000228 }
229}
230
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000231void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
232 MCDataFragment *ShndxF,
233 ELFSymbolData &MSD,
234 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000235 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000236 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000237 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000238
Rafael Espindola7be2c332010-10-31 00:16:26 +0000239 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
240 Data.getSymbol().isVariable();
241
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000242 uint8_t Binding = MCELF::GetBinding(OrigData);
243 uint8_t Visibility = MCELF::GetVisibility(OrigData);
244 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000245
246 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
247 uint8_t Other = Visibility;
248
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000249 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000250 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000251
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000252 assert(!(Data.isCommon() && !Data.isExternal()));
253
Rafael Espindolaf0121242010-12-22 16:03:00 +0000254 const MCExpr *ESize = Data.getSize();
255 if (ESize) {
256 int64_t Res;
257 if (!ESize->EvaluateAsAbsolute(Res, Layout))
258 report_fatal_error("Size expression must be absolute.");
259 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000260 }
261
262 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000263 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
264 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000265}
266
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000267void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
268 MCDataFragment *ShndxF,
269 const MCAssembler &Asm,
270 const MCAsmLayout &Layout,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000271 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000272 // The string table must be emitted first because we need the index
273 // into the string table for all the symbol names.
274 assert(StringTable.size() && "Missing string table");
275
276 // FIXME: Make sure the start of the symbol table is aligned.
277
278 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000279 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000280
281 // Write the symbol table entries.
282 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
283 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
284 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000285 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000286 }
287
Rafael Espindola71859c62010-09-16 19:46:31 +0000288 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000289 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
290 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000291 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000292 static_cast<const MCSectionELF&>(i->getSection());
293 if (Section.getType() == ELF::SHT_RELA ||
294 Section.getType() == ELF::SHT_REL ||
295 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewyckyd2fdb4a2011-10-07 23:29:53 +0000296 Section.getType() == ELF::SHT_SYMTAB ||
297 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedmana44fa242010-08-16 21:17:09 +0000298 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000299 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000300 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
301 false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000302 LastLocalSymbolIndex++;
303 }
Matt Fleming3565a062010-08-16 18:57:57 +0000304
305 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
306 ELFSymbolData &MSD = ExternalSymbolData[i];
307 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000308 assert(((Data.getFlags() & ELF_STB_Global) ||
309 (Data.getFlags() & ELF_STB_Weak)) &&
310 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000311 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000312 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000313 LastLocalSymbolIndex++;
314 }
315
316 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
317 ELFSymbolData &MSD = UndefinedSymbolData[i];
318 MCSymbolData &Data = *MSD.SymbolData;
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
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000325const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
326 const MCValue &Target,
Jason W Kime964d112011-05-11 22:53:06 +0000327 const MCFragment &F,
328 const MCFixup &Fixup,
329 bool IsPCRel) const {
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000330 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000331 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
332 const MCSymbol *Renamed = Renames.lookup(&Symbol);
333 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000334
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000335 if (ASymbol.isUndefined()) {
336 if (Renamed)
337 return Renamed;
338 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000339 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000340
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000341 if (SD.isExternal()) {
342 if (Renamed)
343 return Renamed;
344 return &Symbol;
345 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000346
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000347 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000348 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000349 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000350
Rafael Espindola25958732010-11-24 21:57:39 +0000351 if (secKind.isBSS())
Jason W Kime964d112011-05-11 22:53:06 +0000352 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000353
Rafael Espindola25958732010-11-24 21:57:39 +0000354 if (secKind.isThreadLocal()) {
355 if (Renamed)
356 return Renamed;
357 return &Symbol;
358 }
359
Rafael Espindola8cecf252010-10-06 16:23:36 +0000360 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000361 const MCSectionELF &Sec2 =
362 static_cast<const MCSectionELF&>(F.getParent()->getSection());
363
Rafael Espindola8cecf252010-10-06 16:23:36 +0000364 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000365 (Kind == MCSymbolRefExpr::VK_PLT ||
366 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000367 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000368 if (Renamed)
369 return Renamed;
370 return &Symbol;
371 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000372
Rafael Espindola1c130262011-01-23 04:43:11 +0000373 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000374 if (Target.getConstant() == 0)
Jason W Kime964d112011-05-11 22:53:06 +0000375 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000376 if (Renamed)
377 return Renamed;
378 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000379 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000380
Jason W Kime964d112011-05-11 22:53:06 +0000381 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
382
Rafael Espindola73ffea42010-09-25 05:42:19 +0000383}
384
Matt Fleming3565a062010-08-16 18:57:57 +0000385
Jason W Kim56a39902010-12-06 21:57:34 +0000386void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
387 const MCAsmLayout &Layout,
388 const MCFragment *Fragment,
389 const MCFixup &Fixup,
390 MCValue Target,
391 uint64_t &FixedValue) {
392 int64_t Addend = 0;
393 int Index = 0;
394 int64_t Value = Target.getConstant();
395 const MCSymbol *RelocSymbol = NULL;
396
Rafael Espindola127a6a42010-12-17 07:28:17 +0000397 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000398 if (!Target.isAbsolute()) {
399 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
400 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kime964d112011-05-11 22:53:06 +0000401 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim56a39902010-12-06 21:57:34 +0000402
403 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
404 const MCSymbol &SymbolB = RefB->getSymbol();
405 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
406 IsPCRel = true;
407
408 // Offset of the symbol in the section
409 int64_t a = Layout.getSymbolOffset(&SDB);
410
Bruno Cardoso Lopesa0dd4cb2011-11-04 22:24:36 +0000411 // Offset of the relocation in the section
Jason W Kim56a39902010-12-06 21:57:34 +0000412 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
413 Value += b - a;
414 }
415
416 if (!RelocSymbol) {
417 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
418 MCFragment *F = SD.getFragment();
419
420 Index = F->getParent()->getOrdinal() + 1;
421
422 // Offset of the symbol in the section
423 Value += Layout.getSymbolOffset(&SD);
424 } else {
425 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
426 WeakrefUsedInReloc.insert(RelocSymbol);
427 else
428 UsedInReloc.insert(RelocSymbol);
429 Index = -1;
430 }
431 Addend = Value;
432 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000433 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000434 Value = 0;
435 }
436
437 FixedValue = Value;
438 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
439 (RelocSymbol != 0), Addend);
Rafael Espindolac677e792011-12-21 14:26:29 +0000440 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
441 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
442 if (RelocNeedsGOT(Modifier))
443 NeedsGOT = true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000444
Jason W Kim56a39902010-12-06 21:57:34 +0000445 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
446 Fixup.getOffset();
Roman Divacky2a66cea2011-08-04 19:08:19 +0000447
Rafael Espindolaf3a86fb2011-12-22 01:57:09 +0000448 // FIXME: no tests cover this. Is adjustFixupOffset dead code?
449 TargetObjectWriter->adjustFixupOffset(Fixup, RelocOffset);
Jason W Kim56a39902010-12-06 21:57:34 +0000450
Rafael Espindolabff66a82010-12-18 03:27:34 +0000451 if (!hasRelocationAddend())
452 Addend = 0;
Rafael Espindolabbf9c4a2011-08-04 13:05:26 +0000453
454 if (is64Bit())
455 assert(isInt<64>(Addend));
456 else
457 assert(isInt<32>(Addend));
458
Jason W Kim56a39902010-12-06 21:57:34 +0000459 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
460 Relocations[Fragment->getParent()].push_back(ERE);
461}
462
463
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000464uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000465ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
466 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000467 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000468 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000469}
470
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000471bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
472 const MCSymbolData &Data,
473 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000474 if (Data.getFlags() & ELF_Other_Weakref)
475 return false;
476
Rafael Espindolabd701182010-10-19 19:31:37 +0000477 if (Used)
478 return true;
479
Rafael Espindola88182132010-10-27 15:18:17 +0000480 if (Renamed)
481 return false;
482
Rafael Espindola737cd212010-10-05 18:01:23 +0000483 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000484
Rafael Espindolad1798862010-10-29 23:09:31 +0000485 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
486 return true;
487
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000488 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000489 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
490 return false;
491
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000492 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000493 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000494 return false;
495
Rafael Espindola737cd212010-10-05 18:01:23 +0000496 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
497 return false;
498
Rafael Espindolabd701182010-10-19 19:31:37 +0000499 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000500 return false;
501
502 return true;
503}
504
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000505bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
506 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000507 if (Data.isExternal())
508 return false;
509
510 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000511 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000512
513 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
514 if (isSignature && !isUsedInReloc)
515 return true;
516
Rafael Espindola737cd212010-10-05 18:01:23 +0000517 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000518 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000519
520 return true;
521}
522
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000523void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000524 SectionIndexMapTy &SectionIndexMap,
525 const RelMapTy &RelMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000526 unsigned Index = 1;
527 for (MCAssembler::iterator it = Asm.begin(),
528 ie = Asm.end(); it != ie; ++it) {
529 const MCSectionELF &Section =
530 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000531 if (Section.getType() != ELF::SHT_GROUP)
532 continue;
533 SectionIndexMap[&Section] = Index++;
534 }
535
536 for (MCAssembler::iterator it = Asm.begin(),
537 ie = Asm.end(); it != ie; ++it) {
538 const MCSectionELF &Section =
539 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000540 if (Section.getType() == ELF::SHT_GROUP ||
541 Section.getType() == ELF::SHT_REL ||
542 Section.getType() == ELF::SHT_RELA)
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000543 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000544 SectionIndexMap[&Section] = Index++;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000545 const MCSectionELF *RelSection = RelMap.lookup(&Section);
546 if (RelSection)
547 SectionIndexMap[RelSection] = Index++;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000548 }
549}
550
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000551void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000552 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000553 RevGroupMapTy RevGroupMap,
554 unsigned NumRegularSections) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000555 // FIXME: Is this the correct place to do this?
Rafael Espindola378e0ec2011-06-05 01:20:06 +0000556 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindola5c77c162010-10-05 15:48:37 +0000557 if (NeedsGOT) {
558 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
559 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
560 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
561 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000562 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000563 }
564
Matt Fleming3565a062010-08-16 18:57:57 +0000565 // Index 0 is always the empty string.
566 StringMap<uint64_t> StringIndexMap;
567 StringTable += '\x00';
568
Rafael Espindolad5321da2011-04-07 23:21:52 +0000569 // FIXME: We could optimize suffixes in strtab in the same way we
570 // optimize them in shstrtab.
571
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000572 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000573 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
574 ie = Asm.symbol_end(); it != ie; ++it) {
575 const MCSymbol &Symbol = it->getSymbol();
576
Rafael Espindola484291c2010-11-01 14:28:48 +0000577 bool Used = UsedInReloc.count(&Symbol);
578 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000579 bool isSignature = RevGroupMap.count(&Symbol);
580
581 if (!isInSymtab(Asm, *it,
582 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000583 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000584 continue;
585
Matt Fleming3565a062010-08-16 18:57:57 +0000586 ELFSymbolData MSD;
587 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000588 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000589
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000590 // Undefined symbols are global, but this is the first place we
591 // are able to set it.
592 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000593 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000594 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000595 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
596 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000597 }
598
Rafael Espindola484291c2010-11-01 14:28:48 +0000599 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000600 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000601
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000602 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000603 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000604 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000605 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000606 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000607 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000608 if (isSignature && !Used)
609 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
610 else
611 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000612 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000613 const MCSectionELF &Section =
614 static_cast<const MCSectionELF&>(RefSymbol.getSection());
615 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000616 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
617 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000618 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000619 }
620
Rafael Espindola88182132010-10-27 15:18:17 +0000621 // The @@@ in symbol version is replaced with @ in undefined symbols and
622 // @@ in defined ones.
623 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000624 SmallString<32> Buf;
625
Rafael Espindola88182132010-10-27 15:18:17 +0000626 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000627 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000628 Buf += Name.substr(0, Pos);
629 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
630 Buf += Name.substr(Pos + Skip);
631 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000632 }
633
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000634 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000635 if (!Entry) {
636 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000637 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000638 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000639 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000640 MSD.StringIndex = Entry;
641 if (MSD.SectionIndex == ELF::SHN_UNDEF)
642 UndefinedSymbolData.push_back(MSD);
643 else if (Local)
644 LocalSymbolData.push_back(MSD);
645 else
646 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000647 }
648
649 // Symbols are required to be in lexicographic order.
650 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
651 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
652 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
653
654 // Set the symbol indices. Local symbols must come before all other
655 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000656 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000657 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
658 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000659
660 Index += NumRegularSections;
661
Matt Fleming3565a062010-08-16 18:57:57 +0000662 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
663 ExternalSymbolData[i].SymbolData->setIndex(Index++);
664 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
665 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky7aabcb12011-10-11 03:54:50 +0000666
667 if (NumRegularSections > ELF::SHN_LORESERVE)
668 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000669}
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 {
Jim Grosbachf77d5b12011-12-06 00:03:48 +00001070 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) {
Rafael Espindola7bd27802011-12-22 03:24:43 +00001256 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolaedae8e12011-12-21 17:30:17 +00001257}