llvm.org GIT mirror llvm / 19ab8f8
[yaml2obj][ELF] Add an optional `Size` field to the YAML section declaration. Now the only method to configure ELF section's content and size is to assign a hexadecimal string to the `Content` field. Unfortunately this way is completely useless when you need to declare a really large section. To solve this problem this patch adds one more optional field `Size` to the `RawContentSection` structure. When yaml2obj generates an ELF file it uses the following algorithm: 1. If both `Content` and `Size` fields are missed create an empty section. 2. If only `Content` field is missed take section length from the `Size` field and fill the section by zero. 3. If only `Size` field is missed create a section using data from the `Content` field. 4. If both `Content` and `Size` fields are provided validate that the `Size` value is not less than size of `Content` data. Than take section length from the `Size`, fill beginning of the section by `Content` and the rest by zero. Examples -------- * Create a section 0x10000 bytes long filled by zero Name: .data Type: SHT_PROGBITS Flags: [ SHF_ALLOC ] Size: 0x10000 * Create a section 0x10000 bytes long starting from 'CA' 'FE' 'BA' 'BE' Name: .data Type: SHT_PROGBITS Flags: [ SHF_ALLOC ] Content: CAFEBABE Size: 0x10000 The patch reviewed by Michael Spencer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208995 91177308-0d34-0410-b5e6-96231b3b80d8 Simon Atanasyan 5 years ago
6 changed file(s) with 70 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
8282 };
8383 struct RawContentSection : Section {
8484 object::yaml::BinaryRef Content;
85 llvm::yaml::Hex64 Size;
8586 RawContentSection() : Section(SectionKind::RawContent) {}
8687 static bool classof(const Section *S) {
8788 return S->Kind == SectionKind::RawContent;
192193 template <>
193194 struct MappingTraits> {
194195 static void mapping(IO &IO, std::unique_ptr &Section);
196 static StringRef validate(IO &io, std::unique_ptr &Section);
195197 };
196198
197199 template <>
657657 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
658658 commonSectionMapping(IO, Section);
659659 IO.mapOptional("Content", Section.Content);
660 IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
660661 }
661662
662663 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
686687 }
687688 }
688689
690 StringRef MappingTraits>::validate(
691 IO &io, std::unique_ptr &Section) {
692 const auto *RawSection = dyn_cast(Section.get());
693 if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
694 return StringRef();
695 return "Section size must be greater or equal to the content size";
696 }
697
689698 void MappingTraits::mapping(IO &IO,
690699 ELFYAML::Relocation &Rel) {
691700 IO.mapRequired("Offset", Rel.Offset);
1414 Address: 0xCAFEBABE
1515 Link: .text # Doesn't make sense for SHT_PROGBITS, but good enough for test.
1616 Content: EBFE
17 AddressAlign: 2
18
19 - Name: .data
20 Type: SHT_PROGBITS
21 Flags: [ SHF_ALLOC ]
22 Address: 0xCAFECAFE
23 Content: FEBF
24 Size: 8
1725 AddressAlign: 2
1826
1927 # CHECK: Section {
3745 # CHECK-NEXT: )
3846 #
3947 # CHECK: Section {
48 # CHECK: Name: .data
49 # CHECK-NEXT: Type: SHT_PROGBITS (0x1)
50 # CHECK-NEXT: Flags [ (0x2)
51 # CHECK-NEXT: SHF_ALLOC (0x2)
52 # CHECK-NEXT: ]
53 # CHECK-NEXT: Address: 0xCAFECAFE
54 # CHECK-NEXT: Offset: 0x1D0
55 # CHECK-NEXT: Size: 8
56 # CHECK-NEXT: Link: 0
57 # CHECK-NEXT: Info: 0
58 # CHECK-NEXT: AddressAlignment: 2
59 # CHECK-NEXT: EntrySize: 0
60 # CHECK-NEXT: SectionData (
61 # CHECK-NEXT: 0000: FEBF0000 00000000 |........|
62 # CHECK-NEXT: )
63 #
64 # CHECK: Section {
4065 # CHECK: Name: .symtab (25)
4166 # CHECK: Type: SHT_SYMTAB (0x2)
4267 # CHECK: }
0 # RUN: not yaml2obj -format=elf -o %t %s 2>&1 | FileCheck %s
1
2 !ELF
3 FileHeader:
4 Class: ELFCLASS64
5 Data: ELFDATA2LSB
6 Type: ET_REL
7 Machine: EM_X86_64
8
9 Sections:
10 - Name: .text
11 Type: SHT_PROGBITS
12 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
13 Content: EBFE
14 AddressAlign: 2
15
16 - Name: .data
17 Type: SHT_PROGBITS
18 Flags: [ SHF_ALLOC ]
19 Content: 0000000000000000
20 Size: 2
21
22 # CHECK: YAML:17:5: error: Section size must be greater or equal to the content size
23 # CHECK-NEXT: - Name: .data
24 # CHECK-NEXT: ^
25 # CHECK-NEXT: yaml2obj: Failed to parse YAML file!
253253 if (error_code EC = ContentOrErr.getError())
254254 return EC;
255255 S->Content = object::yaml::BinaryRef(ContentOrErr.get());
256 S->Size = S->Content.binary_size();
256257
257258 return S.release();
258259 }
313313 ELFState::writeSectionContent(Elf_Shdr &SHeader,
314314 const ELFYAML::RawContentSection &Section,
315315 ContiguousBlobAccumulator &CBA) {
316 Section.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
316 assert(Section.Size >= Section.Content.binary_size() &&
317 "Section size and section content are inconsistent");
318 raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
319 Section.Content.writeAsBinary(OS);
320 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
321 OS.write(0);
317322 SHeader.sh_entsize = 0;
318 SHeader.sh_size = Section.Content.binary_size();
323 SHeader.sh_size = Section.Size;
319324 }
320325
321326 template