diff options
Diffstat (limited to 'lib/MC')
34 files changed, 625 insertions, 625 deletions
diff --git a/lib/MC/CMakeLists.txt b/lib/MC/CMakeLists.txt index 7181bdc..ddddd49 100644 --- a/lib/MC/CMakeLists.txt +++ b/lib/MC/CMakeLists.txt @@ -46,6 +46,9 @@ add_llvm_library(LLVMMC WinCOFFObjectWriter.cpp WinCOFFStreamer.cpp YAML.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/MC ) add_subdirectory(MCParser) diff --git a/lib/MC/ConstantPools.cpp b/lib/MC/ConstantPools.cpp index c4cea60..a239a8f 100644 --- a/lib/MC/ConstantPools.cpp +++ b/lib/MC/ConstantPools.cpp @@ -11,10 +11,10 @@ // //===----------------------------------------------------------------------===// #include "llvm/ADT/MapVector.h" +#include "llvm/MC/ConstantPools.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCStreamer.h" -#include "llvm/MC/ConstantPools.h" using namespace llvm; // diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index e4442e1..4819905 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -31,8 +31,8 @@ #include "llvm/MC/StringTableBuilder.h" #include "llvm/Support/Compression.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/Endian.h" #include "llvm/Support/ELF.h" +#include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" #include <vector> using namespace llvm; @@ -219,7 +219,7 @@ class ELFObjectWriter : public MCObjectWriter { const MCSymbolData *SD, uint64_t C, unsigned Type) const; - void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, + void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) override; @@ -247,12 +247,12 @@ class ELFObjectWriter : public MCObjectWriter { const RevGroupMapTy &RevGroupMap, unsigned NumRegularSections); - void ComputeIndexMap(MCAssembler &Asm, + void computeIndexMap(MCAssembler &Asm, SectionIndexMapTy &SectionIndexMap, - const RelMapTy &RelMap); + RelMapTy &RelMap); - void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout, - RelMapTy &RelMap); + MCSectionData *createRelocationSection(MCAssembler &Asm, + const MCSectionData &SD); void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout); @@ -260,23 +260,23 @@ class ELFObjectWriter : public MCObjectWriter { const RelMapTy &RelMap); void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout, - SectionIndexMapTy &SectionIndexMap, - const RelMapTy &RelMap); + SectionIndexMapTy &SectionIndexMap); // Create the sections that show up in the symbol table. Currently // those are the .note.GNU-stack section and the group sections. - void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout, + void createIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout, GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap, SectionIndexMapTy &SectionIndexMap, - const RelMapTy &RelMap); + RelMapTy &RelMap); void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout) override; - void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap, + void writeSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap, const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap, + const RelMapTy &RelMap, const SectionOffsetMapTy &SectionOffsetMap); void ComputeSectionOrder(MCAssembler &Asm, @@ -299,8 +299,9 @@ class ELFObjectWriter : public MCObjectWriter { bool IsPCRel) const override; void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; - void WriteSection(MCAssembler &Asm, + void writeSection(MCAssembler &Asm, const SectionIndexMapTy &SectionIndexMap, + const RelMapTy &RelMap, uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size, uint64_t Alignment, const MCSectionELF &Section); @@ -325,8 +326,7 @@ void SymbolTableWriter::createSymtabShndx() { MCContext &Ctx = Asm.getContext(); const MCSectionELF *SymtabShndxSection = - Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, - SectionKind::getReadOnly(), 4, ""); + Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, ""); MCSectionData *SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection); SymtabShndxSD->setAlignment(4); @@ -789,13 +789,11 @@ static const MCSymbol *getWeakRef(const MCSymbolRefExpr &Ref) { return nullptr; } -void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, +void ELFObjectWriter::RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, - const MCFixup &Fixup, - MCValue Target, - bool &IsPCRel, - uint64_t &FixedValue) { + const MCFixup &Fixup, MCValue Target, + bool &IsPCRel, uint64_t &FixedValue) { const MCSectionData *FixupSection = Fragment->getParent(); uint64_t C = Target.getConstant(); uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); @@ -946,9 +944,9 @@ bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isUsedInReloc) { return true; } -void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm, +void ELFObjectWriter::computeIndexMap(MCAssembler &Asm, SectionIndexMapTy &SectionIndexMap, - const RelMapTy &RelMap) { + RelMapTy &RelMap) { unsigned Index = 1; for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) { @@ -961,16 +959,20 @@ void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm, for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) { + const MCSectionData &SD = *it; const MCSectionELF &Section = - static_cast<const MCSectionELF &>(it->getSection()); + static_cast<const MCSectionELF &>(SD.getSection()); if (Section.getType() == ELF::SHT_GROUP || Section.getType() == ELF::SHT_REL || Section.getType() == ELF::SHT_RELA) continue; SectionIndexMap[&Section] = Index++; - const MCSectionELF *RelSection = RelMap.lookup(&Section); - if (RelSection) + if (MCSectionData *RelSD = createRelocationSection(Asm, SD)) { + const MCSectionELF *RelSection = + static_cast<const MCSectionELF *>(&RelSD->getSection()); + RelMap[RelSection] = &Section; SectionIndexMap[RelSection] = Index++; + } } } @@ -1035,16 +1037,43 @@ ELFObjectWriter::computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout, assert(MSD.SectionIndex && "Invalid section index!"); } - // The @@@ in symbol version is replaced with @ in undefined symbols and - // @@ in defined ones. + // The @@@ in symbol version is replaced with @ in undefined symbols and @@ + // in defined ones. + // + // FIXME: All name handling should be done before we get to the writer, + // including dealing with GNU-style version suffixes. Fixing this isn't + // trivial. + // + // We thus have to be careful to not perform the symbol version replacement + // blindly: + // + // The ELF format is used on Windows by the MCJIT engine. Thus, on + // Windows, the ELFObjectWriter can encounter symbols mangled using the MS + // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC + // C++ name mangling can legally have "@@@" as a sub-string. In that case, + // the EFLObjectWriter should not interpret the "@@@" sub-string as + // specifying GNU-style symbol versioning. The ELFObjectWriter therefore + // checks for the MSVC C++ name mangling prefix which is either "?", "@?", + // "__imp_?" or "__imp_@?". + // + // It would have been interesting to perform the MS mangling prefix check + // only when the target triple is of the form *-pc-windows-elf. But, it + // seems that this information is not easily accessible from the + // ELFObjectWriter. StringRef Name = Symbol.getName(); - SmallString<32> Buf; - size_t Pos = Name.find("@@@"); - if (Pos != StringRef::npos) { - Buf += Name.substr(0, Pos); - unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1; - Buf += Name.substr(Pos + Skip); - Name = Buf; + if (!Name.startswith("?") && !Name.startswith("@?") && + !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) { + // This symbol isn't following the MSVC C++ name mangling convention. We + // can thus safely interpret the @@@ in symbol names as specifying symbol + // versioning. + SmallString<32> Buf; + size_t Pos = Name.find("@@@"); + if (Pos != StringRef::npos) { + Buf += Name.substr(0, Pos); + unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1; + Buf += Name.substr(Pos + Skip); + Name = Buf; + } } // Sections have their own string table @@ -1093,44 +1122,37 @@ ELFObjectWriter::computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout, UndefinedSymbolData[i].SymbolData->setIndex(Index++); } -void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm, - MCAsmLayout &Layout, - RelMapTy &RelMap) { - for (MCAssembler::const_iterator it = Asm.begin(), - ie = Asm.end(); it != ie; ++it) { - const MCSectionData &SD = *it; - if (Relocations[&SD].empty()) - continue; - - MCContext &Ctx = Asm.getContext(); - const MCSectionELF &Section = - static_cast<const MCSectionELF&>(SD.getSection()); +MCSectionData * +ELFObjectWriter::createRelocationSection(MCAssembler &Asm, + const MCSectionData &SD) { + if (Relocations[&SD].empty()) + return nullptr; - const StringRef SectionName = Section.getSectionName(); - std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; - RelaSectionName += SectionName; + MCContext &Ctx = Asm.getContext(); + const MCSectionELF &Section = + static_cast<const MCSectionELF &>(SD.getSection()); - unsigned EntrySize; - if (hasRelocationAddend()) - EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); - else - EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); + const StringRef SectionName = Section.getSectionName(); + std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; + RelaSectionName += SectionName; - unsigned Flags = 0; - StringRef Group = ""; - if (Section.getFlags() & ELF::SHF_GROUP) { - Flags = ELF::SHF_GROUP; - Group = Section.getGroup()->getName(); - } + unsigned EntrySize; + if (hasRelocationAddend()) + EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); + else + EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); - const MCSectionELF *RelaSection = - Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ? - ELF::SHT_RELA : ELF::SHT_REL, Flags, - SectionKind::getReadOnly(), - EntrySize, Group); - RelMap[&Section] = RelaSection; - Asm.getOrCreateSectionData(*RelaSection); + unsigned Flags = 0; + StringRef Group = ""; + if (Section.getFlags() & ELF::SHF_GROUP) { + Flags = ELF::SHF_GROUP; + Group = Section.getGroup()->getName(); } + + const MCSectionELF *RelaSection = Ctx.getELFSection( + RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL, + Flags, EntrySize, Group, true); + return &Asm.getOrCreateSectionData(*RelaSection); } static SmallVector<char, 128> @@ -1280,20 +1302,21 @@ void ELFObjectWriter::CompressDebugSections(MCAssembler &Asm, void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout, const RelMapTy &RelMap) { - for (MCAssembler::const_iterator it = Asm.begin(), - ie = Asm.end(); it != ie; ++it) { - const MCSectionData &SD = *it; - const MCSectionELF &Section = - static_cast<const MCSectionELF&>(SD.getSection()); + for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) { + MCSectionData &RelSD = *it; + const MCSectionELF &RelSection = + static_cast<const MCSectionELF &>(RelSD.getSection()); - const MCSectionELF *RelaSection = RelMap.lookup(&Section); - if (!RelaSection) + unsigned Type = RelSection.getType(); + if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) continue; - MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection); - RelaSD.setAlignment(is64Bit() ? 8 : 4); - MCDataFragment *F = new MCDataFragment(&RelaSD); - WriteRelocationsFragment(Asm, F, &*it); + const MCSectionELF *Section = RelMap.lookup(&RelSection); + MCSectionData &SD = Asm.getOrCreateSectionData(*Section); + RelSD.setAlignment(is64Bit() ? 8 : 4); + + MCDataFragment *F = new MCDataFragment(&RelSD); + WriteRelocationsFragment(Asm, F, &SD); } } @@ -1374,10 +1397,8 @@ void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm, } } -void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, - MCAsmLayout &Layout, - SectionIndexMapTy &SectionIndexMap, - const RelMapTy &RelMap) { +void ELFObjectWriter::CreateMetadataSections( + MCAssembler &Asm, MCAsmLayout &Layout, SectionIndexMapTy &SectionIndexMap) { MCContext &Ctx = Asm.getContext(); MCDataFragment *F; @@ -1385,29 +1406,26 @@ void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, // We construct .shstrtab, .symtab and .strtab in this order to match gnu as. const MCSectionELF *ShstrtabSection = - Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, - SectionKind::getReadOnly()); + Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0); MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); ShstrtabSD.setAlignment(1); + ShstrtabIndex = SectionIndexMap.size() + 1; + SectionIndexMap[ShstrtabSection] = ShstrtabIndex; const MCSectionELF *SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, - SectionKind::getReadOnly(), EntrySize, ""); MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection); SymtabSD.setAlignment(is64Bit() ? 8 : 4); + SymbolTableIndex = SectionIndexMap.size() + 1; + SectionIndexMap[SymtabSection] = SymbolTableIndex; const MCSectionELF *StrtabSection; - StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0, - SectionKind::getReadOnly()); + StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0); MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection); StrtabSD.setAlignment(1); - - ComputeIndexMap(Asm, SectionIndexMap, RelMap); - - ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection); - SymbolTableIndex = SectionIndexMap.lookup(SymtabSection); - StringTableIndex = SectionIndexMap.lookup(StrtabSection); + StringTableIndex = SectionIndexMap.size() + 1; + SectionIndexMap[StrtabSection] = StringTableIndex; // Symbol table F = new MCDataFragment(&SymtabSD); @@ -1430,12 +1448,12 @@ void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, ShStrTabBuilder.data().end()); } -void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm, +void ELFObjectWriter::createIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout, GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap, SectionIndexMapTy &SectionIndexMap, - const RelMapTy &RelMap) { + RelMapTy &RelMap) { MCContext &Ctx = Asm.getContext(); // Build the groups @@ -1459,7 +1477,7 @@ void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm, GroupMap[Group] = SignatureSymbol; } - ComputeIndexMap(Asm, SectionIndexMap, RelMap); + computeIndexMap(Asm, SectionIndexMap, RelMap); // Add sections to the groups for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); @@ -1477,8 +1495,9 @@ void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm, } } -void ELFObjectWriter::WriteSection(MCAssembler &Asm, +void ELFObjectWriter::writeSection(MCAssembler &Asm, const SectionIndexMapTy &SectionIndexMap, + const RelMapTy &RelMap, uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size, uint64_t Alignment, @@ -1494,23 +1513,9 @@ void ELFObjectWriter::WriteSection(MCAssembler &Asm, case ELF::SHT_REL: case ELF::SHT_RELA: { - const MCSectionELF *SymtabSection; - const MCSectionELF *InfoSection; - SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, - 0, - SectionKind::getReadOnly()); - sh_link = SectionIndexMap.lookup(SymtabSection); + sh_link = SymbolTableIndex; assert(sh_link && ".symtab not found"); - - // Remove ".rel" and ".rela" prefixes. - unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5; - StringRef SectionName = Section.getSectionName().substr(SecNameLen); - StringRef GroupName = - Section.getGroup() ? Section.getGroup()->getName() : ""; - - InfoSection = Asm.getContext().getELFSection(SectionName, ELF::SHT_PROGBITS, - 0, SectionKind::getReadOnly(), - 0, GroupName); + const MCSectionELF *InfoSection = RelMap.find(&Section)->second; sh_info = SectionIndexMap.lookup(InfoSection); break; } @@ -1554,18 +1559,14 @@ void ELFObjectWriter::WriteSection(MCAssembler &Asm, Section.getType() == ELF::SHT_ARM_EXIDX) { StringRef SecName(Section.getSectionName()); if (SecName == ".ARM.exidx") { - sh_link = SectionIndexMap.lookup( - Asm.getContext().getELFSection(".text", - ELF::SHT_PROGBITS, - ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, - SectionKind::getText())); + sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection( + ".text", ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC)); } else if (SecName.startswith(".ARM.exidx")) { StringRef GroupName = Section.getGroup() ? Section.getGroup()->getName() : ""; sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection( SecName.substr(sizeof(".ARM.exidx") - 1), ELF::SHT_PROGBITS, - ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, SectionKind::getText(), 0, - GroupName)); + ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, 0, GroupName)); } } @@ -1625,11 +1626,10 @@ void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm, } } -void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm, - const GroupMapTy &GroupMap, - const MCAsmLayout &Layout, - const SectionIndexMapTy &SectionIndexMap, - const SectionOffsetMapTy &SectionOffsetMap) { +void ELFObjectWriter::writeSectionHeader( + MCAssembler &Asm, const GroupMapTy &GroupMap, const MCAsmLayout &Layout, + const SectionIndexMapTy &SectionIndexMap, const RelMapTy &RelMap, + const SectionOffsetMapTy &SectionOffsetMap) { const unsigned NumSections = Asm.size() + 1; std::vector<const MCSectionELF*> Sections; @@ -1660,7 +1660,7 @@ void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm, uint64_t Size = GetSectionAddressSize(Layout, SD); - WriteSection(Asm, SectionIndexMap, GroupSymbolIndex, + writeSection(Asm, SectionIndexMap, RelMap, GroupSymbolIndex, SectionOffsetMap.lookup(&Section), Size, SD.getAlignment(), Section); } @@ -1707,10 +1707,8 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm, CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout)); DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap; - CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap); - const unsigned NumUserAndRelocSections = Asm.size(); - CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap, + createIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap, RevGroupMap, SectionIndexMap, RelMap); const unsigned AllSections = Asm.size(); const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections; @@ -1725,8 +1723,7 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm, CreateMetadataSections(const_cast<MCAssembler&>(Asm), const_cast<MCAsmLayout&>(Layout), - SectionIndexMap, - RelMap); + SectionIndexMap); uint64_t NaturalAlignment = is64Bit() ? 8 : 4; uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) : @@ -1783,7 +1780,7 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm, WriteZeros(Padding); // ... then the section header table ... - WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, + writeSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, RelMap, SectionOffsetMap); // ... and then the remaining sections ... diff --git a/lib/MC/MCAsmInfo.cpp b/lib/MC/MCAsmInfo.cpp index 2fb558f..04b8042 100644 --- a/lib/MC/MCAsmInfo.cpp +++ b/lib/MC/MCAsmInfo.cpp @@ -40,6 +40,7 @@ MCAsmInfo::MCAsmInfo() { LabelSuffix = ":"; UseAssignmentForEHBegin = false; PrivateGlobalPrefix = "L"; + PrivateLabelPrefix = PrivateGlobalPrefix; LinkerPrivateGlobalPrefix = ""; InlineAsmStart = "APP"; InlineAsmEnd = "NO_APP"; @@ -71,6 +72,7 @@ MCAsmInfo::MCAsmInfo() { HasSingleParameterDotFile = true; HasIdentDirective = false; HasNoDeadStrip = false; + WeakDirective = "\t.weak\t"; WeakRefDirective = nullptr; HasWeakDefDirective = false; HasWeakDefCanBeHiddenDirective = false; @@ -107,6 +109,10 @@ MCAsmInfo::MCAsmInfo() { MCAsmInfo::~MCAsmInfo() { } +bool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const { + return false; +} + const MCExpr * MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym, unsigned Encoding, diff --git a/lib/MC/MCAsmInfoDarwin.cpp b/lib/MC/MCAsmInfoDarwin.cpp index 66a138b..a2a2504 100644 --- a/lib/MC/MCAsmInfoDarwin.cpp +++ b/lib/MC/MCAsmInfoDarwin.cpp @@ -15,10 +15,46 @@ #include "llvm/MC/MCAsmInfoDarwin.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCStreamer.h" using namespace llvm; -void MCAsmInfoDarwin::anchor() { } +bool MCAsmInfoDarwin::isSectionAtomizableBySymbols( + const MCSection &Section) const { + const MCSectionMachO &SMO = static_cast<const MCSectionMachO &>(Section); + + // Sections holding 1 byte strings are atomized based on the data they + // contain. + // Sections holding 2 byte strings require symbols in order to be atomized. + // There is no dedicated section for 4 byte strings. + if (SMO.getType() == MachO::S_CSTRING_LITERALS) + return false; + + if (SMO.getSegmentName() == "__DATA" && SMO.getSectionName() == "__cfstring") + return false; + + if (SMO.getSegmentName() == "__DATA" && + SMO.getSectionName() == "__objc_classrefs") + return false; + + switch (SMO.getType()) { + default: + return true; + + // These sections are atomized at the element boundaries without using + // symbols. + case MachO::S_4BYTE_LITERALS: + case MachO::S_8BYTE_LITERALS: + case MachO::S_16BYTE_LITERALS: + case MachO::S_LITERAL_POINTERS: + case MachO::S_NON_LAZY_SYMBOL_POINTERS: + case MachO::S_LAZY_SYMBOL_POINTERS: + case MachO::S_MOD_INIT_FUNC_POINTERS: + case MachO::S_MOD_TERM_FUNC_POINTERS: + case MachO::S_INTERPOSING: + return false; + } +} MCAsmInfoDarwin::MCAsmInfoDarwin() { // Common settings for all Darwin targets. diff --git a/lib/MC/MCAsmInfoELF.cpp b/lib/MC/MCAsmInfoELF.cpp index 9f70d8d..cd61a43 100644 --- a/lib/MC/MCAsmInfoELF.cpp +++ b/lib/MC/MCAsmInfoELF.cpp @@ -22,12 +22,12 @@ void MCAsmInfoELF::anchor() { } const MCSection * MCAsmInfoELF::getNonexecutableStackSection(MCContext &Ctx) const { - return Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, - 0, SectionKind::getMetadata()); + return Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0); } MCAsmInfoELF::MCAsmInfoELF() { HasIdentDirective = true; WeakRefDirective = "\t.weak\t"; PrivateGlobalPrefix = ".L"; + PrivateLabelPrefix = ".L"; } diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp index f60c7fc..2312cd5 100644 --- a/lib/MC/MCAsmStreamer.cpp +++ b/lib/MC/MCAsmStreamer.cpp @@ -8,8 +8,8 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCStreamer.h" -#include "llvm/ADT/SmallString.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCAsmBackend.h" @@ -32,7 +32,6 @@ #include "llvm/Support/MathExtras.h" #include "llvm/Support/Path.h" #include <cctype> -#include <unordered_map> using namespace llvm; namespace { @@ -436,14 +435,18 @@ bool MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, case MCSA_Internal: OS << "\t.internal\t"; break; case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break; case MCSA_Local: OS << "\t.local\t"; break; - case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break; + case MCSA_NoDeadStrip: + if (!MAI->hasNoDeadStrip()) + return false; + OS << "\t.no_dead_strip\t"; + break; case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break; case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break; case MCSA_Protected: OS << "\t.protected\t"; break; case MCSA_Reference: OS << "\t.reference\t"; break; - case MCSA_Weak: OS << "\t.weak\t"; break; + case MCSA_Weak: OS << MAI->getWeakDirective(); break; case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break; @@ -682,7 +685,11 @@ void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, // We truncate our partial emission to fit within the bounds of the // emission domain. This produces nicer output and silences potential // truncation warnings when round tripping through another assembler. - ValueToEmit &= ~0ULL >> (64 - EmissionSize * 8); + uint64_t Shift = 64 - EmissionSize * 8; + assert(Shift < static_cast<uint64_t>( + std::numeric_limits<unsigned long long>::digits) && + "undefined behavior"); + ValueToEmit &= ~0ULL >> Shift; EmitIntValue(ValueToEmit, EmissionSize); Emitted += EmissionSize; } @@ -865,8 +872,6 @@ void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Isa, unsigned Discriminator, StringRef FileName) { - this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags, - Isa, Discriminator, FileName); OS << "\t.loc\t" << FileNo << " " << Line << " " << Column; if (Flags & DWARF2_FLAG_BASIC_BLOCK) OS << " basic_block"; @@ -896,6 +901,8 @@ void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, << Line << ':' << Column; } EmitEOL(); + this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags, + Isa, Discriminator, FileName); } MCSymbol *MCAsmStreamer::getDwarfLineTableSymbol(unsigned CUID) { @@ -1249,7 +1256,7 @@ void MCAsmStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &S // Show the MCInst if enabled. if (ShowInst) { - Inst.dump_pretty(GetCommentOS(), MAI, InstPrinter.get(), "\n "); + Inst.dump_pretty(GetCommentOS(), InstPrinter.get(), "\n "); GetCommentOS() << "\n"; } @@ -1257,7 +1264,7 @@ void MCAsmStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &S if (InstPrinter) InstPrinter->printInst(&Inst, OS, ""); else - Inst.print(OS, MAI); + Inst.print(OS); EmitEOL(); } diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index 85d0c13..50ce845 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCAsmBackend.h" +#include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCAsmLayout.h" #include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCContext.h" @@ -20,6 +21,7 @@ #include "llvm/MC/MCFixupKindInfo.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSection.h" +#include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCValue.h" #include "llvm/Support/Debug.h" @@ -27,7 +29,6 @@ #include "llvm/Support/LEB128.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/MC/MCSectionELF.h" #include <tuple> using namespace llvm; @@ -200,7 +201,17 @@ const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const { if (!A) return nullptr; - return &A->getSymbol(); + const MCSymbol &ASym = A->getSymbol(); + const MCAssembler &Asm = getAssembler(); + const MCSymbolData &ASD = Asm.getSymbolData(ASym); + if (ASD.isCommon()) { + // FIXME: we should probably add a SMLoc to MCExpr. + Asm.getContext().FatalError(SMLoc(), + "Common symbol " + ASym.getName() + + " cannot be used in assignment expr"); + } + + return &ASym; } uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const { @@ -424,6 +435,16 @@ bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const { return true; } +void MCAssembler::addLocalUsedInReloc(const MCSymbol &Sym) { + assert(Sym.isTemporary()); + LocalsUsedInReloc.insert(&Sym); +} + +bool MCAssembler::isLocalUsedInReloc(const MCSymbol &Sym) const { + assert(Sym.isTemporary()); + return LocalsUsedInReloc.count(&Sym); +} + bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const { // Non-temporary labels should always be visible to the linker. if (!Symbol.isTemporary()) @@ -433,8 +454,10 @@ bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const { if (!Symbol.isInSection()) return false; - // Otherwise, check if the section requires symbols even for temporary labels. - return getBackend().doesSectionRequireSymbols(Symbol.getSection()); + if (isLocalUsedInReloc(Symbol)) + return true; + + return false; } const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { @@ -448,8 +471,8 @@ const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { // Non-linker visible symbols in sections which can't be atomized have no // defining atom. - if (!getBackend().isSectionAtomizable( - SD->getFragment()->getParent()->getSection())) + if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols( + SD->getFragment()->getParent()->getSection())) return nullptr; // Otherwise, return the atom for the containing fragment. diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index 8630b25..721edd4 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -130,6 +130,11 @@ MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) { return Sym; } +MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName) { + return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + + "frameallocation_" + FuncName); +} + MCSymbol *MCContext::CreateSymbol(StringRef Name) { // Determine whether this is an assembler temporary or normal label, if used. bool isTemporary = false; @@ -247,10 +252,9 @@ getMachOSection(StringRef Segment, StringRef Section, Reserved2, Kind); } -const MCSectionELF *MCContext:: -getELFSection(StringRef Section, unsigned Type, unsigned Flags, - SectionKind Kind) { - return getELFSection(Section, Type, Flags, Kind, 0, ""); +const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, + unsigned Flags) { + return getELFSection(Section, Type, Flags, 0, ""); } void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { @@ -266,35 +270,45 @@ void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { const_cast<MCSectionELF*>(Section)->setSectionName(CachedName); } -const MCSectionELF *MCContext:: -getELFSection(StringRef Section, unsigned Type, unsigned Flags, - SectionKind Kind, unsigned EntrySize, StringRef Group) { +const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, + unsigned Flags, unsigned EntrySize, + StringRef Group, bool Unique) { // Do the lookup, if we have a hit, return it. auto IterBool = ELFUniquingMap.insert( std::make_pair(SectionGroupPair(Section, Group), nullptr)); auto &Entry = *IterBool.first; - if (!IterBool.second) return Entry.second; - - // Possibly refine the entry size first. - if (!EntrySize) { - EntrySize = MCSectionELF::DetermineEntrySize(Kind); - } + if (!IterBool.second && !Unique) + return Entry.second; MCSymbol *GroupSym = nullptr; if (!Group.empty()) GroupSym = GetOrCreateSymbol(Group); StringRef CachedName = Entry.first.first; + + SectionKind Kind; + if (Flags & ELF::SHF_EXECINSTR) + Kind = SectionKind::getText(); + else + Kind = SectionKind::getReadOnly(); + MCSectionELF *Result = new (*this) - MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym); - Entry.second = Result; + MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, Unique); + if (!Unique) + Entry.second = Result; return Result; } +const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, + unsigned Flags, unsigned EntrySize, + StringRef Group) { + return getELFSection(Section, Type, Flags, EntrySize, Group, false); +} + const MCSectionELF *MCContext::CreateELFGroupSection() { MCSectionELF *Result = - new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, - SectionKind::getReadOnly(), 4, nullptr); + new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, + SectionKind::getReadOnly(), 4, nullptr, false); return Result; } diff --git a/lib/MC/MCDisassembler/Disassembler.cpp b/lib/MC/MCDisassembler/Disassembler.cpp index d0d7f30..d9f01d0 100644 --- a/lib/MC/MCDisassembler/Disassembler.cpp +++ b/lib/MC/MCDisassembler/Disassembler.cpp @@ -77,7 +77,7 @@ LLVMCreateDisasmCPUFeatures(const char *Triple, const char *CPU, return nullptr; std::unique_ptr<MCSymbolizer> Symbolizer(TheTarget->createMCSymbolizer( - Triple, GetOpInfo, SymbolLookUp, DisInfo, Ctx, RelInfo.release())); + Triple, GetOpInfo, SymbolLookUp, DisInfo, Ctx, std::move(RelInfo))); DisAsm->setSymbolizer(std::move(Symbolizer)); // Set up the instruction printer. @@ -151,10 +151,10 @@ static void emitComments(LLVMDisasmContext *DC, DC->CommentStream.resync(); } -/// \brief Gets latency information for \p Inst form the itinerary +/// \brief Gets latency information for \p Inst from the itinerary /// scheduling model, based on \p DC information. /// \return The maximum expected latency over all the operands or -1 -/// if no information are available. +/// if no information is available. static int getItineraryLatency(LLVMDisasmContext *DC, const MCInst &Inst) { const int NoInformationAvailable = -1; @@ -179,7 +179,7 @@ static int getItineraryLatency(LLVMDisasmContext *DC, const MCInst &Inst) { /// \brief Gets latency information for \p Inst, based on \p DC information. /// \return The maximum expected latency over all the definitions or -1 -/// if no information are available. +/// if no information is available. static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) { // Try to compute scheduling information. const MCSubtargetInfo *STI = DC->getSubtargetInfo(); @@ -220,7 +220,7 @@ static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) { static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst) { int Latency = getLatency(DC, Inst); - // Report only interesting latency. + // Report only interesting latencies. if (Latency < 2) return; diff --git a/lib/MC/MCDisassembler/MCExternalSymbolizer.cpp b/lib/MC/MCDisassembler/MCExternalSymbolizer.cpp index 0145623..f306e4e 100644 --- a/lib/MC/MCDisassembler/MCExternalSymbolizer.cpp +++ b/lib/MC/MCDisassembler/MCExternalSymbolizer.cpp @@ -186,13 +186,11 @@ void MCExternalSymbolizer::tryAddingPcLoadReferenceComment(raw_ostream &cStream, namespace llvm { MCSymbolizer *createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp, - void *DisInfo, - MCContext *Ctx, - MCRelocationInfo *RelInfo) { + void *DisInfo, MCContext *Ctx, + std::unique_ptr<MCRelocationInfo> &&RelInfo) { assert(Ctx && "No MCContext given for symbolic disassembly"); - return new MCExternalSymbolizer(*Ctx, - std::unique_ptr<MCRelocationInfo>(RelInfo), - GetOpInfo, SymbolLookUp, DisInfo); + return new MCExternalSymbolizer(*Ctx, std::move(RelInfo), GetOpInfo, + SymbolLookUp, DisInfo); } } diff --git a/lib/MC/MCDwarf.cpp b/lib/MC/MCDwarf.cpp index 5effb01..5d96914 100644 --- a/lib/MC/MCDwarf.cpp +++ b/lib/MC/MCDwarf.cpp @@ -1045,11 +1045,16 @@ static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) { void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, const MCCFIInstruction &Instr) { int dataAlignmentFactor = getDataAlignmentFactor(Streamer); + auto *MRI = Streamer.getContext().getRegisterInfo(); switch (Instr.getOperation()) { case MCCFIInstruction::OpRegister: { unsigned Reg1 = Instr.getRegister(); unsigned Reg2 = Instr.getRegister2(); + if (!IsEH) { + Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false); + Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false); + } Streamer.EmitIntValue(dwarf::DW_CFA_register, 1); Streamer.EmitULEB128IntValue(Reg1); Streamer.EmitULEB128IntValue(Reg2); @@ -1082,8 +1087,11 @@ void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, return; } case MCCFIInstruction::OpDefCfa: { + unsigned Reg = Instr.getRegister(); + if (!IsEH) + Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false); Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); - Streamer.EmitULEB128IntValue(Instr.getRegister()); + Streamer.EmitULEB128IntValue(Reg); CFAOffset = -Instr.getOffset(); Streamer.EmitULEB128IntValue(CFAOffset); @@ -1091,8 +1099,11 @@ void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, } case MCCFIInstruction::OpDefCfaRegister: { + unsigned Reg = Instr.getRegister(); + if (!IsEH) + Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false); Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); - Streamer.EmitULEB128IntValue(Instr.getRegister()); + Streamer.EmitULEB128IntValue(Reg); return; } @@ -1103,6 +1114,9 @@ void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, Instr.getOperation() == MCCFIInstruction::OpRelOffset; unsigned Reg = Instr.getRegister(); + if (!IsEH) + Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false); + int Offset = Instr.getOffset(); if (IsRelative) Offset -= CFAOffset; @@ -1136,6 +1150,8 @@ void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, } case MCCFIInstruction::OpRestore: { unsigned Reg = Instr.getRegister(); + if (!IsEH) + Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false); Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1); return; } @@ -1290,10 +1306,10 @@ const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer, if (CIEVersion == 1) { assert(MRI->getRARegister() <= 255 && "DWARF 2 encodes return_address_register in one byte"); - streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true), 1); + streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1); } else { streamer.EmitULEB128IntValue( - MRI->getDwarfRegNum(MRI->getRARegister(), true)); + MRI->getDwarfRegNum(MRI->getRARegister(), IsEH)); } // Augmentation Data Length (optional) diff --git a/lib/MC/MCELF.cpp b/lib/MC/MCELF.cpp index 386c209..3690634 100644 --- a/lib/MC/MCELF.cpp +++ b/lib/MC/MCELF.cpp @@ -21,7 +21,7 @@ namespace llvm { void MCELF::SetBinding(MCSymbolData &SD, unsigned Binding) { assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || - Binding == ELF::STB_WEAK); + Binding == ELF::STB_WEAK || Binding == ELF::STB_GNU_UNIQUE); uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift); SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift)); } @@ -29,7 +29,7 @@ void MCELF::SetBinding(MCSymbolData &SD, unsigned Binding) { unsigned MCELF::GetBinding(const MCSymbolData &SD) { uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift; assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || - Binding == ELF::STB_WEAK); + Binding == ELF::STB_WEAK || Binding == ELF::STB_GNU_UNIQUE); return Binding; } diff --git a/lib/MC/MCELFStreamer.cpp b/lib/MC/MCELFStreamer.cpp index bdc4a84..199825e 100644 --- a/lib/MC/MCELFStreamer.cpp +++ b/lib/MC/MCELFStreamer.cpp @@ -171,10 +171,16 @@ bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, return false; case MCSA_NoDeadStrip: - case MCSA_ELF_TypeGnuUniqueObject: // Ignore for now. break; + case MCSA_ELF_TypeGnuUniqueObject: + MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), ELF::STT_OBJECT)); + MCELF::SetBinding(SD, ELF::STB_GNU_UNIQUE); + SD.setExternal(true); + BindingExplicitlySet.insert(Symbol); + break; + case MCSA_Global: MCELF::SetBinding(SD, ELF::STB_GLOBAL); SD.setExternal(true); @@ -253,11 +259,8 @@ void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, MCELF::SetType(SD, ELF::STT_OBJECT); if (MCELF::GetBinding(SD) == ELF_STB_Local) { - const MCSection *Section = getAssembler().getContext().getELFSection(".bss", - ELF::SHT_NOBITS, - ELF::SHF_WRITE | - ELF::SHF_ALLOC, - SectionKind::getBSS()); + const MCSection *Section = getAssembler().getContext().getELFSection( + ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); AssignSection(Symbol, Section); @@ -312,8 +315,7 @@ void MCELFStreamer::EmitFileDirective(StringRef Filename) { void MCELFStreamer::EmitIdent(StringRef IdentString) { const MCSection *Comment = getAssembler().getContext().getELFSection( - ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, - SectionKind::getReadOnly(), 1, ""); + ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); PushSection(); SwitchSection(Comment); if (!SeenIdent) { diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp index 6e648b2..709dc6b 100644 --- a/lib/MC/MCExpr.cpp +++ b/lib/MC/MCExpr.cpp @@ -197,6 +197,7 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { case VK_ARM_TARGET1: return "target1"; case VK_ARM_TARGET2: return "target2"; case VK_ARM_PREL31: return "prel31"; + case VK_ARM_SBREL: return "sbrel"; case VK_ARM_TLSLDO: return "tlsldo"; case VK_ARM_TLSCALL: return "tlscall"; case VK_ARM_TLSDESC: return "tlsdesc"; @@ -286,164 +287,87 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { MCSymbolRefExpr::VariantKind MCSymbolRefExpr::getVariantKindForName(StringRef Name) { - return StringSwitch<VariantKind>(Name) - .Case("GOT", VK_GOT) + return StringSwitch<VariantKind>(Name.lower()) .Case("got", VK_GOT) - .Case("GOTOFF", VK_GOTOFF) .Case("gotoff", VK_GOTOFF) - .Case("GOTPCREL", VK_GOTPCREL) .Case("gotpcrel", VK_GOTPCREL) - .Case("GOT_PREL", VK_GOTPCREL) .Case("got_prel", VK_GOTPCREL) - .Case("GOTTPOFF", VK_GOTTPOFF) .Case("gottpoff", VK_GOTTPOFF) - .Case("INDNTPOFF", VK_INDNTPOFF) .Case("indntpoff", VK_INDNTPOFF) - .Case("NTPOFF", VK_NTPOFF) .Case("ntpoff", VK_NTPOFF) - .Case("GOTNTPOFF", VK_GOTNTPOFF) .Case("gotntpoff", VK_GOTNTPOFF) - .Case("PLT", VK_PLT) .Case("plt", VK_PLT) - .Case("TLSGD", VK_TLSGD) .Case("tlsgd", VK_TLSGD) - .Case("TLSLD", VK_TLSLD) .Case("tlsld", VK_TLSLD) - .Case("TLSLDM", VK_TLSLDM) .Case("tlsldm", VK_TLSLDM) - .Case("TPOFF", VK_TPOFF) .Case("tpoff", VK_TPOFF) - .Case("DTPOFF", VK_DTPOFF) .Case("dtpoff", VK_DTPOFF) - .Case("TLVP", VK_TLVP) .Case("tlvp", VK_TLVP) - .Case("TLVPPAGE", VK_TLVPPAGE) .Case("tlvppage", VK_TLVPPAGE) - .Case("TLVPPAGEOFF", VK_TLVPPAGEOFF) .Case("tlvppageoff", VK_TLVPPAGEOFF) - .Case("PAGE", VK_PAGE) .Case("page", VK_PAGE) - .Case("PAGEOFF", VK_PAGEOFF) .Case("pageoff", VK_PAGEOFF) - .Case("GOTPAGE", VK_GOTPAGE) .Case("gotpage", VK_GOTPAGE) - .Case("GOTPAGEOFF", VK_GOTPAGEOFF) .Case("gotpageoff", VK_GOTPAGEOFF) - .Case("IMGREL", VK_COFF_IMGREL32) .Case("imgrel", VK_COFF_IMGREL32) - .Case("SECREL32", VK_SECREL) .Case("secrel32", VK_SECREL) - .Case("L", VK_PPC_LO) .Case("l", VK_PPC_LO) - .Case("H", VK_PPC_HI) .Case("h", VK_PPC_HI) - .Case("HA", VK_PPC_HA) .Case("ha", VK_PPC_HA) - .Case("HIGHER", VK_PPC_HIGHER) .Case("higher", VK_PPC_HIGHER) - .Case("HIGHERA", VK_PPC_HIGHERA) .Case("highera", VK_PPC_HIGHERA) - .Case("HIGHEST", VK_PPC_HIGHEST) .Case("highest", VK_PPC_HIGHEST) - .Case("HIGHESTA", VK_PPC_HIGHESTA) .Case("highesta", VK_PPC_HIGHESTA) - .Case("GOT@L", VK_PPC_GOT_LO) .Case("got@l", VK_PPC_GOT_LO) - .Case("GOT@H", VK_PPC_GOT_HI) .Case("got@h", VK_PPC_GOT_HI) - .Case("GOT@HA", VK_PPC_GOT_HA) .Case("got@ha", VK_PPC_GOT_HA) - .Case("TOCBASE", VK_PPC_TOCBASE) + .Case("local", VK_PPC_LOCAL) .Case("tocbase", VK_PPC_TOCBASE) - .Case("TOC", VK_PPC_TOC) .Case("toc", VK_PPC_TOC) - .Case("TOC@L", VK_PPC_TOC_LO) .Case("toc@l", VK_PPC_TOC_LO) - .Case("TOC@H", VK_PPC_TOC_HI) .Case("toc@h", VK_PPC_TOC_HI) - .Case("TOC@HA", VK_PPC_TOC_HA) .Case("toc@ha", VK_PPC_TOC_HA) - .Case("TLS", VK_PPC_TLS) .Case("tls", VK_PPC_TLS) - .Case("DTPMOD", VK_PPC_DTPMOD) .Case("dtpmod", VK_PPC_DTPMOD) - .Case("TPREL", VK_PPC_TPREL) .Case("tprel", VK_PPC_TPREL) - .Case("TPREL@L", VK_PPC_TPREL_LO) .Case("tprel@l", VK_PPC_TPREL_LO) - .Case("TPREL@H", VK_PPC_TPREL_HI) .Case("tprel@h", VK_PPC_TPREL_HI) - .Case("TPREL@HA", VK_PPC_TPREL_HA) .Case("tprel@ha", VK_PPC_TPREL_HA) - .Case("TPREL@HIGHER", VK_PPC_TPREL_HIGHER) .Case("tprel@higher", VK_PPC_TPREL_HIGHER) - .Case("TPREL@HIGHERA", VK_PPC_TPREL_HIGHERA) .Case("tprel@highera", VK_PPC_TPREL_HIGHERA) - .Case("TPREL@HIGHEST", VK_PPC_TPREL_HIGHEST) .Case("tprel@highest", VK_PPC_TPREL_HIGHEST) - .Case("TPREL@HIGHESTA", VK_PPC_TPREL_HIGHESTA) .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA) - .Case("DTPREL", VK_PPC_DTPREL) .Case("dtprel", VK_PPC_DTPREL) - .Case("DTPREL@L", VK_PPC_DTPREL_LO) .Case("dtprel@l", VK_PPC_DTPREL_LO) - .Case("DTPREL@H", VK_PPC_DTPREL_HI) .Case("dtprel@h", VK_PPC_DTPREL_HI) - .Case("DTPREL@HA", VK_PPC_DTPREL_HA) .Case("dtprel@ha", VK_PPC_DTPREL_HA) - .Case("DTPREL@HIGHER", VK_PPC_DTPREL_HIGHER) .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER) - .Case("DTPREL@HIGHERA", VK_PPC_DTPREL_HIGHERA) .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA) - .Case("DTPREL@HIGHEST", VK_PPC_DTPREL_HIGHEST) .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST) - .Case("DTPREL@HIGHESTA", VK_PPC_DTPREL_HIGHESTA) .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA) - .Case("GOT@TPREL", VK_PPC_GOT_TPREL) .Case("got@tprel", VK_PPC_GOT_TPREL) - .Case("GOT@TPREL@L", VK_PPC_GOT_TPREL_LO) .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO) - .Case("GOT@TPREL@H", VK_PPC_GOT_TPREL_HI) .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI) - .Case("GOT@TPREL@HA", VK_PPC_GOT_TPREL_HA) .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA) - .Case("GOT@DTPREL", VK_PPC_GOT_DTPREL) .Case("got@dtprel", VK_PPC_GOT_DTPREL) - .Case("GOT@DTPREL@L", VK_PPC_GOT_DTPREL_LO) .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO) - .Case("GOT@DTPREL@H", VK_PPC_GOT_DTPREL_HI) .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI) - .Case("GOT@DTPREL@HA", VK_PPC_GOT_DTPREL_HA) .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA) - .Case("GOT@TLSGD", VK_PPC_GOT_TLSGD) .Case("got@tlsgd", VK_PPC_GOT_TLSGD) - .Case("GOT@TLSGD@L", VK_PPC_GOT_TLSGD_LO) .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO) - .Case("GOT@TLSGD@H", VK_PPC_GOT_TLSGD_HI) .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI) - .Case("GOT@TLSGD@HA", VK_PPC_GOT_TLSGD_HA) .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA) - .Case("GOT@TLSLD", VK_PPC_GOT_TLSLD) .Case("got@tlsld", VK_PPC_GOT_TLSLD) - .Case("GOT@TLSLD@L", VK_PPC_GOT_TLSLD_LO) .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO) - .Case("GOT@TLSLD@H", VK_PPC_GOT_TLSLD_HI) .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI) - .Case("GOT@TLSLD@HA", VK_PPC_GOT_TLSLD_HA) .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA) - .Case("NONE", VK_ARM_NONE) .Case("none", VK_ARM_NONE) - .Case("TARGET1", VK_ARM_TARGET1) .Case("target1", VK_ARM_TARGET1) - .Case("TARGET2", VK_ARM_TARGET2) .Case("target2", VK_ARM_TARGET2) - .Case("PREL31", VK_ARM_PREL31) .Case("prel31", VK_ARM_PREL31) - .Case("TLSLDO", VK_ARM_TLSLDO) + .Case("sbrel", VK_ARM_SBREL) .Case("tlsldo", VK_ARM_TLSLDO) - .Case("TLSCALL", VK_ARM_TLSCALL) .Case("tlscall", VK_ARM_TLSCALL) - .Case("TLSDESC", VK_ARM_TLSDESC) .Case("tlsdesc", VK_ARM_TLSDESC) .Default(VK_Invalid); } diff --git a/lib/MC/MCInst.cpp b/lib/MC/MCInst.cpp index d7b80f5..7ef69be 100644 --- a/lib/MC/MCInst.cpp +++ b/lib/MC/MCInst.cpp @@ -15,7 +15,7 @@ using namespace llvm; -void MCOperand::print(raw_ostream &OS, const MCAsmInfo *MAI) const { +void MCOperand::print(raw_ostream &OS) const { OS << "<MCOperand "; if (!isValid()) OS << "INVALID"; @@ -34,22 +34,21 @@ void MCOperand::print(raw_ostream &OS, const MCAsmInfo *MAI) const { #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCOperand::dump() const { - print(dbgs(), nullptr); + print(dbgs()); dbgs() << "\n"; } #endif -void MCInst::print(raw_ostream &OS, const MCAsmInfo *MAI) const { +void MCInst::print(raw_ostream &OS) const { OS << "<MCInst " << getOpcode(); for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { OS << " "; - getOperand(i).print(OS, MAI); + getOperand(i).print(OS); } OS << ">"; } -void MCInst::dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI, - const MCInstPrinter *Printer, +void MCInst::dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer, StringRef Separator) const { OS << "<MCInst #" << getOpcode(); @@ -59,14 +58,14 @@ void MCInst::dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI, for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { OS << Separator; - getOperand(i).print(OS, MAI); + getOperand(i).print(OS); } OS << ">"; } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCInst::dump() const { - print(dbgs(), nullptr); + print(dbgs()); dbgs() << "\n"; } #endif diff --git a/lib/MC/MCInstPrinter.cpp b/lib/MC/MCInstPrinter.cpp index ba71245..0dc3121 100644 --- a/lib/MC/MCInstPrinter.cpp +++ b/lib/MC/MCInstPrinter.cpp @@ -69,11 +69,11 @@ static bool needsLeadingZero(uint64_t Value) return false; } -format_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const { +format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const { return format("%" PRId64, Value); } -format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const { +format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const { switch(PrintHexStyle) { case HexStyle::C: if (Value < 0) @@ -96,7 +96,7 @@ format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const { llvm_unreachable("unsupported print style"); } -format_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const { +format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const { switch(PrintHexStyle) { case HexStyle::C: return format("0x%" PRIx64, Value); diff --git a/lib/MC/MCLinkerOptimizationHint.cpp b/lib/MC/MCLinkerOptimizationHint.cpp index 3f8d620..7739878 100644 --- a/lib/MC/MCLinkerOptimizationHint.cpp +++ b/lib/MC/MCLinkerOptimizationHint.cpp @@ -9,8 +9,8 @@ #include "llvm/MC/MCLinkerOptimizationHint.h" #include "llvm/MC/MCAsmLayout.h" -#include "llvm/Support/LEB128.h" #include "llvm/MC/MCStreamer.h" +#include "llvm/Support/LEB128.h" using namespace llvm; diff --git a/lib/MC/MCMachOStreamer.cpp b/lib/MC/MCMachOStreamer.cpp index a147c3d..79eab49 100644 --- a/lib/MC/MCMachOStreamer.cpp +++ b/lib/MC/MCMachOStreamer.cpp @@ -183,7 +183,7 @@ void MCMachOStreamer::EmitDataRegionEnd() { if (!getAssembler().getBackend().hasDataInCodeSupport()) return; std::vector<DataRegionData> &Regions = getAssembler().getDataRegions(); - assert(Regions.size() && "Mismatched .end_data_region!"); + assert(!Regions.empty() && "Mismatched .end_data_region!"); DataRegionData &Data = Regions.back(); assert(!Data.End && "Mismatched .end_data_region!"); // Create a temporary label to mark the end of the data region. diff --git a/lib/MC/MCObjectFileInfo.cpp b/lib/MC/MCObjectFileInfo.cpp index 1b88462..11c9cc2 100644 --- a/lib/MC/MCObjectFileInfo.cpp +++ b/lib/MC/MCObjectFileInfo.cpp @@ -273,6 +273,12 @@ void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) { case Triple::mips64el: FDECFIEncoding = dwarf::DW_EH_PE_sdata8; break; + case Triple::x86_64: + FDECFIEncoding = dwarf::DW_EH_PE_pcrel | + ((CMModel == CodeModel::Large) ? dwarf::DW_EH_PE_sdata8 + : dwarf::DW_EH_PE_sdata4); + + break; default: FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; break; @@ -401,7 +407,7 @@ void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) { // platform. EHSectionType = ELF::SHT_PROGBITS; EHSectionFlags = ELF::SHF_ALLOC; - if (T.getOS() == Triple::Solaris) { + if (T.isOSSolaris()) { if (T.getArch() == Triple::x86_64) EHSectionType = ELF::SHT_X86_64_UNWIND; else @@ -410,83 +416,54 @@ void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) { // ELF - BSSSection = - Ctx->getELFSection(".bss", ELF::SHT_NOBITS, - ELF::SHF_WRITE | ELF::SHF_ALLOC, - SectionKind::getBSS()); + BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS, + ELF::SHF_WRITE | ELF::SHF_ALLOC); - TextSection = - Ctx->getELFSection(".text", ELF::SHT_PROGBITS, - ELF::SHF_EXECINSTR | - ELF::SHF_ALLOC, - SectionKind::getText()); + TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS, + ELF::SHF_EXECINSTR | ELF::SHF_ALLOC); - DataSection = - Ctx->getELFSection(".data", ELF::SHT_PROGBITS, - ELF::SHF_WRITE |ELF::SHF_ALLOC, - SectionKind::getDataRel()); + DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS, + ELF::SHF_WRITE | ELF::SHF_ALLOC); ReadOnlySection = - Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC, - SectionKind::getReadOnly()); + Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); TLSDataSection = - Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC | ELF::SHF_TLS | - ELF::SHF_WRITE, - SectionKind::getThreadData()); - - TLSBSSSection = - Ctx->getELFSection(".tbss", ELF::SHT_NOBITS, - ELF::SHF_ALLOC | ELF::SHF_TLS | - ELF::SHF_WRITE, - SectionKind::getThreadBSS()); - - DataRelSection = - Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_WRITE, - SectionKind::getDataRel()); - - DataRelLocalSection = - Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_WRITE, - SectionKind::getDataRelLocal()); - - DataRelROSection = - Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_WRITE, - SectionKind::getReadOnlyWithRel()); - - DataRelROLocalSection = - Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_WRITE, - SectionKind::getReadOnlyWithRelLocal()); + Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE); + + TLSBSSSection = Ctx->getELFSection( + ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE); + + DataRelSection = Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_WRITE); + + DataRelLocalSection = Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_WRITE); + + DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_WRITE); + + DataRelROLocalSection = Ctx->getELFSection( + ".data.rel.ro.local", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_WRITE); MergeableConst4Section = - Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_MERGE, - SectionKind::getMergeableConst4()); + Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, ""); MergeableConst8Section = - Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_MERGE, - SectionKind::getMergeableConst8()); + Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, ""); MergeableConst16Section = - Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_MERGE, - SectionKind::getMergeableConst16()); + Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, ""); - StaticCtorSection = - Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_WRITE, - SectionKind::getDataRel()); + StaticCtorSection = Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_WRITE); - StaticDtorSection = - Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC |ELF::SHF_WRITE, - SectionKind::getDataRel()); + StaticDtorSection = Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | ELF::SHF_WRITE); // Exception Handling Sections. @@ -494,103 +471,68 @@ void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) { // it contains relocatable pointers. In PIC mode, this is probably a big // runtime hit for C++ apps. Either the contents of the LSDA need to be // adjusted or this should be a data section. - LSDASection = - Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC, - SectionKind::getReadOnly()); + LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS, + ELF::SHF_ALLOC); COFFDebugSymbolsSection = nullptr; // Debug Info Sections. DwarfAbbrevSection = - Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); - DwarfInfoSection = - Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); - DwarfLineSection = - Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); - DwarfFrameSection = - Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0); + DwarfInfoSection = Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0); + DwarfLineSection = Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0); + DwarfFrameSection = Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0); DwarfPubNamesSection = - Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0); DwarfPubTypesSection = - Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0); DwarfGnuPubNamesSection = - Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0); DwarfGnuPubTypesSection = - Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0); DwarfStrSection = - Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS, - ELF::SHF_MERGE | ELF::SHF_STRINGS, - SectionKind::getMergeable1ByteCString()); - DwarfLocSection = - Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS, + ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); + DwarfLocSection = Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0); DwarfARangesSection = - Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0); DwarfRangesSection = - Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0); DwarfMacroInfoSection = - Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0); // DWARF5 Experimental Debug Info // Accelerator Tables DwarfAccelNamesSection = - Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0); DwarfAccelObjCSection = - Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0); DwarfAccelNamespaceSection = - Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0); DwarfAccelTypesSection = - Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0); // Fission Sections DwarfInfoDWOSection = - Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0); DwarfTypesDWOSection = - Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS, 0); DwarfAbbrevDWOSection = - Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0); DwarfStrDWOSection = - Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS, - ELF::SHF_MERGE | ELF::SHF_STRINGS, - SectionKind::getMergeable1ByteCString()); + Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS, + ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, ""); DwarfLineDWOSection = - Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0); DwarfLocDWOSection = - Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0); DwarfStrOffDWOSection = - Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); - DwarfAddrSection = - Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0, - SectionKind::getMetadata()); + Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0); + DwarfAddrSection = Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0); StackMapSection = - Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC, - SectionKind::getMetadata()); - + Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); } @@ -678,129 +620,153 @@ void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) { DwarfAbbrevSection = Ctx->getCOFFSection(".debug_abbrev", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfInfoSection = Ctx->getCOFFSection(".debug_info", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfLineSection = Ctx->getCOFFSection(".debug_line", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfFrameSection = Ctx->getCOFFSection(".debug_frame", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfPubNamesSection = Ctx->getCOFFSection(".debug_pubnames", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfPubTypesSection = Ctx->getCOFFSection(".debug_pubtypes", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfGnuPubNamesSection = Ctx->getCOFFSection(".debug_gnu_pubnames", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfGnuPubTypesSection = Ctx->getCOFFSection(".debug_gnu_pubtypes", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfStrSection = Ctx->getCOFFSection(".debug_str", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfLocSection = Ctx->getCOFFSection(".debug_loc", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfARangesSection = Ctx->getCOFFSection(".debug_aranges", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfRangesSection = Ctx->getCOFFSection(".debug_ranges", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfMacroInfoSection = Ctx->getCOFFSection(".debug_macinfo", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); DwarfInfoDWOSection = - Ctx->getCOFFSection(".debug_info.dwo", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".debug_info.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfTypesDWOSection = - Ctx->getCOFFSection(".debug_types.dwo", COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".debug_types.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfAbbrevDWOSection = - Ctx->getCOFFSection(".debug_abbrev.dwo", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".debug_abbrev.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfStrDWOSection = - Ctx->getCOFFSection(".debug_str.dwo", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".debug_str.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfLineDWOSection = - Ctx->getCOFFSection(".debug_line.dwo", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".debug_line.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfLocDWOSection = - Ctx->getCOFFSection(".debug_loc.dwo", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".debug_loc.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfStrOffDWOSection = - Ctx->getCOFFSection(".debug_str_offsets.dwo", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); - + Ctx->getCOFFSection(".debug_str_offsets.dwo", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfAddrSection = Ctx->getCOFFSection(".debug_addr", COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata()); - DwarfAccelNamesSection = - Ctx->getCOFFSection(".apple_names", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".apple_names", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfAccelNamespaceSection = - Ctx->getCOFFSection(".apple_namespaces", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".apple_namespaces", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfAccelTypesSection = - Ctx->getCOFFSection(".apple_types", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".apple_types", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfAccelObjCSection = - Ctx->getCOFFSection(".apple_objc", - COFF::IMAGE_SCN_MEM_DISCARDABLE | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getMetadata()); + Ctx->getCOFFSection(".apple_objc", + COFF::IMAGE_SCN_MEM_DISCARDABLE | + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DrectveSection = Ctx->getCOFFSection(".drectve", @@ -878,7 +844,7 @@ void MCObjectFileInfo::InitMCObjectFileInfo(StringRef T, Reloc::Model relocm, const MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const { return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP, - SectionKind::getMetadata(), 0, utostr(Hash)); + 0, utostr(Hash)); } void MCObjectFileInfo::InitEHFrameSection() { @@ -892,9 +858,7 @@ void MCObjectFileInfo::InitEHFrameSection() { SectionKind::getReadOnly()); else if (Env == IsELF) EHFrameSection = - Ctx->getELFSection(".eh_frame", EHSectionType, - EHSectionFlags, - SectionKind::getDataRel()); + Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags); else EHFrameSection = Ctx->getCOFFSection(".eh_frame", diff --git a/lib/MC/MCObjectStreamer.cpp b/lib/MC/MCObjectStreamer.cpp index 21e6867..08fe501 100644 --- a/lib/MC/MCObjectStreamer.cpp +++ b/lib/MC/MCObjectStreamer.cpp @@ -405,7 +405,9 @@ void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { } void MCObjectStreamer::EmitZeros(uint64_t NumBytes) { - unsigned ItemSize = getCurrentSection().first->isVirtualSection() ? 0 : 1; + const MCSection *Sec = getCurrentSection().first; + assert(Sec && "need a section"); + unsigned ItemSize = Sec->isVirtualSection() ? 0 : 1; insert(new MCFillFragment(0, ItemSize, NumBytes)); } diff --git a/lib/MC/MCParser/AsmParser.cpp b/lib/MC/MCParser/AsmParser.cpp index de7d961..ef6a540 100644 --- a/lib/MC/MCParser/AsmParser.cpp +++ b/lib/MC/MCParser/AsmParser.cpp @@ -111,8 +111,8 @@ struct ParseStatementInfo { /// \brief The concrete assembly parser instance. class AsmParser : public MCAsmParser { - AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION; - void operator=(const AsmParser &) LLVM_DELETED_FUNCTION; + AsmParser(const AsmParser &) = delete; + void operator=(const AsmParser &) = delete; private: AsmLexer Lexer; MCContext &Ctx; @@ -1298,6 +1298,9 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, Sym = getContext().GetOrCreateSymbol(IDVal); } else Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal); + + Sym->redefineIfPossible(); + if (!Sym->isUndefined() || Sym->isVariable()) return Error(IDLoc, "invalid symbol redefinition"); @@ -1595,14 +1598,18 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, // directive for the instruction. if (!HadError && getContext().getGenDwarfForAssembly() && getContext().getGenDwarfSectionSyms().count( - getStreamer().getCurrentSection().first)) { - - unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); + getStreamer().getCurrentSection().first)) { + unsigned Line; + if (ActiveMacros.empty()) + Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); + else + Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc, + ActiveMacros.back()->ExitBuffer); // If we previously parsed a cpp hash file line comment then make sure the // current Dwarf File is for the CppHashFilename if not then emit the // Dwarf File table for it and adjust the line number for the .loc. - if (CppHashFilename.size() != 0) { + if (CppHashFilename.size()) { unsigned FileNumber = getStreamer().EmitDwarfFileDirective( 0, StringRef(), CppHashFilename); getContext().setGenDwarfFileNumber(FileNumber); @@ -2213,6 +2220,8 @@ bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, } else Sym = getContext().GetOrCreateSymbol(Name); + Sym->setRedefinable(allow_redef); + // Do the assignment. Out.EmitAssignment(Sym, Value); if (NoDeadStrip) @@ -3266,7 +3275,7 @@ bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { MCAsmMacroParameters Parameters; while (getLexer().isNot(AsmToken::EndOfStatement)) { - if (Parameters.size() && Parameters.back().Vararg) + if (!Parameters.empty() && Parameters.back().Vararg) return Error(Lexer.getLoc(), "Vararg parameter '" + Parameters.back().Name + "' should be last one in the list of parameters."); @@ -3627,21 +3636,27 @@ bool AsmParser::parseDirectiveSpace(StringRef IDVal) { } /// parseDirectiveLEB128 -/// ::= (.sleb128 | .uleb128) expression +/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] bool AsmParser::parseDirectiveLEB128(bool Signed) { checkForValidSection(); const MCExpr *Value; - if (parseExpression(Value)) - return true; + for (;;) { + if (parseExpression(Value)) + return true; - if (getLexer().isNot(AsmToken::EndOfStatement)) - return TokError("unexpected token in directive"); + if (Signed) + getStreamer().EmitSLEB128Value(Value); + else + getStreamer().EmitULEB128Value(Value); - if (Signed) - getStreamer().EmitSLEB128Value(Value); - else - getStreamer().EmitULEB128Value(Value); + if (getLexer().is(AsmToken::EndOfStatement)) + break; + + if (getLexer().isNot(AsmToken::Comma)) + return TokError("unexpected token in directive"); + Lex(); + } return false; } @@ -4662,7 +4677,7 @@ bool AsmParser::parseMSInlineAsm( OS << "$$"; break; case AOK_Label: - OS << Ctx.getAsmInfo()->getPrivateGlobalPrefix() << AR.Label; + OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; break; case AOK_Input: OS << '$' << InputIdx++; diff --git a/lib/MC/MCParser/CMakeLists.txt b/lib/MC/MCParser/CMakeLists.txt index 222f237..957c94e 100644 --- a/lib/MC/MCParser/CMakeLists.txt +++ b/lib/MC/MCParser/CMakeLists.txt @@ -8,4 +8,7 @@ add_llvm_library(LLVMMCParser MCAsmParser.cpp MCAsmParserExtension.cpp MCTargetAsmParser.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/MCParser ) diff --git a/lib/MC/MCParser/COFFAsmParser.cpp b/lib/MC/MCParser/COFFAsmParser.cpp index 6f82e6e..18bdb03 100644 --- a/lib/MC/MCParser/COFFAsmParser.cpp +++ b/lib/MC/MCParser/COFFAsmParser.cpp @@ -582,7 +582,7 @@ bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc) { } bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) { - unsigned Reg; + unsigned Reg = 0; if (ParseSEHRegisterNumber(Reg)) return true; @@ -595,7 +595,7 @@ bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) { } bool COFFAsmParser::ParseSEHDirectiveSetFrame(StringRef, SMLoc L) { - unsigned Reg; + unsigned Reg = 0; int64_t Off; if (ParseSEHRegisterNumber(Reg)) return true; @@ -636,7 +636,7 @@ bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc) { } bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) { - unsigned Reg; + unsigned Reg = 0; int64_t Off; if (ParseSEHRegisterNumber(Reg)) return true; @@ -663,7 +663,7 @@ bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) { // FIXME: This method is inherently x86-specific. It should really be in the // x86 backend. bool COFFAsmParser::ParseSEHDirectiveSaveXMM(StringRef, SMLoc L) { - unsigned Reg; + unsigned Reg = 0; int64_t Off; if (ParseSEHRegisterNumber(Reg)) return true; diff --git a/lib/MC/MCParser/ELFAsmParser.cpp b/lib/MC/MCParser/ELFAsmParser.cpp index e302004..7a120a1 100644 --- a/lib/MC/MCParser/ELFAsmParser.cpp +++ b/lib/MC/MCParser/ELFAsmParser.cpp @@ -199,8 +199,7 @@ bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, return true; } - getStreamer().SwitchSection(getContext().getELFSection( - Section, Type, Flags, Kind), + getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags), Subsection); return false; @@ -269,40 +268,6 @@ bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { return false; } -static SectionKind computeSectionKind(unsigned Flags, unsigned ElemSize) { - if (Flags & ELF::SHF_EXECINSTR) - return SectionKind::getText(); - if (Flags & ELF::SHF_TLS) - return SectionKind::getThreadData(); - if (Flags & ELF::SHF_MERGE) { - if (Flags & ELF::SHF_STRINGS) { - switch (ElemSize) { - default: - break; - case 1: - return SectionKind::getMergeable1ByteCString(); - case 2: - return SectionKind::getMergeable2ByteCString(); - case 4: - return SectionKind::getMergeable4ByteCString(); - } - } else { - switch (ElemSize) { - default: - break; - case 4: - return SectionKind::getMergeableConst4(); - case 8: - return SectionKind::getMergeableConst8(); - case 16: - return SectionKind::getMergeableConst16(); - } - } - } - - return SectionKind::getDataRel(); -} - static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) { unsigned flags = 0; @@ -413,6 +378,8 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { unsigned Flags = 0; const MCExpr *Subsection = nullptr; bool UseLastGroup = false; + StringRef UniqueStr; + bool Unique = false; // Set the defaults first. if (SectionName == ".fini" || SectionName == ".init" || @@ -497,6 +464,14 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { return TokError("Linkage must be 'comdat'"); } } + if (getLexer().is(AsmToken::Comma)) { + Lex(); + if (getParser().parseIdentifier(UniqueStr)) + return TokError("expected identifier in directive"); + if (UniqueStr != "unique") + return TokError("expected 'unique'"); + Unique = true; + } } } @@ -544,9 +519,8 @@ EndStmt: } } - SectionKind Kind = computeSectionKind(Flags, Size); const MCSection *ELFSection = getContext().getELFSection( - SectionName, Type, Flags, Kind, Size, GroupName); + SectionName, Type, Flags, Size, GroupName, Unique); getStreamer().SwitchSection(ELFSection, Subsection); if (getContext().getGenDwarfForAssembly()) { @@ -697,9 +671,7 @@ bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) { Lex(); - const MCSection *Note = - getContext().getELFSection(".note", ELF::SHT_NOTE, 0, - SectionKind::getReadOnly()); + const MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0); getStreamer().PushSection(); getStreamer().SwitchSection(Note); diff --git a/lib/MC/MCSectionCOFF.cpp b/lib/MC/MCSectionCOFF.cpp index e95845f0..4d6298c 100644 --- a/lib/MC/MCSectionCOFF.cpp +++ b/lib/MC/MCSectionCOFF.cpp @@ -47,6 +47,10 @@ void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, } OS << "\t.section\t" << getSectionName() << ",\""; + if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) + OS << 'd'; + if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) + OS << 'b'; if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE) OS << 'x'; if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE) @@ -55,10 +59,6 @@ void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, OS << 'r'; else OS << 'y'; - if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) - OS << 'd'; - if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) - OS << 'b'; if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE) OS << 'n'; if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED) diff --git a/lib/MC/MCSectionELF.cpp b/lib/MC/MCSectionELF.cpp index a29bb97..da38682 100644 --- a/lib/MC/MCSectionELF.cpp +++ b/lib/MC/MCSectionELF.cpp @@ -24,6 +24,9 @@ MCSectionELF::~MCSectionELF() {} // anchor. bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const { + if (Unique) + return false; + // FIXME: Does .section .bss/.data/.text work everywhere?? if (Name == ".text" || Name == ".data" || (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS())) @@ -144,6 +147,10 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, printName(OS, Group->getName()); OS << ",comdat"; } + + if (Unique) + OS << ",unique"; + OS << '\n'; if (Subsection) @@ -157,13 +164,3 @@ bool MCSectionELF::UseCodeAlign() const { bool MCSectionELF::isVirtualSection() const { return getType() == ELF::SHT_NOBITS; } - -unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) { - if (Kind.isMergeable1ByteCString()) return 1; - if (Kind.isMergeable2ByteCString()) return 2; - if (Kind.isMergeable4ByteCString()) return 4; - if (Kind.isMergeableConst4()) return 4; - if (Kind.isMergeableConst8()) return 8; - if (Kind.isMergeableConst16()) return 16; - return 0; -} diff --git a/lib/MC/MCSubtargetInfo.cpp b/lib/MC/MCSubtargetInfo.cpp index b8e42bd..ca3894b 100644 --- a/lib/MC/MCSubtargetInfo.cpp +++ b/lib/MC/MCSubtargetInfo.cpp @@ -35,7 +35,7 @@ MCSubtargetInfo::InitCPUSchedModel(StringRef CPU) { } void -MCSubtargetInfo::InitMCSubtargetInfo(StringRef TT, StringRef CPU, StringRef FS, +MCSubtargetInfo::InitMCSubtargetInfo(StringRef TT, StringRef C, StringRef FS, ArrayRef<SubtargetFeatureKV> PF, ArrayRef<SubtargetFeatureKV> PD, const SubtargetInfoKV *ProcSched, @@ -46,6 +46,7 @@ MCSubtargetInfo::InitMCSubtargetInfo(StringRef TT, StringRef CPU, StringRef FS, const unsigned *OC, const unsigned *FP) { TargetTriple = TT; + CPU = C; ProcFeatures = PF; ProcDesc = PD; ProcSchedModels = ProcSched; diff --git a/lib/MC/MCTargetOptions.cpp b/lib/MC/MCTargetOptions.cpp index 3093ba2..1258d9e 100644 --- a/lib/MC/MCTargetOptions.cpp +++ b/lib/MC/MCTargetOptions.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/StringRef.h" #include "llvm/MC/MCTargetOptions.h" namespace llvm { @@ -15,6 +16,10 @@ MCTargetOptions::MCTargetOptions() : SanitizeAddress(false), MCRelaxAll(false), MCNoExecStack(false), MCFatalWarnings(false), MCSaveTempLabels(false), MCUseDwarfDirectory(false), ShowMCEncoding(false), ShowMCInst(false), - AsmVerbose(false), DwarfVersion(0) {} + AsmVerbose(false), DwarfVersion(0), ABIName() {} + +StringRef MCTargetOptions::getABIName() const { + return ABIName; +} } // end namespace llvm diff --git a/lib/MC/MCValue.cpp b/lib/MC/MCValue.cpp index 9dfc56e..5512e03 100644 --- a/lib/MC/MCValue.cpp +++ b/lib/MC/MCValue.cpp @@ -15,7 +15,7 @@ using namespace llvm; -void MCValue::print(raw_ostream &OS, const MCAsmInfo *MAI) const { +void MCValue::print(raw_ostream &OS) const { if (isAbsolute()) { OS << getConstant(); return; @@ -39,7 +39,7 @@ void MCValue::print(raw_ostream &OS, const MCAsmInfo *MAI) const { #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCValue::dump() const { - print(dbgs(), nullptr); + print(dbgs()); } #endif diff --git a/lib/MC/MCWinEH.cpp b/lib/MC/MCWinEH.cpp index f0c354f..47eaf0f 100644 --- a/lib/MC/MCWinEH.cpp +++ b/lib/MC/MCWinEH.cpp @@ -17,52 +17,45 @@ namespace llvm { namespace WinEH { -static StringRef getSectionSuffix(const MCSymbol *Function) { - if (!Function || !Function->isInSection()) - return ""; - - const MCSection *FunctionSection = &Function->getSection(); - if (const auto Section = dyn_cast<MCSectionCOFF>(FunctionSection)) { - StringRef Name = Section->getSectionName(); - size_t Dollar = Name.find('$'); - size_t Dot = Name.find('.', 1); - - if (Dollar == StringRef::npos && Dot == StringRef::npos) - return ""; - if (Dot == StringRef::npos) - return Name.substr(Dollar); - if (Dollar == StringRef::npos || Dot < Dollar) - return Name.substr(Dot); - - return Name.substr(Dollar); - } - - return ""; -} +/// We can't have one section for all .pdata or .xdata because the Microsoft +/// linker seems to want all code relocations to refer to the same object file +/// section. If the code described is comdat, create a new comdat section +/// associated with that comdat. If the code described is not in the main .text +/// section, make a new section for it. Otherwise use the main unwind info +/// section. static const MCSection *getUnwindInfoSection( StringRef SecName, const MCSectionCOFF *UnwindSec, const MCSymbol *Function, MCContext &Context) { - // If Function is in a COMDAT, get or create an unwind info section in that - // COMDAT group. if (Function && Function->isInSection()) { + // If Function is in a COMDAT, get or create an unwind info section in that + // COMDAT group. const MCSectionCOFF *FunctionSection = cast<MCSectionCOFF>(&Function->getSection()); if (FunctionSection->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { return Context.getAssociativeCOFFSection( UnwindSec, FunctionSection->getCOMDATSymbol()); } + + // If Function is in a section other than .text, create a new .pdata section. + // Otherwise use the plain .pdata section. + if (const auto *Section = dyn_cast<MCSectionCOFF>(FunctionSection)) { + StringRef CodeSecName = Section->getSectionName(); + if (CodeSecName == ".text") + return UnwindSec; + + if (CodeSecName.startswith(".text$")) + CodeSecName = CodeSecName.substr(6); + + return Context.getCOFFSection( + (SecName + Twine('$') + CodeSecName).str(), + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, + SectionKind::getDataRel()); + } } - // If Function is in a section other than .text, create a new .pdata section. - // Otherwise use the plain .pdata section. - StringRef Suffix = getSectionSuffix(Function); - if (Suffix.empty()) - return UnwindSec; - return Context.getCOFFSection((SecName + Suffix).str(), - COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getDataRel()); + return UnwindSec; + } const MCSection *UnwindEmitter::getPDataSection(const MCSymbol *Function, diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index 577c4b7..588d424 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -418,7 +418,7 @@ void MachObjectWriter::WriteLinkeditLoadCommand(uint32_t Type, static unsigned ComputeLinkerOptionsLoadCommandSize( const std::vector<std::string> &Options, bool is64Bit) { - unsigned Size = sizeof(MachO::linker_options_command); + unsigned Size = sizeof(MachO::linker_option_command); for (unsigned i = 0, e = Options.size(); i != e; ++i) Size += Options[i].size() + 1; return RoundUpToAlignment(Size, is64Bit ? 8 : 4); @@ -431,10 +431,10 @@ void MachObjectWriter::WriteLinkerOptionsLoadCommand( uint64_t Start = OS.tell(); (void) Start; - Write32(MachO::LC_LINKER_OPTIONS); + Write32(MachO::LC_LINKER_OPTION); Write32(Size); Write32(Options.size()); - uint64_t BytesWritten = sizeof(MachO::linker_options_command); + uint64_t BytesWritten = sizeof(MachO::linker_option_command); for (unsigned i = 0, e = Options.size(); i != e; ++i) { // Write each string, including the null byte. const std::string &Option = Options[i]; @@ -448,14 +448,11 @@ void MachObjectWriter::WriteLinkerOptionsLoadCommand( assert(OS.tell() - Start == Size); } - -void MachObjectWriter::RecordRelocation(const MCAssembler &Asm, +void MachObjectWriter::RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, - const MCFixup &Fixup, - MCValue Target, - bool &IsPCRel, - uint64_t &FixedValue) { + const MCFixup &Fixup, MCValue Target, + bool &IsPCRel, uint64_t &FixedValue) { TargetObjectWriter->RecordRelocation(this, Asm, Layout, Fragment, Fixup, Target, FixedValue); } @@ -616,6 +613,22 @@ void MachObjectWriter::ComputeSymbolTable( ExternalSymbolData[i].SymbolData->setIndex(Index++); for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) UndefinedSymbolData[i].SymbolData->setIndex(Index++); + + for (const MCSectionData &SD : Asm) { + std::vector<RelAndSymbol> &Relocs = Relocations[&SD]; + for (RelAndSymbol &Rel : Relocs) { + if (!Rel.Sym) + continue; + + // Set the Index and the IsExtern bit. + unsigned Index = Rel.Sym->getIndex(); + assert(isInt<24>(Index)); + if (IsLittleEndian) + Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (-1 << 24)) | Index | (1 << 27); + else + Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4); + } + } } void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm, @@ -662,10 +675,6 @@ void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, // Mark symbol difference expressions in variables (from .set or = directives) // as absolute. markAbsoluteVariableSymbols(Asm, Layout); - - // Compute symbol table information and bind symbol indices. - ComputeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData, - UndefinedSymbolData); } bool MachObjectWriter:: @@ -749,6 +758,10 @@ IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, void MachObjectWriter::WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) { + // Compute symbol table information and bind symbol indices. + ComputeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData, + UndefinedSymbolData); + unsigned NumSections = Asm.size(); const MCAssembler::VersionMinInfoType &VersionInfo = Layout.getAssembler().getVersionMinInfo(); @@ -839,7 +852,7 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm, uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) { - std::vector<MachO::any_relocation_info> &Relocs = Relocations[it]; + std::vector<RelAndSymbol> &Relocs = Relocations[it]; unsigned NumRelocs = Relocs.size(); uint64_t SectionStart = SectionDataStart + getSectionAddress(it); WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs); @@ -933,10 +946,10 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm, ie = Asm.end(); it != ie; ++it) { // Write the section relocation entries, in reverse order to match 'as' // (approximately, the exact algorithm is more complicated than this). - std::vector<MachO::any_relocation_info> &Relocs = Relocations[it]; + std::vector<RelAndSymbol> &Relocs = Relocations[it]; for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { - Write32(Relocs[e - i - 1].r_word0); - Write32(Relocs[e - i - 1].r_word1); + Write32(Relocs[e - i - 1].MRE.r_word0); + Write32(Relocs[e - i - 1].MRE.r_word1); } } diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index 1046e04..c519a9d 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -13,9 +13,9 @@ #include "llvm/MC/MCWinCOFFObjectWriter.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" -#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCAsmLayout.h" #include "llvm/MC/MCAssembler.h" @@ -175,7 +175,7 @@ public: const MCFragment &FB, bool InSet, bool IsPCRel) const override; - void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, + void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) override; @@ -266,12 +266,12 @@ COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) { return createCOFFEntity<COFFSymbol>(Name, Symbols); } -COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol * Symbol){ +COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) { symbol_map::iterator i = SymbolMap.find(Symbol); if (i != SymbolMap.end()) return i->second; - COFFSymbol *RetSymbol - = createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols); + COFFSymbol *RetSymbol = + createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols); SymbolMap[Symbol] = RetSymbol; return RetSymbol; } @@ -640,7 +640,7 @@ void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout) { // "Define" each section & symbol. This creates section & symbol // entries in the staging area. - for (const auto & Section : Asm) + for (const auto &Section : Asm) DefineSection(Section); for (MCSymbolData &SD : Asm.symbols()) @@ -661,13 +661,9 @@ bool WinCOFFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl( InSet, IsPCRel); } -void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm, - const MCAsmLayout &Layout, - const MCFragment *Fragment, - const MCFixup &Fixup, - MCValue Target, - bool &IsPCRel, - uint64_t &FixedValue) { +void WinCOFFObjectWriter::RecordRelocation( + MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, + const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) { assert(Target.getSymA() && "Relocation must reference a symbol!"); const MCSymbol &Symbol = Target.getSymA()->getSymbol(); @@ -710,17 +706,22 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm, CrossSection = &Symbol.getSection() != &B->getSection(); // Offset of the symbol in the section - int64_t a = Layout.getSymbolOffset(&B_SD); - - // Offset of the relocation in the section - int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); + int64_t OffsetOfB = Layout.getSymbolOffset(&B_SD); - FixedValue = b - a; // In the case where we have SymbA and SymB, we just need to store the delta // between the two symbols. Update FixedValue to account for the delta, and // skip recording the relocation. - if (!CrossSection) + if (!CrossSection) { + int64_t OffsetOfA = Layout.getSymbolOffset(&A_SD); + FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant(); return; + } + + // Offset of the relocation in the section + int64_t OffsetOfRelocation = + Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); + + FixedValue = OffsetOfRelocation - OffsetOfB; } else { FixedValue = Target.getConstant(); } @@ -741,8 +742,9 @@ void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm, ++Reloc.Symb->Relocations; Reloc.Data.VirtualAddress += Fixup.getOffset(); - Reloc.Data.Type = TargetObjectWriter->getRelocType(Target, Fixup, - CrossSection); + Reloc.Data.Type = + TargetObjectWriter->getRelocType(Target, Fixup, CrossSection, + Asm.getBackend()); // FIXME: Can anyone explain what this does other than adjust for the size // of the offset? @@ -835,7 +837,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, unsigned Offset = 0; unsigned Length = FI->size(); - for (auto & Aux : file->Aux) { + for (auto &Aux : file->Aux) { Aux.AuxType = ATFile; if (Length > SymbolSize) { @@ -881,7 +883,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, SetSymbolName(*S); // Fixup weak external references. - for (auto & Symbol : Symbols) { + for (auto &Symbol : Symbols) { if (Symbol->Other) { assert(Symbol->Index != -1); assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!"); @@ -892,7 +894,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, } // Fixup associative COMDAT sections. - for (auto & Section : Sections) { + for (auto &Section : Sections) { if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) continue; @@ -928,7 +930,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, offset += COFF::Header16Size; offset += COFF::SectionSize * Header.NumberOfSections; - for (const auto & Section : Asm) { + for (const auto &Section : Asm) { COFFSection *Sec = SectionMap[&Section.getSection()]; if (Sec->Number == -1) @@ -937,6 +939,8 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section); if (IsPhysicalSection(Sec)) { + // Align the section data to a four byte boundary. + offset = RoundUpToAlignment(offset, 4); Sec->Header.PointerToRawData = offset; offset += Sec->Header.SizeOfRawData; @@ -961,7 +965,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, offset += COFF::RelocationSize * Sec->Relocations.size(); - for (auto & Relocation : Sec->Relocations) { + for (auto &Relocation : Sec->Relocations) { assert(Relocation.Symb->Index != -1); Relocation.Data.SymbolTableIndex = Relocation.Symb->Index; } @@ -991,7 +995,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, sections::iterator i, ie; MCAssembler::const_iterator j, je; - for (auto & Section : Sections) { + for (auto &Section : Sections) { if (Section->Number != -1) { if (Section->Relocations.size() >= 0xffff) Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL; @@ -1007,9 +1011,15 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, continue; if ((*i)->Header.PointerToRawData != 0) { - assert(OS.tell() == (*i)->Header.PointerToRawData && + assert(OS.tell() <= (*i)->Header.PointerToRawData && "Section::PointerToRawData is insane!"); + unsigned SectionDataPadding = (*i)->Header.PointerToRawData - OS.tell(); + assert(SectionDataPadding < 4 && + "Should only need at most three bytes of padding!"); + + WriteZeros(SectionDataPadding); + Asm.writeSectionData(j, Layout); } @@ -1027,7 +1037,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, WriteRelocation(r); } - for (const auto & Relocation : (*i)->Relocations) + for (const auto &Relocation : (*i)->Relocations) WriteRelocation(Relocation.Data); } else assert((*i)->Header.PointerToRelocations == 0 && @@ -1038,7 +1048,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, assert(OS.tell() == Header.PointerToSymbolTable && "Header::PointerToSymbolTable is insane!"); - for (auto & Symbol : Symbols) + for (auto &Symbol : Symbols) if (Symbol->Index != -1) WriteSymbol(*Symbol); diff --git a/lib/MC/WinCOFFStreamer.cpp b/lib/MC/WinCOFFStreamer.cpp index 6a8054d..41a3da7 100644 --- a/lib/MC/WinCOFFStreamer.cpp +++ b/lib/MC/WinCOFFStreamer.cpp @@ -12,7 +12,6 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/StringExtras.h" -#include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmLayout.h" #include "llvm/MC/MCAssembler.h" @@ -23,6 +22,7 @@ #include "llvm/MC/MCObjectStreamer.h" #include "llvm/MC/MCSection.h" #include "llvm/MC/MCSectionCOFF.h" +#include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCValue.h" #include "llvm/MC/MCWinCOFFStreamer.h" |