elfparser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 _ELFPARSER_H_
  32. #define _ELFPARSER_H_
  33. #include "binparser.h"
  34. #include "elf_util.h"
  35. #include <assert.h>
  36. #include <stdint.h>
  37. #include <string>
  38. #include <map>
  39. using std::map;
  40. using std::string;
  41. class ElfParser : public BinParser, private Uncopyable {
  42. public:
  43. // The `start_addr' cannot be NULL
  44. ElfParser(const uint8_t* start_addr, uint64_t len);
  45. ~ElfParser();
  46. // Do the parsing job - use it before calling other methods
  47. sgx_status_t run_parser();
  48. bin_fmt_t get_bin_format() const;
  49. uint64_t get_enclave_max_size() const;
  50. uint64_t get_metadata_offset() const;
  51. uint64_t get_metadata_block_size() const;
  52. const uint8_t* get_start_addr() const;
  53. // The `section' here is a section in PE's concept.
  54. // It is in fact a `segment' in ELF's view.
  55. const vector<Section *>& get_sections() const;
  56. const Section* get_tls_section() const;
  57. uint64_t get_symbol_rva(const char* name) const;
  58. bool get_reloc_bitmap(vector<uint8_t> &bitmap);
  59. bool update_global_data(const create_param_t* const create_param,
  60. uint8_t *data,
  61. uint32_t *data_size);
  62. // Get the offsets (relative to the base address) of the relocation
  63. // entries, of which the relocation address falls into the range of
  64. // section identified by `sec_name'.
  65. //
  66. // The relocation entry is of type:
  67. // - Elf64_Rel on x86_64,
  68. // - Elf32_Rel on x86.
  69. //
  70. // To check whether current enclave has any TEXTREL:
  71. // get_reloc_entry_offset(".text", offsets);
  72. void get_reloc_entry_offset(const char* sec_name,
  73. vector<uint64_t>& offsets);
  74. sgx_status_t modify_info(enclave_diff_info_t *enclave_diff_info);
  75. sgx_status_t get_info(enclave_diff_info_t *enclave_diff_info);
  76. void get_executable_sections(vector<const char *>& xsec_names) const;
  77. private:
  78. const uint8_t* m_start_addr;
  79. uint64_t m_len;
  80. bin_fmt_t m_bin_fmt;
  81. vector<Section *> m_sections;
  82. const Section* m_tls_section;
  83. uint64_t m_metadata_offset;
  84. uint64_t m_metadata_block_size;/*multiple metadata block size*/
  85. ElfW(Dyn) m_dyn_info[DT_NUM + DT_ADDRNUM];
  86. // A map from symbol name to its RVA
  87. map<string, uint64_t> m_sym_table;
  88. };
  89. #endif