LLVM API Documentation
00001 //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements ELF object file writer information. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/MC/MCELFObjectWriter.h" 00015 #include "llvm/ADT/STLExtras.h" 00016 #include "llvm/ADT/SmallPtrSet.h" 00017 #include "llvm/ADT/SmallString.h" 00018 #include "llvm/ADT/StringMap.h" 00019 #include "llvm/MC/MCAsmBackend.h" 00020 #include "llvm/MC/MCAsmInfo.h" 00021 #include "llvm/MC/MCAsmLayout.h" 00022 #include "llvm/MC/MCAssembler.h" 00023 #include "llvm/MC/MCContext.h" 00024 #include "llvm/MC/MCELF.h" 00025 #include "llvm/MC/MCELFSymbolFlags.h" 00026 #include "llvm/MC/MCExpr.h" 00027 #include "llvm/MC/MCFixupKindInfo.h" 00028 #include "llvm/MC/MCObjectWriter.h" 00029 #include "llvm/MC/MCSectionELF.h" 00030 #include "llvm/MC/MCValue.h" 00031 #include "llvm/MC/StringTableBuilder.h" 00032 #include "llvm/Support/Compression.h" 00033 #include "llvm/Support/Debug.h" 00034 #include "llvm/Support/Endian.h" 00035 #include "llvm/Support/ELF.h" 00036 #include "llvm/Support/ErrorHandling.h" 00037 #include <vector> 00038 using namespace llvm; 00039 00040 #undef DEBUG_TYPE 00041 #define DEBUG_TYPE "reloc-info" 00042 00043 namespace { 00044 class FragmentWriter { 00045 bool IsLittleEndian; 00046 00047 public: 00048 FragmentWriter(bool IsLittleEndian); 00049 template <typename T> void write(MCDataFragment &F, T Val); 00050 }; 00051 00052 typedef DenseMap<const MCSectionELF *, uint32_t> SectionIndexMapTy; 00053 00054 class SymbolTableWriter { 00055 MCAssembler &Asm; 00056 FragmentWriter &FWriter; 00057 bool Is64Bit; 00058 SectionIndexMapTy &SectionIndexMap; 00059 00060 // The symbol .symtab fragment we are writting to. 00061 MCDataFragment *SymtabF; 00062 00063 // .symtab_shndx fragment we are writting to. 00064 MCDataFragment *ShndxF; 00065 00066 // The numbel of symbols written so far. 00067 unsigned NumWritten; 00068 00069 void createSymtabShndx(); 00070 00071 template <typename T> void write(MCDataFragment &F, T Value); 00072 00073 public: 00074 SymbolTableWriter(MCAssembler &Asm, FragmentWriter &FWriter, bool Is64Bit, 00075 SectionIndexMapTy &SectionIndexMap, 00076 MCDataFragment *SymtabF); 00077 00078 void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size, 00079 uint8_t other, uint32_t shndx, bool Reserved); 00080 }; 00081 00082 struct ELFRelocationEntry { 00083 uint64_t Offset; // Where is the relocation. 00084 bool UseSymbol; // Relocate with a symbol, not the section. 00085 union { 00086 const MCSymbol *Symbol; // The symbol to relocate with. 00087 const MCSectionData *Section; // The section to relocate with. 00088 }; 00089 unsigned Type; // The type of the relocation. 00090 uint64_t Addend; // The addend to use. 00091 00092 ELFRelocationEntry(uint64_t Offset, const MCSymbol *Symbol, unsigned Type, 00093 uint64_t Addend) 00094 : Offset(Offset), UseSymbol(true), Symbol(Symbol), Type(Type), 00095 Addend(Addend) {} 00096 00097 ELFRelocationEntry(uint64_t Offset, const MCSectionData *Section, 00098 unsigned Type, uint64_t Addend) 00099 : Offset(Offset), UseSymbol(false), Section(Section), Type(Type), 00100 Addend(Addend) {} 00101 }; 00102 00103 class ELFObjectWriter : public MCObjectWriter { 00104 FragmentWriter FWriter; 00105 00106 protected: 00107 00108 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind); 00109 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant); 00110 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout); 00111 static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolData &Data, 00112 bool Used, bool Renamed); 00113 static bool isLocal(const MCSymbolData &Data, bool isUsedInReloc); 00114 static bool IsELFMetaDataSection(const MCSectionData &SD); 00115 static uint64_t DataSectionSize(const MCSectionData &SD); 00116 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout, 00117 const MCSectionData &SD); 00118 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout, 00119 const MCSectionData &SD); 00120 00121 void WriteDataSectionData(MCAssembler &Asm, 00122 const MCAsmLayout &Layout, 00123 const MCSectionELF &Section); 00124 00125 /*static bool isFixupKindX86RIPRel(unsigned Kind) { 00126 return Kind == X86::reloc_riprel_4byte || 00127 Kind == X86::reloc_riprel_4byte_movq_load; 00128 }*/ 00129 00130 /// ELFSymbolData - Helper struct for containing some precomputed 00131 /// information on symbols. 00132 struct ELFSymbolData { 00133 MCSymbolData *SymbolData; 00134 uint64_t StringIndex; 00135 uint32_t SectionIndex; 00136 StringRef Name; 00137 00138 // Support lexicographic sorting. 00139 bool operator<(const ELFSymbolData &RHS) const { 00140 return Name < RHS.Name; 00141 } 00142 }; 00143 00144 /// The target specific ELF writer instance. 00145 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter; 00146 00147 SmallPtrSet<const MCSymbol *, 16> UsedInReloc; 00148 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc; 00149 DenseMap<const MCSymbol *, const MCSymbol *> Renames; 00150 00151 llvm::DenseMap<const MCSectionData *, std::vector<ELFRelocationEntry>> 00152 Relocations; 00153 StringTableBuilder ShStrTabBuilder; 00154 00155 /// @} 00156 /// @name Symbol Table Data 00157 /// @{ 00158 00159 StringTableBuilder StrTabBuilder; 00160 std::vector<uint64_t> FileSymbolData; 00161 std::vector<ELFSymbolData> LocalSymbolData; 00162 std::vector<ELFSymbolData> ExternalSymbolData; 00163 std::vector<ELFSymbolData> UndefinedSymbolData; 00164 00165 /// @} 00166 00167 bool NeedsGOT; 00168 00169 // This holds the symbol table index of the last local symbol. 00170 unsigned LastLocalSymbolIndex; 00171 // This holds the .strtab section index. 00172 unsigned StringTableIndex; 00173 // This holds the .symtab section index. 00174 unsigned SymbolTableIndex; 00175 00176 unsigned ShstrtabIndex; 00177 00178 00179 // TargetObjectWriter wrappers. 00180 bool is64Bit() const { return TargetObjectWriter->is64Bit(); } 00181 bool hasRelocationAddend() const { 00182 return TargetObjectWriter->hasRelocationAddend(); 00183 } 00184 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, 00185 bool IsPCRel) const { 00186 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel); 00187 } 00188 00189 public: 00190 ELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_ostream &_OS, 00191 bool IsLittleEndian) 00192 : MCObjectWriter(_OS, IsLittleEndian), FWriter(IsLittleEndian), 00193 TargetObjectWriter(MOTW), NeedsGOT(false) {} 00194 00195 virtual ~ELFObjectWriter(); 00196 00197 void WriteWord(uint64_t W) { 00198 if (is64Bit()) 00199 Write64(W); 00200 else 00201 Write32(W); 00202 } 00203 00204 template <typename T> void write(MCDataFragment &F, T Value) { 00205 FWriter.write(F, Value); 00206 } 00207 00208 void WriteHeader(const MCAssembler &Asm, 00209 uint64_t SectionDataSize, 00210 unsigned NumberOfSections); 00211 00212 void WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD, 00213 const MCAsmLayout &Layout); 00214 00215 void WriteSymbolTable(MCDataFragment *SymtabF, MCAssembler &Asm, 00216 const MCAsmLayout &Layout, 00217 SectionIndexMapTy &SectionIndexMap); 00218 00219 bool shouldRelocateWithSymbol(const MCAssembler &Asm, 00220 const MCSymbolRefExpr *RefA, 00221 const MCSymbolData *SD, uint64_t C, 00222 unsigned Type) const; 00223 00224 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, 00225 const MCFragment *Fragment, const MCFixup &Fixup, 00226 MCValue Target, bool &IsPCRel, 00227 uint64_t &FixedValue) override; 00228 00229 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm, 00230 const MCSymbol *S); 00231 00232 // Map from a group section to the signature symbol 00233 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy; 00234 // Map from a signature symbol to the group section 00235 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy; 00236 // Map from a section to the section with the relocations 00237 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy; 00238 // Map from a section to its offset 00239 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy; 00240 00241 /// Compute the symbol table data 00242 /// 00243 /// \param Asm - The assembler. 00244 /// \param SectionIndexMap - Maps a section to its index. 00245 /// \param RevGroupMap - Maps a signature symbol to the group section. 00246 /// \param NumRegularSections - Number of non-relocation sections. 00247 void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout, 00248 const SectionIndexMapTy &SectionIndexMap, 00249 RevGroupMapTy RevGroupMap, 00250 unsigned NumRegularSections); 00251 00252 void ComputeIndexMap(MCAssembler &Asm, 00253 SectionIndexMapTy &SectionIndexMap, 00254 const RelMapTy &RelMap); 00255 00256 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout, 00257 RelMapTy &RelMap); 00258 00259 void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout); 00260 00261 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout, 00262 const RelMapTy &RelMap); 00263 00264 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout, 00265 SectionIndexMapTy &SectionIndexMap, 00266 const RelMapTy &RelMap); 00267 00268 // Create the sections that show up in the symbol table. Currently 00269 // those are the .note.GNU-stack section and the group sections. 00270 void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout, 00271 GroupMapTy &GroupMap, 00272 RevGroupMapTy &RevGroupMap, 00273 SectionIndexMapTy &SectionIndexMap, 00274 const RelMapTy &RelMap); 00275 00276 void ExecutePostLayoutBinding(MCAssembler &Asm, 00277 const MCAsmLayout &Layout) override; 00278 00279 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap, 00280 const MCAsmLayout &Layout, 00281 const SectionIndexMapTy &SectionIndexMap, 00282 const SectionOffsetMapTy &SectionOffsetMap); 00283 00284 void ComputeSectionOrder(MCAssembler &Asm, 00285 std::vector<const MCSectionELF*> &Sections); 00286 00287 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, 00288 uint64_t Address, uint64_t Offset, 00289 uint64_t Size, uint32_t Link, uint32_t Info, 00290 uint64_t Alignment, uint64_t EntrySize); 00291 00292 void WriteRelocationsFragment(const MCAssembler &Asm, 00293 MCDataFragment *F, 00294 const MCSectionData *SD); 00295 00296 bool 00297 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, 00298 const MCSymbolData &DataA, 00299 const MCFragment &FB, 00300 bool InSet, 00301 bool IsPCRel) const override; 00302 00303 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; 00304 void WriteSection(MCAssembler &Asm, 00305 const SectionIndexMapTy &SectionIndexMap, 00306 uint32_t GroupSymbolIndex, 00307 uint64_t Offset, uint64_t Size, uint64_t Alignment, 00308 const MCSectionELF &Section); 00309 }; 00310 } 00311 00312 FragmentWriter::FragmentWriter(bool IsLittleEndian) 00313 : IsLittleEndian(IsLittleEndian) {} 00314 00315 template <typename T> void FragmentWriter::write(MCDataFragment &F, T Val) { 00316 if (IsLittleEndian) 00317 Val = support::endian::byte_swap<T, support::little>(Val); 00318 else 00319 Val = support::endian::byte_swap<T, support::big>(Val); 00320 const char *Start = (const char *)&Val; 00321 F.getContents().append(Start, Start + sizeof(T)); 00322 } 00323 00324 void SymbolTableWriter::createSymtabShndx() { 00325 if (ShndxF) 00326 return; 00327 00328 MCContext &Ctx = Asm.getContext(); 00329 const MCSectionELF *SymtabShndxSection = 00330 Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 00331 SectionKind::getReadOnly(), 4, ""); 00332 MCSectionData *SymtabShndxSD = 00333 &Asm.getOrCreateSectionData(*SymtabShndxSection); 00334 SymtabShndxSD->setAlignment(4); 00335 ShndxF = new MCDataFragment(SymtabShndxSD); 00336 unsigned Index = SectionIndexMap.size() + 1; 00337 SectionIndexMap[SymtabShndxSection] = Index; 00338 00339 for (unsigned I = 0; I < NumWritten; ++I) 00340 write(*ShndxF, uint32_t(0)); 00341 } 00342 00343 template <typename T> 00344 void SymbolTableWriter::write(MCDataFragment &F, T Value) { 00345 FWriter.write(F, Value); 00346 } 00347 00348 SymbolTableWriter::SymbolTableWriter(MCAssembler &Asm, FragmentWriter &FWriter, 00349 bool Is64Bit, 00350 SectionIndexMapTy &SectionIndexMap, 00351 MCDataFragment *SymtabF) 00352 : Asm(Asm), FWriter(FWriter), Is64Bit(Is64Bit), 00353 SectionIndexMap(SectionIndexMap), SymtabF(SymtabF), ShndxF(nullptr), 00354 NumWritten(0) {} 00355 00356 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value, 00357 uint64_t size, uint8_t other, 00358 uint32_t shndx, bool Reserved) { 00359 bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved; 00360 00361 if (LargeIndex) 00362 createSymtabShndx(); 00363 00364 if (ShndxF) { 00365 if (LargeIndex) 00366 write(*ShndxF, shndx); 00367 else 00368 write(*ShndxF, uint32_t(0)); 00369 } 00370 00371 uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx; 00372 00373 raw_svector_ostream OS(SymtabF->getContents()); 00374 00375 if (Is64Bit) { 00376 write(*SymtabF, name); // st_name 00377 write(*SymtabF, info); // st_info 00378 write(*SymtabF, other); // st_other 00379 write(*SymtabF, Index); // st_shndx 00380 write(*SymtabF, value); // st_value 00381 write(*SymtabF, size); // st_size 00382 } else { 00383 write(*SymtabF, name); // st_name 00384 write(*SymtabF, uint32_t(value)); // st_value 00385 write(*SymtabF, uint32_t(size)); // st_size 00386 write(*SymtabF, info); // st_info 00387 write(*SymtabF, other); // st_other 00388 write(*SymtabF, Index); // st_shndx 00389 } 00390 00391 ++NumWritten; 00392 } 00393 00394 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { 00395 const MCFixupKindInfo &FKI = 00396 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind); 00397 00398 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel; 00399 } 00400 00401 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) { 00402 switch (Variant) { 00403 default: 00404 return false; 00405 case MCSymbolRefExpr::VK_GOT: 00406 case MCSymbolRefExpr::VK_PLT: 00407 case MCSymbolRefExpr::VK_GOTPCREL: 00408 case MCSymbolRefExpr::VK_GOTOFF: 00409 case MCSymbolRefExpr::VK_TPOFF: 00410 case MCSymbolRefExpr::VK_TLSGD: 00411 case MCSymbolRefExpr::VK_GOTTPOFF: 00412 case MCSymbolRefExpr::VK_INDNTPOFF: 00413 case MCSymbolRefExpr::VK_NTPOFF: 00414 case MCSymbolRefExpr::VK_GOTNTPOFF: 00415 case MCSymbolRefExpr::VK_TLSLDM: 00416 case MCSymbolRefExpr::VK_DTPOFF: 00417 case MCSymbolRefExpr::VK_TLSLD: 00418 return true; 00419 } 00420 } 00421 00422 ELFObjectWriter::~ELFObjectWriter() 00423 {} 00424 00425 // Emit the ELF header. 00426 void ELFObjectWriter::WriteHeader(const MCAssembler &Asm, 00427 uint64_t SectionDataSize, 00428 unsigned NumberOfSections) { 00429 // ELF Header 00430 // ---------- 00431 // 00432 // Note 00433 // ---- 00434 // emitWord method behaves differently for ELF32 and ELF64, writing 00435 // 4 bytes in the former and 8 in the latter. 00436 00437 Write8(0x7f); // e_ident[EI_MAG0] 00438 Write8('E'); // e_ident[EI_MAG1] 00439 Write8('L'); // e_ident[EI_MAG2] 00440 Write8('F'); // e_ident[EI_MAG3] 00441 00442 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS] 00443 00444 // e_ident[EI_DATA] 00445 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB); 00446 00447 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION] 00448 // e_ident[EI_OSABI] 00449 Write8(TargetObjectWriter->getOSABI()); 00450 Write8(0); // e_ident[EI_ABIVERSION] 00451 00452 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD); 00453 00454 Write16(ELF::ET_REL); // e_type 00455 00456 Write16(TargetObjectWriter->getEMachine()); // e_machine = target 00457 00458 Write32(ELF::EV_CURRENT); // e_version 00459 WriteWord(0); // e_entry, no entry point in .o file 00460 WriteWord(0); // e_phoff, no program header for .o 00461 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) : 00462 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes 00463 00464 // e_flags = whatever the target wants 00465 Write32(Asm.getELFHeaderEFlags()); 00466 00467 // e_ehsize = ELF header size 00468 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr)); 00469 00470 Write16(0); // e_phentsize = prog header entry size 00471 Write16(0); // e_phnum = # prog header entries = 0 00472 00473 // e_shentsize = Section header entry size 00474 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr)); 00475 00476 // e_shnum = # of section header ents 00477 if (NumberOfSections >= ELF::SHN_LORESERVE) 00478 Write16(ELF::SHN_UNDEF); 00479 else 00480 Write16(NumberOfSections); 00481 00482 // e_shstrndx = Section # of '.shstrtab' 00483 if (ShstrtabIndex >= ELF::SHN_LORESERVE) 00484 Write16(ELF::SHN_XINDEX); 00485 else 00486 Write16(ShstrtabIndex); 00487 } 00488 00489 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data, 00490 const MCAsmLayout &Layout) { 00491 if (Data.isCommon() && Data.isExternal()) 00492 return Data.getCommonAlignment(); 00493 00494 uint64_t Res; 00495 if (!Layout.getSymbolOffset(&Data, Res)) 00496 return 0; 00497 00498 if (Layout.getAssembler().isThumbFunc(&Data.getSymbol())) 00499 Res |= 1; 00500 00501 return Res; 00502 } 00503 00504 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, 00505 const MCAsmLayout &Layout) { 00506 // The presence of symbol versions causes undefined symbols and 00507 // versions declared with @@@ to be renamed. 00508 00509 for (MCSymbolData &OriginalData : Asm.symbols()) { 00510 const MCSymbol &Alias = OriginalData.getSymbol(); 00511 00512 // Not an alias. 00513 if (!Alias.isVariable()) 00514 continue; 00515 auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue()); 00516 if (!Ref) 00517 continue; 00518 const MCSymbol &Symbol = Ref->getSymbol(); 00519 MCSymbolData &SD = Asm.getSymbolData(Symbol); 00520 00521 StringRef AliasName = Alias.getName(); 00522 size_t Pos = AliasName.find('@'); 00523 if (Pos == StringRef::npos) 00524 continue; 00525 00526 // Aliases defined with .symvar copy the binding from the symbol they alias. 00527 // This is the first place we are able to copy this information. 00528 OriginalData.setExternal(SD.isExternal()); 00529 MCELF::SetBinding(OriginalData, MCELF::GetBinding(SD)); 00530 00531 StringRef Rest = AliasName.substr(Pos); 00532 if (!Symbol.isUndefined() && !Rest.startswith("@@@")) 00533 continue; 00534 00535 // FIXME: produce a better error message. 00536 if (Symbol.isUndefined() && Rest.startswith("@@") && 00537 !Rest.startswith("@@@")) 00538 report_fatal_error("A @@ version cannot be undefined"); 00539 00540 Renames.insert(std::make_pair(&Symbol, &Alias)); 00541 } 00542 } 00543 00544 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) { 00545 uint8_t Type = newType; 00546 00547 // Propagation rules: 00548 // IFUNC > FUNC > OBJECT > NOTYPE 00549 // TLS_OBJECT > OBJECT > NOTYPE 00550 // 00551 // dont let the new type degrade the old type 00552 switch (origType) { 00553 default: 00554 break; 00555 case ELF::STT_GNU_IFUNC: 00556 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT || 00557 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS) 00558 Type = ELF::STT_GNU_IFUNC; 00559 break; 00560 case ELF::STT_FUNC: 00561 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || 00562 Type == ELF::STT_TLS) 00563 Type = ELF::STT_FUNC; 00564 break; 00565 case ELF::STT_OBJECT: 00566 if (Type == ELF::STT_NOTYPE) 00567 Type = ELF::STT_OBJECT; 00568 break; 00569 case ELF::STT_TLS: 00570 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || 00571 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC) 00572 Type = ELF::STT_TLS; 00573 break; 00574 } 00575 00576 return Type; 00577 } 00578 00579 void ELFObjectWriter::WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD, 00580 const MCAsmLayout &Layout) { 00581 MCSymbolData &OrigData = *MSD.SymbolData; 00582 assert((!OrigData.getFragment() || 00583 (&OrigData.getFragment()->getParent()->getSection() == 00584 &OrigData.getSymbol().getSection())) && 00585 "The symbol's section doesn't match the fragment's symbol"); 00586 const MCSymbol *Base = Layout.getBaseSymbol(OrigData.getSymbol()); 00587 00588 // This has to be in sync with when computeSymbolTable uses SHN_ABS or 00589 // SHN_COMMON. 00590 bool IsReserved = !Base || OrigData.isCommon(); 00591 00592 // Binding and Type share the same byte as upper and lower nibbles 00593 uint8_t Binding = MCELF::GetBinding(OrigData); 00594 uint8_t Type = MCELF::GetType(OrigData); 00595 MCSymbolData *BaseSD = nullptr; 00596 if (Base) { 00597 BaseSD = &Layout.getAssembler().getSymbolData(*Base); 00598 Type = mergeTypeForSet(Type, MCELF::GetType(*BaseSD)); 00599 } 00600 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift); 00601 00602 // Other and Visibility share the same byte with Visibility using the lower 00603 // 2 bits 00604 uint8_t Visibility = MCELF::GetVisibility(OrigData); 00605 uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift); 00606 Other |= Visibility; 00607 00608 uint64_t Value = SymbolValue(OrigData, Layout); 00609 uint64_t Size = 0; 00610 00611 const MCExpr *ESize = OrigData.getSize(); 00612 if (!ESize && Base) 00613 ESize = BaseSD->getSize(); 00614 00615 if (ESize) { 00616 int64_t Res; 00617 if (!ESize->EvaluateAsAbsolute(Res, Layout)) 00618 report_fatal_error("Size expression must be absolute."); 00619 Size = Res; 00620 } 00621 00622 // Write out the symbol table entry 00623 Writer.writeSymbol(MSD.StringIndex, Info, Value, Size, Other, 00624 MSD.SectionIndex, IsReserved); 00625 } 00626 00627 void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF, 00628 MCAssembler &Asm, 00629 const MCAsmLayout &Layout, 00630 SectionIndexMapTy &SectionIndexMap) { 00631 // The string table must be emitted first because we need the index 00632 // into the string table for all the symbol names. 00633 00634 // FIXME: Make sure the start of the symbol table is aligned. 00635 00636 SymbolTableWriter Writer(Asm, FWriter, is64Bit(), SectionIndexMap, SymtabF); 00637 00638 // The first entry is the undefined symbol entry. 00639 Writer.writeSymbol(0, 0, 0, 0, 0, 0, false); 00640 00641 for (unsigned i = 0, e = FileSymbolData.size(); i != e; ++i) { 00642 Writer.writeSymbol(FileSymbolData[i], ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, 00643 ELF::STV_DEFAULT, ELF::SHN_ABS, true); 00644 } 00645 00646 // Write the symbol table entries. 00647 LastLocalSymbolIndex = FileSymbolData.size() + LocalSymbolData.size() + 1; 00648 00649 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) { 00650 ELFSymbolData &MSD = LocalSymbolData[i]; 00651 WriteSymbol(Writer, MSD, Layout); 00652 } 00653 00654 // Write out a symbol table entry for each regular section. 00655 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; 00656 ++i) { 00657 const MCSectionELF &Section = 00658 static_cast<const MCSectionELF&>(i->getSection()); 00659 if (Section.getType() == ELF::SHT_RELA || 00660 Section.getType() == ELF::SHT_REL || 00661 Section.getType() == ELF::SHT_STRTAB || 00662 Section.getType() == ELF::SHT_SYMTAB || 00663 Section.getType() == ELF::SHT_SYMTAB_SHNDX) 00664 continue; 00665 Writer.writeSymbol(0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, 00666 SectionIndexMap.lookup(&Section), false); 00667 LastLocalSymbolIndex++; 00668 } 00669 00670 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) { 00671 ELFSymbolData &MSD = ExternalSymbolData[i]; 00672 MCSymbolData &Data = *MSD.SymbolData; 00673 assert(((Data.getFlags() & ELF_STB_Global) || 00674 (Data.getFlags() & ELF_STB_Weak)) && 00675 "External symbol requires STB_GLOBAL or STB_WEAK flag"); 00676 WriteSymbol(Writer, MSD, Layout); 00677 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL) 00678 LastLocalSymbolIndex++; 00679 } 00680 00681 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) { 00682 ELFSymbolData &MSD = UndefinedSymbolData[i]; 00683 MCSymbolData &Data = *MSD.SymbolData; 00684 WriteSymbol(Writer, MSD, Layout); 00685 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL) 00686 LastLocalSymbolIndex++; 00687 } 00688 } 00689 00690 // It is always valid to create a relocation with a symbol. It is preferable 00691 // to use a relocation with a section if that is possible. Using the section 00692 // allows us to omit some local symbols from the symbol table. 00693 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm, 00694 const MCSymbolRefExpr *RefA, 00695 const MCSymbolData *SD, 00696 uint64_t C, 00697 unsigned Type) const { 00698 // A PCRel relocation to an absolute value has no symbol (or section). We 00699 // represent that with a relocation to a null section. 00700 if (!RefA) 00701 return false; 00702 00703 MCSymbolRefExpr::VariantKind Kind = RefA->getKind(); 00704 switch (Kind) { 00705 default: 00706 break; 00707 // The .odp creation emits a relocation against the symbol ".TOC." which 00708 // create a R_PPC64_TOC relocation. However the relocation symbol name 00709 // in final object creation should be NULL, since the symbol does not 00710 // really exist, it is just the reference to TOC base for the current 00711 // object file. Since the symbol is undefined, returning false results 00712 // in a relocation with a null section which is the desired result. 00713 case MCSymbolRefExpr::VK_PPC_TOCBASE: 00714 return false; 00715 00716 // These VariantKind cause the relocation to refer to something other than 00717 // the symbol itself, like a linker generated table. Since the address of 00718 // symbol is not relevant, we cannot replace the symbol with the 00719 // section and patch the difference in the addend. 00720 case MCSymbolRefExpr::VK_GOT: 00721 case MCSymbolRefExpr::VK_PLT: 00722 case MCSymbolRefExpr::VK_GOTPCREL: 00723 case MCSymbolRefExpr::VK_Mips_GOT: 00724 case MCSymbolRefExpr::VK_PPC_GOT_LO: 00725 case MCSymbolRefExpr::VK_PPC_GOT_HI: 00726 case MCSymbolRefExpr::VK_PPC_GOT_HA: 00727 return true; 00728 } 00729 00730 // An undefined symbol is not in any section, so the relocation has to point 00731 // to the symbol itself. 00732 const MCSymbol &Sym = SD->getSymbol(); 00733 if (Sym.isUndefined()) 00734 return true; 00735 00736 unsigned Binding = MCELF::GetBinding(*SD); 00737 switch(Binding) { 00738 default: 00739 llvm_unreachable("Invalid Binding"); 00740 case ELF::STB_LOCAL: 00741 break; 00742 case ELF::STB_WEAK: 00743 // If the symbol is weak, it might be overridden by a symbol in another 00744 // file. The relocation has to point to the symbol so that the linker 00745 // can update it. 00746 return true; 00747 case ELF::STB_GLOBAL: 00748 // Global ELF symbols can be preempted by the dynamic linker. The relocation 00749 // has to point to the symbol for a reason analogous to the STB_WEAK case. 00750 return true; 00751 } 00752 00753 // If a relocation points to a mergeable section, we have to be careful. 00754 // If the offset is zero, a relocation with the section will encode the 00755 // same information. With a non-zero offset, the situation is different. 00756 // For example, a relocation can point 42 bytes past the end of a string. 00757 // If we change such a relocation to use the section, the linker would think 00758 // that it pointed to another string and subtracting 42 at runtime will 00759 // produce the wrong value. 00760 auto &Sec = cast<MCSectionELF>(Sym.getSection()); 00761 unsigned Flags = Sec.getFlags(); 00762 if (Flags & ELF::SHF_MERGE) { 00763 if (C != 0) 00764 return true; 00765 00766 // It looks like gold has a bug (http://sourceware.org/PR16794) and can 00767 // only handle section relocations to mergeable sections if using RELA. 00768 if (!hasRelocationAddend()) 00769 return true; 00770 } 00771 00772 // Most TLS relocations use a got, so they need the symbol. Even those that 00773 // are just an offset (@tpoff), require a symbol in some linkers (gold, 00774 // but not bfd ld). 00775 if (Flags & ELF::SHF_TLS) 00776 return true; 00777 00778 // If the symbol is a thumb function the final relocation must set the lowest 00779 // bit. With a symbol that is done by just having the symbol have that bit 00780 // set, so we would lose the bit if we relocated with the section. 00781 // FIXME: We could use the section but add the bit to the relocation value. 00782 if (Asm.isThumbFunc(&Sym)) 00783 return true; 00784 00785 if (TargetObjectWriter->needsRelocateWithSymbol(*SD, Type)) 00786 return true; 00787 return false; 00788 } 00789 00790 static const MCSymbol *getWeakRef(const MCSymbolRefExpr &Ref) { 00791 const MCSymbol &Sym = Ref.getSymbol(); 00792 00793 if (Ref.getKind() == MCSymbolRefExpr::VK_WEAKREF) 00794 return &Sym; 00795 00796 if (!Sym.isVariable()) 00797 return nullptr; 00798 00799 const MCExpr *Expr = Sym.getVariableValue(); 00800 const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr); 00801 if (!Inner) 00802 return nullptr; 00803 00804 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) 00805 return &Inner->getSymbol(); 00806 return nullptr; 00807 } 00808 00809 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, 00810 const MCAsmLayout &Layout, 00811 const MCFragment *Fragment, 00812 const MCFixup &Fixup, 00813 MCValue Target, 00814 bool &IsPCRel, 00815 uint64_t &FixedValue) { 00816 const MCSectionData *FixupSection = Fragment->getParent(); 00817 uint64_t C = Target.getConstant(); 00818 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); 00819 00820 if (const MCSymbolRefExpr *RefB = Target.getSymB()) { 00821 assert(RefB->getKind() == MCSymbolRefExpr::VK_None && 00822 "Should not have constructed this"); 00823 00824 // Let A, B and C being the components of Target and R be the location of 00825 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C). 00826 // If it is pcrel, we want to compute (A - B + C - R). 00827 00828 // In general, ELF has no relocations for -B. It can only represent (A + C) 00829 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can 00830 // replace B to implement it: (A - R - K + C) 00831 if (IsPCRel) 00832 Asm.getContext().FatalError( 00833 Fixup.getLoc(), 00834 "No relocation available to represent this relative expression"); 00835 00836 const MCSymbol &SymB = RefB->getSymbol(); 00837 00838 if (SymB.isUndefined()) 00839 Asm.getContext().FatalError( 00840 Fixup.getLoc(), 00841 Twine("symbol '") + SymB.getName() + 00842 "' can not be undefined in a subtraction expression"); 00843 00844 assert(!SymB.isAbsolute() && "Should have been folded"); 00845 const MCSection &SecB = SymB.getSection(); 00846 if (&SecB != &FixupSection->getSection()) 00847 Asm.getContext().FatalError( 00848 Fixup.getLoc(), "Cannot represent a difference across sections"); 00849 00850 const MCSymbolData &SymBD = Asm.getSymbolData(SymB); 00851 uint64_t SymBOffset = Layout.getSymbolOffset(&SymBD); 00852 uint64_t K = SymBOffset - FixupOffset; 00853 IsPCRel = true; 00854 C -= K; 00855 } 00856 00857 // We either rejected the fixup or folded B into C at this point. 00858 const MCSymbolRefExpr *RefA = Target.getSymA(); 00859 const MCSymbol *SymA = RefA ? &RefA->getSymbol() : nullptr; 00860 const MCSymbolData *SymAD = SymA ? &Asm.getSymbolData(*SymA) : nullptr; 00861 00862 unsigned Type = GetRelocType(Target, Fixup, IsPCRel); 00863 bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymAD, C, Type); 00864 if (!RelocateWithSymbol && SymA && !SymA->isUndefined()) 00865 C += Layout.getSymbolOffset(SymAD); 00866 00867 uint64_t Addend = 0; 00868 if (hasRelocationAddend()) { 00869 Addend = C; 00870 C = 0; 00871 } 00872 00873 FixedValue = C; 00874 00875 // FIXME: What is this!?!? 00876 MCSymbolRefExpr::VariantKind Modifier = 00877 RefA ? RefA->getKind() : MCSymbolRefExpr::VK_None; 00878 if (RelocNeedsGOT(Modifier)) 00879 NeedsGOT = true; 00880 00881 if (!RelocateWithSymbol) { 00882 const MCSection *SecA = 00883 (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr; 00884 const MCSectionData *SecAD = SecA ? &Asm.getSectionData(*SecA) : nullptr; 00885 ELFRelocationEntry Rec(FixupOffset, SecAD, Type, Addend); 00886 Relocations[FixupSection].push_back(Rec); 00887 return; 00888 } 00889 00890 if (SymA) { 00891 if (const MCSymbol *R = Renames.lookup(SymA)) 00892 SymA = R; 00893 00894 if (const MCSymbol *WeakRef = getWeakRef(*RefA)) 00895 WeakrefUsedInReloc.insert(WeakRef); 00896 else 00897 UsedInReloc.insert(SymA); 00898 } 00899 ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend); 00900 Relocations[FixupSection].push_back(Rec); 00901 return; 00902 } 00903 00904 00905 uint64_t 00906 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm, 00907 const MCSymbol *S) { 00908 const MCSymbolData &SD = Asm.getSymbolData(*S); 00909 return SD.getIndex(); 00910 } 00911 00912 bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout, 00913 const MCSymbolData &Data, bool Used, 00914 bool Renamed) { 00915 const MCSymbol &Symbol = Data.getSymbol(); 00916 if (Symbol.isVariable()) { 00917 const MCExpr *Expr = Symbol.getVariableValue(); 00918 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) { 00919 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF) 00920 return false; 00921 } 00922 } 00923 00924 if (Used) 00925 return true; 00926 00927 if (Renamed) 00928 return false; 00929 00930 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_") 00931 return true; 00932 00933 if (Symbol.isVariable()) { 00934 const MCSymbol *Base = Layout.getBaseSymbol(Symbol); 00935 if (Base && Base->isUndefined()) 00936 return false; 00937 } 00938 00939 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL; 00940 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal) 00941 return false; 00942 00943 if (Symbol.isTemporary()) 00944 return false; 00945 00946 return true; 00947 } 00948 00949 bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isUsedInReloc) { 00950 if (Data.isExternal()) 00951 return false; 00952 00953 const MCSymbol &Symbol = Data.getSymbol(); 00954 if (Symbol.isDefined()) 00955 return true; 00956 00957 if (isUsedInReloc) 00958 return false; 00959 00960 return true; 00961 } 00962 00963 void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm, 00964 SectionIndexMapTy &SectionIndexMap, 00965 const RelMapTy &RelMap) { 00966 unsigned Index = 1; 00967 for (MCAssembler::iterator it = Asm.begin(), 00968 ie = Asm.end(); it != ie; ++it) { 00969 const MCSectionELF &Section = 00970 static_cast<const MCSectionELF &>(it->getSection()); 00971 if (Section.getType() != ELF::SHT_GROUP) 00972 continue; 00973 SectionIndexMap[&Section] = Index++; 00974 } 00975 00976 for (MCAssembler::iterator it = Asm.begin(), 00977 ie = Asm.end(); it != ie; ++it) { 00978 const MCSectionELF &Section = 00979 static_cast<const MCSectionELF &>(it->getSection()); 00980 if (Section.getType() == ELF::SHT_GROUP || 00981 Section.getType() == ELF::SHT_REL || 00982 Section.getType() == ELF::SHT_RELA) 00983 continue; 00984 SectionIndexMap[&Section] = Index++; 00985 const MCSectionELF *RelSection = RelMap.lookup(&Section); 00986 if (RelSection) 00987 SectionIndexMap[RelSection] = Index++; 00988 } 00989 } 00990 00991 void 00992 ELFObjectWriter::computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout, 00993 const SectionIndexMapTy &SectionIndexMap, 00994 RevGroupMapTy RevGroupMap, 00995 unsigned NumRegularSections) { 00996 // FIXME: Is this the correct place to do this? 00997 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed? 00998 if (NeedsGOT) { 00999 StringRef Name = "_GLOBAL_OFFSET_TABLE_"; 01000 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name); 01001 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym); 01002 Data.setExternal(true); 01003 MCELF::SetBinding(Data, ELF::STB_GLOBAL); 01004 } 01005 01006 // Add the data for the symbols. 01007 for (MCSymbolData &SD : Asm.symbols()) { 01008 const MCSymbol &Symbol = SD.getSymbol(); 01009 01010 bool Used = UsedInReloc.count(&Symbol); 01011 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol); 01012 bool isSignature = RevGroupMap.count(&Symbol); 01013 01014 if (!isInSymtab(Layout, SD, 01015 Used || WeakrefUsed || isSignature, 01016 Renames.count(&Symbol))) 01017 continue; 01018 01019 ELFSymbolData MSD; 01020 MSD.SymbolData = &SD; 01021 const MCSymbol *BaseSymbol = Layout.getBaseSymbol(Symbol); 01022 01023 // Undefined symbols are global, but this is the first place we 01024 // are able to set it. 01025 bool Local = isLocal(SD, Used); 01026 if (!Local && MCELF::GetBinding(SD) == ELF::STB_LOCAL) { 01027 assert(BaseSymbol); 01028 MCSymbolData &BaseData = Asm.getSymbolData(*BaseSymbol); 01029 MCELF::SetBinding(SD, ELF::STB_GLOBAL); 01030 MCELF::SetBinding(BaseData, ELF::STB_GLOBAL); 01031 } 01032 01033 if (!BaseSymbol) { 01034 MSD.SectionIndex = ELF::SHN_ABS; 01035 } else if (SD.isCommon()) { 01036 assert(!Local); 01037 MSD.SectionIndex = ELF::SHN_COMMON; 01038 } else if (BaseSymbol->isUndefined()) { 01039 if (isSignature && !Used) 01040 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]); 01041 else 01042 MSD.SectionIndex = ELF::SHN_UNDEF; 01043 if (!Used && WeakrefUsed) 01044 MCELF::SetBinding(SD, ELF::STB_WEAK); 01045 } else { 01046 const MCSectionELF &Section = 01047 static_cast<const MCSectionELF&>(BaseSymbol->getSection()); 01048 MSD.SectionIndex = SectionIndexMap.lookup(&Section); 01049 assert(MSD.SectionIndex && "Invalid section index!"); 01050 } 01051 01052 // The @@@ in symbol version is replaced with @ in undefined symbols and 01053 // @@ in defined ones. 01054 StringRef Name = Symbol.getName(); 01055 SmallString<32> Buf; 01056 size_t Pos = Name.find("@@@"); 01057 if (Pos != StringRef::npos) { 01058 Buf += Name.substr(0, Pos); 01059 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1; 01060 Buf += Name.substr(Pos + Skip); 01061 Name = Buf; 01062 } 01063 MSD.Name = StrTabBuilder.add(Name); 01064 01065 if (MSD.SectionIndex == ELF::SHN_UNDEF) 01066 UndefinedSymbolData.push_back(MSD); 01067 else if (Local) 01068 LocalSymbolData.push_back(MSD); 01069 else 01070 ExternalSymbolData.push_back(MSD); 01071 } 01072 01073 for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i) 01074 StrTabBuilder.add(*i); 01075 01076 StrTabBuilder.finalize(); 01077 01078 for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i) 01079 FileSymbolData.push_back(StrTabBuilder.getOffset(*i)); 01080 01081 for (ELFSymbolData& MSD : LocalSymbolData) 01082 MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name); 01083 for (ELFSymbolData& MSD : ExternalSymbolData) 01084 MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name); 01085 for (ELFSymbolData& MSD : UndefinedSymbolData) 01086 MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name); 01087 01088 // Symbols are required to be in lexicographic order. 01089 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); 01090 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); 01091 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); 01092 01093 // Set the symbol indices. Local symbols must come before all other 01094 // symbols with non-local bindings. 01095 unsigned Index = FileSymbolData.size() + 1; 01096 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) 01097 LocalSymbolData[i].SymbolData->setIndex(Index++); 01098 01099 Index += NumRegularSections; 01100 01101 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) 01102 ExternalSymbolData[i].SymbolData->setIndex(Index++); 01103 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) 01104 UndefinedSymbolData[i].SymbolData->setIndex(Index++); 01105 } 01106 01107 void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm, 01108 MCAsmLayout &Layout, 01109 RelMapTy &RelMap) { 01110 for (MCAssembler::const_iterator it = Asm.begin(), 01111 ie = Asm.end(); it != ie; ++it) { 01112 const MCSectionData &SD = *it; 01113 if (Relocations[&SD].empty()) 01114 continue; 01115 01116 MCContext &Ctx = Asm.getContext(); 01117 const MCSectionELF &Section = 01118 static_cast<const MCSectionELF&>(SD.getSection()); 01119 01120 const StringRef SectionName = Section.getSectionName(); 01121 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; 01122 RelaSectionName += SectionName; 01123 01124 unsigned EntrySize; 01125 if (hasRelocationAddend()) 01126 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); 01127 else 01128 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); 01129 01130 unsigned Flags = 0; 01131 StringRef Group = ""; 01132 if (Section.getFlags() & ELF::SHF_GROUP) { 01133 Flags = ELF::SHF_GROUP; 01134 Group = Section.getGroup()->getName(); 01135 } 01136 01137 const MCSectionELF *RelaSection = 01138 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ? 01139 ELF::SHT_RELA : ELF::SHT_REL, Flags, 01140 SectionKind::getReadOnly(), 01141 EntrySize, Group); 01142 RelMap[&Section] = RelaSection; 01143 Asm.getOrCreateSectionData(*RelaSection); 01144 } 01145 } 01146 01147 static SmallVector<char, 128> 01148 getUncompressedData(MCAsmLayout &Layout, 01149 MCSectionData::FragmentListType &Fragments) { 01150 SmallVector<char, 128> UncompressedData; 01151 for (const MCFragment &F : Fragments) { 01152 const SmallVectorImpl<char> *Contents; 01153 switch (F.getKind()) { 01154 case MCFragment::FT_Data: 01155 Contents = &cast<MCDataFragment>(F).getContents(); 01156 break; 01157 case MCFragment::FT_Dwarf: 01158 Contents = &cast<MCDwarfLineAddrFragment>(F).getContents(); 01159 break; 01160 case MCFragment::FT_DwarfFrame: 01161 Contents = &cast<MCDwarfCallFrameFragment>(F).getContents(); 01162 break; 01163 default: 01164 llvm_unreachable( 01165 "Not expecting any other fragment types in a debug_* section"); 01166 } 01167 UncompressedData.append(Contents->begin(), Contents->end()); 01168 } 01169 return UncompressedData; 01170 } 01171 01172 // Include the debug info compression header: 01173 // "ZLIB" followed by 8 bytes representing the uncompressed size of the section, 01174 // useful for consumers to preallocate a buffer to decompress into. 01175 static bool 01176 prependCompressionHeader(uint64_t Size, 01177 SmallVectorImpl<char> &CompressedContents) { 01178 static const StringRef Magic = "ZLIB"; 01179 if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size()) 01180 return false; 01181 if (sys::IsLittleEndianHost) 01182 sys::swapByteOrder(Size); 01183 CompressedContents.insert(CompressedContents.begin(), 01184 Magic.size() + sizeof(Size), 0); 01185 std::copy(Magic.begin(), Magic.end(), CompressedContents.begin()); 01186 std::copy(reinterpret_cast<char *>(&Size), 01187 reinterpret_cast<char *>(&Size + 1), 01188 CompressedContents.begin() + Magic.size()); 01189 return true; 01190 } 01191 01192 // Return a single fragment containing the compressed contents of the whole 01193 // section. Null if the section was not compressed for any reason. 01194 static std::unique_ptr<MCDataFragment> 01195 getCompressedFragment(MCAsmLayout &Layout, 01196 MCSectionData::FragmentListType &Fragments) { 01197 std::unique_ptr<MCDataFragment> CompressedFragment(new MCDataFragment()); 01198 01199 // Gather the uncompressed data from all the fragments, recording the 01200 // alignment fragment, if seen, and any fixups. 01201 SmallVector<char, 128> UncompressedData = 01202 getUncompressedData(Layout, Fragments); 01203 01204 SmallVectorImpl<char> &CompressedContents = CompressedFragment->getContents(); 01205 01206 zlib::Status Success = zlib::compress( 01207 StringRef(UncompressedData.data(), UncompressedData.size()), 01208 CompressedContents); 01209 if (Success != zlib::StatusOK) 01210 return nullptr; 01211 01212 if (!prependCompressionHeader(UncompressedData.size(), CompressedContents)) 01213 return nullptr; 01214 01215 return CompressedFragment; 01216 } 01217 01218 typedef DenseMap<const MCSectionData *, std::vector<MCSymbolData *>> 01219 DefiningSymbolMap; 01220 01221 static void UpdateSymbols(const MCAsmLayout &Layout, 01222 const std::vector<MCSymbolData *> &Symbols, 01223 MCFragment &NewFragment) { 01224 for (MCSymbolData *Sym : Symbols) { 01225 Sym->setOffset(Sym->getOffset() + 01226 Layout.getFragmentOffset(Sym->getFragment())); 01227 Sym->setFragment(&NewFragment); 01228 } 01229 } 01230 01231 static void CompressDebugSection(MCAssembler &Asm, MCAsmLayout &Layout, 01232 const DefiningSymbolMap &DefiningSymbols, 01233 const MCSectionELF &Section, 01234 MCSectionData &SD) { 01235 StringRef SectionName = Section.getSectionName(); 01236 MCSectionData::FragmentListType &Fragments = SD.getFragmentList(); 01237 01238 std::unique_ptr<MCDataFragment> CompressedFragment = 01239 getCompressedFragment(Layout, Fragments); 01240 01241 // Leave the section as-is if the fragments could not be compressed. 01242 if (!CompressedFragment) 01243 return; 01244 01245 // Update the fragment+offsets of any symbols referring to fragments in this 01246 // section to refer to the new fragment. 01247 auto I = DefiningSymbols.find(&SD); 01248 if (I != DefiningSymbols.end()) 01249 UpdateSymbols(Layout, I->second, *CompressedFragment); 01250 01251 // Invalidate the layout for the whole section since it will have new and 01252 // different fragments now. 01253 Layout.invalidateFragmentsFrom(&Fragments.front()); 01254 Fragments.clear(); 01255 01256 // Complete the initialization of the new fragment 01257 CompressedFragment->setParent(&SD); 01258 CompressedFragment->setLayoutOrder(0); 01259 Fragments.push_back(CompressedFragment.release()); 01260 01261 // Rename from .debug_* to .zdebug_* 01262 Asm.getContext().renameELFSection(&Section, 01263 (".z" + SectionName.drop_front(1)).str()); 01264 } 01265 01266 void ELFObjectWriter::CompressDebugSections(MCAssembler &Asm, 01267 MCAsmLayout &Layout) { 01268 if (!Asm.getContext().getAsmInfo()->compressDebugSections()) 01269 return; 01270 01271 DefiningSymbolMap DefiningSymbols; 01272 01273 for (MCSymbolData &SD : Asm.symbols()) 01274 if (MCFragment *F = SD.getFragment()) 01275 DefiningSymbols[F->getParent()].push_back(&SD); 01276 01277 for (MCSectionData &SD : Asm) { 01278 const MCSectionELF &Section = 01279 static_cast<const MCSectionELF &>(SD.getSection()); 01280 StringRef SectionName = Section.getSectionName(); 01281 01282 // Compressing debug_frame requires handling alignment fragments which is 01283 // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow 01284 // for writing to arbitrary buffers) for little benefit. 01285 if (!SectionName.startswith(".debug_") || SectionName == ".debug_frame") 01286 continue; 01287 01288 CompressDebugSection(Asm, Layout, DefiningSymbols, Section, SD); 01289 } 01290 } 01291 01292 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout, 01293 const RelMapTy &RelMap) { 01294 for (MCAssembler::const_iterator it = Asm.begin(), 01295 ie = Asm.end(); it != ie; ++it) { 01296 const MCSectionData &SD = *it; 01297 const MCSectionELF &Section = 01298 static_cast<const MCSectionELF&>(SD.getSection()); 01299 01300 const MCSectionELF *RelaSection = RelMap.lookup(&Section); 01301 if (!RelaSection) 01302 continue; 01303 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection); 01304 RelaSD.setAlignment(is64Bit() ? 8 : 4); 01305 01306 MCDataFragment *F = new MCDataFragment(&RelaSD); 01307 WriteRelocationsFragment(Asm, F, &*it); 01308 } 01309 } 01310 01311 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, 01312 uint64_t Flags, uint64_t Address, 01313 uint64_t Offset, uint64_t Size, 01314 uint32_t Link, uint32_t Info, 01315 uint64_t Alignment, 01316 uint64_t EntrySize) { 01317 Write32(Name); // sh_name: index into string table 01318 Write32(Type); // sh_type 01319 WriteWord(Flags); // sh_flags 01320 WriteWord(Address); // sh_addr 01321 WriteWord(Offset); // sh_offset 01322 WriteWord(Size); // sh_size 01323 Write32(Link); // sh_link 01324 Write32(Info); // sh_info 01325 WriteWord(Alignment); // sh_addralign 01326 WriteWord(EntrySize); // sh_entsize 01327 } 01328 01329 // ELF doesn't require relocations to be in any order. We sort by the r_offset, 01330 // just to match gnu as for easier comparison. The use type is an arbitrary way 01331 // of making the sort deterministic. 01332 static int cmpRel(const ELFRelocationEntry *AP, const ELFRelocationEntry *BP) { 01333 const ELFRelocationEntry &A = *AP; 01334 const ELFRelocationEntry &B = *BP; 01335 if (A.Offset != B.Offset) 01336 return B.Offset - A.Offset; 01337 if (B.Type != A.Type) 01338 return A.Type - B.Type; 01339 llvm_unreachable("ELFRelocs might be unstable!"); 01340 } 01341 01342 static void sortRelocs(const MCAssembler &Asm, 01343 std::vector<ELFRelocationEntry> &Relocs) { 01344 array_pod_sort(Relocs.begin(), Relocs.end(), cmpRel); 01345 } 01346 01347 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm, 01348 MCDataFragment *F, 01349 const MCSectionData *SD) { 01350 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD]; 01351 01352 sortRelocs(Asm, Relocs); 01353 01354 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 01355 const ELFRelocationEntry &Entry = Relocs[e - i - 1]; 01356 01357 unsigned Index; 01358 if (Entry.UseSymbol) { 01359 Index = getSymbolIndexInSymbolTable(Asm, Entry.Symbol); 01360 } else { 01361 const MCSectionData *Sec = Entry.Section; 01362 if (Sec) 01363 Index = Sec->getOrdinal() + FileSymbolData.size() + 01364 LocalSymbolData.size() + 1; 01365 else 01366 Index = 0; 01367 } 01368 01369 if (is64Bit()) { 01370 write(*F, Entry.Offset); 01371 if (TargetObjectWriter->isN64()) { 01372 write(*F, uint32_t(Index)); 01373 01374 write(*F, TargetObjectWriter->getRSsym(Entry.Type)); 01375 write(*F, TargetObjectWriter->getRType3(Entry.Type)); 01376 write(*F, TargetObjectWriter->getRType2(Entry.Type)); 01377 write(*F, TargetObjectWriter->getRType(Entry.Type)); 01378 } else { 01379 struct ELF::Elf64_Rela ERE64; 01380 ERE64.setSymbolAndType(Index, Entry.Type); 01381 write(*F, ERE64.r_info); 01382 } 01383 if (hasRelocationAddend()) 01384 write(*F, Entry.Addend); 01385 } else { 01386 write(*F, uint32_t(Entry.Offset)); 01387 01388 struct ELF::Elf32_Rela ERE32; 01389 ERE32.setSymbolAndType(Index, Entry.Type); 01390 write(*F, ERE32.r_info); 01391 01392 if (hasRelocationAddend()) 01393 write(*F, uint32_t(Entry.Addend)); 01394 } 01395 } 01396 } 01397 01398 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, 01399 MCAsmLayout &Layout, 01400 SectionIndexMapTy &SectionIndexMap, 01401 const RelMapTy &RelMap) { 01402 MCContext &Ctx = Asm.getContext(); 01403 MCDataFragment *F; 01404 01405 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; 01406 01407 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as. 01408 const MCSectionELF *ShstrtabSection = 01409 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, 01410 SectionKind::getReadOnly()); 01411 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); 01412 ShstrtabSD.setAlignment(1); 01413 01414 const MCSectionELF *SymtabSection = 01415 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, 01416 SectionKind::getReadOnly(), 01417 EntrySize, ""); 01418 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection); 01419 SymtabSD.setAlignment(is64Bit() ? 8 : 4); 01420 01421 const MCSectionELF *StrtabSection; 01422 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0, 01423 SectionKind::getReadOnly()); 01424 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection); 01425 StrtabSD.setAlignment(1); 01426 01427 ComputeIndexMap(Asm, SectionIndexMap, RelMap); 01428 01429 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection); 01430 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection); 01431 StringTableIndex = SectionIndexMap.lookup(StrtabSection); 01432 01433 // Symbol table 01434 F = new MCDataFragment(&SymtabSD); 01435 WriteSymbolTable(F, Asm, Layout, SectionIndexMap); 01436 01437 F = new MCDataFragment(&StrtabSD); 01438 F->getContents().append(StrTabBuilder.data().begin(), 01439 StrTabBuilder.data().end()); 01440 01441 F = new MCDataFragment(&ShstrtabSD); 01442 01443 // Section header string table. 01444 for (auto it = Asm.begin(), ie = Asm.end(); it != ie; ++it) { 01445 const MCSectionELF &Section = 01446 static_cast<const MCSectionELF&>(it->getSection()); 01447 ShStrTabBuilder.add(Section.getSectionName()); 01448 } 01449 ShStrTabBuilder.finalize(); 01450 F->getContents().append(ShStrTabBuilder.data().begin(), 01451 ShStrTabBuilder.data().end()); 01452 } 01453 01454 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm, 01455 MCAsmLayout &Layout, 01456 GroupMapTy &GroupMap, 01457 RevGroupMapTy &RevGroupMap, 01458 SectionIndexMapTy &SectionIndexMap, 01459 const RelMapTy &RelMap) { 01460 // Create the .note.GNU-stack section if needed. 01461 MCContext &Ctx = Asm.getContext(); 01462 if (Asm.getNoExecStack()) { 01463 const MCSectionELF *GnuStackSection = 01464 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0, 01465 SectionKind::getReadOnly()); 01466 Asm.getOrCreateSectionData(*GnuStackSection); 01467 } 01468 01469 // Build the groups 01470 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); 01471 it != ie; ++it) { 01472 const MCSectionELF &Section = 01473 static_cast<const MCSectionELF&>(it->getSection()); 01474 if (!(Section.getFlags() & ELF::SHF_GROUP)) 01475 continue; 01476 01477 const MCSymbol *SignatureSymbol = Section.getGroup(); 01478 Asm.getOrCreateSymbolData(*SignatureSymbol); 01479 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol]; 01480 if (!Group) { 01481 Group = Ctx.CreateELFGroupSection(); 01482 MCSectionData &Data = Asm.getOrCreateSectionData(*Group); 01483 Data.setAlignment(4); 01484 MCDataFragment *F = new MCDataFragment(&Data); 01485 write(*F, uint32_t(ELF::GRP_COMDAT)); 01486 } 01487 GroupMap[Group] = SignatureSymbol; 01488 } 01489 01490 ComputeIndexMap(Asm, SectionIndexMap, RelMap); 01491 01492 // Add sections to the groups 01493 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); 01494 it != ie; ++it) { 01495 const MCSectionELF &Section = 01496 static_cast<const MCSectionELF&>(it->getSection()); 01497 if (!(Section.getFlags() & ELF::SHF_GROUP)) 01498 continue; 01499 const MCSectionELF *Group = RevGroupMap[Section.getGroup()]; 01500 MCSectionData &Data = Asm.getOrCreateSectionData(*Group); 01501 // FIXME: we could use the previous fragment 01502 MCDataFragment *F = new MCDataFragment(&Data); 01503 uint32_t Index = SectionIndexMap.lookup(&Section); 01504 write(*F, Index); 01505 } 01506 } 01507 01508 void ELFObjectWriter::WriteSection(MCAssembler &Asm, 01509 const SectionIndexMapTy &SectionIndexMap, 01510 uint32_t GroupSymbolIndex, 01511 uint64_t Offset, uint64_t Size, 01512 uint64_t Alignment, 01513 const MCSectionELF &Section) { 01514 uint64_t sh_link = 0; 01515 uint64_t sh_info = 0; 01516 01517 switch(Section.getType()) { 01518 case ELF::SHT_DYNAMIC: 01519 sh_link = ShStrTabBuilder.getOffset(Section.getSectionName()); 01520 sh_info = 0; 01521 break; 01522 01523 case ELF::SHT_REL: 01524 case ELF::SHT_RELA: { 01525 const MCSectionELF *SymtabSection; 01526 const MCSectionELF *InfoSection; 01527 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 01528 0, 01529 SectionKind::getReadOnly()); 01530 sh_link = SectionIndexMap.lookup(SymtabSection); 01531 assert(sh_link && ".symtab not found"); 01532 01533 // Remove ".rel" and ".rela" prefixes. 01534 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5; 01535 StringRef SectionName = Section.getSectionName().substr(SecNameLen); 01536 StringRef GroupName = 01537 Section.getGroup() ? Section.getGroup()->getName() : ""; 01538 01539 InfoSection = Asm.getContext().getELFSection(SectionName, ELF::SHT_PROGBITS, 01540 0, SectionKind::getReadOnly(), 01541 0, GroupName); 01542 sh_info = SectionIndexMap.lookup(InfoSection); 01543 break; 01544 } 01545 01546 case ELF::SHT_SYMTAB: 01547 case ELF::SHT_DYNSYM: 01548 sh_link = StringTableIndex; 01549 sh_info = LastLocalSymbolIndex; 01550 break; 01551 01552 case ELF::SHT_SYMTAB_SHNDX: 01553 sh_link = SymbolTableIndex; 01554 break; 01555 01556 case ELF::SHT_PROGBITS: 01557 case ELF::SHT_STRTAB: 01558 case ELF::SHT_NOBITS: 01559 case ELF::SHT_NOTE: 01560 case ELF::SHT_NULL: 01561 case ELF::SHT_ARM_ATTRIBUTES: 01562 case ELF::SHT_INIT_ARRAY: 01563 case ELF::SHT_FINI_ARRAY: 01564 case ELF::SHT_PREINIT_ARRAY: 01565 case ELF::SHT_X86_64_UNWIND: 01566 case ELF::SHT_MIPS_REGINFO: 01567 case ELF::SHT_MIPS_OPTIONS: 01568 case ELF::SHT_MIPS_ABIFLAGS: 01569 // Nothing to do. 01570 break; 01571 01572 case ELF::SHT_GROUP: 01573 sh_link = SymbolTableIndex; 01574 sh_info = GroupSymbolIndex; 01575 break; 01576 01577 default: 01578 llvm_unreachable("FIXME: sh_type value not supported!"); 01579 } 01580 01581 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM && 01582 Section.getType() == ELF::SHT_ARM_EXIDX) { 01583 StringRef SecName(Section.getSectionName()); 01584 if (SecName == ".ARM.exidx") { 01585 sh_link = SectionIndexMap.lookup( 01586 Asm.getContext().getELFSection(".text", 01587 ELF::SHT_PROGBITS, 01588 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, 01589 SectionKind::getText())); 01590 } else if (SecName.startswith(".ARM.exidx")) { 01591 StringRef GroupName = 01592 Section.getGroup() ? Section.getGroup()->getName() : ""; 01593 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection( 01594 SecName.substr(sizeof(".ARM.exidx") - 1), ELF::SHT_PROGBITS, 01595 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, SectionKind::getText(), 0, 01596 GroupName)); 01597 } 01598 } 01599 01600 WriteSecHdrEntry(ShStrTabBuilder.getOffset(Section.getSectionName()), 01601 Section.getType(), 01602 Section.getFlags(), 0, Offset, Size, sh_link, sh_info, 01603 Alignment, Section.getEntrySize()); 01604 } 01605 01606 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) { 01607 return SD.getOrdinal() == ~UINT32_C(0) && 01608 !SD.getSection().isVirtualSection(); 01609 } 01610 01611 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) { 01612 uint64_t Ret = 0; 01613 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e; 01614 ++i) { 01615 const MCFragment &F = *i; 01616 assert(F.getKind() == MCFragment::FT_Data); 01617 Ret += cast<MCDataFragment>(F).getContents().size(); 01618 } 01619 return Ret; 01620 } 01621 01622 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout, 01623 const MCSectionData &SD) { 01624 if (IsELFMetaDataSection(SD)) 01625 return DataSectionSize(SD); 01626 return Layout.getSectionFileSize(&SD); 01627 } 01628 01629 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout, 01630 const MCSectionData &SD) { 01631 if (IsELFMetaDataSection(SD)) 01632 return DataSectionSize(SD); 01633 return Layout.getSectionAddressSize(&SD); 01634 } 01635 01636 void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm, 01637 const MCAsmLayout &Layout, 01638 const MCSectionELF &Section) { 01639 const MCSectionData &SD = Asm.getOrCreateSectionData(Section); 01640 01641 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment()); 01642 WriteZeros(Padding); 01643 01644 if (IsELFMetaDataSection(SD)) { 01645 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e; 01646 ++i) { 01647 const MCFragment &F = *i; 01648 assert(F.getKind() == MCFragment::FT_Data); 01649 WriteBytes(cast<MCDataFragment>(F).getContents()); 01650 } 01651 } else { 01652 Asm.writeSectionData(&SD, Layout); 01653 } 01654 } 01655 01656 void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm, 01657 const GroupMapTy &GroupMap, 01658 const MCAsmLayout &Layout, 01659 const SectionIndexMapTy &SectionIndexMap, 01660 const SectionOffsetMapTy &SectionOffsetMap) { 01661 const unsigned NumSections = Asm.size() + 1; 01662 01663 std::vector<const MCSectionELF*> Sections; 01664 Sections.resize(NumSections - 1); 01665 01666 for (SectionIndexMapTy::const_iterator i= 01667 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) { 01668 const std::pair<const MCSectionELF*, uint32_t> &p = *i; 01669 Sections[p.second - 1] = p.first; 01670 } 01671 01672 // Null section first. 01673 uint64_t FirstSectionSize = 01674 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0; 01675 uint32_t FirstSectionLink = 01676 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0; 01677 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0); 01678 01679 for (unsigned i = 0; i < NumSections - 1; ++i) { 01680 const MCSectionELF &Section = *Sections[i]; 01681 const MCSectionData &SD = Asm.getOrCreateSectionData(Section); 01682 uint32_t GroupSymbolIndex; 01683 if (Section.getType() != ELF::SHT_GROUP) 01684 GroupSymbolIndex = 0; 01685 else 01686 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, 01687 GroupMap.lookup(&Section)); 01688 01689 uint64_t Size = GetSectionAddressSize(Layout, SD); 01690 01691 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex, 01692 SectionOffsetMap.lookup(&Section), Size, 01693 SD.getAlignment(), Section); 01694 } 01695 } 01696 01697 void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm, 01698 std::vector<const MCSectionELF*> &Sections) { 01699 for (MCAssembler::iterator it = Asm.begin(), 01700 ie = Asm.end(); it != ie; ++it) { 01701 const MCSectionELF &Section = 01702 static_cast<const MCSectionELF &>(it->getSection()); 01703 if (Section.getType() == ELF::SHT_GROUP) 01704 Sections.push_back(&Section); 01705 } 01706 01707 for (MCAssembler::iterator it = Asm.begin(), 01708 ie = Asm.end(); it != ie; ++it) { 01709 const MCSectionELF &Section = 01710 static_cast<const MCSectionELF &>(it->getSection()); 01711 if (Section.getType() != ELF::SHT_GROUP && 01712 Section.getType() != ELF::SHT_REL && 01713 Section.getType() != ELF::SHT_RELA) 01714 Sections.push_back(&Section); 01715 } 01716 01717 for (MCAssembler::iterator it = Asm.begin(), 01718 ie = Asm.end(); it != ie; ++it) { 01719 const MCSectionELF &Section = 01720 static_cast<const MCSectionELF &>(it->getSection()); 01721 if (Section.getType() == ELF::SHT_REL || 01722 Section.getType() == ELF::SHT_RELA) 01723 Sections.push_back(&Section); 01724 } 01725 } 01726 01727 void ELFObjectWriter::WriteObject(MCAssembler &Asm, 01728 const MCAsmLayout &Layout) { 01729 GroupMapTy GroupMap; 01730 RevGroupMapTy RevGroupMap; 01731 SectionIndexMapTy SectionIndexMap; 01732 01733 unsigned NumUserSections = Asm.size(); 01734 01735 CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout)); 01736 01737 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap; 01738 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap); 01739 01740 const unsigned NumUserAndRelocSections = Asm.size(); 01741 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap, 01742 RevGroupMap, SectionIndexMap, RelMap); 01743 const unsigned AllSections = Asm.size(); 01744 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections; 01745 01746 unsigned NumRegularSections = NumUserSections + NumIndexedSections; 01747 01748 // Compute symbol table information. 01749 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, 01750 NumRegularSections); 01751 01752 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap); 01753 01754 CreateMetadataSections(const_cast<MCAssembler&>(Asm), 01755 const_cast<MCAsmLayout&>(Layout), 01756 SectionIndexMap, 01757 RelMap); 01758 01759 uint64_t NaturalAlignment = is64Bit() ? 8 : 4; 01760 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) : 01761 sizeof(ELF::Elf32_Ehdr); 01762 uint64_t FileOff = HeaderSize; 01763 01764 std::vector<const MCSectionELF*> Sections; 01765 ComputeSectionOrder(Asm, Sections); 01766 unsigned NumSections = Sections.size(); 01767 SectionOffsetMapTy SectionOffsetMap; 01768 for (unsigned i = 0; i < NumRegularSections + 1; ++i) { 01769 const MCSectionELF &Section = *Sections[i]; 01770 const MCSectionData &SD = Asm.getOrCreateSectionData(Section); 01771 01772 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); 01773 01774 // Remember the offset into the file for this section. 01775 SectionOffsetMap[&Section] = FileOff; 01776 01777 // Get the size of the section in the output file (including padding). 01778 FileOff += GetSectionFileSize(Layout, SD); 01779 } 01780 01781 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment); 01782 01783 const unsigned SectionHeaderOffset = FileOff - HeaderSize; 01784 01785 uint64_t SectionHeaderEntrySize = is64Bit() ? 01786 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr); 01787 FileOff += (NumSections + 1) * SectionHeaderEntrySize; 01788 01789 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) { 01790 const MCSectionELF &Section = *Sections[i]; 01791 const MCSectionData &SD = Asm.getOrCreateSectionData(Section); 01792 01793 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); 01794 01795 // Remember the offset into the file for this section. 01796 SectionOffsetMap[&Section] = FileOff; 01797 01798 // Get the size of the section in the output file (including padding). 01799 FileOff += GetSectionFileSize(Layout, SD); 01800 } 01801 01802 // Write out the ELF header ... 01803 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1); 01804 01805 // ... then the regular sections ... 01806 // + because of .shstrtab 01807 for (unsigned i = 0; i < NumRegularSections + 1; ++i) 01808 WriteDataSectionData(Asm, Layout, *Sections[i]); 01809 01810 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment); 01811 WriteZeros(Padding); 01812 01813 // ... then the section header table ... 01814 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, 01815 SectionOffsetMap); 01816 01817 // ... and then the remaining sections ... 01818 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) 01819 WriteDataSectionData(Asm, Layout, *Sections[i]); 01820 } 01821 01822 bool 01823 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, 01824 const MCSymbolData &DataA, 01825 const MCFragment &FB, 01826 bool InSet, 01827 bool IsPCRel) const { 01828 if (DataA.getFlags() & ELF_STB_Weak || MCELF::GetType(DataA) == ELF::STT_GNU_IFUNC) 01829 return false; 01830 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl( 01831 Asm, DataA, FB,InSet, IsPCRel); 01832 } 01833 01834 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW, 01835 raw_ostream &OS, 01836 bool IsLittleEndian) { 01837 return new ELFObjectWriter(MOTW, OS, IsLittleEndian); 01838 }