• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- 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 
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCAsmLayout.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCELF.h"
25 #include "llvm/MC/MCELFSymbolFlags.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCFixupKindInfo.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCValue.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ELF.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include <vector>
35 using namespace llvm;
36 
37 #undef  DEBUG_TYPE
38 #define DEBUG_TYPE "reloc-info"
39 
40 namespace {
41 class ELFObjectWriter : public MCObjectWriter {
42   protected:
43 
44     static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
45     static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
46     static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
47     static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
48                            bool Used, bool Renamed);
49     static bool isLocal(const MCSymbolData &Data, bool isSignature,
50                         bool isUsedInReloc);
51     static bool IsELFMetaDataSection(const MCSectionData &SD);
52     static uint64_t DataSectionSize(const MCSectionData &SD);
53     static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
54                                        const MCSectionData &SD);
55     static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
56                                           const MCSectionData &SD);
57 
58     void WriteDataSectionData(MCAssembler &Asm,
59                               const MCAsmLayout &Layout,
60                               const MCSectionELF &Section);
61 
62     /*static bool isFixupKindX86RIPRel(unsigned Kind) {
63       return Kind == X86::reloc_riprel_4byte ||
64         Kind == X86::reloc_riprel_4byte_movq_load;
65     }*/
66 
67     /// ELFSymbolData - Helper struct for containing some precomputed
68     /// information on symbols.
69     struct ELFSymbolData {
70       MCSymbolData *SymbolData;
71       uint64_t StringIndex;
72       uint32_t SectionIndex;
73 
74       // Support lexicographic sorting.
operator <__anonce3554260111::ELFObjectWriter::ELFSymbolData75       bool operator<(const ELFSymbolData &RHS) const {
76         if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
77           return true;
78         if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
79           return false;
80         return SymbolData->getSymbol().getName() <
81                RHS.SymbolData->getSymbol().getName();
82       }
83     };
84 
85     /// The target specific ELF writer instance.
86     llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
87 
88     SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
89     SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
90     DenseMap<const MCSymbol *, const MCSymbol *> Renames;
91 
92     llvm::DenseMap<const MCSectionData*,
93                    std::vector<ELFRelocationEntry> > Relocations;
94     DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
95 
96     /// @}
97     /// @name Symbol Table Data
98     /// @{
99 
100     SmallString<256> StringTable;
101     std::vector<ELFSymbolData> LocalSymbolData;
102     std::vector<ELFSymbolData> ExternalSymbolData;
103     std::vector<ELFSymbolData> UndefinedSymbolData;
104 
105     /// @}
106 
107     bool NeedsGOT;
108 
109     bool NeedsSymtabShndx;
110 
111     // This holds the symbol table index of the last local symbol.
112     unsigned LastLocalSymbolIndex;
113     // This holds the .strtab section index.
114     unsigned StringTableIndex;
115     // This holds the .symtab section index.
116     unsigned SymbolTableIndex;
117 
118     unsigned ShstrtabIndex;
119 
120 
121     const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
122                                   const MCValue &Target,
123                                   const MCFragment &F,
124                                   const MCFixup &Fixup,
125                                   bool IsPCRel) const;
126 
127     // TargetObjectWriter wrappers.
ExplicitRelSym(const MCAssembler & Asm,const MCValue & Target,const MCFragment & F,const MCFixup & Fixup,bool IsPCRel) const128     const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
129                                    const MCValue &Target,
130                                    const MCFragment &F,
131                                    const MCFixup &Fixup,
132                                    bool IsPCRel) const {
133       return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
134     }
undefinedExplicitRelSym(const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const135     const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
136                                             const MCFixup &Fixup,
137                                             bool IsPCRel) const {
138       return TargetObjectWriter->undefinedExplicitRelSym(Target, Fixup,
139                                                          IsPCRel);
140     }
141 
is64Bit() const142     bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
hasRelocationAddend() const143     bool hasRelocationAddend() const {
144       return TargetObjectWriter->hasRelocationAddend();
145     }
GetRelocType(const MCValue & Target,const MCFixup & Fixup,bool IsPCRel,bool IsRelocWithSymbol,int64_t Addend) const146     unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
147                           bool IsPCRel, bool IsRelocWithSymbol,
148                           int64_t Addend) const {
149       return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
150                                               IsRelocWithSymbol, Addend);
151     }
152 
153   public:
ELFObjectWriter(MCELFObjectTargetWriter * MOTW,raw_ostream & _OS,bool IsLittleEndian)154     ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
155                     raw_ostream &_OS, bool IsLittleEndian)
156       : MCObjectWriter(_OS, IsLittleEndian),
157         TargetObjectWriter(MOTW),
158         NeedsGOT(false), NeedsSymtabShndx(false) {
159     }
160 
161     virtual ~ELFObjectWriter();
162 
WriteWord(uint64_t W)163     void WriteWord(uint64_t W) {
164       if (is64Bit())
165         Write64(W);
166       else
167         Write32(W);
168     }
169 
StringLE16(char * buf,uint16_t Value)170     void StringLE16(char *buf, uint16_t Value) {
171       buf[0] = char(Value >> 0);
172       buf[1] = char(Value >> 8);
173     }
174 
StringLE32(char * buf,uint32_t Value)175     void StringLE32(char *buf, uint32_t Value) {
176       StringLE16(buf, uint16_t(Value >> 0));
177       StringLE16(buf + 2, uint16_t(Value >> 16));
178     }
179 
StringLE64(char * buf,uint64_t Value)180     void StringLE64(char *buf, uint64_t Value) {
181       StringLE32(buf, uint32_t(Value >> 0));
182       StringLE32(buf + 4, uint32_t(Value >> 32));
183     }
184 
StringBE16(char * buf,uint16_t Value)185     void StringBE16(char *buf ,uint16_t Value) {
186       buf[0] = char(Value >> 8);
187       buf[1] = char(Value >> 0);
188     }
189 
StringBE32(char * buf,uint32_t Value)190     void StringBE32(char *buf, uint32_t Value) {
191       StringBE16(buf, uint16_t(Value >> 16));
192       StringBE16(buf + 2, uint16_t(Value >> 0));
193     }
194 
StringBE64(char * buf,uint64_t Value)195     void StringBE64(char *buf, uint64_t Value) {
196       StringBE32(buf, uint32_t(Value >> 32));
197       StringBE32(buf + 4, uint32_t(Value >> 0));
198     }
199 
String8(MCDataFragment & F,uint8_t Value)200     void String8(MCDataFragment &F, uint8_t Value) {
201       char buf[1];
202       buf[0] = Value;
203       F.getContents().append(&buf[0], &buf[1]);
204     }
205 
String16(MCDataFragment & F,uint16_t Value)206     void String16(MCDataFragment &F, uint16_t Value) {
207       char buf[2];
208       if (isLittleEndian())
209         StringLE16(buf, Value);
210       else
211         StringBE16(buf, Value);
212       F.getContents().append(&buf[0], &buf[2]);
213     }
214 
String32(MCDataFragment & F,uint32_t Value)215     void String32(MCDataFragment &F, uint32_t Value) {
216       char buf[4];
217       if (isLittleEndian())
218         StringLE32(buf, Value);
219       else
220         StringBE32(buf, Value);
221       F.getContents().append(&buf[0], &buf[4]);
222     }
223 
String64(MCDataFragment & F,uint64_t Value)224     void String64(MCDataFragment &F, uint64_t Value) {
225       char buf[8];
226       if (isLittleEndian())
227         StringLE64(buf, Value);
228       else
229         StringBE64(buf, Value);
230       F.getContents().append(&buf[0], &buf[8]);
231     }
232 
233     void WriteHeader(const MCAssembler &Asm,
234                      uint64_t SectionDataSize,
235                      unsigned NumberOfSections);
236 
237     void WriteSymbolEntry(MCDataFragment *SymtabF,
238                           MCDataFragment *ShndxF,
239                           uint64_t name, uint8_t info,
240                           uint64_t value, uint64_t size,
241                           uint8_t other, uint32_t shndx,
242                           bool Reserved);
243 
244     void WriteSymbol(MCDataFragment *SymtabF,  MCDataFragment *ShndxF,
245                      ELFSymbolData &MSD,
246                      const MCAsmLayout &Layout);
247 
248     typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
249     void WriteSymbolTable(MCDataFragment *SymtabF,
250                           MCDataFragment *ShndxF,
251                           const MCAssembler &Asm,
252                           const MCAsmLayout &Layout,
253                           const SectionIndexMapTy &SectionIndexMap);
254 
255     virtual void RecordRelocation(const MCAssembler &Asm,
256                                   const MCAsmLayout &Layout,
257                                   const MCFragment *Fragment,
258                                   const MCFixup &Fixup,
259                                   MCValue Target, uint64_t &FixedValue);
260 
261     uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
262                                          const MCSymbol *S);
263 
264     // Map from a group section to the signature symbol
265     typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
266     // Map from a signature symbol to the group section
267     typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
268     // Map from a section to the section with the relocations
269     typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
270     // Map from a section to its offset
271     typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
272 
273     /// ComputeSymbolTable - Compute the symbol table data
274     ///
275     /// \param Asm - The assembler.
276     /// \param SectionIndexMap - Maps a section to its index.
277     /// \param RevGroupMap - Maps a signature symbol to the group section.
278     /// \param NumRegularSections - Number of non-relocation sections.
279     void ComputeSymbolTable(MCAssembler &Asm,
280                             const SectionIndexMapTy &SectionIndexMap,
281                             RevGroupMapTy RevGroupMap,
282                             unsigned NumRegularSections);
283 
284     void ComputeIndexMap(MCAssembler &Asm,
285                          SectionIndexMapTy &SectionIndexMap,
286                          const RelMapTy &RelMap);
287 
288     void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
289                                   RelMapTy &RelMap);
290 
291     void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
292                           const RelMapTy &RelMap);
293 
294     void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
295                                 SectionIndexMapTy &SectionIndexMap,
296                                 const RelMapTy &RelMap);
297 
298     // Create the sections that show up in the symbol table. Currently
299     // those are the .note.GNU-stack section and the group sections.
300     void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
301                                GroupMapTy &GroupMap,
302                                RevGroupMapTy &RevGroupMap,
303                                SectionIndexMapTy &SectionIndexMap,
304                                const RelMapTy &RelMap);
305 
306     virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
307                                           const MCAsmLayout &Layout);
308 
309     void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
310                             const MCAsmLayout &Layout,
311                             const SectionIndexMapTy &SectionIndexMap,
312                             const SectionOffsetMapTy &SectionOffsetMap);
313 
314     void ComputeSectionOrder(MCAssembler &Asm,
315                              std::vector<const MCSectionELF*> &Sections);
316 
317     void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
318                           uint64_t Address, uint64_t Offset,
319                           uint64_t Size, uint32_t Link, uint32_t Info,
320                           uint64_t Alignment, uint64_t EntrySize);
321 
322     void WriteRelocationsFragment(const MCAssembler &Asm,
323                                   MCDataFragment *F,
324                                   const MCSectionData *SD);
325 
326     virtual bool
327     IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
328                                            const MCSymbolData &DataA,
329                                            const MCFragment &FB,
330                                            bool InSet,
331                                            bool IsPCRel) const;
332 
333     virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
334     void WriteSection(MCAssembler &Asm,
335                       const SectionIndexMapTy &SectionIndexMap,
336                       uint32_t GroupSymbolIndex,
337                       uint64_t Offset, uint64_t Size, uint64_t Alignment,
338                       const MCSectionELF &Section);
339   };
340 }
341 
isFixupKindPCRel(const MCAssembler & Asm,unsigned Kind)342 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
343   const MCFixupKindInfo &FKI =
344     Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
345 
346   return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
347 }
348 
RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant)349 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
350   switch (Variant) {
351   default:
352     return false;
353   case MCSymbolRefExpr::VK_GOT:
354   case MCSymbolRefExpr::VK_PLT:
355   case MCSymbolRefExpr::VK_GOTPCREL:
356   case MCSymbolRefExpr::VK_GOTOFF:
357   case MCSymbolRefExpr::VK_TPOFF:
358   case MCSymbolRefExpr::VK_TLSGD:
359   case MCSymbolRefExpr::VK_GOTTPOFF:
360   case MCSymbolRefExpr::VK_INDNTPOFF:
361   case MCSymbolRefExpr::VK_NTPOFF:
362   case MCSymbolRefExpr::VK_GOTNTPOFF:
363   case MCSymbolRefExpr::VK_TLSLDM:
364   case MCSymbolRefExpr::VK_DTPOFF:
365   case MCSymbolRefExpr::VK_TLSLD:
366     return true;
367   }
368 }
369 
~ELFObjectWriter()370 ELFObjectWriter::~ELFObjectWriter()
371 {}
372 
373 // Emit the ELF header.
WriteHeader(const MCAssembler & Asm,uint64_t SectionDataSize,unsigned NumberOfSections)374 void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
375                                   uint64_t SectionDataSize,
376                                   unsigned NumberOfSections) {
377   // ELF Header
378   // ----------
379   //
380   // Note
381   // ----
382   // emitWord method behaves differently for ELF32 and ELF64, writing
383   // 4 bytes in the former and 8 in the latter.
384 
385   Write8(0x7f); // e_ident[EI_MAG0]
386   Write8('E');  // e_ident[EI_MAG1]
387   Write8('L');  // e_ident[EI_MAG2]
388   Write8('F');  // e_ident[EI_MAG3]
389 
390   Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
391 
392   // e_ident[EI_DATA]
393   Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
394 
395   Write8(ELF::EV_CURRENT);        // e_ident[EI_VERSION]
396   // e_ident[EI_OSABI]
397   Write8(TargetObjectWriter->getOSABI());
398   Write8(0);                  // e_ident[EI_ABIVERSION]
399 
400   WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
401 
402   Write16(ELF::ET_REL);             // e_type
403 
404   Write16(TargetObjectWriter->getEMachine()); // e_machine = target
405 
406   Write32(ELF::EV_CURRENT);         // e_version
407   WriteWord(0);                    // e_entry, no entry point in .o file
408   WriteWord(0);                    // e_phoff, no program header for .o
409   WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
410             sizeof(ELF::Elf32_Ehdr)));  // e_shoff = sec hdr table off in bytes
411 
412   // e_flags = whatever the target wants
413   Write32(Asm.getELFHeaderEFlags());
414 
415   // e_ehsize = ELF header size
416   Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
417 
418   Write16(0);                  // e_phentsize = prog header entry size
419   Write16(0);                  // e_phnum = # prog header entries = 0
420 
421   // e_shentsize = Section header entry size
422   Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
423 
424   // e_shnum     = # of section header ents
425   if (NumberOfSections >= ELF::SHN_LORESERVE)
426     Write16(ELF::SHN_UNDEF);
427   else
428     Write16(NumberOfSections);
429 
430   // e_shstrndx  = Section # of '.shstrtab'
431   if (ShstrtabIndex >= ELF::SHN_LORESERVE)
432     Write16(ELF::SHN_XINDEX);
433   else
434     Write16(ShstrtabIndex);
435 }
436 
WriteSymbolEntry(MCDataFragment * SymtabF,MCDataFragment * ShndxF,uint64_t name,uint8_t info,uint64_t value,uint64_t size,uint8_t other,uint32_t shndx,bool Reserved)437 void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
438                                        MCDataFragment *ShndxF,
439                                        uint64_t name,
440                                        uint8_t info, uint64_t value,
441                                        uint64_t size, uint8_t other,
442                                        uint32_t shndx,
443                                        bool Reserved) {
444   if (ShndxF) {
445     if (shndx >= ELF::SHN_LORESERVE && !Reserved)
446       String32(*ShndxF, shndx);
447     else
448       String32(*ShndxF, 0);
449   }
450 
451   uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
452     uint16_t(ELF::SHN_XINDEX) : shndx;
453 
454   if (is64Bit()) {
455     String32(*SymtabF, name);  // st_name
456     String8(*SymtabF, info);   // st_info
457     String8(*SymtabF, other);  // st_other
458     String16(*SymtabF, Index); // st_shndx
459     String64(*SymtabF, value); // st_value
460     String64(*SymtabF, size);  // st_size
461   } else {
462     String32(*SymtabF, name);  // st_name
463     String32(*SymtabF, value); // st_value
464     String32(*SymtabF, size);  // st_size
465     String8(*SymtabF, info);   // st_info
466     String8(*SymtabF, other);  // st_other
467     String16(*SymtabF, Index); // st_shndx
468   }
469 }
470 
SymbolValue(MCSymbolData & Data,const MCAsmLayout & Layout)471 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
472                                       const MCAsmLayout &Layout) {
473   if (Data.isCommon() && Data.isExternal())
474     return Data.getCommonAlignment();
475 
476   const MCSymbol &Symbol = Data.getSymbol();
477 
478   if (Symbol.isAbsolute() && Symbol.isVariable()) {
479     if (const MCExpr *Value = Symbol.getVariableValue()) {
480       int64_t IntValue;
481       if (Value->EvaluateAsAbsolute(IntValue, Layout))
482         return (uint64_t)IntValue;
483     }
484   }
485 
486   if (!Symbol.isInSection())
487     return 0;
488 
489 
490   if (Data.getFragment()) {
491     if (Data.getFlags() & ELF_Other_ThumbFunc)
492       return Layout.getSymbolOffset(&Data)+1;
493     else
494       return Layout.getSymbolOffset(&Data);
495   }
496 
497   return 0;
498 }
499 
ExecutePostLayoutBinding(MCAssembler & Asm,const MCAsmLayout & Layout)500 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
501                                                const MCAsmLayout &Layout) {
502   // The presence of symbol versions causes undefined symbols and
503   // versions declared with @@@ to be renamed.
504 
505   for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
506          ie = Asm.symbol_end(); it != ie; ++it) {
507     const MCSymbol &Alias = it->getSymbol();
508     const MCSymbol &Symbol = Alias.AliasedSymbol();
509     MCSymbolData &SD = Asm.getSymbolData(Symbol);
510 
511     // Not an alias.
512     if (&Symbol == &Alias)
513       continue;
514 
515     StringRef AliasName = Alias.getName();
516     size_t Pos = AliasName.find('@');
517     if (Pos == StringRef::npos)
518       continue;
519 
520     // Aliases defined with .symvar copy the binding from the symbol they alias.
521     // This is the first place we are able to copy this information.
522     it->setExternal(SD.isExternal());
523     MCELF::SetBinding(*it, MCELF::GetBinding(SD));
524 
525     StringRef Rest = AliasName.substr(Pos);
526     if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
527       continue;
528 
529     // FIXME: produce a better error message.
530     if (Symbol.isUndefined() && Rest.startswith("@@") &&
531         !Rest.startswith("@@@"))
532       report_fatal_error("A @@ version cannot be undefined");
533 
534     Renames.insert(std::make_pair(&Symbol, &Alias));
535   }
536 }
537 
WriteSymbol(MCDataFragment * SymtabF,MCDataFragment * ShndxF,ELFSymbolData & MSD,const MCAsmLayout & Layout)538 void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
539                                   MCDataFragment *ShndxF,
540                                   ELFSymbolData &MSD,
541                                   const MCAsmLayout &Layout) {
542   MCSymbolData &OrigData = *MSD.SymbolData;
543   MCSymbolData &Data =
544     Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
545 
546   bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
547     Data.getSymbol().isVariable();
548 
549   // Binding and Type share the same byte as upper and lower nibbles
550   uint8_t Binding = MCELF::GetBinding(OrigData);
551   uint8_t Type = MCELF::GetType(Data);
552   uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
553 
554   // Other and Visibility share the same byte with Visability using the lower
555   // 2 bits
556   uint8_t Visibility = MCELF::GetVisibility(OrigData);
557   uint8_t Other = MCELF::getOther(OrigData) <<
558     (ELF_Other_Shift - ELF_STV_Shift);
559   Other |= Visibility;
560 
561   uint64_t Value = SymbolValue(Data, Layout);
562   uint64_t Size = 0;
563 
564   assert(!(Data.isCommon() && !Data.isExternal()));
565 
566   const MCExpr *ESize = Data.getSize();
567   if (ESize) {
568     int64_t Res;
569     if (!ESize->EvaluateAsAbsolute(Res, Layout))
570       report_fatal_error("Size expression must be absolute.");
571     Size = Res;
572   }
573 
574   // Write out the symbol table entry
575   WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
576                    Size, Other, MSD.SectionIndex, IsReserved);
577 }
578 
WriteSymbolTable(MCDataFragment * SymtabF,MCDataFragment * ShndxF,const MCAssembler & Asm,const MCAsmLayout & Layout,const SectionIndexMapTy & SectionIndexMap)579 void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
580                                        MCDataFragment *ShndxF,
581                                        const MCAssembler &Asm,
582                                        const MCAsmLayout &Layout,
583                                     const SectionIndexMapTy &SectionIndexMap) {
584   // The string table must be emitted first because we need the index
585   // into the string table for all the symbol names.
586   assert(StringTable.size() && "Missing string table");
587 
588   // FIXME: Make sure the start of the symbol table is aligned.
589 
590   // The first entry is the undefined symbol entry.
591   WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
592 
593   // Write the symbol table entries.
594   LastLocalSymbolIndex = LocalSymbolData.size() + 1;
595   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
596     ELFSymbolData &MSD = LocalSymbolData[i];
597     WriteSymbol(SymtabF, ShndxF, MSD, Layout);
598   }
599 
600   // Write out a symbol table entry for each regular section.
601   for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
602        ++i) {
603     const MCSectionELF &Section =
604       static_cast<const MCSectionELF&>(i->getSection());
605     if (Section.getType() == ELF::SHT_RELA ||
606         Section.getType() == ELF::SHT_REL ||
607         Section.getType() == ELF::SHT_STRTAB ||
608         Section.getType() == ELF::SHT_SYMTAB ||
609         Section.getType() == ELF::SHT_SYMTAB_SHNDX)
610       continue;
611     WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
612                      ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
613                      false);
614     LastLocalSymbolIndex++;
615   }
616 
617   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
618     ELFSymbolData &MSD = ExternalSymbolData[i];
619     MCSymbolData &Data = *MSD.SymbolData;
620     assert(((Data.getFlags() & ELF_STB_Global) ||
621             (Data.getFlags() & ELF_STB_Weak)) &&
622            "External symbol requires STB_GLOBAL or STB_WEAK flag");
623     WriteSymbol(SymtabF, ShndxF, MSD, Layout);
624     if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
625       LastLocalSymbolIndex++;
626   }
627 
628   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
629     ELFSymbolData &MSD = UndefinedSymbolData[i];
630     MCSymbolData &Data = *MSD.SymbolData;
631     WriteSymbol(SymtabF, ShndxF, MSD, Layout);
632     if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
633       LastLocalSymbolIndex++;
634   }
635 }
636 
SymbolToReloc(const MCAssembler & Asm,const MCValue & Target,const MCFragment & F,const MCFixup & Fixup,bool IsPCRel) const637 const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
638                                                const MCValue &Target,
639                                                const MCFragment &F,
640                                                const MCFixup &Fixup,
641                                                bool IsPCRel) const {
642   const MCSymbol &Symbol = Target.getSymA()->getSymbol();
643   const MCSymbol &ASymbol = Symbol.AliasedSymbol();
644   const MCSymbol *Renamed = Renames.lookup(&Symbol);
645   const MCSymbolData &SD = Asm.getSymbolData(Symbol);
646 
647   if (ASymbol.isUndefined()) {
648     if (Renamed)
649       return Renamed;
650     return undefinedExplicitRelSym(Target, Fixup, IsPCRel);
651   }
652 
653   if (SD.isExternal()) {
654     if (Renamed)
655       return Renamed;
656     return &Symbol;
657   }
658 
659   const MCSectionELF &Section =
660     static_cast<const MCSectionELF&>(ASymbol.getSection());
661   const SectionKind secKind = Section.getKind();
662 
663   if (secKind.isBSS())
664     return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
665 
666   if (secKind.isThreadLocal()) {
667     if (Renamed)
668       return Renamed;
669     return &Symbol;
670   }
671 
672   MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
673   const MCSectionELF &Sec2 =
674     static_cast<const MCSectionELF&>(F.getParent()->getSection());
675 
676   if (&Sec2 != &Section &&
677       (Kind == MCSymbolRefExpr::VK_PLT ||
678        Kind == MCSymbolRefExpr::VK_GOTPCREL ||
679        Kind == MCSymbolRefExpr::VK_GOTOFF)) {
680     if (Renamed)
681       return Renamed;
682     return &Symbol;
683   }
684 
685   if (Section.getFlags() & ELF::SHF_MERGE) {
686     if (Target.getConstant() == 0)
687       return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
688     if (Renamed)
689       return Renamed;
690     return &Symbol;
691   }
692 
693   return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
694 
695 }
696 
697 
RecordRelocation(const MCAssembler & Asm,const MCAsmLayout & Layout,const MCFragment * Fragment,const MCFixup & Fixup,MCValue Target,uint64_t & FixedValue)698 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
699                                        const MCAsmLayout &Layout,
700                                        const MCFragment *Fragment,
701                                        const MCFixup &Fixup,
702                                        MCValue Target,
703                                        uint64_t &FixedValue) {
704   int64_t Addend = 0;
705   int Index = 0;
706   int64_t Value = Target.getConstant();
707   const MCSymbol *RelocSymbol = NULL;
708 
709   bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
710   if (!Target.isAbsolute()) {
711     const MCSymbol &Symbol = Target.getSymA()->getSymbol();
712     const MCSymbol &ASymbol = Symbol.AliasedSymbol();
713     RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
714 
715     if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
716       const MCSymbol &SymbolB = RefB->getSymbol();
717       MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
718       IsPCRel = true;
719 
720       // Offset of the symbol in the section
721       int64_t a = Layout.getSymbolOffset(&SDB);
722 
723       // Offset of the relocation in the section
724       int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
725       Value += b - a;
726     }
727 
728     if (!RelocSymbol) {
729       MCSymbolData &SD = Asm.getSymbolData(ASymbol);
730       MCFragment *F = SD.getFragment();
731 
732       if (F) {
733         Index = F->getParent()->getOrdinal() + 1;
734         // Offset of the symbol in the section
735         Value += Layout.getSymbolOffset(&SD);
736       } else {
737         Index = 0;
738       }
739     } else {
740       if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
741         WeakrefUsedInReloc.insert(RelocSymbol);
742       else
743         UsedInReloc.insert(RelocSymbol);
744       Index = -1;
745     }
746     Addend = Value;
747     if (hasRelocationAddend())
748       Value = 0;
749   }
750 
751   FixedValue = Value;
752   unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
753                                (RelocSymbol != 0), Addend);
754   MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
755     MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
756   if (RelocNeedsGOT(Modifier))
757     NeedsGOT = true;
758 
759   uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
760     Fixup.getOffset();
761 
762   if (!hasRelocationAddend())
763     Addend = 0;
764 
765   if (is64Bit())
766     assert(isInt<64>(Addend));
767   else
768     assert(isInt<32>(Addend));
769 
770   ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
771   Relocations[Fragment->getParent()].push_back(ERE);
772 }
773 
774 
775 uint64_t
getSymbolIndexInSymbolTable(const MCAssembler & Asm,const MCSymbol * S)776 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
777                                              const MCSymbol *S) {
778   MCSymbolData &SD = Asm.getSymbolData(*S);
779   return SD.getIndex();
780 }
781 
isInSymtab(const MCAssembler & Asm,const MCSymbolData & Data,bool Used,bool Renamed)782 bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
783                                  const MCSymbolData &Data,
784                                  bool Used, bool Renamed) {
785   if (Data.getFlags() & ELF_Other_Weakref)
786     return false;
787 
788   if (Used)
789     return true;
790 
791   if (Renamed)
792     return false;
793 
794   const MCSymbol &Symbol = Data.getSymbol();
795 
796   if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
797     return true;
798 
799   const MCSymbol &A = Symbol.AliasedSymbol();
800   if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
801     return false;
802 
803   bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
804   if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
805     return false;
806 
807   if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
808     return false;
809 
810   if (Symbol.isTemporary())
811     return false;
812 
813   return true;
814 }
815 
isLocal(const MCSymbolData & Data,bool isSignature,bool isUsedInReloc)816 bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
817                               bool isUsedInReloc) {
818   if (Data.isExternal())
819     return false;
820 
821   const MCSymbol &Symbol = Data.getSymbol();
822   const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
823 
824   if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
825     if (isSignature && !isUsedInReloc)
826       return true;
827 
828     return false;
829   }
830 
831   return true;
832 }
833 
ComputeIndexMap(MCAssembler & Asm,SectionIndexMapTy & SectionIndexMap,const RelMapTy & RelMap)834 void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
835                                       SectionIndexMapTy &SectionIndexMap,
836                                       const RelMapTy &RelMap) {
837   unsigned Index = 1;
838   for (MCAssembler::iterator it = Asm.begin(),
839          ie = Asm.end(); it != ie; ++it) {
840     const MCSectionELF &Section =
841       static_cast<const MCSectionELF &>(it->getSection());
842     if (Section.getType() != ELF::SHT_GROUP)
843       continue;
844     SectionIndexMap[&Section] = Index++;
845   }
846 
847   for (MCAssembler::iterator it = Asm.begin(),
848          ie = Asm.end(); it != ie; ++it) {
849     const MCSectionELF &Section =
850       static_cast<const MCSectionELF &>(it->getSection());
851     if (Section.getType() == ELF::SHT_GROUP ||
852         Section.getType() == ELF::SHT_REL ||
853         Section.getType() == ELF::SHT_RELA)
854       continue;
855     SectionIndexMap[&Section] = Index++;
856     const MCSectionELF *RelSection = RelMap.lookup(&Section);
857     if (RelSection)
858       SectionIndexMap[RelSection] = Index++;
859   }
860 }
861 
ComputeSymbolTable(MCAssembler & Asm,const SectionIndexMapTy & SectionIndexMap,RevGroupMapTy RevGroupMap,unsigned NumRegularSections)862 void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
863                                       const SectionIndexMapTy &SectionIndexMap,
864                                          RevGroupMapTy RevGroupMap,
865                                          unsigned NumRegularSections) {
866   // FIXME: Is this the correct place to do this?
867   // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
868   if (NeedsGOT) {
869     StringRef Name = "_GLOBAL_OFFSET_TABLE_";
870     MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
871     MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
872     Data.setExternal(true);
873     MCELF::SetBinding(Data, ELF::STB_GLOBAL);
874   }
875 
876   // Index 0 is always the empty string.
877   StringMap<uint64_t> StringIndexMap;
878   StringTable += '\x00';
879 
880   // FIXME: We could optimize suffixes in strtab in the same way we
881   // optimize them in shstrtab.
882 
883   // Add the data for the symbols.
884   for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
885          ie = Asm.symbol_end(); it != ie; ++it) {
886     const MCSymbol &Symbol = it->getSymbol();
887 
888     bool Used = UsedInReloc.count(&Symbol);
889     bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
890     bool isSignature = RevGroupMap.count(&Symbol);
891 
892     if (!isInSymtab(Asm, *it,
893                     Used || WeakrefUsed || isSignature,
894                     Renames.count(&Symbol)))
895       continue;
896 
897     ELFSymbolData MSD;
898     MSD.SymbolData = it;
899     const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
900 
901     // Undefined symbols are global, but this is the first place we
902     // are able to set it.
903     bool Local = isLocal(*it, isSignature, Used);
904     if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
905       MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
906       MCELF::SetBinding(*it, ELF::STB_GLOBAL);
907       MCELF::SetBinding(SD, ELF::STB_GLOBAL);
908     }
909 
910     if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
911       MCELF::SetBinding(*it, ELF::STB_WEAK);
912 
913     if (it->isCommon()) {
914       assert(!Local);
915       MSD.SectionIndex = ELF::SHN_COMMON;
916     } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
917       MSD.SectionIndex = ELF::SHN_ABS;
918     } else if (RefSymbol.isUndefined()) {
919       if (isSignature && !Used)
920         MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
921       else
922         MSD.SectionIndex = ELF::SHN_UNDEF;
923     } else {
924       const MCSectionELF &Section =
925         static_cast<const MCSectionELF&>(RefSymbol.getSection());
926       MSD.SectionIndex = SectionIndexMap.lookup(&Section);
927       if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
928         NeedsSymtabShndx = true;
929       assert(MSD.SectionIndex && "Invalid section index!");
930     }
931 
932     // The @@@ in symbol version is replaced with @ in undefined symbols and
933     // @@ in defined ones.
934     StringRef Name = Symbol.getName();
935     SmallString<32> Buf;
936 
937     size_t Pos = Name.find("@@@");
938     if (Pos != StringRef::npos) {
939       Buf += Name.substr(0, Pos);
940       unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
941       Buf += Name.substr(Pos + Skip);
942       Name = Buf;
943     }
944 
945     uint64_t &Entry = StringIndexMap[Name];
946     if (!Entry) {
947       Entry = StringTable.size();
948       StringTable += Name;
949       StringTable += '\x00';
950     }
951     MSD.StringIndex = Entry;
952     if (MSD.SectionIndex == ELF::SHN_UNDEF)
953       UndefinedSymbolData.push_back(MSD);
954     else if (Local)
955       LocalSymbolData.push_back(MSD);
956     else
957       ExternalSymbolData.push_back(MSD);
958   }
959 
960   // Symbols are required to be in lexicographic order.
961   array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
962   array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
963   array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
964 
965   // Set the symbol indices. Local symbols must come before all other
966   // symbols with non-local bindings.
967   unsigned Index = 1;
968   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
969     LocalSymbolData[i].SymbolData->setIndex(Index++);
970 
971   Index += NumRegularSections;
972 
973   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
974     ExternalSymbolData[i].SymbolData->setIndex(Index++);
975   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
976     UndefinedSymbolData[i].SymbolData->setIndex(Index++);
977 
978   if (Index >= ELF::SHN_LORESERVE)
979     NeedsSymtabShndx = true;
980 }
981 
CreateRelocationSections(MCAssembler & Asm,MCAsmLayout & Layout,RelMapTy & RelMap)982 void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
983                                                MCAsmLayout &Layout,
984                                                RelMapTy &RelMap) {
985   for (MCAssembler::const_iterator it = Asm.begin(),
986          ie = Asm.end(); it != ie; ++it) {
987     const MCSectionData &SD = *it;
988     if (Relocations[&SD].empty())
989       continue;
990 
991     MCContext &Ctx = Asm.getContext();
992     const MCSectionELF &Section =
993       static_cast<const MCSectionELF&>(SD.getSection());
994 
995     const StringRef SectionName = Section.getSectionName();
996     std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
997     RelaSectionName += SectionName;
998 
999     unsigned EntrySize;
1000     if (hasRelocationAddend())
1001       EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1002     else
1003       EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
1004 
1005     unsigned Flags = 0;
1006     StringRef Group = "";
1007     if (Section.getFlags() & ELF::SHF_GROUP) {
1008         Flags = ELF::SHF_GROUP;
1009         Group = Section.getGroup()->getName();
1010     }
1011 
1012     const MCSectionELF *RelaSection =
1013       Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
1014                         ELF::SHT_RELA : ELF::SHT_REL, Flags,
1015                         SectionKind::getReadOnly(),
1016                         EntrySize, Group);
1017     RelMap[&Section] = RelaSection;
1018     Asm.getOrCreateSectionData(*RelaSection);
1019   }
1020 }
1021 
WriteRelocations(MCAssembler & Asm,MCAsmLayout & Layout,const RelMapTy & RelMap)1022 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1023                                        const RelMapTy &RelMap) {
1024   for (MCAssembler::const_iterator it = Asm.begin(),
1025          ie = Asm.end(); it != ie; ++it) {
1026     const MCSectionData &SD = *it;
1027     const MCSectionELF &Section =
1028       static_cast<const MCSectionELF&>(SD.getSection());
1029 
1030     const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1031     if (!RelaSection)
1032       continue;
1033     MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
1034     RelaSD.setAlignment(is64Bit() ? 8 : 4);
1035 
1036     MCDataFragment *F = new MCDataFragment(&RelaSD);
1037     WriteRelocationsFragment(Asm, F, &*it);
1038   }
1039 }
1040 
WriteSecHdrEntry(uint32_t Name,uint32_t Type,uint64_t Flags,uint64_t Address,uint64_t Offset,uint64_t Size,uint32_t Link,uint32_t Info,uint64_t Alignment,uint64_t EntrySize)1041 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1042                                        uint64_t Flags, uint64_t Address,
1043                                        uint64_t Offset, uint64_t Size,
1044                                        uint32_t Link, uint32_t Info,
1045                                        uint64_t Alignment,
1046                                        uint64_t EntrySize) {
1047   Write32(Name);        // sh_name: index into string table
1048   Write32(Type);        // sh_type
1049   WriteWord(Flags);     // sh_flags
1050   WriteWord(Address);   // sh_addr
1051   WriteWord(Offset);    // sh_offset
1052   WriteWord(Size);      // sh_size
1053   Write32(Link);        // sh_link
1054   Write32(Info);        // sh_info
1055   WriteWord(Alignment); // sh_addralign
1056   WriteWord(EntrySize); // sh_entsize
1057 }
1058 
WriteRelocationsFragment(const MCAssembler & Asm,MCDataFragment * F,const MCSectionData * SD)1059 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1060                                                MCDataFragment *F,
1061                                                const MCSectionData *SD) {
1062   std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1063 
1064   // Sort the relocation entries. Most targets just sort by r_offset, but some
1065   // (e.g., MIPS) have additional constraints.
1066   TargetObjectWriter->sortRelocs(Asm, Relocs);
1067 
1068   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1069     ELFRelocationEntry entry = Relocs[e - i - 1];
1070 
1071     if (!entry.Index)
1072       ;
1073     else if (entry.Index < 0)
1074       entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1075     else
1076       entry.Index += LocalSymbolData.size();
1077     if (is64Bit()) {
1078       String64(*F, entry.r_offset);
1079       if (TargetObjectWriter->isN64()) {
1080         String32(*F, entry.Index);
1081 
1082         String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1083         String8(*F, TargetObjectWriter->getRType3(entry.Type));
1084         String8(*F, TargetObjectWriter->getRType2(entry.Type));
1085         String8(*F, TargetObjectWriter->getRType(entry.Type));
1086       }
1087       else {
1088         struct ELF::Elf64_Rela ERE64;
1089         ERE64.setSymbolAndType(entry.Index, entry.Type);
1090         String64(*F, ERE64.r_info);
1091       }
1092       if (hasRelocationAddend())
1093         String64(*F, entry.r_addend);
1094     } else {
1095       String32(*F, entry.r_offset);
1096 
1097       struct ELF::Elf32_Rela ERE32;
1098       ERE32.setSymbolAndType(entry.Index, entry.Type);
1099       String32(*F, ERE32.r_info);
1100 
1101       if (hasRelocationAddend())
1102         String32(*F, entry.r_addend);
1103     }
1104   }
1105 }
1106 
compareBySuffix(const void * a,const void * b)1107 static int compareBySuffix(const void *a, const void *b) {
1108   const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
1109   const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
1110   const StringRef &NameA = secA->getSectionName();
1111   const StringRef &NameB = secB->getSectionName();
1112   const unsigned sizeA = NameA.size();
1113   const unsigned sizeB = NameB.size();
1114   const unsigned len = std::min(sizeA, sizeB);
1115   for (unsigned int i = 0; i < len; ++i) {
1116     char ca = NameA[sizeA - i - 1];
1117     char cb = NameB[sizeB - i - 1];
1118     if (ca != cb)
1119       return cb - ca;
1120   }
1121 
1122   return sizeB - sizeA;
1123 }
1124 
CreateMetadataSections(MCAssembler & Asm,MCAsmLayout & Layout,SectionIndexMapTy & SectionIndexMap,const RelMapTy & RelMap)1125 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1126                                              MCAsmLayout &Layout,
1127                                              SectionIndexMapTy &SectionIndexMap,
1128                                              const RelMapTy &RelMap) {
1129   MCContext &Ctx = Asm.getContext();
1130   MCDataFragment *F;
1131 
1132   unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1133 
1134   // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
1135   const MCSectionELF *ShstrtabSection =
1136     Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1137                       SectionKind::getReadOnly());
1138   MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1139   ShstrtabSD.setAlignment(1);
1140 
1141   const MCSectionELF *SymtabSection =
1142     Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1143                       SectionKind::getReadOnly(),
1144                       EntrySize, "");
1145   MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
1146   SymtabSD.setAlignment(is64Bit() ? 8 : 4);
1147 
1148   MCSectionData *SymtabShndxSD = NULL;
1149 
1150   if (NeedsSymtabShndx) {
1151     const MCSectionELF *SymtabShndxSection =
1152       Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
1153                         SectionKind::getReadOnly(), 4, "");
1154     SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1155     SymtabShndxSD->setAlignment(4);
1156   }
1157 
1158   const MCSectionELF *StrtabSection;
1159   StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1160                                     SectionKind::getReadOnly());
1161   MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1162   StrtabSD.setAlignment(1);
1163 
1164   ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1165 
1166   ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1167   SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1168   StringTableIndex = SectionIndexMap.lookup(StrtabSection);
1169 
1170   // Symbol table
1171   F = new MCDataFragment(&SymtabSD);
1172   MCDataFragment *ShndxF = NULL;
1173   if (NeedsSymtabShndx) {
1174     ShndxF = new MCDataFragment(SymtabShndxSD);
1175   }
1176   WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
1177 
1178   F = new MCDataFragment(&StrtabSD);
1179   F->getContents().append(StringTable.begin(), StringTable.end());
1180 
1181   F = new MCDataFragment(&ShstrtabSD);
1182 
1183   std::vector<const MCSectionELF*> Sections;
1184   for (MCAssembler::const_iterator it = Asm.begin(),
1185          ie = Asm.end(); it != ie; ++it) {
1186     const MCSectionELF &Section =
1187       static_cast<const MCSectionELF&>(it->getSection());
1188     Sections.push_back(&Section);
1189   }
1190   array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1191 
1192   // Section header string table.
1193   //
1194   // The first entry of a string table holds a null character so skip
1195   // section 0.
1196   uint64_t Index = 1;
1197   F->getContents().push_back('\x00');
1198 
1199   for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1200     const MCSectionELF &Section = *Sections[I];
1201 
1202     StringRef Name = Section.getSectionName();
1203     if (I != 0) {
1204       StringRef PreviousName = Sections[I - 1]->getSectionName();
1205       if (PreviousName.endswith(Name)) {
1206         SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1207         continue;
1208       }
1209     }
1210     // Remember the index into the string table so we can write it
1211     // into the sh_name field of the section header table.
1212     SectionStringTableIndex[&Section] = Index;
1213 
1214     Index += Name.size() + 1;
1215     F->getContents().append(Name.begin(), Name.end());
1216     F->getContents().push_back('\x00');
1217   }
1218 }
1219 
CreateIndexedSections(MCAssembler & Asm,MCAsmLayout & Layout,GroupMapTy & GroupMap,RevGroupMapTy & RevGroupMap,SectionIndexMapTy & SectionIndexMap,const RelMapTy & RelMap)1220 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1221                                             MCAsmLayout &Layout,
1222                                             GroupMapTy &GroupMap,
1223                                             RevGroupMapTy &RevGroupMap,
1224                                             SectionIndexMapTy &SectionIndexMap,
1225                                             const RelMapTy &RelMap) {
1226   // Create the .note.GNU-stack section if needed.
1227   MCContext &Ctx = Asm.getContext();
1228   if (Asm.getNoExecStack()) {
1229     const MCSectionELF *GnuStackSection =
1230       Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1231                         SectionKind::getReadOnly());
1232     Asm.getOrCreateSectionData(*GnuStackSection);
1233   }
1234 
1235   // Build the groups
1236   for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1237        it != ie; ++it) {
1238     const MCSectionELF &Section =
1239       static_cast<const MCSectionELF&>(it->getSection());
1240     if (!(Section.getFlags() & ELF::SHF_GROUP))
1241       continue;
1242 
1243     const MCSymbol *SignatureSymbol = Section.getGroup();
1244     Asm.getOrCreateSymbolData(*SignatureSymbol);
1245     const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
1246     if (!Group) {
1247       Group = Ctx.CreateELFGroupSection();
1248       MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1249       Data.setAlignment(4);
1250       MCDataFragment *F = new MCDataFragment(&Data);
1251       String32(*F, ELF::GRP_COMDAT);
1252     }
1253     GroupMap[Group] = SignatureSymbol;
1254   }
1255 
1256   ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1257 
1258   // Add sections to the groups
1259   for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1260        it != ie; ++it) {
1261     const MCSectionELF &Section =
1262       static_cast<const MCSectionELF&>(it->getSection());
1263     if (!(Section.getFlags() & ELF::SHF_GROUP))
1264       continue;
1265     const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
1266     MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1267     // FIXME: we could use the previous fragment
1268     MCDataFragment *F = new MCDataFragment(&Data);
1269     unsigned Index = SectionIndexMap.lookup(&Section);
1270     String32(*F, Index);
1271   }
1272 }
1273 
WriteSection(MCAssembler & Asm,const SectionIndexMapTy & SectionIndexMap,uint32_t GroupSymbolIndex,uint64_t Offset,uint64_t Size,uint64_t Alignment,const MCSectionELF & Section)1274 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1275                                    const SectionIndexMapTy &SectionIndexMap,
1276                                    uint32_t GroupSymbolIndex,
1277                                    uint64_t Offset, uint64_t Size,
1278                                    uint64_t Alignment,
1279                                    const MCSectionELF &Section) {
1280   uint64_t sh_link = 0;
1281   uint64_t sh_info = 0;
1282 
1283   switch(Section.getType()) {
1284   case ELF::SHT_DYNAMIC:
1285     sh_link = SectionStringTableIndex[&Section];
1286     sh_info = 0;
1287     break;
1288 
1289   case ELF::SHT_REL:
1290   case ELF::SHT_RELA: {
1291     const MCSectionELF *SymtabSection;
1292     const MCSectionELF *InfoSection;
1293     SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1294                                                    0,
1295                                                    SectionKind::getReadOnly());
1296     sh_link = SectionIndexMap.lookup(SymtabSection);
1297     assert(sh_link && ".symtab not found");
1298 
1299     // Remove ".rel" and ".rela" prefixes.
1300     unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1301     StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1302 
1303     InfoSection = Asm.getContext().getELFSection(SectionName,
1304                                                  ELF::SHT_PROGBITS, 0,
1305                                                  SectionKind::getReadOnly());
1306     sh_info = SectionIndexMap.lookup(InfoSection);
1307     break;
1308   }
1309 
1310   case ELF::SHT_SYMTAB:
1311   case ELF::SHT_DYNSYM:
1312     sh_link = StringTableIndex;
1313     sh_info = LastLocalSymbolIndex;
1314     break;
1315 
1316   case ELF::SHT_SYMTAB_SHNDX:
1317     sh_link = SymbolTableIndex;
1318     break;
1319 
1320   case ELF::SHT_PROGBITS:
1321   case ELF::SHT_STRTAB:
1322   case ELF::SHT_NOBITS:
1323   case ELF::SHT_NOTE:
1324   case ELF::SHT_NULL:
1325   case ELF::SHT_ARM_ATTRIBUTES:
1326   case ELF::SHT_INIT_ARRAY:
1327   case ELF::SHT_FINI_ARRAY:
1328   case ELF::SHT_PREINIT_ARRAY:
1329   case ELF::SHT_X86_64_UNWIND:
1330   case ELF::SHT_MIPS_REGINFO:
1331   case ELF::SHT_MIPS_OPTIONS:
1332     // Nothing to do.
1333     break;
1334 
1335   case ELF::SHT_GROUP:
1336     sh_link = SymbolTableIndex;
1337     sh_info = GroupSymbolIndex;
1338     break;
1339 
1340   default:
1341     assert(0 && "FIXME: sh_type value not supported!");
1342     break;
1343   }
1344 
1345   if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1346       Section.getType() == ELF::SHT_ARM_EXIDX) {
1347     StringRef SecName(Section.getSectionName());
1348     if (SecName == ".ARM.exidx") {
1349       sh_link = SectionIndexMap.lookup(
1350         Asm.getContext().getELFSection(".text",
1351                                        ELF::SHT_PROGBITS,
1352                                        ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1353                                        SectionKind::getText()));
1354     } else if (SecName.startswith(".ARM.exidx")) {
1355       sh_link = SectionIndexMap.lookup(
1356         Asm.getContext().getELFSection(SecName.substr(sizeof(".ARM.exidx") - 1),
1357                                        ELF::SHT_PROGBITS,
1358                                        ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1359                                        SectionKind::getText()));
1360     }
1361   }
1362 
1363   WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1364                    Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1365                    Alignment, Section.getEntrySize());
1366 }
1367 
IsELFMetaDataSection(const MCSectionData & SD)1368 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
1369   return SD.getOrdinal() == ~UINT32_C(0) &&
1370     !SD.getSection().isVirtualSection();
1371 }
1372 
DataSectionSize(const MCSectionData & SD)1373 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
1374   uint64_t Ret = 0;
1375   for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1376        ++i) {
1377     const MCFragment &F = *i;
1378     assert(F.getKind() == MCFragment::FT_Data);
1379     Ret += cast<MCDataFragment>(F).getContents().size();
1380   }
1381   return Ret;
1382 }
1383 
GetSectionFileSize(const MCAsmLayout & Layout,const MCSectionData & SD)1384 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1385                                              const MCSectionData &SD) {
1386   if (IsELFMetaDataSection(SD))
1387     return DataSectionSize(SD);
1388   return Layout.getSectionFileSize(&SD);
1389 }
1390 
GetSectionAddressSize(const MCAsmLayout & Layout,const MCSectionData & SD)1391 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1392                                                 const MCSectionData &SD) {
1393   if (IsELFMetaDataSection(SD))
1394     return DataSectionSize(SD);
1395   return Layout.getSectionAddressSize(&SD);
1396 }
1397 
WriteDataSectionData(MCAssembler & Asm,const MCAsmLayout & Layout,const MCSectionELF & Section)1398 void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1399                                            const MCAsmLayout &Layout,
1400                                            const MCSectionELF &Section) {
1401   const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1402 
1403   uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
1404   WriteZeros(Padding);
1405 
1406   if (IsELFMetaDataSection(SD)) {
1407     for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1408          ++i) {
1409       const MCFragment &F = *i;
1410       assert(F.getKind() == MCFragment::FT_Data);
1411       WriteBytes(cast<MCDataFragment>(F).getContents());
1412     }
1413   } else {
1414     Asm.writeSectionData(&SD, Layout);
1415   }
1416 }
1417 
WriteSectionHeader(MCAssembler & Asm,const GroupMapTy & GroupMap,const MCAsmLayout & Layout,const SectionIndexMapTy & SectionIndexMap,const SectionOffsetMapTy & SectionOffsetMap)1418 void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1419                                          const GroupMapTy &GroupMap,
1420                                          const MCAsmLayout &Layout,
1421                                       const SectionIndexMapTy &SectionIndexMap,
1422                                    const SectionOffsetMapTy &SectionOffsetMap) {
1423   const unsigned NumSections = Asm.size() + 1;
1424 
1425   std::vector<const MCSectionELF*> Sections;
1426   Sections.resize(NumSections - 1);
1427 
1428   for (SectionIndexMapTy::const_iterator i=
1429          SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1430     const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1431     Sections[p.second - 1] = p.first;
1432   }
1433 
1434   // Null section first.
1435   uint64_t FirstSectionSize =
1436     NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1437   uint32_t FirstSectionLink =
1438     ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1439   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1440 
1441   for (unsigned i = 0; i < NumSections - 1; ++i) {
1442     const MCSectionELF &Section = *Sections[i];
1443     const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1444     uint32_t GroupSymbolIndex;
1445     if (Section.getType() != ELF::SHT_GROUP)
1446       GroupSymbolIndex = 0;
1447     else
1448       GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1449                                                      GroupMap.lookup(&Section));
1450 
1451     uint64_t Size = GetSectionAddressSize(Layout, SD);
1452 
1453     WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1454                  SectionOffsetMap.lookup(&Section), Size,
1455                  SD.getAlignment(), Section);
1456   }
1457 }
1458 
ComputeSectionOrder(MCAssembler & Asm,std::vector<const MCSectionELF * > & Sections)1459 void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1460                                   std::vector<const MCSectionELF*> &Sections) {
1461   for (MCAssembler::iterator it = Asm.begin(),
1462          ie = Asm.end(); it != ie; ++it) {
1463     const MCSectionELF &Section =
1464       static_cast<const MCSectionELF &>(it->getSection());
1465     if (Section.getType() == ELF::SHT_GROUP)
1466       Sections.push_back(&Section);
1467   }
1468 
1469   for (MCAssembler::iterator it = Asm.begin(),
1470          ie = Asm.end(); it != ie; ++it) {
1471     const MCSectionELF &Section =
1472       static_cast<const MCSectionELF &>(it->getSection());
1473     if (Section.getType() != ELF::SHT_GROUP &&
1474         Section.getType() != ELF::SHT_REL &&
1475         Section.getType() != ELF::SHT_RELA)
1476       Sections.push_back(&Section);
1477   }
1478 
1479   for (MCAssembler::iterator it = Asm.begin(),
1480          ie = Asm.end(); it != ie; ++it) {
1481     const MCSectionELF &Section =
1482       static_cast<const MCSectionELF &>(it->getSection());
1483     if (Section.getType() == ELF::SHT_REL ||
1484         Section.getType() == ELF::SHT_RELA)
1485       Sections.push_back(&Section);
1486   }
1487 }
1488 
WriteObject(MCAssembler & Asm,const MCAsmLayout & Layout)1489 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1490                                   const MCAsmLayout &Layout) {
1491   GroupMapTy GroupMap;
1492   RevGroupMapTy RevGroupMap;
1493   SectionIndexMapTy SectionIndexMap;
1494 
1495   unsigned NumUserSections = Asm.size();
1496 
1497   DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1498   CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1499 
1500   const unsigned NumUserAndRelocSections = Asm.size();
1501   CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1502                         RevGroupMap, SectionIndexMap, RelMap);
1503   const unsigned AllSections = Asm.size();
1504   const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1505 
1506   unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1507 
1508   // Compute symbol table information.
1509   ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1510 
1511 
1512   WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1513 
1514   CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1515                          const_cast<MCAsmLayout&>(Layout),
1516                          SectionIndexMap,
1517                          RelMap);
1518 
1519   uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1520   uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1521                                     sizeof(ELF::Elf32_Ehdr);
1522   uint64_t FileOff = HeaderSize;
1523 
1524   std::vector<const MCSectionELF*> Sections;
1525   ComputeSectionOrder(Asm, Sections);
1526   unsigned NumSections = Sections.size();
1527   SectionOffsetMapTy SectionOffsetMap;
1528   for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1529     const MCSectionELF &Section = *Sections[i];
1530     const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1531 
1532     FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1533 
1534     // Remember the offset into the file for this section.
1535     SectionOffsetMap[&Section] = FileOff;
1536 
1537     // Get the size of the section in the output file (including padding).
1538     FileOff += GetSectionFileSize(Layout, SD);
1539   }
1540 
1541   FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1542 
1543   const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1544 
1545   uint64_t SectionHeaderEntrySize = is64Bit() ?
1546     sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1547   FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1548 
1549   for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1550     const MCSectionELF &Section = *Sections[i];
1551     const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1552 
1553     FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1554 
1555     // Remember the offset into the file for this section.
1556     SectionOffsetMap[&Section] = FileOff;
1557 
1558     // Get the size of the section in the output file (including padding).
1559     FileOff += GetSectionFileSize(Layout, SD);
1560   }
1561 
1562   // Write out the ELF header ...
1563   WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
1564 
1565   // ... then the regular sections ...
1566   // + because of .shstrtab
1567   for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1568     WriteDataSectionData(Asm, Layout, *Sections[i]);
1569 
1570   uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
1571   WriteZeros(Padding);
1572 
1573   // ... then the section header table ...
1574   WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1575                      SectionOffsetMap);
1576 
1577   // ... and then the remaining sections ...
1578   for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1579     WriteDataSectionData(Asm, Layout, *Sections[i]);
1580 }
1581 
1582 bool
IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler & Asm,const MCSymbolData & DataA,const MCFragment & FB,bool InSet,bool IsPCRel) const1583 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1584                                                       const MCSymbolData &DataA,
1585                                                       const MCFragment &FB,
1586                                                       bool InSet,
1587                                                       bool IsPCRel) const {
1588   if (DataA.getFlags() & ELF_STB_Weak)
1589     return false;
1590   return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1591                                                  Asm, DataA, FB,InSet, IsPCRel);
1592 }
1593 
createELFObjectWriter(MCELFObjectTargetWriter * MOTW,raw_ostream & OS,bool IsLittleEndian)1594 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1595                                             raw_ostream &OS,
1596                                             bool IsLittleEndian) {
1597   return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
1598 }
1599