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
|
/*
* dlf.c
*
* $Id: dlf.c,v 1.4 2009/04/09 17:53:08 source Exp $
*
* dynamic load functions
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2006 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "Dk.h"
#include "dlf.h"
#include <errno.h>
#ifdef DLDAPI_DEFINED
#undef DLDAPI_DEFINED
#endif
/*********************************
*
* SRV4 compatible dlopen
*
*********************************/
#ifdef DLDAPI_SVR4_DLFCN
#define DLDAPI_DEFINED
DLF_VERSION ("SVR4 (dlfcn)");
#endif
/*********************************
*
* HP/UX
*
*********************************/
#ifdef DLDAPI_HP_SHL
#define DLDAPI_DEFINED
DLF_VERSION ("HP/UX (shl)");
#include <dl.h>
void *
dlopen (char *path, int mode)
{
return (void *) shl_load ((char *) (path), BIND_IMMEDIATE | BIND_NONFATAL, 0L);
}
void *
dlsym (void *hdll, char *sym)
{
void *symaddr = 0;
int ret;
#if 0
/*
* VOS does not need a handle to itself
*/
if (!hdll)
hdll = (void *) PROG_HANDLE;
#endif
/*
* Remember, a driver may export calls as function pointers (i.e. with type TYPE_DATA) rather than as functions (i.e. with
* type TYPE_PROCEDURE). Thus, to be safe, we uses TYPE_UNDEFINED to cover all of them.
*/
ret = shl_findsym ((shl_t *) & hdll, sym, TYPE_UNDEFINED, &symaddr);
if (ret == -1)
return 0;
return symaddr;
}
char *
dlerror ()
{
extern char *strerror ();
return strerror (errno);
}
int
dlclose (void *hdll)
{
struct shl_descriptor d;
/*
* As HP/UX does not use a reference counter for unloading,
* we can only unload the driver when it is loaded once.
*/
if (shl_gethandle_r ((shl_t) hdll, &d) < 0 || d.ref_count > 1)
{
return 0;
}
return shl_unload ((shl_t) hdll);
}
#endif /* end of HP/UX Section */
/*********************************
*
* IBM AIX
*
*********************************/
#ifdef DLDAPI_AIX_LOAD
#define DLDAPI_DEFINED
DLF_VERSION ("AIX (ldr)");
#include <sys/types.h>
#include <sys/ldr.h>
#include <sys/stat.h>
#include <nlist.h>
#ifndef HTAB_SIZE
#define HTAB_SIZE 256
#endif
#define FACTOR 0.618039887 /* i.e. (sqrt(5) - 1)/2 */
#ifndef ENTRY_SYM
#define ENTRY_SYM ".__start" /* default entry point for aix */
#endif
typedef struct slot_s
{
char *sym;
long fdesc[3]; /* 12 bytes function descriptor */
struct slot_s *next;
}
slot_t;
/*
* Note: on AIX, a function pointer actually points to a function descriptor,
* a 12 bytes data.
*
* The first 4 bytes is the virtual address of the function.
* The next 4 bytes is the virtual address of TOC (Table of Contents) of the
* object module the function belong to.
* The last 4 bytes are always 0 for C and Fortran functions.
*
* Every object module has an entry point (which can be specified at link
* time by -e ld option). iODBC driver manager requires ODBC driver shared
* library always use the default entry point (so you shouldn't use -e ld
* option when creating a driver share library).
*
* load() returns the function descriptor of a module's entry point.
* From which we can calculate function descriptors of other functions in the
* same module by using the fact that the load() does not change the relative
* offset of functions to their module entry point (i.e the offset in memory
* loaded by load() will be as same as in the module library file).
*/
typedef slot_t *hent_t;
typedef struct nlist nlist_t;
typedef struct stat stat_t;
typedef struct obj
{
int dev; /* device id */
int ino; /* inode number */
char *path; /* file name */
int (*pentry) (); /* entry point of this share library */
int refn; /* number of reference */
hent_t htab[HTAB_SIZE];
struct obj *next;
}
obj_t;
static char *errmsg = 0;
static void
init_htab (hent_t * ht)
/* initialize a hashing table */
{
int i;
for (i = 0; i < HTAB_SIZE; i++)
ht[i] = (slot_t *) 0;
return;
}
static void
clean_htab (hent_t * ht)
/* free all slots */
{
int i;
slot_t *ent;
slot_t *tent;
for (i = 0; i < HTAB_SIZE; i++)
{
for (ent = ht[i]; ent;)
{
tent = ent->next;
free (ent->sym);
free (ent);
ent = tent;
}
ht[i] = 0;
}
return;
}
static int
hash (char *sym)
{
int a, key;
double f;
if (!sym || !*sym)
return 0;
for (key = *sym; *sym; sym++)
{
key += *sym;
a = key;
key = (int) ((a << 8) + (key >> 8));
key = (key > 0) ? key : -key;
}
f = key * FACTOR;
a = (int) f;
return (int) ((HTAB_SIZE - 1) * (f - a));
}
static hent_t
search (hent_t * htab, char *sym)
/* search hashing table to find a matched slot */
{
int key;
slot_t *ent;
key = hash (sym);
for (ent = htab[key]; ent; ent = ent->next)
{
if (!strcmp (ent->sym, sym))
return ent;
}
return 0; /* no match */
}
static void
insert (hent_t * htab, slot_t * ent)
/* insert a new slot to hashing table */
{
int key;
key = hash (ent->sym);
ent->next = htab[key];
htab[key] = ent;
return;
}
static slot_t *
slot_alloc (char *sym)
/* allocate a new slot with symbol */
{
slot_t *ent;
ent = (slot_t *) malloc (sizeof (slot_t));
ent->sym = (char *) malloc (strlen (sym) + 1);
if (!ent->sym)
{
free (ent);
return 0;
}
strcpy (ent->sym, sym);
return ent;
}
static obj_t *obj_list = 0;
void *
dlopen (char *file, int mode)
{
stat_t st;
obj_t *pobj;
char buf[1024];
if (!file || !*file)
{
errno = EINVAL;
return 0;
}
errno = 0;
errmsg = 0;
if (stat (file, &st))
return 0;
for (pobj = obj_list; pobj; pobj = pobj->next)
/* find a match object */
{
if (pobj->ino == st.st_ino && pobj->dev == st.st_dev)
{
/*
* found a match. increase its reference count and return its address
*/
pobj->refn++;
return pobj;
}
}
pobj = (obj_t *) malloc (sizeof (obj_t));
if (!pobj)
return 0;
pobj->path = (char *) malloc (strlen (file) + 1);
if (!pobj->path)
{
free (pobj);
return 0;
}
strcpy (pobj->path, file);
pobj->dev = st.st_dev;
pobj->ino = st.st_ino;
pobj->refn = 1;
pobj->pentry = (int (*)()) load (file, 0, 0);
if (!pobj->pentry)
{
free (pobj->path);
free (pobj);
return 0;
}
init_htab (pobj->htab);
pobj->next = obj_list;
obj_list = pobj;
return pobj;
}
int
dlclose (void *hobj)
{
obj_t *pobj = (obj_t *) hobj;
obj_t *tpobj;
int match = 0;
if (!hobj)
{
errno = EINVAL;
return -1;
}
errno = 0;
errmsg = 0;
if (pobj == obj_list)
{
pobj->refn--;
if (pobj->refn)
return 0;
match = 1;
obj_list = pobj->next;
}
for (tpobj = obj_list; !match && tpobj; tpobj = tpobj->next)
{
if (tpobj->next == pobj)
{
pobj->refn--;
if (pobj->refn)
return 0;
match = 1;
tpobj->next = pobj->next;
}
}
if (match)
{
unload ((void *) (pobj->pentry));
clean_htab (pobj->htab);
free (pobj->path);
free (pobj);
}
return 0;
}
char *
dlerror ()
{
extern char *sys_errlist[];
if (!errmsg || !errmsg[0])
{
if (errno >= 0)
return sys_errlist[errno];
return "";
}
return errmsg;
}
void *
dlsym (void *hdl, char *sym)
{
nlist_t nl[3];
obj_t *pobj = (obj_t *) hdl;
slot_t *ent;
int (*fp) ();
long lbuf[3];
if (!hdl || !(pobj->htab) || !sym || !*sym)
{
errno = EINVAL;
return 0;
}
errno = 0;
errmsg = 0;
ent = search (pobj->htab, sym);
if (ent)
return ent->fdesc;
#define n_name _n._n_name
nl[0].n_name = ENTRY_SYM;
nl[1].n_name = sym;
nl[2].n_name = 0;
/*
* There is a potential problem here.
* If application did not pass a full path name, and changed the working
* directory after the load(), then nlist() will be unable to open the
* original shared library file to resolve the symbols. There are 3 ways
* to working round this:
* 1. convert to full pathname in driver manager.
* 2. applications always pass driver's full path name.
* 3. if driver itself do not support SQLGetFunctions(), call it with
* SQL_ALL_FUNCTIONS as flag immediately after SQLConnect(),
* SQLDriverConnect() and SQLBrowseConnect() to force the driver
* manager resolving all will be used symbols.
*/
if (nlist (pobj->path, nl) == -1)
return 0;
if (!nl[0].n_type && !nl[0].n_value)
{
errmsg = "can't locate module entry symbol";
return 0;
}
/*
* Note: On AIX 3.x if the object library is not built with -g compiling
* option, .n_type field is always 0. While on 4.x it will be 32.
* On AIX 4.x, if the symbol is a entry point, n_value will be 0.
* However, one thing is for sure that if a symbol does not exists in the
* file, both .n_type and .n_value would be 0.
*/
if (!nl[1].n_type && !nl[1].n_value)
{
errmsg = "symbol does not exist in this module";
return 0;
}
ent = slot_alloc (sym);
if (!ent)
return 0;
/* catch it with a slot in the hashing table */
insert (pobj->htab, ent);
memcpy (ent->fdesc, pobj->pentry, sizeof (ent->fdesc));
/*
* now ent->fdesc[0] is the virtual address of entry point and
* ent->fdesc[1] is the TOC of the module
*
* let's calculate the virtual address of the symbol by adding a relative
* offset getting from the module file symbol table, i.e
*
* function virtual address = entry point virtual address +
* ( function offset in file - entry point offset in file )
*/
(ent->fdesc)[0] = (ent->fdesc)[0] + (nl[1].n_value - nl[0].n_value);
/* return the function descriptor */
return ent->fdesc;
}
#endif /* end of IBM AIX Section */
/*********************************
*
* Windows 3.x, 95, NT
*
*********************************/
#ifdef DLDAPI_WINDOWS
#define DLDAPI_DEFINED
DLF_VERSION ("Windows (loadlibrary)");
#include <windows.h>
void *
dlopen (char * dll, int mode)
{
HINSTANCE hint;
if (dll == NULL)
{
return (void *) GetWindowLongPtr (NULL, GWLP_HINSTANCE);
}
hint = LoadLibrary (dll);
if (hint < (HINSTANCE) HINSTANCE_ERROR)
{
return NULL;
}
return (void *) hint;
}
void *
dlsym (void * hdll, char * sym)
{
return (void *) GetProcAddress (hdll, sym);
}
char *
dlerror ()
{
static char lpMsgBuf[512];
if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
(LPTSTR) & lpMsgBuf, sizeof (lpMsgBuf), NULL))
{
/* Handle the error. */
return "";
}
/* Process any inserts in lpMsgBuf. */
/* ... */
return (char *) &(lpMsgBuf[0]);
}
int
dlclose (void * hdll)
{
FreeLibrary ((HINSTANCE) hdll);
return 0;
}
#endif /* end of Windows family */
/***********************************
*
* VMS
*
***********************************/
#ifdef VMS
#define DLDAPI_DEFINED
DLF_VERSION ("VMS");
#include <stdio.h>
#include <descrip.h>
#include <starlet.h>
#include <ssdef.h>
#include <libdef.h>
#include <lib$routines>
#include <rmsdef.h>
#include <fabdef.h>
#include <namdef.h>
#ifndef LIB$M_FIS_MIXCASE
#define LIB$M_FIS_MIXCASE 1<<4
#endif
typedef struct
{
struct dsc$descriptor_s filename_d;
struct dsc$descriptor_s image_d;
char filename[NAM$C_MAXRSS]; /* $PARSEd image name */
}
dll_t;
/*
* The following static int contains the last VMS error returned. It is kept
* static so that dlerror() can get it. This method is dangerous if you have
* threaded applications, but this is the way the UNIX dlopen() etc
* is defined.
*/
static int saved_status = SS$_NORMAL;
static char dlerror_buf[256];
static int
__virtuoso_find_image_symbol (struct dsc$descriptor_s *filename_d,
struct dsc$descriptor_s *symbol_d, void **rp, struct dsc$descriptor_s *image_d, int flag)
{
lib$establish (lib$sig_to_ret);
return lib$find_image_symbol (filename_d, symbol_d, rp, image_d, flag);
}
void *
dlopen (char *path, int unused_flag)
{
int status;
dll_t *dll;
struct FAB imgfab;
struct NAM imgnam;
static char defimg[] = "SYS$SHARE:.EXE";
if (path == NULL)
{
saved_status = SS$_UNSUPPORTED;
return NULL;
}
dll = malloc (sizeof (dll_t));
if (dll == NULL)
{
saved_status = SS$_INSFMEM;
return NULL;
}
imgfab = cc$rms_fab;
imgfab.fab$l_fna = path;
imgfab.fab$b_fns = strlen (path);
imgfab.fab$w_ifi = 0;
imgfab.fab$l_dna = defimg;
imgfab.fab$b_dns = sizeof (defimg);
imgfab.fab$l_fop = FAB$M_NAM;
imgfab.fab$l_nam = &imgnam;
imgnam = cc$rms_nam;
imgnam.nam$l_esa = dll->filename;
imgnam.nam$b_ess = NAM$C_MAXRSS;
status = sys$parse (&imgfab);
if (!(status & 1))
{
free (dll);
saved_status = status;
return NULL;
}
dll->filename_d.dsc$b_dtype = DSC$K_DTYPE_T;
dll->filename_d.dsc$b_class = DSC$K_CLASS_S;
dll->filename_d.dsc$a_pointer = imgnam.nam$l_name;
dll->filename_d.dsc$w_length = imgnam.nam$b_name;
dll->image_d.dsc$b_dtype = DSC$K_DTYPE_T;
dll->image_d.dsc$b_class = DSC$K_CLASS_S;
dll->image_d.dsc$a_pointer = dll->filename;
dll->image_d.dsc$w_length = imgnam.nam$b_esl;
/*
* VMS does not have the concept of first opening a shared library and then
* asking for symbols; the LIB$FIND_IMAGE_SYMBOL routine does both.
* Since I want my implementation of dlopen() to return an error if the
* shared library can not be loaded, I try to find a dummy symbol in the
* library.
*/
dlsym (dll, "THIS_ROUTINE_MIGHT_NOT_EXIST");
if (!((saved_status ^ LIB$_KEYNOTFOU) & ~7))
{
saved_status = SS$_NORMAL;
}
if (saved_status & 1)
{
return dll;
}
else
{
free (dll);
return NULL;
}
}
void *
dlsym (void *hdll, char *sym)
{
int status;
dll_t *dll;
struct dsc$descriptor_s symbol_d;
void *rp;
dll = hdll;
if (dll == NULL)
return NULL;
symbol_d.dsc$b_dtype = DSC$K_DTYPE_T;
symbol_d.dsc$b_class = DSC$K_CLASS_S;
symbol_d.dsc$a_pointer = sym;
symbol_d.dsc$w_length = strlen (sym);
status = __virtuoso_find_image_symbol (&dll->filename_d, &symbol_d, &rp, &dll->image_d, 0);
if (!((saved_status ^ LIB$_KEYNOTFOU) & ~7))
{
status = __virtuoso_find_image_symbol (&dll->filename_d, &symbol_d, &rp, &dll->image_d, LIB$M_FIS_MIXCASE);
}
if (status & 1)
{
return rp;
}
else
{
saved_status = status;
return NULL;
}
}
char *
dlerror ()
{
struct dsc$descriptor desc;
short outlen;
int status;
if (saved_status & 1)
{
return NULL;
}
desc.dsc$b_dtype = DSC$K_DTYPE_T;
desc.dsc$b_class = DSC$K_CLASS_S;
desc.dsc$a_pointer = dlerror_buf;
desc.dsc$w_length = sizeof (dlerror_buf);
status = sys$getmsg (saved_status, &outlen, &desc, 15, 0);
if (status & 1)
{
dlerror_buf[outlen] = '\0';
}
else
{
snprintf (dlerror_buf, sizeof (dlerror_buf), "Message number %8X", saved_status);
}
saved_status = SS$_NORMAL;
return (dlerror_buf);
}
int
dlclose (void *hdll)
{
/*
* Not really implemented since VMS does not support unloading images.
* The hdll pointer is released though.
*/
free (hdll);
return 0;
}
#endif /* VMS */
/*********************************
*
* Apple MacOS X Rhapsody
*
*********************************/
#ifdef DLDAPI_DYLD
#define DLDAPI_DEFINED
DLF_VERSION ("Mac OS X (dyld)");
#include <stdio.h>
#include <mach-o/dyld.h>
static void
undefined_symbol_handler (const char *symbolName)
{
fprintf (stderr, "dyld found undefined symbol: %s\n", symbolName);
abort ();
}
static NSModule
multiple_symbol_handler (NSSymbol s, NSModule old, NSModule new)
{
/*
* Since we can't unload symbols, we're going to run into this
* every time we reload a module. Workaround here is to just
* rebind to the new symbol, and forget about the old one.
* This is crummy, because it's basically a memory leak.
*/
return (new);
}
static void
linkEdit_symbol_handler (NSLinkEditErrors c, int errorNumber, const char *fileName, const char *errorString)
{
fprintf (stderr, "dyld errors during link edit for file %s\n%s\n", fileName, errorString);
abort ();
}
void *
dlopen (char *path, int mode)
{
NSObjectFileImage image;
NSLinkEditErrorHandlers handlers;
NSModule handle = NULL;
int i;
/*
* Install error handler
*/
handlers.undefined = undefined_symbol_handler;
#if !defined (NSLINKMODULE_OPTION_PRIVATE)
handlers.multiple = multiple_symbol_handler;
#endif
handlers.linkEdit = linkEdit_symbol_handler;
NSInstallLinkEditErrorHandlers (&handlers);
/*
* Load object
*/
i = NSCreateObjectFileImageFromFile (path, &image);
if (i != NSObjectFileImageSuccess)
{
static char *ErrorStrings[] = {
"%s(%d): Object Image Load Failure\n",
"%s(%d): Object Image Load Success\n",
"%s(%d): Not an recognizable object file\n",
"%s(%d): No valid architecture\n",
"%s(%d): Object image has an invalid format\n",
"%s(%d): Invalid access (permissions?)\n",
"%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
};
if (i < 0 || i > 6)
i = 6;
fprintf (stderr, ErrorStrings[i], path, i);
}
else
{
#if !defined (NSLINKMODULE_OPTION_PRIVATE)
handle = NSLinkModule (image, path, TRUE);
#else
handle = NSLinkModule (image, path, NSLINKMODULE_OPTION_PRIVATE);
#endif
}
return (void *) handle;
}
void *
dlsym (void *hdll, char *sym)
{
NSSymbol symbol;
char sym2[255];
snprintf (sym2, sizeof (sym2), "_%s", sym);
#if !defined (NSLINKMODULE_OPTION_PRIVATE)
if (NSIsSymbolNameDefined (sym2))
{
symbol = NSLookupAndBindSymbol (sym2);
return NSAddressOfSymbol (symbol);
}
return NULL;
#else
symbol = NSLookupSymbolInModule ((NSModule) hdll, sym2);
return NSAddressOfSymbol (symbol);
#endif
}
char *
dlerror ()
{
return NULL;
}
int
dlclose (void *hdll)
{
NSUnLinkModule (hdll, FALSE);
return 0;
}
#endif /* end of Rhapsody Section */
/*********************************
*
* Apple MacOS X Rhapsody
*
*********************************/
#ifdef DLDAPI_MACX
#define DLDAPI_DEFINED
DLF_VERSION ("Mac OS X 10.x (dyld)");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "mach-o/dyld.h"
#define RTLD_LAZY 0x1
#define RTLD_NOW 0x2
#define RTLD_LOCAL 0x4
#define RTLD_GLOBAL 0x8
#define RTLD_NOLOAD 0x10
#define RTLD_SHARED 0x20 /* not used, the default */
#define RTLD_UNSHARED 0x40
#define RTLD_NODELETE 0x80
#define RTLD_LAZY_UNDEF 0x100
enum ofile_type
{
OFILE_UNKNOWN,
OFILE_FAT,
OFILE_ARCHIVE,
OFILE_Mach_O
};
enum byte_sex
{
UNKNOWN_BYTE_SEX,
BIG_ENDIAN_BYTE_SEX,
LITTLE_ENDIAN_BYTE_SEX
};
/*
* The structure describing an architecture flag with the string of the flag
* name, and the cputype and cpusubtype.
*/
struct arch_flag
{
char *name;
cpu_type_t cputype;
cpu_subtype_t cpusubtype;
};
/*
* The structure used by ofile_*() routines for object files.
*/
struct ofile
{
char *file_name; /* pointer to name malloc'ed by ofile_map */
char *file_addr; /* pointer to vm_allocate'ed memory */
unsigned long file_size; /* size of vm_allocate'ed memory */
enum ofile_type file_type; /* type of the file */
struct fat_header *fat_header; /* If a fat file these are filled in and */
struct fat_arch *fat_archs; /* if needed converted to host byte sex */
/*
* If this is a fat file then these are valid and filled in
*/
unsigned long narch; /* the current architecture */
enum ofile_type arch_type; /* the type of file for this arch. */
struct arch_flag arch_flag; /* the arch_flag for this arch, the name */
/* field is pointing at space malloc'ed */
/* by ofile_map. */
/*
* If this structure is currently referencing an archive member or
* an object file that is an archive member these are valid and filled in.
*/
unsigned long member_offset; /* logical offset to the member starting */
char *member_addr; /* pointer to the member contents */
unsigned long member_size; /* actual size of the member (not rounded)*/
struct ar_hdr *member_ar_hdr; /* pointer to the ar_hdr for this member */
char *member_name; /* name of this member */
unsigned long member_name_size; /* size of the member name */
enum ofile_type member_type; /* the type of file for this member */
cpu_type_t archive_cputype; /* if the archive contains objects then */
cpu_subtype_t /* these two fields reflect the object */
archive_cpusubtype; /* at are in the archive. */
/*
* If this structure is currently referencing a dynamic library module
* these are valid and filled in.
*/
struct dylib_module *modtab; /* the module table */
unsigned long nmodtab; /* the number of module table entries */
struct dylib_module /* pointer to the dylib_module for this */
*dylib_module; /* module */
char *dylib_module_name; /* the name of the module */
/*
* If this structure is currently referencing an object file these are
* valid and filled in. The mach_header and load commands have been
* converted to the host byte sex if needed
*/
char *object_addr; /* the address of the object file */
unsigned long object_size; /* the size of the object file */
enum byte_sex object_byte_sex; /* the byte sex of the object file */
struct mach_header *mh; /* the mach_header of the object file */
struct load_command /* the start of the load commands */
*load_commands;
};
/*
* The structure of a dlopen() handle.
*/
struct dlopen_handle
{
dev_t dev; /* the path's device and inode number from stat(2) */
ino_t ino;
int dlopen_mode; /* current dlopen mode for this handle */
int dlopen_count; /* number of times dlopen() called on this handle */
NSModule module; /* the NSModule returned by NSLinkModule() */
struct dlopen_handle *prev;
struct dlopen_handle *next;
};
static struct dlopen_handle *dlopen_handles = NULL;
static const struct dlopen_handle main_program_handle = { 0 };
static char *dlerror_pointer = NULL;
/*
* NSMakePrivateModulePublic() is not part of the public dyld API so we define
* it here. The internal dyld function pointer for
* __dyld_NSMakePrivateModulePublic is returned so thats all that matters to get
* the functionality need to implement the dlopen() interfaces.
*/
static int
NSMakePrivateModulePublic (NSModule module)
{
static int (*p) (NSModule module) = NULL;
if (p == NULL)
_dyld_func_lookup ("__dyld_NSMakePrivateModulePublic", (void *) &p);
if (p == NULL)
{
#ifdef DEBUG
printf ("_dyld_func_lookup of __dyld_NSMakePrivateModulePublic " "failed\n");
#endif
return (FALSE);
}
return (p (module));
}
/*
* dlopen() the MacOS X version of the FreeBSD dlopen() interface.
*/
void *
dlopen (char * path, int mode)
{
void *retval;
struct stat stat_buf;
NSObjectFileImage objectFileImage;
NSObjectFileImageReturnCode ofile_result_code;
NSModule module;
struct dlopen_handle *p;
unsigned long options;
NSSymbol NSSymbol;
void (*init) (void);
static char errbuf[640];
dlerror_pointer = NULL;
/*
* A NULL path is to indicate the caller wants a handle for the
* main program.
*/
if (path == NULL)
{
retval = (void *) &main_program_handle;
return (retval);
}
/* see if the path exists and if so get the device and inode number */
if (stat (path, &stat_buf) == -1)
{
dlerror_pointer = strerror (errno);
return (NULL);
}
/*
* If we do not want an unshared handle see if we already have a handle
* for this path.
*/
if ((mode & RTLD_UNSHARED) != RTLD_UNSHARED)
{
p = dlopen_handles;
while (p != NULL)
{
if (p->dev == stat_buf.st_dev && p->ino == stat_buf.st_ino)
{
/* skip unshared handles */
if ((p->dlopen_mode & RTLD_UNSHARED) == RTLD_UNSHARED)
continue;
/*
* We have already created a handle for this path. The
* caller might be trying to promote an RTLD_LOCAL handle
* to a RTLD_GLOBAL. Or just looking it up with
* RTLD_NOLOAD.
*/
if ((p->dlopen_mode & RTLD_LOCAL) == RTLD_LOCAL && (mode & RTLD_GLOBAL) == RTLD_GLOBAL)
{
/* promote the handle */
if (NSMakePrivateModulePublic (p->module) == TRUE)
{
p->dlopen_mode &= ~RTLD_LOCAL;
p->dlopen_mode |= RTLD_GLOBAL;
p->dlopen_count++;
return (p);
}
else
{
dlerror_pointer = "can't promote handle from RTLD_LOCAL to RTLD_GLOBAL";
return (NULL);
}
}
p->dlopen_count++;
return (p);
}
p = p->next;
}
}
/*
* We do not have a handle for this path if we were just trying to
* look it up return NULL to indicate we do not have it.
*/
if ((mode & RTLD_NOLOAD) == RTLD_NOLOAD)
{
dlerror_pointer = "no existing handle for path RTLD_NOLOAD test";
return (NULL);
}
/* try to create an object file image from this path */
ofile_result_code = NSCreateObjectFileImageFromFile (path, &objectFileImage);
if (ofile_result_code != NSObjectFileImageSuccess)
{
switch (ofile_result_code)
{
case NSObjectFileImageFailure:
dlerror_pointer = "object file setup failure";
return (NULL);
case NSObjectFileImageInappropriateFile:
dlerror_pointer = "not a Mach-O MH_BUNDLE file type";
return (NULL);
case NSObjectFileImageArch:
dlerror_pointer = "no object for this architecture";
return (NULL);
case NSObjectFileImageFormat:
dlerror_pointer = "bad object file format";
return (NULL);
case NSObjectFileImageAccess:
dlerror_pointer = "can't read object file";
return (NULL);
default:
dlerror_pointer = "unknown error from " "NSCreateObjectFileImageFromFile()";
return (NULL);
}
}
/* try to link in this object file image */
options = NSLINKMODULE_OPTION_NONE | NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR;
if ((mode & RTLD_NOW) == RTLD_NOW)
options |= NSLINKMODULE_OPTION_BINDNOW;
module = NSLinkModule (objectFileImage, path, options);
NSDestroyObjectFileImage (objectFileImage);
if (module == NULL)
{
NSLinkEditErrors lerr;
int errNum;
const char *fname;
const char *errStr;
NSLinkEditError (&lerr, &errNum, &fname, &errStr);
snprintf (errbuf, sizeof(errbuf), "NSLinkModule() failed for dlopen() ([%.256s][%.256s])", fname, errStr);
dlerror_pointer = errbuf;
return (NULL);
}
/*
* If the handle is to be global promote the handle. It is done this
* way to avoid multiply defined symbols.
*/
if ((mode & RTLD_GLOBAL) == RTLD_GLOBAL)
{
if (NSMakePrivateModulePublic (module) == FALSE)
{
dlerror_pointer = "can't promote handle from RTLD_LOCAL to " "RTLD_GLOBAL";
return (NULL);
}
}
p = malloc (sizeof (struct dlopen_handle));
if (p == NULL)
{
dlerror_pointer = "can't allocate memory for the dlopen handle";
return (NULL);
}
/* fill in the handle */
p->dev = stat_buf.st_dev;
p->ino = stat_buf.st_ino;
if (mode & RTLD_GLOBAL)
p->dlopen_mode = RTLD_GLOBAL;
else
p->dlopen_mode = RTLD_LOCAL;
p->dlopen_mode |= (mode & RTLD_UNSHARED) | (mode & RTLD_NODELETE) | (mode & RTLD_LAZY_UNDEF);
p->dlopen_count = 1;
p->module = module;
p->prev = NULL;
p->next = dlopen_handles;
if (dlopen_handles != NULL)
dlopen_handles->prev = p;
dlopen_handles = p;
/* call the init function if one exists */
NSSymbol = NSLookupSymbolInModule (p->module, "__init");
if (NSSymbol != NULL)
{
init = NSAddressOfSymbol (NSSymbol);
init ();
}
return (p);
}
/*
* dlsym() the MacOS X version of the FreeBSD dlopen() interface.
*/
void *
dlsym (void * handle, char * symbol)
{
struct dlopen_handle *dlopen_handle, *p;
char symbol2[1024];
NSSymbol NSSymbol;
void *address;
snprintf (symbol2, "_%s", symbol);
dlopen_handle = (struct dlopen_handle *) handle;
/*
* If this is the handle for the main program do a global lookup.
*/
if (dlopen_handle == (struct dlopen_handle *) &main_program_handle)
{
if (NSIsSymbolNameDefined (symbol2) == TRUE)
{
NSSymbol = NSLookupAndBindSymbol (symbol2);
address = NSAddressOfSymbol (NSSymbol);
dlerror_pointer = NULL;
return (address);
}
else
{
dlerror_pointer = "symbol not found";
return (NULL);
}
}
/*
* Find this handle and do a lookup in just this module.
*/
p = dlopen_handles;
while (p != NULL)
{
if (dlopen_handle == p)
{
NSSymbol = NSLookupSymbolInModule (p->module, symbol2);
if (NSSymbol != NULL)
{
address = NSAddressOfSymbol (NSSymbol);
dlerror_pointer = NULL;
return (address);
}
else
{
dlerror_pointer = "symbol not found";
return (NULL);
}
}
p = p->next;
}
dlerror_pointer = "bad handle passed to dlsym()";
return (NULL);
}
/*
* dlerror() the MacOS X version of the FreeBSD dlopen() interface.
*/
char *
dlerror (void)
{
const char *p;
p = (const char *) dlerror_pointer;
dlerror_pointer = NULL;
return (p);
}
/*
* dlclose() the MacOS X version of the FreeBSD dlopen() interface.
*/
int
dlclose (void * handle)
{
struct dlopen_handle *p, *q;
unsigned long options;
NSSymbol NSSymbol;
void (*fini) (void);
dlerror_pointer = NULL;
q = (struct dlopen_handle *) handle;
p = dlopen_handles;
while (p != NULL)
{
if (p == q)
{
/* if the dlopen() count is not zero we are done */
p->dlopen_count--;
if (p->dlopen_count != 0)
return (0);
/* call the fini function if one exists */
NSSymbol = NSLookupSymbolInModule (p->module, "__fini");
if (NSSymbol != NULL)
{
fini = NSAddressOfSymbol (NSSymbol);
fini ();
}
/* unlink the module for this handle */
options = 0;
if (p->dlopen_mode & RTLD_NODELETE)
options |= NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED;
if (p->dlopen_mode & RTLD_LAZY_UNDEF)
options |= NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES;
if (NSUnLinkModule (p->module, options) == FALSE)
{
dlerror_pointer = "NSUnLinkModule() failed for dlclose()";
return (-1);
}
if (p->prev != NULL)
p->prev->next = p->next;
if (p->next != NULL)
p->next->prev = p->prev;
if (dlopen_handles == p)
dlopen_handles = p->next;
free (p);
return (0);
}
p = p->next;
}
dlerror_pointer = "invalid handle passed to dlclose()";
return (-1);
}
#endif /* end of Rhapsody Section */
/*********************************
*
* Macintosh
*
*********************************/
#ifdef DLDAPI_MAC
#define DLDAPI_DEFINED
DLF_VERSION ("Macintosh");
#include <CodeFragments.h>
#include <strconv.h>
static char *msg_error = NULL;
void *
dlopen (char *dll, int mode)
{
#ifdef __POWERPC__
CFragConnectionID conn_id;
Ptr main_addr;
Str255 name;
OSErr err;
if (dll == NULL)
{
msg_error = "Library name not valid.";
return NULL;
}
if ((err = GetSharedLibrary ((unsigned char *) str_to_Str255 (dll),
kPowerPCCFragArch, kLoadCFrag, &conn_id, &main_addr, name)) != noErr)
{
msg_error = "Library cannot be loaded.";
return NULL;
}
msg_error = NULL;
return (void *) conn_id;
#else
CFragConnectionID conn_id;
Ptr main_addr;
Str255 name;
OSErr err;
if (dll == NULL)
{
msg_error = "Library name not valid.";
return NULL;
}
if ((err = GetSharedLibrary ((unsigned char *) str_to_Str255 (dll),
kMotorola68KCFragArch, kLoadCFrag, &conn_id, &main_addr, name)) != noErr)
{
msg_error = "Library cannot be loaded.";
return NULL;
}
msg_error = NULL;
return (void *) conn_id;
#endif
}
void *
dlsym (void *hdll, char *sym)
{
#ifdef __POWERPC__
Ptr symbol;
CFragSymbolClass symbol_type;
OSErr err;
if (sym == NULL)
{
msg_error = "Symbol name not valid.";
return NULL;
}
if ((err = FindSymbol ((CFragConnectionID) hdll, (unsigned char *) str_to_Str255 (sym), &symbol, &symbol_type)) != noErr)
{
msg_error = "Symbol cannot be loaded.";
return NULL;
}
msg_error = NULL;
return symbol;
#else
Ptr symbol;
CFragSymbolClass symbol_type;
if (sym == NULL)
{
msg_error = "Symbol name not valid.";
return NULL;
}
if (FindSymbol ((CFragConnectionID) hdll, (unsigned char *) str_to_Str255 (sym), &symbol, &symbol_type) != noErr)
{
msg_error = "Symbol cannot be loaded.";
return NULL;
}
msg_error = NULL;
return symbol;
#endif
}
char *
dlerror ()
{
return (msg_error) ? msg_error : "No error detected.";
}
int
dlclose (void *hdll)
{
#ifdef __POWERPC__
#if 0
/*
* It should be something like this but some applications like Office 2001 have a problem with that.
* Just let the Mac unload the library when the application stops.
*/
if (CloseConnection ((CFragConnectionID *) hdll))
{
msg_error = "Library cannot be unloaded.";
return 1;
}
msg_error = NULL;
#endif
#else
if (CloseConnection ((CFragConnectionID *) hdll))
{
msg_error = "Library cannot be unloaded.";
return 1;
}
msg_error = NULL;
#endif
return 0;
}
#endif /* end of Macintosh family */
/*********************************
*
* BeOS
*
*********************************/
#ifdef DLDAPI_BE
#define DLDAPI_DEFINED
DLF_VERSION ("BeOS");
#include <kernel/image.h>
#include <be/support/Errors.h>
static char *msg_error = NULL;
void *
dlopen (char *dll, int mode)
{
image_id dll_id;
if (dll == NULL)
{
msg_error = "Library name not valid.";
return NULL;
}
dll_id = load_add_on (dll);
if (dll_id == B_ERROR)
{
msg_error = "Library cannot be loaded.";
return NULL;
}
msg_error = NULL;
return (void *) dll_id;
}
void *
dlsym (void *hdll, char *sym)
{
void *address = NULL;
if (sym == NULL)
{
msg_error = "Symbol name not valid.";
return NULL;
}
if (get_image_symbol ((image_id) hdll, sym, B_SYMBOL_TYPE_ANY, &address) != B_OK)
{
msg_error = "Symbol cannot be loaded.";
return NULL;
}
msg_error = NULL;
return address;
}
char *
dlerror ()
{
return (msg_error) ? msg_error : "No error detected.";
}
int
dlclose (void *hdll)
{
if (unload_add_on ((image_id) hdll) != B_OK)
{
msg_error = "Library cannot be unloaded.";
return 1;
}
msg_error = NULL;
return 0;
}
#endif /* end of BeOS */
/***********************************
*
* other platforms
*
***********************************/
#ifndef DLDAPI_DEFINED
#error "dynamic load editor undefined"
#endif
|