Index: lib/ObjectYAML/ELFEmitter.cpp =================================================================== --- lib/ObjectYAML/ELFEmitter.cpp +++ lib/ObjectYAML/ELFEmitter.cpp @@ -114,12 +114,20 @@ NameToIdxMap DynSymN2I; ELFYAML::Object &Doc; - bool buildSectionIndex(); - bool buildSymbolIndexes(); + bool HasError = false; + + std::vector toELFSymbols(ArrayRef Symbols, + const StringTableBuilder &Strtab); + unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = ""); + unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic); + void reportError(const Twine &Msg); + + void buildSectionIndex(); + void buildSymbolIndexes(); void initProgramHeaders(std::vector &PHeaders); bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header, StringRef SecName, ELFYAML::Section *YAMLSec); - bool initSectionHeaders(std::vector &SHeaders, + void initSectionHeaders(std::vector &SHeaders, ContiguousBlobAccumulator &CBA); void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType, ContiguousBlobAccumulator &CBA, @@ -132,34 +140,33 @@ std::vector &SHeaders); void finalizeStrings(); void writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::SymverSection &Section, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::VerneedSection &Section, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::VerdefSection &Section, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::MipsABIFlags &Section, ContiguousBlobAccumulator &CBA); - bool writeSectionContent(Elf_Shdr &SHeader, + void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::DynamicSection &Section, ContiguousBlobAccumulator &CBA); ELFState(ELFYAML::Object &D); - public: static int writeELF(raw_ostream &OS, ELFYAML::Object &Doc); }; @@ -258,14 +265,36 @@ } } -static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName, - StringRef IndexSrc, unsigned &IndexDest) { - if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) { - WithColor::error() << "Unknown section referenced: '" << IndexSrc - << "' at YAML section '" << SecName << "'.\n"; - return false; +template +unsigned ELFState::toSectionIndex(StringRef S, StringRef LocSec, + StringRef LocSym) { + unsigned Index; + if (SN2I.lookup(S, Index) || to_integer(S, Index)) + return Index; + + assert(LocSec.empty() || LocSym.empty()); + if (!LocSym.empty()) + reportError("unknown section referenced: '" + S + "' by YAML symbol '" + + LocSym + "'"); + else + reportError("unknown section referenced: '" + S + "' by YAML section '" + + LocSec + "'"); + return 0; +} + +template +unsigned ELFState::toSymbolIndex(StringRef S, StringRef LocSec, + bool IsDynamic) { + const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I; + unsigned Index; + // Here we try to look up S in the symbol table. If it is not there, + // treat its value as a symbol index. + if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) { + reportError("unknown symbol referenced: '" + S + "' by YAML section '" + + LocSec + "'"); + return 0; } - return true; + return Index; } template @@ -310,7 +339,7 @@ } template -bool ELFState::initSectionHeaders(std::vector &SHeaders, +void ELFState::initSectionHeaders(std::vector &SHeaders, ContiguousBlobAccumulator &CBA) { // Ensure SHN_UNDEF entry is present. An all-zero section header is a // valid SHN_UNDEF entry since SHT_NULL == 0. @@ -340,12 +369,8 @@ SHeader.sh_addr = Sec->Address; SHeader.sh_addralign = Sec->AddressAlign; - if (!Sec->Link.empty()) { - unsigned Index; - if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index)) - return false; - SHeader.sh_link = Index; - } + if (!Sec->Link.empty()) + SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name); if (I == 0) { if (auto RawSec = dyn_cast(Sec)) { @@ -358,20 +383,15 @@ if (Sec->EntSize) SHeader.sh_entsize = *Sec->EntSize; } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { SHeader.sh_entsize = 0; SHeader.sh_size = S->Size; @@ -379,19 +399,16 @@ // so just to setup the section offset. CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; + writeSectionContent(SHeader, *S, CBA); } else if (auto S = dyn_cast(Sec)) { - if (!writeSectionContent(SHeader, *S, CBA)) - return false; - } else + writeSectionContent(SHeader, *S, CBA); + } else { llvm_unreachable("Unknown section type"); + } // Override the fields if requested. if (Sec) { @@ -403,8 +420,6 @@ SHeader.sh_size = *Sec->ShSize; } } - - return true; } static size_t findFirstNonGlobal(ArrayRef Symbols) { @@ -430,11 +445,9 @@ } template -static std::vector -toELFSymbols(NameToIdxMap &SN2I, ArrayRef Symbols, - const StringTableBuilder &Strtab) { - using Elf_Sym = typename ELFT::Sym; - +std::vector +ELFState::toELFSymbols(ArrayRef Symbols, + const StringTableBuilder &Strtab) { std::vector Ret; Ret.resize(Symbols.size() + 1); @@ -451,18 +464,11 @@ Symbol.st_name = Strtab.getOffset(dropUniqueSuffix(Sym.Name)); Symbol.setBindingAndType(Sym.Binding, Sym.Type); - if (!Sym.Section.empty()) { - unsigned Index; - if (!SN2I.lookup(Sym.Section, Index)) { - WithColor::error() << "Unknown section referenced: '" << Sym.Section - << "' by YAML symbol " << Sym.Name << ".\n"; - exit(1); - } - Symbol.st_shndx = Index; - } else if (Sym.Index) { + if (!Sym.Section.empty()) + Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name); + else if (Sym.Index) Symbol.st_shndx = *Sym.Index; - } - // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier. + Symbol.st_value = Sym.Value; Symbol.st_other = Sym.Other ? *Sym.Other : 0; Symbol.st_size = Sym.Size; @@ -484,18 +490,14 @@ dyn_cast_or_null(YAMLSec); if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) { if (RawSec->Content) - WithColor::error() << "Cannot specify both `Content` and " + - (IsStatic ? Twine("`Symbols`") - : Twine("`DynamicSymbols`")) + - " for symbol table section '" - << RawSec->Name << "'.\n"; + reportError("cannot specify both `Content` and " + + (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) + + " for symbol table section '" + RawSec->Name + "'"); if (RawSec->Size) - WithColor::error() << "Cannot specify both `Size` and " + - (IsStatic ? Twine("`Symbols`") - : Twine("`DynamicSymbols`")) + - " for symbol table section '" - << RawSec->Name << "'.\n"; - exit(1); + reportError("cannot specify both `Size` and " + + (IsStatic ? Twine("`Symbols`") : Twine("`DynamicSymbols`")) + + " for symbol table section '" + RawSec->Name + "'"); + return; } zero(SHeader); @@ -509,10 +511,7 @@ if (RawSec && !RawSec->Link.empty()) { // If the Link field is explicitly defined in the document, // we should use it. - unsigned Index; - if (!convertSectionIndex(SN2I, RawSec->Name, RawSec->Link, Index)) - return; - SHeader.sh_link = Index; + SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name); } else { // When we describe the .dynsym section in the document explicitly, it is // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not @@ -549,7 +548,7 @@ } std::vector Syms = - toELFSymbols(SN2I, Symbols, IsStatic ? DotStrtab : DotDynstr); + toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr); writeArrayData(OS, makeArrayRef(Syms)); SHeader.sh_size = arrayDataSize(makeArrayRef(Syms)); } @@ -592,6 +591,11 @@ SHeader.sh_addr = YAMLSec->Address; } +template void ELFState::reportError(const Twine &Msg) { + WithColor::error() << Msg << "\n"; + HasError = true; +} + template void ELFState::setProgramHeaderLayout(std::vector &PHeaders, std::vector &SHeaders) { @@ -603,9 +607,9 @@ for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) { unsigned Index; if (!SN2I.lookup(SecName.Section, Index)) { - WithColor::error() << "Unknown section referenced: '" << SecName.Section - << "' by program header.\n"; - exit(1); + reportError("unknown section referenced: '" + SecName.Section + + "' by program header"); + continue; } Sections.push_back(&SHeaders[Index]); } @@ -668,7 +672,7 @@ } template -bool ELFState::writeSectionContent( +void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA) { raw_ostream &OS = @@ -684,8 +688,6 @@ if (Section.Info) SHeader.sh_info = *Section.Info; - - return true; } static bool isMips64EL(const ELFYAML::Object &Doc) { @@ -695,7 +697,7 @@ } template -bool ELFState::writeSectionContent( +void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section, ContiguousBlobAccumulator &CBA) { assert((Section.Type == llvm::ELF::SHT_REL || @@ -710,26 +712,14 @@ if (Section.Link.empty()) SHeader.sh_link = SN2I.get(".symtab"); - unsigned Index = 0; - if (!Section.RelocatableSec.empty() && - !convertSectionIndex(SN2I, Section.Name, Section.RelocatableSec, Index)) - return false; - SHeader.sh_info = Index; + if (!Section.RelocatableSec.empty()) + SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name); auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); - - const NameToIdxMap &SymMap = Section.Link == ".dynsym" ? DynSymN2I : SymN2I; for (const auto &Rel : Section.Relocations) { - unsigned SymIdx = 0; - // If a relocation references a symbol, try to look one up in the symbol - // table. If it is not there, treat the value as a symbol index. - if (Rel.Symbol && !SymMap.lookup(*Rel.Symbol, SymIdx) && - !to_integer(*Rel.Symbol, SymIdx)) { - WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol - << "' at YAML section '" << Section.Name << "'.\n"; - return false; - } - + unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, + Section.Link == ".dynsym") + : 0; if (IsRela) { Elf_Rela REntry; zero(REntry); @@ -745,11 +735,10 @@ OS.write((const char *)&REntry, sizeof(REntry)); } } - return true; } template -bool ELFState::writeSectionContent( +void ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx, ContiguousBlobAccumulator &CBA) { raw_ostream &OS = @@ -760,11 +749,10 @@ SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4; SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize; - return true; } template -bool ELFState::writeSectionContent(Elf_Shdr &SHeader, +void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Section, ContiguousBlobAccumulator &CBA) { assert(Section.Type == llvm::ELF::SHT_GROUP && @@ -772,15 +760,8 @@ SHeader.sh_entsize = 4; SHeader.sh_size = SHeader.sh_entsize * Section.Members.size(); - - unsigned SymIdx; - if (!SymN2I.lookup(Section.Signature, SymIdx) && - !to_integer(Section.Signature, SymIdx)) { - WithColor::error() << "Unknown symbol referenced: '" << Section.Signature - << "' at YAML section '" << Section.Name << "'.\n"; - return false; - } - SHeader.sh_info = SymIdx; + SHeader.sh_info = + toSymbolIndex(Section.Signature, Section.Name, /*IsDynamic=*/false); raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign); @@ -789,16 +770,14 @@ unsigned int SectionIndex = 0; if (Member.sectionNameOrType == "GRP_COMDAT") SectionIndex = llvm::ELF::GRP_COMDAT; - else if (!convertSectionIndex(SN2I, Section.Name, Member.sectionNameOrType, - SectionIndex)) - return false; + else + SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name); support::endian::write(OS, SectionIndex, ELFT::TargetEndianness); } - return true; } template -bool ELFState::writeSectionContent(Elf_Shdr &SHeader, +void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::SymverSection &Section, ContiguousBlobAccumulator &CBA) { raw_ostream &OS = @@ -808,11 +787,10 @@ SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2; SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize; - return true; } template -bool ELFState::writeSectionContent(Elf_Shdr &SHeader, +void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::VerdefSection &Section, ContiguousBlobAccumulator &CBA) { typedef typename ELFT::Verdef Elf_Verdef; @@ -852,12 +830,10 @@ SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Verdef) + AuxCnt * sizeof(Elf_Verdaux); SHeader.sh_info = Section.Info; - - return true; } template -bool ELFState::writeSectionContent(Elf_Shdr &SHeader, +void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::VerneedSection &Section, ContiguousBlobAccumulator &CBA) { typedef typename ELFT::Verneed Elf_Verneed; @@ -900,12 +876,10 @@ SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) + AuxCnt * sizeof(Elf_Vernaux); SHeader.sh_info = Section.Info; - - return true; } template -bool ELFState::writeSectionContent(Elf_Shdr &SHeader, +void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::MipsABIFlags &Section, ContiguousBlobAccumulator &CBA) { assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS && @@ -929,12 +903,10 @@ Flags.flags1 = Section.Flags1; Flags.flags2 = Section.Flags2; OS.write((const char *)&Flags, sizeof(Flags)); - - return true; } template -bool ELFState::writeSectionContent(Elf_Shdr &SHeader, +void ELFState::writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::DynamicSection &Section, ContiguousBlobAccumulator &CBA) { typedef typename ELFT::uint uintX_t; @@ -942,13 +914,10 @@ assert(Section.Type == llvm::ELF::SHT_DYNAMIC && "Section type is not SHT_DYNAMIC"); - if (!Section.Entries.empty() && Section.Content) { - WithColor::error() - << "Cannot specify both raw content and explicit entries " - "for dynamic section '" - << Section.Name << "'.\n"; - return false; - } + if (!Section.Entries.empty() && Section.Content) + reportError("cannot specify both raw content and explicit entries " + "for dynamic section '" + + Section.Name + "'"); if (Section.Content) SHeader.sh_size = Section.Content->binary_size(); @@ -967,42 +936,34 @@ } if (Section.Content) Section.Content->writeAsBinary(OS); - - return true; } -template bool ELFState::buildSectionIndex() { +template void ELFState::buildSectionIndex() { for (unsigned I = 0, E = Doc.Sections.size(); I != E; ++I) { StringRef Name = Doc.Sections[I]->Name; if (Name.empty()) continue; DotShStrtab.add(dropUniqueSuffix(Name)); - if (!SN2I.addName(Name, I)) { - WithColor::error() << "Repeated section name: '" << Name - << "' at YAML section number " << I << ".\n"; - return false; - } + if (!SN2I.addName(Name, I)) + reportError("repeated section name: '" + Name + + "' at YAML section number " + Twine(I)); } DotShStrtab.finalize(); - return true; } -static bool buildSymbolsMap(ArrayRef V, NameToIdxMap &Map) { - for (size_t I = 0, S = V.size(); I < S; ++I) { - const ELFYAML::Symbol &Sym = V[I]; - if (Sym.Name.empty() || Map.addName(Sym.Name, I + 1)) - continue; - WithColor::error() << "Repeated symbol name: '" << Sym.Name << "'.\n"; - return false; - } - return true; -} +template void ELFState::buildSymbolIndexes() { + auto Build = [this](ArrayRef V, NameToIdxMap &Map) { + for (size_t I = 0, S = V.size(); I < S; ++I) { + const ELFYAML::Symbol &Sym = V[I]; + if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1)) + reportError("repeated symbol name: '" + Sym.Name + "'"); + } + }; -template bool ELFState::buildSymbolIndexes() { - return buildSymbolsMap(Doc.Symbols, SymN2I) && - buildSymbolsMap(Doc.DynamicSymbols, DynSymN2I); + Build(Doc.Symbols, SymN2I); + Build(Doc.DynamicSymbols, DynSymN2I); } template void ELFState::finalizeStrings() { @@ -1043,8 +1004,8 @@ // sections that might want to use them. State.finalizeStrings(); - if (!State.buildSectionIndex() || !State.buildSymbolIndexes()) - return 1; + State.buildSectionIndex(); + State.buildSymbolIndexes(); std::vector PHeaders; State.initProgramHeaders(PHeaders); @@ -1056,12 +1017,14 @@ ContiguousBlobAccumulator CBA(SectionContentBeginOffset); std::vector SHeaders; - if (!State.initSectionHeaders(SHeaders, CBA)) - return 1; + State.initSectionHeaders(SHeaders, CBA); // Now we can decide segment offsets State.setProgramHeaderLayout(PHeaders, SHeaders); + if (State.HasError) + return 1; + State.writeELFHeader(CBA, OS); writeArrayData(OS, makeArrayRef(PHeaders)); CBA.writeBlobToStream(OS); Index: test/tools/yaml2obj/duplicate-section-names.test =================================================================== --- test/tools/yaml2obj/duplicate-section-names.test +++ test/tools/yaml2obj/duplicate-section-names.test @@ -29,7 +29,8 @@ ## sections with equal names and suffixes. # RUN: not yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --check-prefix=CASE2 -# CASE2: error: Repeated section name: '.foo [1]' at YAML section number 2. +# CASE2: error: repeated section name: '.foo [1]' at YAML section number 2 +# CASE2: error: repeated section name: '.foo [1]' at YAML section number 3 --- !ELF FileHeader: @@ -42,13 +43,15 @@ Type: SHT_PROGBITS - Name: '.foo [1]' Type: SHT_PROGBITS + - Name: '.foo [1]' + Type: SHT_PROGBITS ## Check that yaml2obj reports an error in case we have ## symbols without suffixes in the names and their ## names are equal. # RUN: not yaml2obj --docnum=3 %s 2>&1 | FileCheck %s --check-prefix=CASE3 -# CASE3: error: Repeated section name: '.foo' at YAML section number 2. +# CASE3: error: repeated section name: '.foo' at YAML section number 2 --- !ELF FileHeader: Index: test/tools/yaml2obj/duplicate-symbol-names.test =================================================================== --- test/tools/yaml2obj/duplicate-symbol-names.test +++ test/tools/yaml2obj/duplicate-symbol-names.test @@ -21,7 +21,8 @@ ## symbols with equal names and suffixes. # RUN: not yaml2obj --docnum=2 %s 2>&1| FileCheck %s --check-prefix=CASE2 -# CASE2: error: Repeated symbol name: 'localfoo [1]'. +# CASE2: error: repeated symbol name: 'localfoo [1]' +# CASE2: error: repeated symbol name: 'localfoo [1]' --- !ELF FileHeader: @@ -32,13 +33,15 @@ Symbols: - Name: 'localfoo [1]' - Name: 'localfoo [1]' + - Name: 'localfoo [1]' ## Check that yaml2obj reports an error when we have ## symbols without suffixes in the names and their ## names are equal. # RUN: not yaml2obj --docnum=3 %s 2>&1| FileCheck %s --check-prefix=CASE3 -# CASE3: error: Repeated symbol name: 'localfoo'. +# CASE3: error: repeated symbol name: 'localfoo' +# CASE3: error: repeated symbol name: 'localfoo' --- !ELF FileHeader: @@ -47,10 +50,9 @@ Type: ET_REL Machine: EM_X86_64 Symbols: - - Name: localfoo - Section: .text.foo.1 - - Name: localfoo - Section: .text.foo.2 + - Name: localfoo + - Name: localfoo + - Name: localfoo ## Check that yaml2obj can produce correct relocations that ## reference symbols with name suffixes. Index: test/tools/yaml2obj/dynamic-section-raw-content.yaml =================================================================== --- test/tools/yaml2obj/dynamic-section-raw-content.yaml +++ test/tools/yaml2obj/dynamic-section-raw-content.yaml @@ -15,9 +15,6 @@ # RAW: Hex dump of section '.dynamic': # RAW-NEXT: 0x00000000 01234567 89012345 67890000 00000000 {{.*}} -# RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --check-prefix=ERR -# ERR: Cannot specify both raw content and explicit entries for dynamic section '.dynamic'. - --- !ELF FileHeader: Class: ELFCLASS64 @@ -29,6 +26,11 @@ Type: SHT_DYNAMIC Content: "01234567890123456789000000000000" +# RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --check-prefix=ERR + +# ERR: cannot specify both raw content and explicit entries for dynamic section '.dynamic1' +# ERR: cannot specify both raw content and explicit entries for dynamic section '.dynamic2' + --- !ELF FileHeader: Class: ELFCLASS64 @@ -36,7 +38,13 @@ Type: ET_EXEC Machine: EM_X86_64 Sections: - - Name: .dynamic + - Name: .dynamic1 + Type: SHT_DYNAMIC + Content: "0123456789" + Entries: + - Tag: DT_STRSZ + Value: 0 + - Name: .dynamic2 Type: SHT_DYNAMIC Content: "0123456789" Entries: Index: test/tools/yaml2obj/dynamic-symbols.yaml =================================================================== --- test/tools/yaml2obj/dynamic-symbols.yaml +++ test/tools/yaml2obj/dynamic-symbols.yaml @@ -1,9 +1,10 @@ -# Ensures that implicitly added sections can be ordered within Sections. -# RUN: yaml2obj %s -o %t -# RUN: llvm-readobj --sections %t | FileCheck %s --check-prefix=SECTION +## Ensures that implicitly added sections can be ordered within Sections. + +# RUN: yaml2obj --docnum=1 %s -o %t1 +# RUN: llvm-readobj --sections %t1 | FileCheck %s --check-prefix=SECTION # RUN: llvm-nm --dynamic %t | FileCheck %s --check-prefix=SYMBOL -!ELF +--- !ELF FileHeader: Class: ELFCLASS64 Data: ELFDATA2LSB @@ -39,3 +40,57 @@ # SYMBOL-DAG: d dynlocal # SYMBOL-DAG: D dynglobal # SYMBOL-DAG: V dynweak + +## Check we can use numeric values to refer to sections. + +# RUN: yaml2obj --docnum=2 %s -o %t2 +# RUN: not llvm-readobj --dyn-symbols %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=NUM + +# NUM: Name: foo +# NUM: Section: +# NUM-SAME: .data (0x1) + +# NUM: Name: bar +# NUM: Section: +# NUM-SAME: .symtab (0x2) + +# NUM: error: '[[FILE]]': invalid section index: 255 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .data + Type: SHT_PROGBITS +DynamicSymbols: + - Name: foo + Section: 1 + - Name: bar + Section: 2 + - Name: zed + Section: 0xff + +## Check we report errors when unknown sections are referenced by dynamic symbols. + +# RUN: not yaml2obj --docnum=3 %s 2>&1 | FileCheck -DFILE=%t3 %s --check-prefix=ERR + +# ERR: error: unknown section referenced: '.sec1' by YAML symbol 'foo' +# ERR: error: unknown section referenced: '.sec2' by YAML symbol 'bar' + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .data + Type: SHT_PROGBITS +DynamicSymbols: + - Name: foo + Section: .sec1 + - Name: bar + Section: .sec2 Index: test/tools/yaml2obj/dynsymtab-implicit-sections-size-content.yaml =================================================================== --- test/tools/yaml2obj/dynsymtab-implicit-sections-size-content.yaml +++ test/tools/yaml2obj/dynsymtab-implicit-sections-size-content.yaml @@ -27,10 +27,12 @@ - Name: foo Binding: STB_GLOBAL -## Specifying both `Size` and symbols at the same time is not allowed. -# RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --check-prefix=CASE2 +## Specifying both `Size` and symbols at the same time is not allowed for .dynsym. -# CASE2: error: Cannot specify both `Size` and `DynamicSymbols` for symbol table section '.dynsym'. +# RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --implicit-check-not=error --check-prefix=CASE2 + +# CASE2: error: cannot specify both `Size` and `DynamicSymbols` for symbol table section '.dynsym' +# CASE2: error: yaml2obj failed --- !ELF FileHeader: @@ -42,14 +44,19 @@ - Name: .dynsym Type: SHT_DYNSYM Size: 0x100 + - Name: .dynsym2 + Type: SHT_DYNSYM + Size: 0x100 DynamicSymbols: - Name: foo Binding: STB_GLOBAL -## Specifying both `Content` and symbols at the same time is not allowed. -# RUN: not yaml2obj --docnum=3 %s -o %t3 2>&1 | FileCheck %s --check-prefix=CASE3 +## Specifying both `Content` and symbols at the same time is not allowed for .dynsym. -# CASE3: error: Cannot specify both `Content` and `DynamicSymbols` for symbol table section '.dynsym'. +# RUN: not yaml2obj --docnum=3 %s -o %t3 2>&1 | FileCheck %s --implicit-check-not=error --check-prefix=CASE3 + +# CASE3: error: cannot specify both `Content` and `DynamicSymbols` for symbol table section '.dynsym' +# CASE3: error: yaml2obj failed --- !ELF FileHeader: @@ -61,6 +68,9 @@ - Name: .dynsym Type: SHT_DYNSYM Content: "00" + - Name: .dynsym2 + Type: SHT_DYNSYM + Content: "00" DynamicSymbols: - Name: foo Binding: STB_GLOBAL Index: test/tools/yaml2obj/elf-comdat-broken-info.yaml =================================================================== --- test/tools/yaml2obj/elf-comdat-broken-info.yaml +++ test/tools/yaml2obj/elf-comdat-broken-info.yaml @@ -1,5 +1,7 @@ -# RUN: yaml2obj %s -o %t -# RUN: llvm-readobj --sections %t | FileCheck %s +## Check we are able to produce an SHT_GROUP section with a custom Info value (12345). + +# RUN: yaml2obj %s -o %t1 +# RUN: llvm-readobj --sections %t1 | FileCheck %s --- !ELF FileHeader: @@ -15,7 +17,6 @@ Members: - SectionOrType: GRP_COMDAT -## Check we are able to produce SHT_GROUP section with a custom Info value (12345). # CHECK: Name: .group # CHECK-NEXT: Type: SHT_GROUP # CHECK-NEXT: Flags [ @@ -25,3 +26,30 @@ # CHECK-NEXT: Size: # CHECK-NEXT: Link: # CHECK-NEXT: Info: 12345 + +## Check we report multiple errors when multiple unknown symbols are referenced by SHT_GROUP sections. + +# RUN: not yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --check-prefix=ERR + +# ERR: error: unknown symbol referenced: 'foo' by YAML section '.group1' +# ERR: error: unknown symbol referenced: 'bar' by YAML section '.group2' + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .group1 + Type: SHT_GROUP + Link: .symtab + Info: foo + Members: + - SectionOrType: GRP_COMDAT + - Name: .group2 + Type: SHT_GROUP + Link: .symtab + Info: bar + Members: + - SectionOrType: GRP_COMDAT Index: test/tools/yaml2obj/elf-comdat-broken-members.yaml =================================================================== --- /dev/null +++ test/tools/yaml2obj/elf-comdat-broken-members.yaml @@ -0,0 +1,22 @@ +## Check we report an error when SHT_GROUP refereces unknown section in its members list. + +# RUN: not yaml2obj %s 2>&1 | FileCheck %s + +# CHECK: error: unknown section referenced: '.foo' by YAML section '.group' +# CHECK: error: unknown section referenced: '.bar' by YAML section '.group' + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .group + Type: SHT_GROUP + Link: .symtab + Info: 0 + Members: + - SectionOrType: GRP_COMDAT + - SectionOrType: .foo + - SectionOrType: .bar Index: test/tools/yaml2obj/elf-custom-null-section.yaml =================================================================== --- test/tools/yaml2obj/elf-custom-null-section.yaml +++ test/tools/yaml2obj/elf-custom-null-section.yaml @@ -130,7 +130,7 @@ # RUN: not yaml2obj --docnum=6 %s -o %t6 2>&1 | FileCheck %s --check-prefix=CASE4 -# CASE4: error: Unknown section referenced: '.foo' at YAML section ''. +# CASE4: error: unknown section referenced: '.foo' by YAML section '' --- !ELF FileHeader: Index: test/tools/yaml2obj/program-header.yaml =================================================================== --- test/tools/yaml2obj/program-header.yaml +++ test/tools/yaml2obj/program-header.yaml @@ -80,15 +80,18 @@ ## Check we do not allow referencing sections that do not exist. # RUN: not yaml2obj --docnum=2 %s -o %t 2>&1 | FileCheck %s --check-prefix=ERR -# ERR: error: Unknown section referenced: '.foo' by program header. + +# ERR: error: unknown section referenced: '.foo' by program header +# ERR: error: unknown section referenced: '.bar' by program header --- !ELF FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_EXEC - Machine: EM_X86_64 + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 ProgramHeaders: - Type: PT_LOAD Sections: - Section: .foo + - Section: .bar Index: test/tools/yaml2obj/reloc-sec-info.yaml =================================================================== --- test/tools/yaml2obj/reloc-sec-info.yaml +++ test/tools/yaml2obj/reloc-sec-info.yaml @@ -1,5 +1,5 @@ -# RUN: yaml2obj %s -o %t -# RUN: llvm-readobj --sections %t | FileCheck %s +# RUN: yaml2obj --docnum=1 %s -o %t1 +# RUN: llvm-readobj --sections %t1 | FileCheck %s # CHECK: Name: .rela.text # CHECK-NEXT: Type: SHT_RELA @@ -23,3 +23,31 @@ Link: .symtab Info: 12345 Relocations: + +## Check we report an error when relocation section references an unknown section via Info field. + +# RUN: not yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --implicit-check-not=error --check-prefix=ERR + +# ERR: error: unknown section referenced: '.unknown1' by YAML section '.foo' +# ERR: error: unknown section referenced: '.unknown2' by YAML section '.bar' +# ERR: error: yaml2obj failed + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_RELA + Info: .unknown1 + Relocations: + - Name: .bar + Type: SHT_RELA + Info: .unknown2 + Relocations: + - Name: .zed + Type: SHT_RELA + Info: 0xFF + Relocations: Index: test/tools/yaml2obj/relocation-missing-symbol.yaml =================================================================== --- test/tools/yaml2obj/relocation-missing-symbol.yaml +++ test/tools/yaml2obj/relocation-missing-symbol.yaml @@ -1,9 +1,12 @@ -# Show that yaml2obj rejects a symbol reference from a relocation if the symbol -# does not exist. +## Show that yaml2obj rejects a symbol reference from a relocation if the symbol +## does not exist. # RUN: not yaml2obj %s -o %t 2>&1 | FileCheck %s -# CHECK: Unknown symbol referenced: 'does_not_exist' at YAML section '.rela.text' +## Check we are able to report multiple errors. + +# CHECK: error: unknown symbol referenced: 'does_not_exist1' by YAML section '.rela.text' +# CHECK: error: unknown symbol referenced: 'does_not_exist2' by YAML section '.rela.text' --- !ELF FileHeader: @@ -21,4 +24,7 @@ Relocations: - Type: R_X86_64_PC32 Offset: 0 - Symbol: does_not_exist + Symbol: does_not_exist1 + - Type: R_X86_64_PC32 + Offset: 0 + Symbol: does_not_exist2 Index: test/tools/yaml2obj/section-link.yaml =================================================================== --- test/tools/yaml2obj/section-link.yaml +++ test/tools/yaml2obj/section-link.yaml @@ -1,5 +1,5 @@ -# RUN: yaml2obj %s -o %t -# RUN: llvm-readobj --sections %t | FileCheck %s +# RUN: yaml2obj --docnum=1 %s -o %t1 +# RUN: llvm-readobj --sections %t1 | FileCheck %s # CHECK: Name: .text # CHECK-NEXT: Type: SHT_PROGBITS @@ -23,3 +23,24 @@ Type: SHT_PROGBITS Flags: [ SHF_ALLOC, SHF_EXECINSTR ] Link: 12345 + +## Check we report an error when unknown section is referenced via Link field. + +# RUN: not yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --check-prefix=ERR + +# ERR: error: unknown section referenced: '.unknown1' by YAML section '.foo' +# ERR: error: unknown section referenced: '.unknown2' by YAML section '.bar' + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + Link: .unknown1 + - Name: .bar + Type: SHT_PROGBITS + Link: .unknown2 Index: test/tools/yaml2obj/symtab-implicit-sections-size-content.yaml =================================================================== --- test/tools/yaml2obj/symtab-implicit-sections-size-content.yaml +++ test/tools/yaml2obj/symtab-implicit-sections-size-content.yaml @@ -28,7 +28,7 @@ ## Specifying both `Size` and symbols at the same time is not allowed. # RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --check-prefix=CASE2 -# CASE2: error: Cannot specify both `Size` and `Symbols` for symbol table section '.symtab'. +# CASE2: error: cannot specify both `Size` and `Symbols` for symbol table section '.symtab' --- !ELF FileHeader: @@ -46,7 +46,7 @@ ## Specifying both `Content` and symbols at the same time is not allowed. # RUN: not yaml2obj --docnum=3 %s -o %t3 2>&1 | FileCheck %s --check-prefix=CASE3 -# CASE3: error: Cannot specify both `Content` and `Symbols` for symbol table section '.symtab'. +# CASE3: error: cannot specify both `Content` and `Symbols` for symbol table section '.symtab' --- !ELF FileHeader: