load_symbol_cmd.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in
  13. # the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Intel Corporation nor the names of its
  16. # contributors may be used to endorse or promote products derived
  17. # from this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #
  31. #
  32. try:
  33. from cStringIO import StringIO
  34. except ImportError:
  35. from io import StringIO
  36. import traceback, errno, string, re, sys, time, readelf;
  37. def GetLoadSymbolCommand(EnclaveFile, Base):
  38. text = readelf.ReadElf(EnclaveFile)
  39. if text == None:
  40. return -1
  41. SegsFile = StringIO(text)
  42. try:
  43. FileList = SegsFile.readlines()
  44. n=4;
  45. m=100;
  46. Out = [[[] for ni in range(n)] for mi in range(m)]
  47. i=0;
  48. Out[99][2] = '0';
  49. # Parse the readelf output file to extract the section names and
  50. # their offsets and add the Proj base address.
  51. for line in FileList:
  52. list = line.split();
  53. if(len(list) > 0):
  54. SegOffset = -1;
  55. # The readelf will put a space after the open bracket for single
  56. # digit section numbers. This causes the line.split to create
  57. # an extra element in the array for these lines.
  58. if(re.match('\[\s*[0-9]+\]',list[0])):
  59. SegOffset = 0;
  60. if(re.match('\s*[0-9]+\]',list[1])):
  61. SegOffset = 1;
  62. if(SegOffset != -1):
  63. if (list[SegOffset+1][0] == '.'):
  64. # If it is the .text section, put it in a special place in the array
  65. # because the 'add-symbol-file' command treats it differently.
  66. #print "%#08x" % (int(list[SegOffset+3], 16))
  67. if(list[SegOffset+1].find(".text") != -1):
  68. Out[99][0] = "-s";
  69. Out[99][1] = list[SegOffset+1];
  70. Out[99][2] = str(int(list[SegOffset+3], 16) + int(Base, 10));
  71. Out[99][3] = " ";
  72. elif(int(list[SegOffset+3], 16) != 0):
  73. Out[i][0] = "-s";
  74. Out[i][1] = list[SegOffset+1];
  75. Out[i][2] = str(int(list[SegOffset+3], 16) + int(Base, 10));
  76. Out[i][3] = " ";
  77. i = i+1;
  78. if('0' != Out[99][2]):
  79. # The last section must not have the '\' line continuation character.
  80. Out[i-1][3] = '';
  81. # Write the GDB 'add-symbol-file' command with all the arguments to the setup GDB command file.
  82. # Note: The mandatory argument for the 'add-symbol-file' command is the .text section without a
  83. # '-s .SectionName'. All other sections need the '-s .SectionName'.
  84. gdbcmd = "add-symbol-file '" + EnclaveFile + "' " + '%(Location)#08x' % {'Location':int(Out[99][2])} + " -readnow "
  85. for j in range(i):
  86. gdbcmd += Out[j][0] + " " + Out[j][1] + " " + '%(Location)#08x' % {'Location' : int(Out[j][2])} + " " + Out[j][3]
  87. else:
  88. return -1
  89. return gdbcmd
  90. except:
  91. print ("Error parsing enclave file. Check format of file.")
  92. return -1
  93. def GetUnloadSymbolCommand(EnclaveFile, Base):
  94. text = readelf.ReadElf(EnclaveFile)
  95. if text == None:
  96. return -1
  97. SegsFile = StringIO(text)
  98. try:
  99. FileList = SegsFile.readlines()
  100. # Parse the readelf output file to extract the section names and
  101. # their offsets and add the Proj base address.
  102. for line in FileList:
  103. list = line.split();
  104. if(len(list) > 0):
  105. SegOffset = -1;
  106. # The readelf will put a space after the open bracket for single
  107. # digit section numbers. This causes the line.split to create
  108. # an extra element in the array for these lines.
  109. if(re.match('\[\s*[0-9]+\]',list[0])):
  110. SegOffset = 0;
  111. if(re.match('\s*[0-9]+\]',list[1])):
  112. SegOffset = 1;
  113. if(SegOffset != -1):
  114. if (list[SegOffset+1][0] == '.'):
  115. # If it is the .text section, get the .text start address and plus enclave start address
  116. if(list[SegOffset+1].find(".text") != -1):
  117. return "remove-symbol-file -a " + str(int(list[SegOffset+3], 16) + int(Base, 10))
  118. except:
  119. print ("Error parsing enclave file. Check format of file.")
  120. return -1