1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- S Y S T E M . O B J E C T _ R E A D E R --
-- --
-- B o d y --
-- --
-- Copyright (C) 2009-2022, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with Ada.Unchecked_Conversion;
with Interfaces.C;
with System.CRTL;
package body System.Object_Reader is
use Interfaces;
use Interfaces.C;
use System.Mmap;
SSU : constant := System.Storage_Unit;
function To_int32 is new Ada.Unchecked_Conversion (uint32, int32);
function Trim_Trailing_Nuls (Str : String) return String;
-- Return a copy of a string with any trailing NUL characters truncated
procedure Check_Read_Offset (S : Mapped_Stream; Size : uint32);
-- Check that the SIZE bytes at the current offset are still in the stream
-------------------------------------
-- ELF object file format handling --
-------------------------------------
generic
type uword is mod <>;
package ELF_Ops is
-- ELF version codes
ELFCLASS32 : constant := 1; -- 32 bit ELF
ELFCLASS64 : constant := 2; -- 64 bit ELF
-- ELF machine codes
EM_NONE : constant := 0; -- No machine
EM_SPARC : constant := 2; -- SUN SPARC
EM_386 : constant := 3; -- Intel 80386
EM_MIPS : constant := 8; -- MIPS RS3000 Big-Endian
EM_MIPS_RS3_LE : constant := 10; -- MIPS RS3000 Little-Endian
EM_SPARC32PLUS : constant := 18; -- Sun SPARC 32+
EM_PPC : constant := 20; -- PowerPC
EM_PPC64 : constant := 21; -- PowerPC 64-bit
EM_ARM : constant := 40; -- ARM
EM_SPARCV9 : constant := 43; -- SPARC v9 64-bit
EM_IA_64 : constant := 50; -- Intel Merced
EM_X86_64 : constant := 62; -- AMD x86-64 architecture
EM_AARCH64 : constant := 183; -- Aarch64
EN_NIDENT : constant := 16;
type E_Ident_Type is array (0 .. EN_NIDENT - 1) of uint8;
type Header is record
E_Ident : E_Ident_Type; -- Magic number and other info
E_Type : uint16; -- Object file type
E_Machine : uint16; -- Architecture
E_Version : uint32; -- Object file version
E_Entry : uword; -- Entry point virtual address
E_Phoff : uword; -- Program header table file offset
E_Shoff : uword; -- Section header table file offset
E_Flags : uint32; -- Processor-specific flags
E_Ehsize : uint16; -- ELF header size in bytes
E_Phentsize : uint16; -- Program header table entry size
E_Phnum : uint16; -- Program header table entry count
E_Shentsize : uint16; -- Section header table entry size
E_Shnum : uint16; -- Section header table entry count
E_Shstrndx : uint16; -- Section header string table index
end record;
type Section_Header is record
Sh_Name : uint32; -- Section name string table index
Sh_Type : uint32; -- Section type
Sh_Flags : uword; -- Section flags
Sh_Addr : uword; -- Section virtual addr at execution
Sh_Offset : uword; -- Section file offset
Sh_Size : uword; -- Section size in bytes
Sh_Link : uint32; -- Link to another section
Sh_Info : uint32; -- Additional section information
Sh_Addralign : uword; -- Section alignment
Sh_Entsize : uword; -- Entry size if section holds table
end record;
SHF_ALLOC : constant := 2;
SHF_EXECINSTR : constant := 4;
type Symtab_Entry32 is record
St_Name : uint32; -- Name (string table index)
St_Value : uint32; -- Value
St_Size : uint32; -- Size in bytes
St_Info : uint8; -- Type and binding attributes
St_Other : uint8; -- Undefined
St_Shndx : uint16; -- Defining section
end record;
type Symtab_Entry64 is record
St_Name : uint32; -- Name (string table index)
St_Info : uint8; -- Type and binding attributes
St_Other : uint8; -- Undefined
St_Shndx : uint16; -- Defining section
St_Value : uint64; -- Value
St_Size : uint64; -- Size in bytes
end record;
function Read_Header (F : in out Mapped_Stream) return Header;
-- Read a header from an ELF format object
function First_Symbol
(Obj : in out ELF_Object_File) return Object_Symbol;
-- Return the first element in the symbol table, or Null_Symbol if the
-- symbol table is empty.
function Read_Symbol
(Obj : in out ELF_Object_File;
Off : Offset) return Object_Symbol;
-- Read a symbol at offset Off
function Name
(Obj : in out ELF_Object_File;
Sym : Object_Symbol) return String_Ptr_Len;
-- Return the name of the symbol
function Name
(Obj : in out ELF_Object_File;
Sec : Object_Section) return String;
-- Return the name of a section
function Get_Section
(Obj : in out ELF_Object_File;
Shnum : uint32) return Object_Section;
-- Fetch a section by index from zero
function Initialize
(F : Mapped_File;
Hdr : Header;
In_Exception : Boolean) return ELF_Object_File;
-- Initialize an object file
end ELF_Ops;
-----------------------------------
-- PECOFF object format handling --
-----------------------------------
package PECOFF_Ops is
-- Constants and data layout are taken from the document "Microsoft
-- Portable Executable and Common Object File Format Specification"
-- Revision 8.1.
Signature_Loc_Offset : constant := 16#3C#;
-- Offset of pointer to the file signature
Size_Of_Standard_Header_Fields : constant := 16#18#;
-- Length in bytes of the standard header record
Function_Symbol_Type : constant := 16#20#;
-- Type field value indicating a symbol refers to a function
Not_Function_Symbol_Type : constant := 16#00#;
-- Type field value indicating a symbol does not refer to a function
type Magic_Array is array (0 .. 3) of uint8;
-- Array of magic numbers from the header
-- Magic numbers for PECOFF variants
VARIANT_PE32 : constant := 16#010B#;
VARIANT_PE32_PLUS : constant := 16#020B#;
-- PECOFF machine codes
IMAGE_FILE_MACHINE_I386 : constant := 16#014C#;
IMAGE_FILE_MACHINE_IA64 : constant := 16#0200#;
IMAGE_FILE_MACHINE_AMD64 : constant := 16#8664#;
-- PECOFF Data layout
type Header is record
Magics : Magic_Array;
Machine : uint16;
NumberOfSections : uint16;
TimeDateStamp : uint32;
PointerToSymbolTable : uint32;
NumberOfSymbols : uint32;
SizeOfOptionalHeader : uint16;
Characteristics : uint16;
Variant : uint16;
end record;
pragma Pack (Header);
type Optional_Header_PE32 is record
Magic : uint16;
MajorLinkerVersion : uint8;
MinorLinkerVersion : uint8;
SizeOfCode : uint32;
SizeOfInitializedData : uint32;
SizeOfUninitializedData : uint32;
AddressOfEntryPoint : uint32;
BaseOfCode : uint32;
BaseOfData : uint32; -- Note: not in PE32+
ImageBase : uint32;
SectionAlignment : uint32;
FileAlignment : uint32;
MajorOperatingSystemVersion : uint16;
MinorOperationSystemVersion : uint16;
MajorImageVersion : uint16;
MinorImageVersion : uint16;
MajorSubsystemVersion : uint16;
MinorSubsystemVersion : uint16;
Win32VersionValue : uint32;
SizeOfImage : uint32;
SizeOfHeaders : uint32;
Checksum : uint32;
Subsystem : uint16;
DllCharacteristics : uint16;
SizeOfStackReserve : uint32;
SizeOfStackCommit : uint32;
SizeOfHeapReserve : uint32;
SizeOfHeapCommit : uint32;
LoaderFlags : uint32;
NumberOfRvaAndSizes : uint32;
end record;
pragma Pack (Optional_Header_PE32);
pragma Assert (Optional_Header_PE32'Size = 96 * SSU);
type Optional_Header_PE64 is record
Magic : uint16;
MajorLinkerVersion : uint8;
MinorLinkerVersion : uint8;
SizeOfCode : uint32;
SizeOfInitializedData : uint32;
SizeOfUninitializedData : uint32;
AddressOfEntryPoint : uint32;
BaseOfCode : uint32;
ImageBase : uint64;
SectionAlignment : uint32;
FileAlignment : uint32;
MajorOperatingSystemVersion : uint16;
MinorOperationSystemVersion : uint16;
MajorImageVersion : uint16;
MinorImageVersion : uint16;
MajorSubsystemVersion : uint16;
MinorSubsystemVersion : uint16;
Win32VersionValue : uint32;
SizeOfImage : uint32;
SizeOfHeaders : uint32;
Checksum : uint32;
Subsystem : uint16;
DllCharacteristics : uint16;
SizeOfStackReserve : uint64;
SizeOfStackCommit : uint64;
SizeOfHeapReserve : uint64;
SizeOfHeapCommit : uint64;
LoaderFlags : uint32;
NumberOfRvaAndSizes : uint32;
end record;
pragma Pack (Optional_Header_PE64);
pragma Assert (Optional_Header_PE64'Size = 112 * SSU);
subtype Name_Str is String (1 .. 8);
type Section_Header is record
Name : Name_Str;
VirtualSize : uint32;
VirtualAddress : uint32;
SizeOfRawData : uint32;
PointerToRawData : uint32;
PointerToRelocations : uint32;
PointerToLinenumbers : uint32;
NumberOfRelocations : uint16;
NumberOfLinenumbers : uint16;
Characteristics : uint32;
end record;
pragma Pack (Section_Header);
IMAGE_SCN_CNT_CODE : constant := 16#0020#;
type Symtab_Entry is record
Name : Name_Str;
Value : uint32;
SectionNumber : int16;
TypeField : uint16;
StorageClass : uint8;
NumberOfAuxSymbols : uint8;
end record;
pragma Pack (Symtab_Entry);
type Auxent_Section is record
Length : uint32;
NumberOfRelocations : uint16;
NumberOfLinenumbers : uint16;
CheckSum : uint32;
Number : uint16;
Selection : uint8;
Unused1 : uint8;
Unused2 : uint8;
Unused3 : uint8;
end record;
for Auxent_Section'Size use 18 * 8;
function Read_Header (F : in out Mapped_Stream) return Header;
-- Read the object file header
function First_Symbol
(Obj : in out PECOFF_Object_File) return Object_Symbol;
-- Return the first element in the symbol table, or Null_Symbol if the
-- symbol table is empty.
function Read_Symbol
(Obj : in out PECOFF_Object_File;
Off : Offset) return Object_Symbol;
-- Read a symbol at offset Off
function Name
(Obj : in out PECOFF_Object_File;
Sym : Object_Symbol) return String_Ptr_Len;
-- Return the name of the symbol
function Name
(Obj : in out PECOFF_Object_File;
Sec : Object_Section) return String;
-- Return the name of a section
function Get_Section
(Obj : in out PECOFF_Object_File;
Index : uint32) return Object_Section;
-- Fetch a section by index from zero
function Initialize
(F : Mapped_File;
Hdr : Header;
In_Exception : Boolean) return PECOFF_Object_File;
-- Initialize an object file
end PECOFF_Ops;
-------------------------------------
-- XCOFF-32 object format handling --
-------------------------------------
package XCOFF32_Ops is
-- XCOFF Data layout
type Header is record
f_magic : uint16;
f_nscns : uint16;
f_timdat : uint32;
f_symptr : uint32;
f_nsyms : uint32;
f_opthdr : uint16;
f_flags : uint16;
end record;
type Auxiliary_Header is record
o_mflag : uint16;
o_vstamp : uint16;
o_tsize : uint32;
o_dsize : uint32;
o_bsize : uint32;
o_entry : uint32;
o_text_start : uint32;
o_data_start : uint32;
o_toc : uint32;
o_snentry : uint16;
o_sntext : uint16;
o_sndata : uint16;
o_sntoc : uint16;
o_snloader : uint16;
o_snbss : uint16;
o_algntext : uint16;
o_algndata : uint16;
o_modtype : uint16;
o_cpuflag : uint8;
o_cputype : uint8;
o_maxstack : uint32;
o_maxdata : uint32;
o_debugger : uint32;
o_flags : uint8;
o_sntdata : uint16;
o_sntbss : uint16;
end record;
pragma Unreferenced (Auxiliary_Header);
-- Not used, but not removed (just in case)
subtype Name_Str is String (1 .. 8);
type Section_Header is record
s_name : Name_Str;
s_paddr : uint32;
s_vaddr : uint32;
s_size : uint32;
s_scnptr : uint32;
s_relptr : uint32;
s_lnnoptr : uint32;
s_nreloc : uint16;
s_nlnno : uint16;
s_flags : uint32;
end record;
pragma Pack (Section_Header);
STYP_TEXT : constant := 16#0020#;
type Symbol_Entry is record
n_name : Name_Str;
n_value : uint32;
n_scnum : uint16;
n_type : uint16;
n_sclass : uint8;
n_numaux : uint8;
end record;
for Symbol_Entry'Size use 18 * 8;
type Aux_Entry is record
x_scnlen : uint32;
x_parmhash : uint32;
x_snhash : uint16;
x_smtyp : uint8;
x_smclass : uint8;
x_stab : uint32;
x_snstab : uint16;
end record;
for Aux_Entry'Size use 18 * 8;
pragma Pack (Aux_Entry);
C_EXT : constant := 2;
C_HIDEXT : constant := 107;
C_WEAKEXT : constant := 111;
XTY_LD : constant := 2;
-- Magic constant should be documented, especially since it's changed???
function Read_Header (F : in out Mapped_Stream) return Header;
-- Read the object file header
function First_Symbol
(Obj : in out XCOFF32_Object_File) return Object_Symbol;
-- Return the first element in the symbol table, or Null_Symbol if the
-- symbol table is empty.
function Read_Symbol
(Obj : in out XCOFF32_Object_File;
Off : Offset) return Object_Symbol;
-- Read a symbol at offset Off
function Name
(Obj : in out XCOFF32_Object_File;
Sym : Object_Symbol) return String_Ptr_Len;
-- Return the name of the symbol
function Name
(Obj : in out XCOFF32_Object_File;
Sec : Object_Section) return String;
-- Return the name of a section
function Initialize
(F : Mapped_File;
Hdr : Header;
In_Exception : Boolean) return XCOFF32_Object_File;
-- Initialize an object file
function Get_Section
(Obj : in out XCOFF32_Object_File;
Index : uint32) return Object_Section;
-- Fetch a section by index from zero
end XCOFF32_Ops;
-------------
-- ELF_Ops --
-------------
package body ELF_Ops is
function Get_String_Table (Obj : in out ELF_Object_File)
return Object_Section;
-- Fetch the section containing the string table
function Get_Symbol_Table (Obj : in out ELF_Object_File)
return Object_Section;
-- Fetch the section containing the symbol table
function Read_Section_Header
(Obj : in out ELF_Object_File;
Shnum : uint32) return Section_Header;
-- Read the header for an ELF format object section indexed from zero
------------------
-- First_Symbol --
------------------
function First_Symbol
(Obj : in out ELF_Object_File) return Object_Symbol
is
begin
if Obj.Symtab_Last = 0 then
return Null_Symbol;
else
return Read_Symbol (Obj, 0);
end if;
end First_Symbol;
-----------------
-- Get_Section --
-----------------
function Get_Section
(Obj : in out ELF_Object_File;
Shnum : uint32) return Object_Section
is
SHdr : constant Section_Header := Read_Section_Header (Obj, Shnum);
begin
return (Shnum,
Offset (SHdr.Sh_Offset),
uint64 (SHdr.Sh_Addr),
uint64 (SHdr.Sh_Size),
(SHdr.Sh_Flags and SHF_EXECINSTR) /= 0);
end Get_Section;
------------------------
-- Get_String_Table --
------------------------
function Get_String_Table
(Obj : in out ELF_Object_File) return Object_Section
is
begin
-- All cases except MIPS IRIX, string table located in .strtab
if Obj.Arch /= MIPS then
return Get_Section (Obj, ".strtab");
-- On IRIX only .dynstr is available
else
return Get_Section (Obj, ".dynstr");
end if;
end Get_String_Table;
------------------------
-- Get_Symbol_Table --
------------------------
function Get_Symbol_Table
(Obj : in out ELF_Object_File) return Object_Section
is
begin
-- All cases except MIPS IRIX, symbol table located in .symtab
if Obj.Arch /= MIPS then
return Get_Section (Obj, ".symtab");
-- On IRIX, symbol table located somewhere other than .symtab
else
return Get_Section (Obj, ".dynsym");
end if;
end Get_Symbol_Table;
----------------
-- Initialize --
----------------
function Initialize
(F : Mapped_File;
Hdr : Header;
In_Exception : Boolean) return ELF_Object_File
is
Res : ELF_Object_File
(Format => (case uword'Size is
when 64 => ELF64,
when 32 => ELF32,
when others => raise Program_Error));
Sec : Object_Section;
begin
Res.MF := F;
Res.In_Exception := In_Exception;
Res.Num_Sections := uint32 (Hdr.E_Shnum);
case Hdr.E_Machine is
when EM_SPARC
| EM_SPARC32PLUS
=>
Res.Arch := SPARC;
when EM_386 =>
Res.Arch := i386;
when EM_MIPS
| EM_MIPS_RS3_LE
=>
Res.Arch := MIPS;
when EM_PPC =>
Res.Arch := PPC;
when EM_PPC64 =>
Res.Arch := PPC64;
when EM_SPARCV9 =>
Res.Arch := SPARC64;
when EM_IA_64 =>
Res.Arch := IA64;
when EM_X86_64 =>
Res.Arch := x86_64;
when EM_ARM =>
Res.Arch := ARM;
when EM_AARCH64 =>
Res.Arch := AARCH64;
when others =>
raise Format_Error with "unrecognized architecture";
end case;
-- Map section table and section string table
Res.Sectab_Stream := Create_Stream
(F, File_Size (Hdr.E_Shoff),
File_Size (Hdr.E_Shnum) * File_Size (Hdr.E_Shentsize));
Sec := Get_Section (Res, uint32 (Hdr.E_Shstrndx));
Res.Secstr_Stream := Create_Stream (Res, Sec);
-- Map symbol and string table
Sec := Get_Symbol_Table (Res);
Res.Symtab_Stream := Create_Stream (Res, Sec);
Res.Symtab_Last := Offset (Sec.Size);
Sec := Get_String_Table (Res);
Res.Symstr_Stream := Create_Stream (Res, Sec);
return Res;
end Initialize;
-----------------
-- Read_Header --
-----------------
function Read_Header (F : in out Mapped_Stream) return Header is
Hdr : Header;
begin
Seek (F, 0);
Read_Raw (F, Hdr'Address, uint32 (Hdr'Size / SSU));
return Hdr;
end Read_Header;
-------------------------
-- Read_Section_Header --
-------------------------
function Read_Section_Header
(Obj : in out ELF_Object_File;
Shnum : uint32) return Section_Header
is
Shdr : Section_Header;
begin
Seek (Obj.Sectab_Stream, Offset (Shnum * Section_Header'Size / SSU));
Read_Raw (Obj.Sectab_Stream, Shdr'Address, Section_Header'Size / SSU);
return Shdr;
end Read_Section_Header;
-----------------
-- Read_Symbol --
-----------------
function Read_Symbol
(Obj : in out ELF_Object_File;
Off : Offset) return Object_Symbol
is
ST_Entry32 : Symtab_Entry32;
ST_Entry64 : Symtab_Entry64;
Res : Object_Symbol;
begin
Seek (Obj.Symtab_Stream, Off);
case uword'Size is
when 32 =>
Read_Raw (Obj.Symtab_Stream, ST_Entry32'Address,
uint32 (ST_Entry32'Size / SSU));
Res := (Off,
Off + ST_Entry32'Size / SSU,
uint64 (ST_Entry32.St_Value),
uint64 (ST_Entry32.St_Size));
when 64 =>
Read_Raw (Obj.Symtab_Stream, ST_Entry64'Address,
uint32 (ST_Entry64'Size / SSU));
Res := (Off,
Off + ST_Entry64'Size / SSU,
ST_Entry64.St_Value,
ST_Entry64.St_Size);
when others =>
raise Program_Error;
end case;
return Res;
end Read_Symbol;
----------
-- Name --
----------
function Name
(Obj : in out ELF_Object_File;
Sec : Object_Section) return String
is
SHdr : Section_Header;
begin
SHdr := Read_Section_Header (Obj, Sec.Num);
return Offset_To_String (Obj.Secstr_Stream, Offset (SHdr.Sh_Name));
end Name;
function Name
(Obj : in out ELF_Object_File;
Sym : Object_Symbol) return String_Ptr_Len
is
ST_Entry32 : Symtab_Entry32;
ST_Entry64 : Symtab_Entry64;
Name_Off : Offset;
begin
-- Test that this symbol is not null
if Sym = Null_Symbol then
return (null, 0);
end if;
-- Read the symbol table entry
Seek (Obj.Symtab_Stream, Sym.Off);
case uword'Size is
when 32 =>
Read_Raw (Obj.Symtab_Stream, ST_Entry32'Address,
uint32 (ST_Entry32'Size / SSU));
Name_Off := Offset (ST_Entry32.St_Name);
when 64 =>
Read_Raw (Obj.Symtab_Stream, ST_Entry64'Address,
uint32 (ST_Entry64'Size / SSU));
Name_Off := Offset (ST_Entry64.St_Name);
when others =>
raise Program_Error;
end case;
-- Fetch the name from the string table
Seek (Obj.Symstr_Stream, Name_Off);
return Read (Obj.Symstr_Stream);
end Name;
end ELF_Ops;
package ELF32_Ops is new ELF_Ops (uint32);
package ELF64_Ops is new ELF_Ops (uint64);
----------------
-- PECOFF_Ops --
----------------
package body PECOFF_Ops is
function Decode_Name
(Obj : in out PECOFF_Object_File;
Raw_Name : String) return String;
-- A section name is an 8 byte field padded on the right with null
-- characters, or a '\' followed by an ASCII decimal string indicating
-- an offset in to the string table. This routine decodes this
function Get_Section_Virtual_Address
(Obj : in out PECOFF_Object_File;
Index : uint32) return uint64;
-- Fetch the address at which a section is loaded
function Read_Section_Header
(Obj : in out PECOFF_Object_File;
Index : uint32) return Section_Header;
-- Read a header from section table
function String_Table
(Obj : in out PECOFF_Object_File;
Index : Offset) return String;
-- Return an entry from the string table
-----------------
-- Decode_Name --
-----------------
function Decode_Name
(Obj : in out PECOFF_Object_File;
Raw_Name : String) return String
is
Name_Or_Ref : constant String := Trim_Trailing_Nuls (Raw_Name);
Off : Offset;
begin
-- We should never find a symbol with a zero length name. If we do it
-- probably means we are not parsing the symbol table correctly. If
-- this happens we raise a fatal error.
if Name_Or_Ref'Length = 0 then
raise Format_Error with
"found zero length symbol in symbol table";
end if;
if Name_Or_Ref (1) /= '/' then
return Name_Or_Ref;
else
Off := Offset'Value (Name_Or_Ref (2 .. Name_Or_Ref'Last));
return String_Table (Obj, Off);
end if;
end Decode_Name;
------------------
-- First_Symbol --
------------------
function First_Symbol
(Obj : in out PECOFF_Object_File) return Object_Symbol
is
begin
-- Return Null_Symbol in the case that the symbol table is empty
if Obj.Symtab_Last = 0 then
return Null_Symbol;
end if;
return Read_Symbol (Obj, 0);
end First_Symbol;
-----------------
-- Get_Section --
-----------------
function Get_Section
(Obj : in out PECOFF_Object_File;
Index : uint32) return Object_Section
is
Sec : constant Section_Header := Read_Section_Header (Obj, Index);
begin
-- Use VirtualSize instead of SizeOfRawData. The latter is rounded to
-- the page size, so it may add garbage to the content. On the other
-- side, the former may be larger than the latter in case of 0
-- padding.
return (Index,
Offset (Sec.PointerToRawData),
uint64 (Sec.VirtualAddress) + Obj.ImageBase,
uint64 (Sec.VirtualSize),
(Sec.Characteristics and IMAGE_SCN_CNT_CODE) /= 0);
end Get_Section;
---------------------------------
-- Get_Section_Virtual_Address --
---------------------------------
function Get_Section_Virtual_Address
(Obj : in out PECOFF_Object_File;
Index : uint32) return uint64
is
Sec : Section_Header;
begin
-- Try cache
if Index = Obj.GSVA_Sec then
return Obj.GSVA_Addr;
end if;
Obj.GSVA_Sec := Index;
Sec := Read_Section_Header (Obj, Index);
Obj.GSVA_Addr := Obj.ImageBase + uint64 (Sec.VirtualAddress);
return Obj.GSVA_Addr;
end Get_Section_Virtual_Address;
----------------
-- Initialize --
----------------
function Initialize
(F : Mapped_File;
Hdr : Header;
In_Exception : Boolean) return PECOFF_Object_File
is
Res : PECOFF_Object_File
(Format => (case Hdr.Variant is
when PECOFF_Ops.VARIANT_PE32 => PECOFF,
when PECOFF_Ops.VARIANT_PE32_PLUS => PECOFF_PLUS,
when others => raise Program_Error
with "unrecognized PECOFF variant"));
Symtab_Size : constant Offset :=
Offset (Hdr.NumberOfSymbols) * (Symtab_Entry'Size / SSU);
Strtab_Size : uint32;
Hdr_Offset : Offset;
Opt_Offset : File_Size;
Opt_Stream : Mapped_Stream;
begin
Res.MF := F;
Res.In_Exception := In_Exception;
case Hdr.Machine is
when PECOFF_Ops.IMAGE_FILE_MACHINE_I386 =>
Res.Arch := i386;
when PECOFF_Ops.IMAGE_FILE_MACHINE_IA64 =>
Res.Arch := IA64;
when PECOFF_Ops.IMAGE_FILE_MACHINE_AMD64 =>
Res.Arch := x86_64;
when others =>
raise Format_Error with "unrecognized architecture";
end case;
Res.Num_Sections := uint32 (Hdr.NumberOfSections);
-- Map symbol table and the first following word (which is the length
-- of the string table).
Res.Symtab_Last := Symtab_Size;
Res.Symtab_Stream := Create_Stream
(F,
File_Size (Hdr.PointerToSymbolTable),
File_Size (Symtab_Size + 4));
-- Map string table. The first 4 bytes are the length of the string
-- table and are part of it.
Seek (Res.Symtab_Stream, Symtab_Size);
Strtab_Size := Read (Res.Symtab_Stream);
Res.Symstr_Stream := Create_Stream
(F,
File_Size (Hdr.PointerToSymbolTable) + File_Size (Symtab_Size),
File_Size (Strtab_Size));
-- Map section table
Opt_Stream := Create_Stream (Res.Mf, Signature_Loc_Offset, 4);
Hdr_Offset := Offset (uint32'(Read (Opt_Stream)));
Close (Opt_Stream);
Res.Sectab_Stream := Create_Stream
(F,
File_Size (Hdr_Offset +
Size_Of_Standard_Header_Fields +
Offset (Hdr.SizeOfOptionalHeader)),
File_Size (Res.Num_Sections)
* File_Size (Section_Header'Size / SSU));
-- Read optional header and extract image base
Opt_Offset := File_Size (Hdr_Offset + Size_Of_Standard_Header_Fields);
if Res.Format = PECOFF then
declare
Opt_32 : Optional_Header_PE32;
begin
Opt_Stream := Create_Stream
(Res.Mf, Opt_Offset, Opt_32'Size / SSU);
Read_Raw
(Opt_Stream, Opt_32'Address, uint32 (Opt_32'Size / SSU));
Res.ImageBase := uint64 (Opt_32.ImageBase);
Close (Opt_Stream);
end;
else
declare
Opt_64 : Optional_Header_PE64;
begin
Opt_Stream := Create_Stream
(Res.Mf, Opt_Offset, Opt_64'Size / SSU);
Read_Raw
(Opt_Stream, Opt_64'Address, uint32 (Opt_64'Size / SSU));
Res.ImageBase := Opt_64.ImageBase;
Close (Opt_Stream);
end;
end if;
return Res;
end Initialize;
-----------------
-- Read_Symbol --
-----------------
function Read_Symbol
(Obj : in out PECOFF_Object_File;
Off : Offset) return Object_Symbol
is
ST_Entry : Symtab_Entry;
ST_Last : Symtab_Entry;
Aux_Entry : Auxent_Section;
Sz : constant Offset := ST_Entry'Size / SSU;
Result : Object_Symbol;
Noff : Offset;
Sym_Off : Offset;
begin
-- Seek to the successor of Prev
Noff := Off;
loop
Sym_Off := Noff;
Seek (Obj.Symtab_Stream, Sym_Off);
Read_Raw (Obj.Symtab_Stream, ST_Entry'Address, uint32 (Sz));
-- Skip AUX entries
Noff := Noff + Offset (1 + ST_Entry.NumberOfAuxSymbols) * Sz;
exit when ST_Entry.TypeField = Function_Symbol_Type
and then ST_Entry.SectionNumber > 0;
if Noff >= Obj.Symtab_Last then
return Null_Symbol;
end if;
end loop;
-- Construct the symbol
Result :=
(Off => Sym_Off,
Next => Noff,
Value => uint64 (ST_Entry.Value),
Size => 0);
-- Set the size as accurately as possible
-- The size of a symbol is not directly available so we try scanning
-- to the next function and assuming the code ends there.
loop
-- Read symbol and AUX entries
Sym_Off := Noff;
Seek (Obj.Symtab_Stream, Sym_Off);
Read_Raw (Obj.Symtab_Stream, ST_Last'Address, uint32 (Sz));
for I in 1 .. ST_Last.NumberOfAuxSymbols loop
Read_Raw (Obj.Symtab_Stream, Aux_Entry'Address, uint32 (Sz));
end loop;
Noff := Noff + Offset (1 + ST_Last.NumberOfAuxSymbols) * Sz;
if ST_Last.TypeField = Function_Symbol_Type then
if ST_Last.SectionNumber = ST_Entry.SectionNumber
and then ST_Last.Value >= ST_Entry.Value
then
-- Symbol is a function past ST_Entry
Result.Size := uint64 (ST_Last.Value - ST_Entry.Value);
else
-- Not correlated function
Result.Next := Sym_Off;
end if;
exit;
elsif ST_Last.SectionNumber = ST_Entry.SectionNumber
and then ST_Last.TypeField = Not_Function_Symbol_Type
and then ST_Last.StorageClass = 3
and then ST_Last.NumberOfAuxSymbols = 1
then
-- Symbol is a section
Result.Size := uint64 (ST_Last.Value + Aux_Entry.Length
- ST_Entry.Value);
Result.Next := Noff;
exit;
end if;
exit when Noff >= Obj.Symtab_Last;
end loop;
-- Relocate the address
Result.Value :=
Result.Value + Get_Section_Virtual_Address
(Obj, uint32 (ST_Entry.SectionNumber - 1));
return Result;
end Read_Symbol;
------------------
-- Read_Header --
------------------
function Read_Header (F : in out Mapped_Stream) return Header is
Hdr : Header;
Off : int32;
begin
-- Skip the MSDOS stub, and seek directly to the file offset
Seek (F, Signature_Loc_Offset);
Off := Read (F);
-- Read the COFF file header
Seek (F, Offset (Off));
Read_Raw (F, Hdr'Address, uint32 (Hdr'Size / SSU));
return Hdr;
end Read_Header;
-------------------------
-- Read_Section_Header --
-------------------------
function Read_Section_Header
(Obj : in out PECOFF_Object_File;
Index : uint32) return Section_Header
is
Sec : Section_Header;
begin
Seek (Obj.Sectab_Stream, Offset (Index * Section_Header'Size / SSU));
Read_Raw (Obj.Sectab_Stream, Sec'Address, Section_Header'Size / SSU);
return Sec;
end Read_Section_Header;
----------
-- Name --
----------
function Name
(Obj : in out PECOFF_Object_File;
Sec : Object_Section) return String
is
Shdr : constant Section_Header := Read_Section_Header (Obj, Sec.Num);
begin
return Decode_Name (Obj, Shdr.Name);
end Name;
-------------------
-- String_Table --
-------------------
function String_Table
(Obj : in out PECOFF_Object_File;
Index : Offset) return String
is
begin
-- An index of zero is used to represent an empty string, as the
-- first word of the string table is specified to contain the length
-- of the table rather than its contents.
if Index = 0 then
return "";
else
return Offset_To_String (Obj.Symstr_Stream, Index);
end if;
end String_Table;
----------
-- Name --
----------
function Name
(Obj : in out PECOFF_Object_File;
Sym : Object_Symbol) return String_Ptr_Len
is
ST_Entry : Symtab_Entry;
begin
Seek (Obj.Symtab_Stream, Sym.Off);
Read_Raw (Obj.Symtab_Stream, ST_Entry'Address, ST_Entry'Size / SSU);
declare
-- Symbol table entries are packed and Table_Entry.Name may not be
-- sufficiently aligned to interpret as a 32 bit word, so it is
-- copied to a temporary
Aligned_Name : Name_Str := ST_Entry.Name;
for Aligned_Name'Alignment use 4;
First_Word : uint32;
pragma Import (Ada, First_Word);
-- Suppress initialization in Normalized_Scalars mode
for First_Word'Address use Aligned_Name (1)'Address;
Second_Word : uint32;
pragma Import (Ada, Second_Word);
-- Suppress initialization in Normalized_Scalars mode
for Second_Word'Address use Aligned_Name (5)'Address;
begin
if First_Word = 0 then
-- Second word is an offset in the symbol table
if Second_Word = 0 then
return (null, 0);
else
Seek (Obj.Symstr_Stream, int64 (Second_Word));
return Read (Obj.Symstr_Stream);
end if;
else
-- Inlined symbol name
Seek (Obj.Symtab_Stream, Sym.Off);
return To_String_Ptr_Len (Read (Obj.Symtab_Stream), 8);
end if;
end;
end Name;
end PECOFF_Ops;
-----------------
-- XCOFF32_Ops --
-----------------
package body XCOFF32_Ops is
function Read_Section_Header
(Obj : in out XCOFF32_Object_File;
Index : uint32) return Section_Header;
-- Read a header from section table
-----------------
-- Read_Symbol --
-----------------
function Read_Symbol
(Obj : in out XCOFF32_Object_File;
Off : Offset) return Object_Symbol
is
Sym : Symbol_Entry;
Sz : constant Offset := Symbol_Entry'Size / SSU;
Aux : Aux_Entry;
Result : Object_Symbol;
Noff : Offset;
Sym_Off : Offset;
procedure Read_LD_Symbol;
-- Read the next LD symbol
--------------------
-- Read_LD_Symbol --
--------------------
procedure Read_LD_Symbol is
begin
loop
Sym_Off := Noff;
Read_Raw (Obj.Symtab_Stream, Sym'Address, uint32 (Sz));
Noff := Noff + Offset (1 + Sym.n_numaux) * Sz;
for J in 1 .. Sym.n_numaux loop
Read_Raw (Obj.Symtab_Stream, Aux'Address, uint32 (Sz));
end loop;
exit when Noff >= Obj.Symtab_Last;
exit when Sym.n_numaux = 1
and then Sym.n_scnum /= 0
and then (Sym.n_sclass = C_EXT
or else Sym.n_sclass = C_HIDEXT
or else Sym.n_sclass = C_WEAKEXT)
and then Aux.x_smtyp = XTY_LD;
end loop;
end Read_LD_Symbol;
-- Start of processing for Read_Symbol
begin
Seek (Obj.Symtab_Stream, Off);
Noff := Off;
Read_LD_Symbol;
if Noff >= Obj.Symtab_Last then
return Null_Symbol;
end if;
-- Construct the symbol
Result := (Off => Sym_Off,
Next => Noff,
Value => uint64 (Sym.n_value),
Size => 0);
-- Look for the next symbol to compute the size
Read_LD_Symbol;
if Noff >= Obj.Symtab_Last then
return Null_Symbol;
end if;
Result.Size := uint64 (Sym.n_value) - Result.Value;
Result.Next := Sym_Off;
return Result;
end Read_Symbol;
------------------
-- First_Symbol --
------------------
function First_Symbol
(Obj : in out XCOFF32_Object_File) return Object_Symbol
is
begin
-- Return Null_Symbol in the case that the symbol table is empty
if Obj.Symtab_Last = 0 then
return Null_Symbol;
end if;
return Read_Symbol (Obj, 0);
end First_Symbol;
----------------
-- Initialize --
----------------
function Initialize
(F : Mapped_File;
Hdr : Header;
In_Exception : Boolean) return XCOFF32_Object_File
is
Res : XCOFF32_Object_File (Format => XCOFF32);
Strtab_Sz : uint32;
begin
Res.Mf := F;
Res.In_Exception := In_Exception;
Res.Arch := PPC;
-- Map sections table
Res.Num_Sections := uint32 (Hdr.f_nscns);
Res.Sectab_Stream := Create_Stream
(F,
File_Size (Header'Size / SSU) + File_Size (Hdr.f_opthdr),
File_Size (Hdr.f_nscns) * (Section_Header'Size / SSU));
-- Map symbols table
Res.Symtab_Last := Offset (Hdr.f_nscns) * (Symbol_Entry'Size / SSU);
Res.Symtab_Stream := Create_Stream
(F,
File_Size (Hdr.f_symptr),
File_Size (Res.Symtab_Last) + 4);
-- Map string table
Seek (Res.Symtab_Stream, Res.Symtab_Last);
Strtab_Sz := Read (Res.Symtab_Stream);
Res.Symstr_Stream := Create_Stream
(F,
File_Size (Res.Symtab_Last) + 4,
File_Size (Strtab_Sz) - 4);
return Res;
end Initialize;
-----------------
-- Get_Section --
-----------------
function Get_Section
(Obj : in out XCOFF32_Object_File;
Index : uint32) return Object_Section
is
Sec : constant Section_Header := Read_Section_Header (Obj, Index);
begin
return (Index, Offset (Sec.s_scnptr),
uint64 (Sec.s_vaddr),
uint64 (Sec.s_size),
(Sec.s_flags and STYP_TEXT) /= 0);
end Get_Section;
-----------------
-- Read_Header --
-----------------
function Read_Header (F : in out Mapped_Stream) return Header is
Hdr : Header;
begin
Seek (F, 0);
Read_Raw (F, Hdr'Address, uint32 (Hdr'Size / SSU));
return Hdr;
end Read_Header;
-------------------------
-- Read_Section_Header --
-------------------------
function Read_Section_Header
(Obj : in out XCOFF32_Object_File;
Index : uint32) return Section_Header
is
Sec : Section_Header;
begin
-- Seek to the end of the object header
Seek (Obj.Sectab_Stream, Offset (Index * Section_Header'Size / SSU));
-- Read the section
Read_Raw (Obj.Sectab_Stream, Sec'Address, Section_Header'Size / SSU);
return Sec;
end Read_Section_Header;
----------
-- Name --
----------
function Name
(Obj : in out XCOFF32_Object_File;
Sec : Object_Section) return String
is
Hdr : Section_Header;
begin
Hdr := Read_Section_Header (Obj, Sec.Num);
return Trim_Trailing_Nuls (Hdr.s_name);
end Name;
----------
-- Name --
----------
function Name
(Obj : in out XCOFF32_Object_File;
Sym : Object_Symbol) return String_Ptr_Len
is
Symbol : Symbol_Entry;
begin
Seek (Obj.Symtab_Stream, Sym.Off);
Read_Raw (Obj.Symtab_Stream, Symbol'Address, Symbol'Size / SSU);
declare
First_Word : uint32;
pragma Import (Ada, First_Word);
-- Suppress initialization in Normalized_Scalars mode
for First_Word'Address use Symbol.n_name (1)'Address;
Second_Word : uint32;
pragma Import (Ada, Second_Word);
-- Suppress initialization in Normalized_Scalars mode
for Second_Word'Address use Symbol.n_name (5)'Address;
begin
if First_Word = 0 then
if Second_Word = 0 then
return (null, 0);
else
Seek (Obj.Symstr_Stream, int64 (Second_Word));
return Read (Obj.Symstr_Stream);
end if;
else
Seek (Obj.Symtab_Stream, Sym.Off);
return To_String_Ptr_Len (Read (Obj.Symstr_Stream), 8);
end if;
end;
end Name;
end XCOFF32_Ops;
----------
-- Arch --
----------
function Arch (Obj : Object_File) return Object_Arch is
begin
return Obj.Arch;
end Arch;
function Create_Stream
(Mf : Mapped_File;
File_Offset : File_Size;
File_Length : File_Size)
return Mapped_Stream
is
Region : Mapped_Region;
begin
Read (Mf, Region, File_Offset, File_Length, False);
return (Region, 0, Offset (File_Length));
end Create_Stream;
function Create_Stream
(Obj : Object_File;
Sec : Object_Section) return Mapped_Stream
is
begin
return Create_Stream (Obj.Mf, File_Size (Sec.Off), File_Size (Sec.Size));
end Create_Stream;
procedure Tell (Obj : in out Mapped_Stream; Off : out Offset) is
begin
Off := Obj.Off;
end Tell;
function Tell (Obj : Mapped_Stream) return Offset is
begin
return Obj.Off;
end Tell;
function Length (Obj : Mapped_Stream) return Offset is
begin
return Obj.Len;
end Length;
-----------
-- Close --
-----------
procedure Close (S : in out Mapped_Stream) is
begin
Free (S.Region);
end Close;
procedure Close (Obj : in out Object_File) is
begin
Close (Obj.Symtab_Stream);
Close (Obj.Symstr_Stream);
Close (Obj.Sectab_Stream);
case Obj.Format is
when ELF =>
Close (Obj.Secstr_Stream);
when Any_PECOFF =>
null;
when XCOFF32 =>
null;
end case;
Close (Obj.Mf);
end Close;
------------------------
-- Strip_Leading_Char --
------------------------
function Strip_Leading_Char
(Obj : in out Object_File;
Sym : String_Ptr_Len) return Positive
is
begin
if (Obj.Format = PECOFF and then Sym.Ptr (1) = '_')
or else
(Obj.Format = XCOFF32 and then Sym.Ptr (1) = '.')
then
return 2;
else
return 1;
end if;
end Strip_Leading_Char;
----------------------
-- Decoded_Ada_Name --
----------------------
function Decoded_Ada_Name
(Obj : in out Object_File;
Sym : String_Ptr_Len) return String
is
procedure gnat_decode
(Coded_Name_Addr : Address;
Ada_Name_Addr : Address;
Verbose : int);
pragma Import (C, gnat_decode, "__gnat_decode");
subtype size_t is Interfaces.C.size_t;
Sym_Name : constant String :=
String (Sym.Ptr (1 .. Sym.Len)) & ASCII.NUL;
Decoded : char_array (0 .. size_t (Sym.Len) * 2 + 60);
Off : Natural;
begin
-- In the PECOFF case most but not all symbol table entries have an
-- extra leading underscore. In this case we trim it.
Off := Strip_Leading_Char (Obj, Sym);
gnat_decode (Sym_Name (Off)'Address, Decoded'Address, 0);
return To_Ada (Decoded);
end Decoded_Ada_Name;
------------------
-- First_Symbol --
------------------
function First_Symbol (Obj : in out Object_File) return Object_Symbol is
begin
case Obj.Format is
when ELF32 => return ELF32_Ops.First_Symbol (Obj);
when ELF64 => return ELF64_Ops.First_Symbol (Obj);
when Any_PECOFF => return PECOFF_Ops.First_Symbol (Obj);
when XCOFF32 => return XCOFF32_Ops.First_Symbol (Obj);
end case;
end First_Symbol;
------------
-- Format --
------------
function Format (Obj : Object_File) return Object_Format is
begin
return Obj.Format;
end Format;
----------------------
-- Get_Load_Address --
----------------------
function Get_Load_Address (Obj : Object_File) return uint64 is
begin
case Obj.Format is
when ELF => return 0;
when Any_PECOFF => return Obj.ImageBase;
when XCOFF32 => raise Format_Error;
end case;
end Get_Load_Address;
-----------------
-- Get_Section --
-----------------
function Get_Section
(Obj : in out Object_File;
Shnum : uint32) return Object_Section
is
begin
case Obj.Format is
when ELF32 => return ELF32_Ops.Get_Section (Obj, Shnum);
when ELF64 => return ELF64_Ops.Get_Section (Obj, Shnum);
when Any_PECOFF => return PECOFF_Ops.Get_Section (Obj, Shnum);
when XCOFF32 => return XCOFF32_Ops.Get_Section (Obj, Shnum);
end case;
end Get_Section;
function Get_Section
(Obj : in out Object_File;
Sec_Name : String) return Object_Section
is
Sec : Object_Section;
begin
for J in 0 .. Obj.Num_Sections - 1 loop
Sec := Get_Section (Obj, J);
if Name (Obj, Sec) = Sec_Name then
return Sec;
end if;
end loop;
if Obj.In_Exception then
return Null_Section;
else
raise Format_Error with "could not find section in object file";
end if;
end Get_Section;
----------------------
-- Get_Xcode_Bounds --
----------------------
procedure Get_Xcode_Bounds
(Obj : in out Object_File;
Low, High : out uint64)
is
Sec : Object_Section;
begin
-- First set as an empty range
Low := uint64'Last;
High := uint64'First;
-- Now find the lowest and highest offsets
-- attached to executable code sections
for Idx in 1 .. Num_Sections (Obj) loop
Sec := Get_Section (Obj, Idx - 1);
if Sec.Flag_Xcode then
if Sec.Addr < Low then
Low := Sec.Addr;
end if;
if Sec.Addr + Sec.Size > High then
High := Sec.Addr + Sec.Size;
end if;
end if;
end loop;
end Get_Xcode_Bounds;
----------
-- Name --
----------
function Name
(Obj : in out Object_File;
Sec : Object_Section) return String
is
begin
case Obj.Format is
when ELF32 => return ELF32_Ops.Name (Obj, Sec);
when ELF64 => return ELF64_Ops.Name (Obj, Sec);
when Any_PECOFF => return PECOFF_Ops.Name (Obj, Sec);
when XCOFF32 => return XCOFF32_Ops.Name (Obj, Sec);
end case;
end Name;
function Name
(Obj : in out Object_File;
Sym : Object_Symbol) return String_Ptr_Len
is
begin
case Obj.Format is
when ELF32 => return ELF32_Ops.Name (Obj, Sym);
when ELF64 => return ELF64_Ops.Name (Obj, Sym);
when Any_PECOFF => return PECOFF_Ops.Name (Obj, Sym);
when XCOFF32 => return XCOFF32_Ops.Name (Obj, Sym);
end case;
end Name;
-----------------
-- Next_Symbol --
-----------------
function Next_Symbol
(Obj : in out Object_File;
Prev : Object_Symbol) return Object_Symbol
is
begin
-- Test whether we've reached the end of the symbol table
if Prev.Next >= Obj.Symtab_Last then
return Null_Symbol;
end if;
return Read_Symbol (Obj, Prev.Next);
end Next_Symbol;
---------
-- Num --
---------
function Num (Sec : Object_Section) return uint32 is
begin
return Sec.Num;
end Num;
------------------
-- Num_Sections --
------------------
function Num_Sections (Obj : Object_File) return uint32 is
begin
return Obj.Num_Sections;
end Num_Sections;
---------
-- Off --
---------
function Off (Sec : Object_Section) return Offset is
begin
return Sec.Off;
end Off;
function Off (Sym : Object_Symbol) return Offset is
begin
return Sym.Off;
end Off;
----------------------
-- Offset_To_String --
----------------------
function Offset_To_String
(S : in out Mapped_Stream;
Off : Offset) return String
is
Buf : Buffer;
begin
Seek (S, Off);
Read_C_String (S, Buf);
return To_String (Buf);
end Offset_To_String;
----------
-- Open --
----------
function Open
(File_Name : String;
In_Exception : Boolean := False) return Object_File_Access
is
F : Mapped_File;
Hdr_Stream : Mapped_Stream;
begin
-- Open the file
F := Open_Read_No_Exception (File_Name);
if F = Invalid_Mapped_File then
if In_Exception then
return null;
else
raise IO_Error with "could not open object file";
end if;
end if;
Hdr_Stream := Create_Stream (F, 0, 4096);
declare
Hdr : constant ELF32_Ops.Header := ELF32_Ops.Read_Header (Hdr_Stream);
begin
-- Look for the magic numbers for the ELF case
if Hdr.E_Ident (0) = 16#7F# and then
Hdr.E_Ident (1) = Character'Pos ('E') and then
Hdr.E_Ident (2) = Character'Pos ('L') and then
Hdr.E_Ident (3) = Character'Pos ('F') and then
Hdr.E_Ident (4) = ELF32_Ops.ELFCLASS32
then
Close (Hdr_Stream);
return new Object_File'
(ELF32_Ops.Initialize (F, Hdr, In_Exception));
end if;
end;
declare
Hdr : constant ELF64_Ops.Header :=
ELF64_Ops.Read_Header (Hdr_Stream);
begin
-- Look for the magic numbers for the ELF case
if Hdr.E_Ident (0) = 16#7F# and then
Hdr.E_Ident (1) = Character'Pos ('E') and then
Hdr.E_Ident (2) = Character'Pos ('L') and then
Hdr.E_Ident (3) = Character'Pos ('F') and then
Hdr.E_Ident (4) = ELF32_Ops.ELFCLASS64
then
Close (Hdr_Stream);
return new Object_File'
(ELF64_Ops.Initialize (F, Hdr, In_Exception));
end if;
end;
declare
Hdr : constant PECOFF_Ops.Header :=
PECOFF_Ops.Read_Header (Hdr_Stream);
begin
-- Test the magic numbers
if Hdr.Magics (0) = Character'Pos ('P') and then
Hdr.Magics (1) = Character'Pos ('E') and then
Hdr.Magics (2) = 0 and then
Hdr.Magics (3) = 0
then
Close (Hdr_Stream);
return new Object_File'
(PECOFF_Ops.Initialize (F, Hdr, In_Exception));
end if;
exception
-- If this is not a PECOFF file then we've done a seek and read to a
-- random address, possibly raising IO_Error
when IO_Error =>
null;
end;
declare
Hdr : constant XCOFF32_Ops.Header :=
XCOFF32_Ops.Read_Header (Hdr_Stream);
begin
-- Test the magic numbers
if Hdr.f_magic = 8#0737# then
Close (Hdr_Stream);
return new Object_File'
(XCOFF32_Ops.Initialize (F, Hdr, In_Exception));
end if;
end;
Close (Hdr_Stream);
if In_Exception then
return null;
else
raise Format_Error with "unrecognized object format";
end if;
end Open;
----------
-- Read --
----------
function Read (S : in out Mapped_Stream) return Mmap.Str_Access is
function To_Str_Access is
new Ada.Unchecked_Conversion (Address, Str_Access);
begin
return To_Str_Access (Data (S.Region) (Natural (S.Off + 1))'Address);
end Read;
function Read (S : in out Mapped_Stream) return String_Ptr_Len is
begin
return To_String_Ptr_Len (Read (S));
end Read;
procedure Check_Read_Offset (S : Mapped_Stream; Size : uint32) is
begin
if S.Off + Offset (Size) > Offset (Last (S.Region)) then
raise IO_Error with "could not read from object file";
end if;
end Check_Read_Offset;
procedure Read_Raw
(S : in out Mapped_Stream;
Addr : Address;
Size : uint32)
is
function To_Str_Access is
new Ada.Unchecked_Conversion (Address, Str_Access);
Sz : constant Offset := Offset (Size);
begin
-- Check size
pragma Debug (Check_Read_Offset (S, Size));
-- Copy data
To_Str_Access (Addr) (1 .. Positive (Sz)) :=
Data (S.Region) (Positive (S.Off + 1) .. Positive (S.Off + Sz));
-- Update offset
S.Off := S.Off + Sz;
end Read_Raw;
function Read (S : in out Mapped_Stream) return uint8 is
Data : uint8;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
function Read (S : in out Mapped_Stream) return uint16 is
Data : uint16;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
function Read (S : in out Mapped_Stream) return uint32 is
Data : uint32;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
function Read (S : in out Mapped_Stream) return uint64 is
Data : uint64;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
function Read (S : in out Mapped_Stream) return int8 is
Data : int8;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
function Read (S : in out Mapped_Stream) return int16 is
Data : int16;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
function Read (S : in out Mapped_Stream) return int32 is
Data : int32;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
function Read (S : in out Mapped_Stream) return int64 is
Data : int64;
begin
Read_Raw (S, Data'Address, Data'Size / SSU);
return Data;
end Read;
------------------
-- Read_Address --
------------------
function Read_Address
(Obj : Object_File; S : in out Mapped_Stream) return uint64
is
Address_32 : uint32;
Address_64 : uint64;
begin
case Obj.Arch is
when i386
| MIPS
| PPC
| SPARC
| ARM
=>
Address_32 := Read (S);
return uint64 (Address_32);
when AARCH64
| IA64
| PPC64
| SPARC64
| x86_64
=>
Address_64 := Read (S);
return Address_64;
when Unknown =>
raise Format_Error with "unrecognized machine architecture";
end case;
end Read_Address;
-------------------
-- Read_C_String --
-------------------
procedure Read_C_String (S : in out Mapped_Stream; B : out Buffer) is
J : Integer := 0;
begin
loop
-- Handle overflow case
if J = B'Last then
B (J) := 0;
exit;
end if;
B (J) := Read (S);
exit when B (J) = 0;
J := J + 1;
end loop;
end Read_C_String;
-------------------
-- Read_C_String --
-------------------
function Read_C_String (S : in out Mapped_Stream) return Str_Access is
Res : constant Str_Access := Read (S);
begin
for J in Res'Range loop
if S.Off + Offset (J - 1) > Offset (Last (S.Region)) then
raise IO_Error with "could not read from object file";
end if;
if Res (J) = ASCII.NUL then
S.Off := S.Off + Offset (J);
return Res;
end if;
end loop;
-- Overflow case
raise Constraint_Error;
end Read_C_String;
-----------------
-- Read_LEB128 --
-----------------
function Read_LEB128 (S : in out Mapped_Stream) return uint32 is
B : uint8;
Shift : Integer := 0;
Res : uint32 := 0;
begin
loop
B := Read (S);
Res := Res or Shift_Left (uint32 (B and 16#7f#), Shift);
exit when (B and 16#80#) = 0;
Shift := Shift + 7;
end loop;
return Res;
end Read_LEB128;
function Read_LEB128 (S : in out Mapped_Stream) return int32 is
B : uint8;
Shift : Integer := 0;
Res : uint32 := 0;
begin
loop
B := Read (S);
Res := Res or Shift_Left (uint32 (B and 16#7f#), Shift);
Shift := Shift + 7;
exit when (B and 16#80#) = 0;
end loop;
if Shift < 32 and then (Res and Shift_Left (1, Shift - 1)) /= 0 then
Res := Res or Shift_Left (-1, Shift);
end if;
return To_int32 (Res);
end Read_LEB128;
-----------------
-- Read_Symbol --
-----------------
function Read_Symbol
(Obj : in out Object_File;
Off : Offset) return Object_Symbol
is
begin
case Obj.Format is
when ELF32 => return ELF32_Ops.Read_Symbol (Obj, Off);
when ELF64 => return ELF64_Ops.Read_Symbol (Obj, Off);
when Any_PECOFF => return PECOFF_Ops.Read_Symbol (Obj, Off);
when XCOFF32 => return XCOFF32_Ops.Read_Symbol (Obj, Off);
end case;
end Read_Symbol;
----------
-- Seek --
----------
procedure Seek (S : in out Mapped_Stream; Off : Offset) is
begin
if Off < 0 or else Off > Offset (Last (S.Region)) then
raise IO_Error with "could not seek to offset in object file";
end if;
S.Off := Off;
end Seek;
----------
-- Size --
----------
function Size (Sec : Object_Section) return uint64 is
begin
return Sec.Size;
end Size;
function Size (Sym : Object_Symbol) return uint64 is
begin
return Sym.Size;
end Size;
------------
-- Strlen --
------------
function Strlen (Buf : Buffer) return int32 is
begin
return int32 (CRTL.strlen (Buf'Address));
end Strlen;
-----------
-- Spans --
-----------
function Spans (Sym : Object_Symbol; Addr : uint64) return Boolean is
begin
return Addr >= Sym.Value and then Addr < Sym.Value + Sym.Size;
end Spans;
---------------
-- To_String --
---------------
function To_String (Buf : Buffer) return String is
Result : String (1 .. Integer (CRTL.strlen (Buf'Address)));
for Result'Address use Buf'Address;
pragma Import (Ada, Result);
begin
return Result;
end To_String;
-----------------------
-- To_String_Ptr_Len --
-----------------------
function To_String_Ptr_Len
(Ptr : Mmap.Str_Access;
Max_Len : Natural := Natural'Last) return String_Ptr_Len
is
begin
for I in 1 .. Max_Len loop
if Ptr (I) = ASCII.NUL then
return (Ptr, I - 1);
end if;
end loop;
return (Ptr, Max_Len);
end To_String_Ptr_Len;
------------------------
-- Trim_Trailing_Nuls --
------------------------
function Trim_Trailing_Nuls (Str : String) return String is
begin
for J in Str'Range loop
if Str (J) = ASCII.NUL then
return Str (Str'First .. J - 1);
end if;
end loop;
return Str;
end Trim_Trailing_Nuls;
-----------
-- Value --
-----------
function Value (Sym : Object_Symbol) return uint64 is
begin
return Sym.Value;
end Value;
end System.Object_Reader;
|