013-backport-mips-support-elflint.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. The errors were:
  2. $ src/elflint --gnu src/nm
  3. section [ 2] '.MIPS.options' contains unknown flag(s) 0x8000000
  4. section [ 7] '.dynsym': symbol 165 (_DYNAMIC_LINKING): non-local section symbol
  5. section [24] '.got' contains invalid processor-specific flag(s) 0x10000000
  6. section [25] '.sdata' contains invalid processor-specific flag(s) 0x10000000
  7. section [29] '.debug_aranges' has wrong type: expected PROGBITS, is MIPS_DWARF
  8. section [30] '.debug_info' has wrong type: expected PROGBITS, is MIPS_DWARF
  9. section [31] '.debug_abbrev' has wrong type: expected PROGBITS, is MIPS_DWARF
  10. section [32] '.debug_line' has wrong type: expected PROGBITS, is MIPS_DWARF
  11. section [33] '.debug_frame' has wrong type: expected PROGBITS, is MIPS_DWARF
  12. section [34] '.debug_str' has wrong type: expected PROGBITS, is MIPS_DWARF
  13. section [35] '.debug_loc' has wrong type: expected PROGBITS, is MIPS_DWARF
  14. section [36] '.debug_ranges' has wrong type: expected PROGBITS, is MIPS_DWARF
  15. section [38] '.symtab': symbol 785 (_gp): st_value out of bounds
  16. section [38] '.symtab': symbol 910 (_fbss): st_value out of bounds
  17. section [38] '.symtab': symbol 1051 (_DYNAMIC_LINKING): non-local section symbol
  18. After fixing:
  19. $ src/elflint --gnu src/nm
  20. No errors
  21. Signed-off-by: Ying Huang <[email protected]>
  22. ---
  23. backends/mips_init.c | 3 +++
  24. backends/mips_symbol.c | 37 +++++++++++++++++++++++++++++++++++++
  25. src/elflint.c | 26 +++++++++++++++++++++-----
  26. 3 files changed, 61 insertions(+), 5 deletions(-)
  27. --- a/backends/mips_init.c
  28. +++ b/backends/mips_init.c
  29. @@ -51,10 +51,13 @@ mips_init (Elf *elf __attribute__ ((unus
  30. HOOK (eh, section_type_name);
  31. HOOK (eh, machine_flag_check);
  32. HOOK (eh, machine_flag_name);
  33. + HOOK (eh, machine_section_flag_check);
  34. HOOK (eh, segment_type_name);
  35. HOOK (eh, dynamic_tag_check);
  36. HOOK (eh, dynamic_tag_name);
  37. HOOK (eh, check_object_attribute);
  38. + HOOK (eh, check_special_symbol);
  39. + HOOK (eh, check_reloc_target_type);
  40. HOOK (eh, set_initial_registers_tid);
  41. HOOK (eh, abi_cfi);
  42. HOOK (eh, unwind);
  43. --- a/backends/mips_symbol.c
  44. +++ b/backends/mips_symbol.c
  45. @@ -158,6 +158,43 @@ mips_section_type_name (int type,
  46. return NULL;
  47. }
  48. +bool
  49. +mips_check_reloc_target_type (Ebl *ebl __attribute__ ((unused)), Elf64_Word sh_type)
  50. +{
  51. + return (sh_type == SHT_MIPS_DWARF);
  52. +}
  53. +
  54. +/* Check whether given symbol's st_value and st_size are OK despite failing
  55. + normal checks. */
  56. +bool
  57. +mips_check_special_symbol (Elf *elf,
  58. + const GElf_Sym *sym __attribute__ ((unused)),
  59. + const char *name __attribute__ ((unused)),
  60. + const GElf_Shdr *destshdr)
  61. +{
  62. + size_t shstrndx;
  63. + if (elf_getshdrstrndx (elf, &shstrndx) != 0)
  64. + return false;
  65. + const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
  66. + if (sname == NULL)
  67. + return false;
  68. + return (strcmp (sname, ".got") == 0 || strcmp (sname, ".bss") == 0);
  69. +}
  70. +
  71. +/* Check whether SHF_MASKPROC flags are valid. */
  72. +bool
  73. +mips_machine_section_flag_check (GElf_Xword sh_flags)
  74. +{
  75. + return ((sh_flags &~ (SHF_MIPS_GPREL |
  76. + SHF_MIPS_MERGE |
  77. + SHF_MIPS_ADDR |
  78. + SHF_MIPS_STRINGS |
  79. + SHF_MIPS_NOSTRIP |
  80. + SHF_MIPS_LOCAL |
  81. + SHF_MIPS_NAMES |
  82. + SHF_MIPS_NODUPE)) == 0);
  83. +}
  84. +
  85. /* Check whether machine flags are valid. */
  86. bool
  87. mips_machine_flag_check (GElf_Word flags)
  88. --- a/src/elflint.c
  89. +++ b/src/elflint.c
  90. @@ -936,7 +936,9 @@ section [%2d] '%s': symbol %zu (%s): non
  91. }
  92. if (GELF_ST_TYPE (sym->st_info) == STT_SECTION
  93. - && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
  94. + && GELF_ST_BIND (sym->st_info) != STB_LOCAL
  95. + && ehdr->e_machine != EM_MIPS
  96. + && strcmp (name, "_DYNAMIC_LINKING") != 0)
  97. ERROR (_("\
  98. section [%2d] '%s': symbol %zu (%s): non-local section symbol\n"),
  99. idx, section_name (ebl, idx), cnt, name);
  100. @@ -3829,6 +3831,10 @@ cannot get section header for section [%
  101. && ebl_bss_plt_p (ebl))
  102. good_type = SHT_NOBITS;
  103. + if (ehdr->e_machine == EM_MIPS
  104. + && (strstr(special_sections[s].name, ".debug") != NULL))
  105. + good_type = SHT_MIPS_DWARF;
  106. +
  107. /* In a debuginfo file, any normal section can be SHT_NOBITS.
  108. This is only invalid for DWARF sections and .shstrtab. */
  109. if (shdr->sh_type != good_type
  110. @@ -3989,12 +3995,21 @@ section [%2zu] '%s': size not multiple o
  111. ERROR (_("section [%2zu] '%s'"
  112. " contains invalid processor-specific flag(s)"
  113. " %#" PRIx64 "\n"),
  114. - cnt, section_name (ebl, cnt), sh_flags & SHF_MASKPROC);
  115. + cnt, section_name (ebl, cnt), sh_flags & SHF_MASKPROC);
  116. sh_flags &= ~(GElf_Xword) SHF_MASKPROC;
  117. }
  118. if (sh_flags & SHF_MASKOS)
  119. - if (gnuld)
  120. - sh_flags &= ~(GElf_Xword) SHF_GNU_RETAIN;
  121. + {
  122. + if (gnuld)
  123. + sh_flags &= ~(GElf_Xword) SHF_GNU_RETAIN;
  124. + if (!ebl_machine_section_flag_check (ebl,
  125. + sh_flags & SHF_MASKOS))
  126. + ERROR (_("section [%2zu] '%s'"
  127. + " contains invalid os-specific flag(s)"
  128. + " %#" PRIx64 "\n"),
  129. + cnt, section_name (ebl, cnt), sh_flags & SHF_MASKOS);
  130. + sh_flags &= ~(GElf_Xword) SHF_MASKOS;
  131. + }
  132. if (sh_flags != 0)
  133. ERROR (_("section [%2zu] '%s' contains unknown flag(s)"
  134. " %#" PRIx64 "\n"),
  135. @@ -4060,6 +4075,7 @@ section [%2zu] '%s': merge flag set but
  136. switch (shdr->sh_type)
  137. {
  138. case SHT_PROGBITS:
  139. + case SHT_MIPS_DWARF:
  140. break;
  141. case SHT_NOBITS:
  142. @@ -4717,7 +4733,7 @@ program header offset in ELF header and
  143. if (shdr != NULL
  144. && ((is_debuginfo && shdr->sh_type == SHT_NOBITS)
  145. || (! is_debuginfo
  146. - && (shdr->sh_type == SHT_PROGBITS
  147. + && (is_debug_section_type(shdr->sh_type)
  148. || shdr->sh_type == SHT_X86_64_UNWIND)))
  149. && elf_strptr (ebl->elf, shstrndx, shdr->sh_name) != NULL
  150. && ! strcmp (".eh_frame_hdr",