llvm.org GIT mirror llvm / 3fa9339
[yaml2obj]Re-allow dynamic sections to have raw content Recently, support was added to yaml2obj to allow dynamic sections to have a list of entries, to make it easier to write tests with dynamic sections. However, this change also removed the ability to provide custom contents to the dynamic section, making it hard to test malformed contents (e.g. because the section is not a valid size to contain an array of entries). This change reinstates this. An error is emitted if raw content and dynamic entries are both specified. Reviewed by: grimar, ruiu Differential Review: https://reviews.llvm.org/D58543 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354770 91177308-0d34-0410-b5e6-96231b3b80d8 James Henderson 7 months ago
4 changed file(s) with 66 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
140140
141141 struct DynamicSection : Section {
142142 std::vector Entries;
143 Optional Content;
143144
144145 DynamicSection() : Section(SectionKind::Dynamic) {}
145146
854854 static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) {
855855 commonSectionMapping(IO, Section);
856856 IO.mapOptional("Entries", Section.Entries);
857 IO.mapOptional("Content", Section.Content);
857858 }
858859
859860 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
0 # Show that yaml2obj can handle a dynamic section with raw content instead of
1 # entries. Also show that it rejects raw content when entries are also provided.
2
3 # RUN: yaml2obj --docnum=1 %s -o %t1
4 # RUN: llvm-readobj -x .dynamic --sections %t1 | FileCheck %s --check-prefix=RAW
5
6 # RAW: Name: .dynamic
7 # RAW-NEXT: Type: SHT_DYNAMIC
8 # RAW-NEXT: Flags [
9 # RAW-NEXT: ]
10 # RAW-NEXT: Address:
11 # RAW-NEXT: Offset:
12 # RAW-NEXT: Size: 5
13
14 # RAW: Hex dump of section '.dynamic':
15 # RAW-NEXT: 0x00000000 01234567 89 {{.*}}
16
17 # RUN: not yaml2obj --docnum=2 %s -o %t2 2>&1 | FileCheck %s --check-prefix=ERR
18 # ERR: Cannot specify both raw content and explicit entries for dynamic section '.dynamic'.
19
20 --- !ELF
21 FileHeader:
22 Class: ELFCLASS64
23 Data: ELFDATA2LSB
24 Type: ET_EXEC
25 Machine: EM_X86_64
26 Sections:
27 - Name: .dynamic
28 Type: SHT_DYNAMIC
29 Content: "0123456789"
30
31 --- !ELF
32 FileHeader:
33 Class: ELFCLASS64
34 Data: ELFDATA2LSB
35 Type: ET_EXEC
36 Machine: EM_X86_64
37 Sections:
38 - Name: .dynamic
39 Type: SHT_DYNAMIC
40 Content: "0123456789"
41 Entries:
42 - Tag: DT_STRSZ
43 Value: 0
168168 bool writeSectionContent(Elf_Shdr &SHeader,
169169 const ELFYAML::MipsABIFlags &Section,
170170 ContiguousBlobAccumulator &CBA);
171 void writeSectionContent(Elf_Shdr &SHeader,
171 bool writeSectionContent(Elf_Shdr &SHeader,
172172 const ELFYAML::DynamicSection &Section,
173173 ContiguousBlobAccumulator &CBA);
174174 bool hasDynamicSymbols() const;
308308 // so just to setup the section offset.
309309 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
310310 } else if (auto S = dyn_cast(Sec.get())) {
311 writeSectionContent(SHeader, *S, CBA);
311 if (!writeSectionContent(SHeader, *S, CBA))
312 return false;
312313 } else if (auto S = dyn_cast(Sec.get())) {
313314 writeSectionContent(SHeader, *S, CBA);
314315 } else if (auto S = dyn_cast(Sec.get())) {
712713 }
713714
714715 template
715 void ELFState::writeSectionContent(Elf_Shdr &SHeader,
716 bool ELFState::writeSectionContent(Elf_Shdr &SHeader,
716717 const ELFYAML::DynamicSection &Section,
717718 ContiguousBlobAccumulator &CBA) {
718719 typedef typename ELFT::uint uintX_t;
720721 assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
721722 "Section type is not SHT_DYNAMIC");
722723
723 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
724 if (!Section.Entries.empty() && Section.Content) {
725 WithColor::error()
726 << "Cannot specify both raw content and explicit entries "
727 "for dynamic section '"
728 << Section.Name << "'.\n";
729 return false;
730 }
731
732 if (Section.Content)
733 SHeader.sh_size = Section.Content->binary_size();
734 else
735 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
724736 if (Section.EntSize)
725737 SHeader.sh_entsize = *Section.EntSize;
726738 else
731743 support::endian::write(OS, DE.Tag, ELFT::TargetEndianness);
732744 support::endian::write(OS, DE.Val, ELFT::TargetEndianness);
733745 }
746 if (Section.Content)
747 Section.Content->writeAsBinary(OS);
748
749 return true;
734750 }
735751
736752 template bool ELFState::buildSectionIndex() {