elf_helper.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _ELF_HELPER_H_
  32. #define _ELF_HELPER_H_
  33. #include <cstddef>
  34. #include <iostream>
  35. #include "elf32parser.h"
  36. #include "elf64parser.h"
  37. static void dump_textrel(const uint64_t& offset)
  38. {
  39. using namespace std;
  40. cerr << "warning: TEXTRELs found at offset: "
  41. << hex << showbase /* show the '0x' prefix */
  42. << offset << endl;
  43. }
  44. template <int N>
  45. struct ParserType
  46. {
  47. };
  48. template <>
  49. struct ParserType<32>
  50. {
  51. typedef Elf32Parser elf_parser_t;
  52. typedef Elf32_Rel elf_rel_t;
  53. };
  54. template <>
  55. struct ParserType<64>
  56. {
  57. typedef Elf64Parser elf_parser_t;
  58. typedef Elf64_Rela elf_rel_t;
  59. };
  60. template <int N>
  61. class ElfHelper
  62. {
  63. typedef typename ParserType<N>::elf_parser_t elf_parser_t;
  64. typedef typename ParserType<N>::elf_rel_t elf_rel_t;
  65. public:
  66. #define get_rel_entry_addr(p, rel_entry_offset) \
  67. ((elf_rel_t *)const_cast<uint8_t *>(p->get_start_addr() + rel_entry_offset))
  68. static uint64_t get_r_offset_from_entry(const elf_parser_t* p,
  69. uint64_t offsets)
  70. {
  71. const elf_rel_t *rel = get_rel_entry_addr(p, offsets);
  72. return (uint64_t)rel->r_offset;
  73. }
  74. static bool dump_textrels(BinParser *bp)
  75. {
  76. vector<uint64_t> offsets;
  77. bool no_rel = true;
  78. /* The dynamic_cast<> shouldn't fail. */
  79. elf_parser_t * p = dynamic_cast<elf_parser_t*>(bp);
  80. if (p == NULL)
  81. return no_rel;
  82. vector<const char *> sec_names;
  83. p->get_executable_sections(sec_names);
  84. /* Warn user of TEXTRELs */
  85. for (unsigned i = 0; i< sec_names.size(); i++)
  86. {
  87. p->get_reloc_entry_offset(sec_names[i], offsets);
  88. if(offsets.size() != 0 && no_rel == true)
  89. no_rel = false;
  90. for (size_t idx = 0; idx < offsets.size(); ++idx)
  91. {
  92. dump_textrel(get_r_offset_from_entry(p, offsets[idx]));
  93. }
  94. }
  95. return no_rel;
  96. }
  97. };
  98. #endif