llvm.org GIT mirror llvm / 7845a5f
[yaml2obj] - Report when unknown section is referenced from program header declaration block. Previously we did not report this. Also this removes multiple lookups in the map what cleanups the code. Differential revision: https://reviews.llvm.org/D61322 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359663 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 5 months ago
3 changed file(s) with 42 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
1717 - Type: PT_NOTE
1818 Flags: [ PF_R ]
1919 Sections:
20 - Section: .note.gnu.foo
20 - Section: .note.foo
None # RUN: yaml2obj %s -o %t
0 # RUN: yaml2obj --docnum=1 %s -o %t
11 # RUN: llvm-readobj -l %t | FileCheck %s
22
3 !ELF
3 --- !ELF
44 FileHeader:
55 Class: ELFCLASS64
66 Data: ELFDATA2LSB
6464 #CHECK-NEXT: Alignment: 4096
6565 #CHECK-NEXT: }
6666 #CHECK-NEXT:]
67
68 ## Check we do not allow referencing sections that do not exist.
69 # RUN: not yaml2obj --docnum=2 %s -o %t 2>&1 | FileCheck %s --check-prefix=ERR
70 # ERR: error: Unknown section referenced: '.foo' by program header.
71
72 --- !ELF
73 FileHeader:
74 Class: ELFCLASS64
75 Data: ELFDATA2LSB
76 Type: ET_EXEC
77 Machine: EM_X86_64
78 ProgramHeaders:
79 - Type: PT_LOAD
80 Sections:
81 - Section: .foo
389389 std::vector &SHeaders) {
390390 uint32_t PhdrIdx = 0;
391391 for (auto &YamlPhdr : Doc.ProgramHeaders) {
392 auto &PHeader = PHeaders[PhdrIdx++];
392 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
393
394 std::vector Sections;
395 for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
396 unsigned Index;
397 if (SN2I.lookup(SecName.Section, Index)) {
398 WithColor::error() << "Unknown section referenced: '" << SecName.Section
399 << "' by program header.\n";
400 exit(1);
401 }
402 Sections.push_back(&SHeaders[Index]);
403 }
393404
394405 if (YamlPhdr.Offset) {
395406 PHeader.p_offset = *YamlPhdr.Offset;
400411 PHeader.p_offset = 0;
401412
402413 // Find the minimum offset for the program header.
403 for (auto SecName : YamlPhdr.Sections) {
404 uint32_t Index = 0;
405 SN2I.lookup(SecName.Section, Index);
406 const auto &SHeader = SHeaders[Index];
407 PHeader.p_offset = std::min(PHeader.p_offset, SHeader.sh_offset);
408 }
414 for (Elf_Shdr *SHeader : Sections)
415 PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset);
409416 }
410417
411418 // Find the maximum offset of the end of a section in order to set p_filesz,
414421 PHeader.p_filesz = *YamlPhdr.FileSize;
415422 } else {
416423 PHeader.p_filesz = 0;
417 for (auto SecName : YamlPhdr.Sections) {
418 uint32_t Index = 0;
419 SN2I.lookup(SecName.Section, Index);
420 const auto &SHeader = SHeaders[Index];
424 for (Elf_Shdr *SHeader : Sections) {
421425 uint64_t EndOfSection;
422 if (SHeader.sh_type == llvm::ELF::SHT_NOBITS)
423 EndOfSection = SHeader.sh_offset;
426 if (SHeader->sh_type == llvm::ELF::SHT_NOBITS)
427 EndOfSection = SHeader->sh_offset;
424428 else
425 EndOfSection = SHeader.sh_offset + SHeader.sh_size;
429 EndOfSection = SHeader->sh_offset + SHeader->sh_size;
426430 uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
427431 EndOfSegment = std::max(EndOfSegment, EndOfSection);
428432 PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
436440 PHeader.p_memsz = *YamlPhdr.MemSize;
437441 } else {
438442 PHeader.p_memsz = PHeader.p_filesz;
439 for (auto SecName : YamlPhdr.Sections) {
440 uint32_t Index = 0;
441 SN2I.lookup(SecName.Section, Index);
442 const auto &SHeader = SHeaders[Index];
443 if (SHeader.sh_offset == PHeader.p_offset + PHeader.p_filesz)
444 PHeader.p_memsz += SHeader.sh_size;
445 }
443 for (Elf_Shdr *SHeader : Sections)
444 if (SHeader->sh_offset == PHeader.p_offset + PHeader.p_filesz)
445 PHeader.p_memsz += SHeader->sh_size;
446446 }
447447
448448 // Set the alignment of the segment to be the same as the maximum alignment
452452 PHeader.p_align = *YamlPhdr.Align;
453453 } else {
454454 PHeader.p_align = 1;
455 for (auto SecName : YamlPhdr.Sections) {
456 uint32_t Index = 0;
457 SN2I.lookup(SecName.Section, Index);
458 const auto &SHeader = SHeaders[Index];
459 if (SHeader.sh_offset == PHeader.p_offset)
460 PHeader.p_align = std::max(PHeader.p_align, SHeader.sh_addralign);
461 }
455 for (Elf_Shdr *SHeader : Sections)
456 if (SHeader->sh_offset == PHeader.p_offset)
457 PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign);
462458 }
463459 }
464460 }