PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/gcc/ada/symbols-processing-vms-ia64.adb

https://bitbucket.org/pizzafactory/pf-gcc
Ada | 430 lines | 239 code | 116 blank | 75 comment | 25 complexity | bf8e04a5f99d39d6216f1435f11087e0 MD5 | raw file
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT COMPILER COMPONENTS --
  4. -- --
  5. -- S Y M B O L S . P R O C E S S I N G --
  6. -- --
  7. -- B o d y --
  8. -- --
  9. -- Copyright (C) 2004-2009, Free Software Foundation, Inc. --
  10. -- --
  11. -- GNAT is free software; you can redistribute it and/or modify it under --
  12. -- terms of the GNU General Public License as published by the Free Soft- --
  13. -- ware Foundation; either version 3, or (at your option) any later ver- --
  14. -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
  15. -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
  16. -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
  17. -- for more details. You should have received a copy of the GNU General --
  18. -- Public License distributed with GNAT; see file COPYING3. If not, go to --
  19. -- http://www.gnu.org/licenses for a complete copy of the license. --
  20. -- --
  21. -- GNAT was originally developed by the GNAT team at New York University. --
  22. -- Extensive contributions were provided by Ada Core Technologies Inc. --
  23. -- --
  24. ------------------------------------------------------------------------------
  25. -- This is the VMS/IA64 version of this package
  26. with Ada.IO_Exceptions;
  27. with Ada.Unchecked_Deallocation;
  28. separate (Symbols)
  29. package body Processing is
  30. type String_Array is array (Positive range <>) of String_Access;
  31. type Strings_Ptr is access String_Array;
  32. procedure Free is
  33. new Ada.Unchecked_Deallocation (String_Array, Strings_Ptr);
  34. type Section_Header is record
  35. Shname : Integer;
  36. Shtype : Integer;
  37. Shoffset : Integer;
  38. Shsize : Integer;
  39. Shlink : Integer;
  40. end record;
  41. type Section_Header_Array is array (Natural range <>) of Section_Header;
  42. type Section_Header_Ptr is access Section_Header_Array;
  43. procedure Free is
  44. new Ada.Unchecked_Deallocation (Section_Header_Array, Section_Header_Ptr);
  45. -------------
  46. -- Process --
  47. -------------
  48. procedure Process
  49. (Object_File : String;
  50. Success : out Boolean)
  51. is
  52. B : Byte;
  53. W : Integer;
  54. Str : String (1 .. 1000) := (others => ' ');
  55. Str_Last : Natural;
  56. Strings : Strings_Ptr;
  57. Shoff : Integer;
  58. Shnum : Integer;
  59. Shentsize : Integer;
  60. Shname : Integer;
  61. Shtype : Integer;
  62. Shoffset : Integer;
  63. Shsize : Integer;
  64. Shlink : Integer;
  65. Symtab_Index : Natural := 0;
  66. String_Table_Index : Natural := 0;
  67. End_Symtab : Integer;
  68. Stname : Integer;
  69. Stinfo : Character;
  70. Stother : Character;
  71. Sttype : Integer;
  72. Stbind : Integer;
  73. Stshndx : Integer;
  74. Stvis : Integer;
  75. STV_Internal : constant := 1;
  76. STV_Hidden : constant := 2;
  77. Section_Headers : Section_Header_Ptr;
  78. Offset : Natural := 0;
  79. OK : Boolean := True;
  80. procedure Get_Byte (B : out Byte);
  81. -- Read one byte from the object file
  82. procedure Get_Half (H : out Integer);
  83. -- Read one half work from the object file
  84. procedure Get_Word (W : out Integer);
  85. -- Read one full word from the object file
  86. procedure Reset;
  87. -- Restart reading the object file
  88. procedure Skip_Half;
  89. -- Read and disregard one half word from the object file
  90. --------------
  91. -- Get_Byte --
  92. --------------
  93. procedure Get_Byte (B : out Byte) is
  94. begin
  95. Byte_IO.Read (File, B);
  96. Offset := Offset + 1;
  97. end Get_Byte;
  98. --------------
  99. -- Get_Half --
  100. --------------
  101. procedure Get_Half (H : out Integer) is
  102. C1, C2 : Character;
  103. begin
  104. Get_Byte (C1); Get_Byte (C2);
  105. H :=
  106. Integer'(Character'Pos (C2)) * 256 + Integer'(Character'Pos (C1));
  107. end Get_Half;
  108. --------------
  109. -- Get_Word --
  110. --------------
  111. procedure Get_Word (W : out Integer) is
  112. H1, H2 : Integer;
  113. begin
  114. Get_Half (H1); Get_Half (H2);
  115. W := H2 * 256 * 256 + H1;
  116. end Get_Word;
  117. -----------
  118. -- Reset --
  119. -----------
  120. procedure Reset is
  121. begin
  122. Offset := 0;
  123. Byte_IO.Reset (File);
  124. end Reset;
  125. ---------------
  126. -- Skip_Half --
  127. ---------------
  128. procedure Skip_Half is
  129. B : Byte;
  130. pragma Unreferenced (B);
  131. begin
  132. Byte_IO.Read (File, B);
  133. Byte_IO.Read (File, B);
  134. Offset := Offset + 2;
  135. end Skip_Half;
  136. -- Start of processing for Process
  137. begin
  138. -- Open the object file with Byte_IO. Return with Success = False if
  139. -- this fails.
  140. begin
  141. Open (File, In_File, Object_File);
  142. exception
  143. when others =>
  144. Put_Line
  145. ("*** Unable to open object file """ & Object_File & """");
  146. Success := False;
  147. return;
  148. end;
  149. -- Assume that the object file has a correct format
  150. Success := True;
  151. -- Skip ELF identification
  152. while Offset < 16 loop
  153. Get_Byte (B);
  154. end loop;
  155. -- Skip e_type
  156. Skip_Half;
  157. -- Skip e_machine
  158. Skip_Half;
  159. -- Skip e_version
  160. Get_Word (W);
  161. -- Skip e_entry
  162. for J in 1 .. 8 loop
  163. Get_Byte (B);
  164. end loop;
  165. -- Skip e_phoff
  166. for J in 1 .. 8 loop
  167. Get_Byte (B);
  168. end loop;
  169. Get_Word (Shoff);
  170. -- Skip upper half of Shoff
  171. for J in 1 .. 4 loop
  172. Get_Byte (B);
  173. end loop;
  174. -- Skip e_flags
  175. Get_Word (W);
  176. -- Skip e_ehsize
  177. Skip_Half;
  178. -- Skip e_phentsize
  179. Skip_Half;
  180. -- Skip e_phnum
  181. Skip_Half;
  182. Get_Half (Shentsize);
  183. Get_Half (Shnum);
  184. Section_Headers := new Section_Header_Array (0 .. Shnum - 1);
  185. -- Go to Section Headers
  186. while Offset < Shoff loop
  187. Get_Byte (B);
  188. end loop;
  189. -- Reset Symtab_Index
  190. Symtab_Index := 0;
  191. for J in Section_Headers'Range loop
  192. -- Get the data for each Section Header
  193. Get_Word (Shname);
  194. Get_Word (Shtype);
  195. for K in 1 .. 16 loop
  196. Get_Byte (B);
  197. end loop;
  198. Get_Word (Shoffset);
  199. Get_Word (W);
  200. Get_Word (Shsize);
  201. Get_Word (W);
  202. Get_Word (Shlink);
  203. while (Offset - Shoff) mod Shentsize /= 0 loop
  204. Get_Byte (B);
  205. end loop;
  206. -- If this is the Symbol Table Section Header, record its index
  207. if Shtype = 2 then
  208. Symtab_Index := J;
  209. end if;
  210. Section_Headers (J) := (Shname, Shtype, Shoffset, Shsize, Shlink);
  211. end loop;
  212. if Symtab_Index = 0 then
  213. Success := False;
  214. return;
  215. end if;
  216. End_Symtab :=
  217. Section_Headers (Symtab_Index).Shoffset +
  218. Section_Headers (Symtab_Index).Shsize;
  219. String_Table_Index := Section_Headers (Symtab_Index).Shlink;
  220. Strings :=
  221. new String_Array (1 .. Section_Headers (String_Table_Index).Shsize);
  222. -- Go get the String Table section for the Symbol Table
  223. Reset;
  224. while Offset < Section_Headers (String_Table_Index).Shoffset loop
  225. Get_Byte (B);
  226. end loop;
  227. Offset := 0;
  228. Get_Byte (B); -- zero
  229. while Offset < Section_Headers (String_Table_Index).Shsize loop
  230. Str_Last := 0;
  231. loop
  232. Get_Byte (B);
  233. if B /= ASCII.NUL then
  234. Str_Last := Str_Last + 1;
  235. Str (Str_Last) := B;
  236. else
  237. Strings (Offset - Str_Last - 1) :=
  238. new String'(Str (1 .. Str_Last));
  239. exit;
  240. end if;
  241. end loop;
  242. end loop;
  243. -- Go get the Symbol Table
  244. Reset;
  245. while Offset < Section_Headers (Symtab_Index).Shoffset loop
  246. Get_Byte (B);
  247. end loop;
  248. while Offset < End_Symtab loop
  249. Get_Word (Stname);
  250. Get_Byte (Stinfo);
  251. Get_Byte (Stother);
  252. Get_Half (Stshndx);
  253. for J in 1 .. 4 loop
  254. Get_Word (W);
  255. end loop;
  256. Sttype := Integer'(Character'Pos (Stinfo)) mod 16;
  257. Stbind := Integer'(Character'Pos (Stinfo)) / 16;
  258. Stvis := Integer'(Character'Pos (Stother)) mod 4;
  259. if (Sttype = 1 or else Sttype = 2)
  260. and then Stbind /= 0
  261. and then Stshndx /= 0
  262. and then Stvis /= STV_Internal
  263. and then Stvis /= STV_Hidden
  264. then
  265. -- Check if this is a symbol from a generic body
  266. OK := True;
  267. for J in Strings (Stname)'First .. Strings (Stname)'Last - 2 loop
  268. if Strings (Stname) (J) = 'G'
  269. and then Strings (Stname) (J + 1) = 'P'
  270. and then Strings (Stname) (J + 2) in '0' .. '9'
  271. then
  272. OK := False;
  273. exit;
  274. end if;
  275. end loop;
  276. if OK then
  277. declare
  278. S_Data : Symbol_Data;
  279. begin
  280. S_Data.Name := new String'(Strings (Stname).all);
  281. if Sttype = 1 then
  282. S_Data.Kind := Data;
  283. else
  284. S_Data.Kind := Proc;
  285. end if;
  286. -- Put the new symbol in the table
  287. Symbol_Table.Append (Complete_Symbols, S_Data);
  288. end;
  289. end if;
  290. end if;
  291. end loop;
  292. -- The object file has been processed, close it
  293. Close (File);
  294. -- Free the allocated memory
  295. Free (Section_Headers);
  296. for J in Strings'Range loop
  297. if Strings (J) /= null then
  298. Free (Strings (J));
  299. end if;
  300. end loop;
  301. Free (Strings);
  302. exception
  303. -- For any exception, output an error message, close the object file
  304. -- and return with Success = False.
  305. when Ada.IO_Exceptions.End_Error =>
  306. Close (File);
  307. when X : others =>
  308. Put_Line ("unexpected exception raised while processing """
  309. & Object_File & """");
  310. Put_Line (Exception_Information (X));
  311. Close (File);
  312. Success := False;
  313. end Process;
  314. end Processing;