elfparser.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2011-2018 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 has_text_reloc() const;
  60. uint32_t get_global_data_size();
  61. bool update_global_data(const metadata_t *const metadata,
  62. const create_param_t* const create_param,
  63. uint8_t *data,
  64. uint32_t *data_size);
  65. // Get the offsets (relative to the base address) of the relocation
  66. // entries, of which the relocation address falls into the range of
  67. // section identified by `sec_name'.
  68. //
  69. // The relocation entry is of type:
  70. // - Elf64_Rel on x86_64,
  71. // - Elf32_Rel on x86.
  72. //
  73. // To check whether current enclave has any TEXTREL:
  74. // get_reloc_entry_offset(".text", offsets);
  75. void get_reloc_entry_offset(const char* sec_name,
  76. vector<uint64_t>& offsets);
  77. sgx_status_t modify_info(enclave_diff_info_t *enclave_diff_info);
  78. sgx_status_t get_info(enclave_diff_info_t *enclave_diff_info);
  79. void get_executable_sections(vector<const char *>& xsec_names) const;
  80. bool is_enclave_encrypted() const;
  81. bool set_memory_protection(uint64_t enclave_base_addr, bool is_after_initialization);
  82. void get_pages_to_protect(uint64_t enclave_base_addr, std::vector<std::tuple<uint64_t, uint64_t, uint32_t>>&) const;
  83. bool has_init_section() const;
  84. private:
  85. const uint8_t* m_start_addr;
  86. uint64_t m_len;
  87. bin_fmt_t m_bin_fmt;
  88. vector<Section *> m_sections;
  89. const Section* m_tls_section;
  90. uint64_t m_metadata_offset;
  91. uint64_t m_metadata_block_size;/*multiple metadata block size*/
  92. ElfW(Dyn) m_dyn_info[DT_NUM + DT_ADDRNUM];
  93. // A map from symbol name to its RVA
  94. map<string, uint64_t> m_sym_table;
  95. };
  96. #endif