Back to index

glibc  2.9
vismain.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 
00023 #include "vismod.h"
00024 
00025 /* Prototype for our test function.  */
00026 extern int do_test (void);
00027 
00028 #define TEST_FUNCTION do_test ()
00029 
00030 /* This defines the `main' function and some more.  */
00031 #include <test-skeleton.c>
00032 
00033 
00034 /* Prototypes for local functions.  */
00035 extern int protlocal (void);
00036 
00037 const char *protvarlocal = __FILE__;
00038 extern const char *protvarinmod;
00039 extern const char *protvaritcpt;
00040 
00041 int
00042 do_test (void)
00043 {
00044   int res = 0;
00045   int val;
00046 
00047   /* First test: check whether .protected is handled correctly by the
00048      assembler/linker.  The uses of `protlocal' in the DSOs and in the
00049      main program should all be resolved with the local definitions.  */
00050   val = protlocal () + calllocal1 () + calllocal2 ();
00051   if (val != 0x155)
00052     {
00053       puts ("\
00054 The handling of `.protected' seems to be implemented incorrectly: giving up");
00055       abort ();
00056     }
00057   puts ("`.protected' seems to be handled correctly, good!");
00058 
00059   /* Function pointers: for functions which are marked local and for
00060      which definitions are available all function pointers must be
00061      distinct.  */
00062   if (protlocal == getlocal1 ())
00063     {
00064       puts ("`protlocal' in main and mod1 have same address");
00065       res = 1;
00066     }
00067   if (protlocal == getlocal2 ())
00068     {
00069       puts ("`protlocal' in main and mod2 have same address");
00070       res = 1;
00071     }
00072   if (getlocal1 () == getlocal2 ())
00073     {
00074       puts ("`protlocal' in mod1 and mod2 have same address");
00075       res = 1;
00076     }
00077   if (getlocal1 () () + getlocal2 () () != 0x44)
00078     {
00079       puts ("pointers to `protlocal' in mod1 or mod2 incorrect");
00080       res = 1;
00081     }
00082 
00083   /* Next test.  This is similar to the last one but the function we
00084      are calling is not defined in the main object.  This means that
00085      the invocation in the main object uses the definition in the
00086      first DSO.  */
00087   if (protinmod != getinmod1 ())
00088     {
00089       printf ("&protinmod in main (%p) != &protinmod in mod1 (%p)\n",
00090              protinmod, getinmod1 ());
00091       res = 1;
00092     }
00093   if (protinmod == getinmod2 ())
00094     {
00095       puts ("`protinmod' in main and mod2 have same address");
00096       res = 1;
00097     }
00098   if (getinmod1 () == getinmod2 ())
00099     {
00100       puts ("`protinmod' in mod1 and mod2 have same address");
00101       res = 1;
00102     }
00103   if (protinmod () + getinmod1 () () + getinmod2 () () != 0x4800)
00104     {
00105       puts ("pointers to `protinmod' in mod1 or mod2 incorrect");
00106       res = 1;
00107     }
00108   val = protinmod () + callinmod1 () + callinmod2 ();
00109   if (val != 0x15800)
00110     {
00111       printf ("calling of `protinmod' leads to wrong result (%#x)\n", val);
00112       res = 1;
00113     }
00114 
00115   /* A very similar text.  Same setup for the main object and the modules
00116      but this time we have another definition in a preloaded module. This
00117      one intercepts the references from the main object.  */
00118   if (protitcpt != getitcpt3 ())
00119     {
00120       printf ("&protitcpt in main (%p) != &protitcpt in mod3 (%p)\n",
00121              &protitcpt, getitcpt3 ());
00122       res = 1;
00123     }
00124   if (protitcpt == getitcpt1 ())
00125     {
00126       puts ("`protitcpt' in main and mod1 have same address");
00127       res = 1;
00128     }
00129   if (protitcpt == getitcpt2 ())
00130     {
00131       puts ("`protitcpt' in main and mod2 have same address");
00132       res = 1;
00133     }
00134   if (getitcpt1 () == getitcpt2 ())
00135     {
00136       puts ("`protitcpt' in mod1 and mod2 have same address");
00137       res = 1;
00138     }
00139   val = protitcpt () + getitcpt1 () () + getitcpt2 () () + getitcpt3 () ();
00140   if (val != 0x8440000)
00141     {
00142       printf ("\
00143 pointers to `protitcpt' in mod1 or mod2 or mod3 incorrect (%#x)\n", val);
00144       res = 1;
00145     }
00146   val = protitcpt () + callitcpt1 () + callitcpt2 () + callitcpt3 ();
00147   if (val != 0x19540000)
00148     {
00149       printf ("calling of `protitcpt' leads to wrong result (%#x)\n", val);
00150       res = 1;
00151     }
00152 
00153   /* Now look at variables.  First a variable which is available
00154      everywhere.  We must have three different addresses.  */
00155   if (&protvarlocal == getvarlocal1 ())
00156     {
00157       puts ("`protvarlocal' in main and mod1 have same address");
00158       res = 1;
00159     }
00160   if (&protvarlocal == getvarlocal2 ())
00161     {
00162       puts ("`protvarlocal' in main and mod2 have same address");
00163       res = 1;
00164     }
00165   if (getvarlocal1 () == getvarlocal2 ())
00166     {
00167       puts ("`protvarlocal' in mod1 and mod2 have same address");
00168       res = 1;
00169     }
00170   if (strcmp (protvarlocal, __FILE__) != 0)
00171     {
00172       puts ("`protvarlocal in main has wrong value");
00173       res = 1;
00174     }
00175   if (strcmp (*getvarlocal1 (), "vismod1.c") != 0)
00176     {
00177       puts ("`getvarlocal1' returns wrong value");
00178       res = 1;
00179     }
00180   if (strcmp (*getvarlocal2 (), "vismod2.c") != 0)
00181     {
00182       puts ("`getvarlocal2' returns wrong value");
00183       res = 1;
00184     }
00185 
00186   /* Now the case where there is no local definition.  */
00187   if (&protvarinmod != getvarinmod1 ())
00188     {
00189       printf ("&protvarinmod in main (%p) != &protitcpt in mod1 (%p)\n",
00190              &protvarinmod, getvarinmod1 ());
00191       // XXX Possibly enable once fixed.
00192       // res = 1;
00193     }
00194   if (&protvarinmod == getvarinmod2 ())
00195     {
00196       puts ("`protvarinmod' in main and mod2 have same address");
00197       res = 1;
00198     }
00199   if (strcmp (*getvarinmod1 (), "vismod1.c") != 0)
00200     {
00201       puts ("`getvarinmod1' returns wrong value");
00202       res = 1;
00203     }
00204   if (strcmp (*getvarinmod2 (), "vismod2.c") != 0)
00205     {
00206       puts ("`getvarinmod2' returns wrong value");
00207       res = 1;
00208     }
00209 
00210   /* And a test where a variable definition is intercepted.  */
00211   if (&protvaritcpt == getvaritcpt1 ())
00212     {
00213       puts ("`protvaritcpt' in main and mod1 have same address");
00214       res = 1;
00215     }
00216   if (&protvaritcpt == getvaritcpt2 ())
00217     {
00218       puts ("`protvaritcpt' in main and mod2 have same address");
00219       res = 1;
00220     }
00221   if (&protvaritcpt != getvaritcpt3 ())
00222     {
00223       printf ("&protvaritcpt in main (%p) != &protvaritcpt in mod3 (%p)\n",
00224              &protvaritcpt, getvaritcpt3 ());
00225       // XXX Possibly enable once fixed.
00226       // res = 1;
00227     }
00228   if (getvaritcpt1 () == getvaritcpt2 ())
00229     {
00230       puts ("`protvaritcpt' in mod1 and mod2 have same address");
00231       res = 1;
00232     }
00233   if (strcmp (protvaritcpt, "vismod3.c") != 0)
00234     {
00235       puts ("`protvaritcpt in main has wrong value");
00236       res = 1;
00237     }
00238   if (strcmp (*getvaritcpt1 (), "vismod1.c") != 0)
00239     {
00240       puts ("`getvaritcpt1' returns wrong value");
00241       res = 1;
00242     }
00243   if (strcmp (*getvaritcpt2 (), "vismod2.c") != 0)
00244     {
00245       puts ("`getvaritcpt2' returns wrong value");
00246       res = 1;
00247     }
00248 
00249   return res;
00250 }
00251 
00252 
00253 int
00254 protlocal (void)
00255 {
00256   return 0x1;
00257 }