Shared libraries

John Coolidge coolidge at casca.cs.uiuc.edu
Wed Aug 15 05:26:17 AEST 1990


coolidge at casca.cs.uiuc.edu (John Coolidge) writes:
>It looks like it, indeed. Given the simplicity of perror it
>shouldn't even be hard to write one and built a shared version
>of it which incorporates the strings from libc. Perhaps I'll
>try it tonight... (Note: this is being written away from my
>A/UX machine; I reserve the right to discover that this is a bad
>idea :-)).

Well, I discovered that it's not a stunning idea, anyway :-).
The size savings is only about 3K using shared perror. It's
an interesting idea, though...

What should be done is to build several shared libraries with
pieces of libc. For instance, the AT&T notes mention a shared
libnet which includes all the network code. If all the system
programs were linked with such a library, only those who used
networking would use the shared library (and hence pay the
space penalty) --- the rest would just ignore it. The same goes
for shared perror, shared math routines, etc.

At any rate, I've build a shared liberr which includes perror,
sys_nerr, and sys_errlist. I simply grab the strings from libc
to implement sys_errlist. Testing indicates that my liberr is
fully compatible with Apple's implementation (unless I missed
something during testing :-)). Since it's small, I'm appending
it to this posting. If you've got gcc, I advise (strongly!)
compiling it with 'make CC=gcc'. If you don't have gcc, I advise
getting it :-)

Enjoy!

--John

--------------------------------------------------------------------------
#!/bin/sh
# This is a shell archive (shar 3.10)
# made 08/14/1990 17:56 UTC by coolidge at cs.uiuc.edu
# Source directory /home/johnson/coolidge/liberr
#
# existing files WILL be overwritten
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#    854 -rw-r--r-- README
#    717 -rw-r--r-- Makefile
#    566 -rwxr-xr-x create_errlist
#    609 -rw-r--r-- liberr.spec
#   1141 -rw-r--r-- perror.c
#    342 -rw-r--r-- pointer.c
#    369 -rw-r--r-- shared.h
#
touch 2>&1 | fgrep '[-amc]' > /tmp/s3_touch$$
if [ -s /tmp/s3_touch$$ ]
then
	TOUCH=can
else
	TOUCH=cannot
fi
rm -f /tmp/s3_touch$$
# ============= README ==============
sed 's/^X//' << 'SHAR_EOF' > README &&
XThis package will build a shared liberr_s which implements
Xperror(3) and sys_nerr/sys_errlist. The error table is 
Xextracted from the one used in /lib/libc.a (I was in a hurry;
Xbesides, it's easily kept up to date).
X
XA good part of the motivation for doing this was to release
Xan example of working shared library code. The import
Xmechanism for shared libraries used here should be a good 
Xexample of how to import external functions (here we
Ximport errno, strlen, and writev).
X
XThere's one bit of minor customization needed --- if you
Xwant the shared part of the library to be somewhere other
Xthan /usr/local/shlib, you need to edit the #target
Xdirective to point at the correct location.
X
XThis whole package is in the public domain. I'd appreciate
Xsome mention if you use it in a package you distribute,
Xthough...
X
X--John Coolidge
Xcoolidge at cs.uiuc.edu
SHAR_EOF
chmod 0644 README || echo "restore of README fails"
if [ $TOUCH = can ]
then
    touch -am 0814125290 README
fi
# ============= Makefile ==============
sed 's/^X//' << 'SHAR_EOF' > Makefile &&
X# Makefile for liberr_s, a shared library implementation of perror and
X# sys_errlist/sys_nerr.
X#
X# John L. Coolidge, coolidge at cs.uiuc.edu                       8/14/90
X
X# Public domain (I'd appreciate a mention if you use it, though)
X
X
XSHELL=/bin/sh
X
Xall: liberr_s
X
Xliberr_s: perror.o errlist.o pointer.o
X       mkshlib -s liberr.spec -h liberr_s.a -t liberr_s        
X
Xerrlist.c: create_errlist /lib/libc_s.a
X# Grab sys_errlist out of errlst.o (this makes us compatible with future
X# versions --- just remake the library).
X       ar x /lib/libc_s.a errlst.o
X       $(SHELL) create_errlist
X       rm errlst.o
X
Xclean:
X       rm -f *.o
X       rm -f liberr_s.a liberr_s errlist.c
X
Xperror.c: shared.h
Xerrlist.c: shared.h
SHAR_EOF
chmod 0644 Makefile || echo "restore of Makefile fails"
if [ $TOUCH = can ]
then
    touch -am 0814124490 Makefile
fi
# ============= create_errlist ==============
sed 's/^X//' << 'SHAR_EOF' > create_errlist &&
X#! /bin/sh
X
X# create_errlist: Munch the strings table of errlst.o and produce a
X#              file errlst.c which implements the same strings.
X# Intended to allow for shared library implementation under A/UX
X
X# John L. Coolidge, coolidge at cs.uiuc.edu                       8/14/90
X
X# Public domain (I'd appreciate a mention if you use it, though)
X
Xstrings errlst.o | awk -F% '\
XBEGIN {$num = 0; printf "char * sys_errlist[]={\n"}\
X{printf "\"%s\",\n",$1; $num = $num+1}\
XEND {printf "\"\"}; \n\nint sys_nerr = %d;\n",$num }
X' | sed 's/gError 0/Error 0/' >>errlist.c
SHAR_EOF
chmod 0755 create_errlist || echo "restore of create_errlist fails"
if [ $TOUCH = can ]
then
    touch -am 0814124490 create_errlist
fi
# ============= liberr.spec ==============
sed 's/^X//' << 'SHAR_EOF' > liberr.spec &&
X## liberr.spec: mkshlib specification file for shared liberr_s
X## Intended to allow for shared library implementation under A/UX
X
X## John L. Coolidge, coolidge at cs.uiuc.edu                       8/14/90
X## Public domain (I'd appreciate a mention if you use it, though)
X
X## Use the lowest addresses in user shared lib space for now.
X#address .text 0x48000000
X#address .data 0x48040000
X
X#target /usr/local/shlib/liberr_s
X
X#branch
X       perror  1
X
X#objects
X       perror.o
X       errlist.o
X       pointer.o
X
X#init perror.o
X       errno   _perror_errno
X       strlen  _perror_strlen
X       writev  _perror_writev
SHAR_EOF
chmod 0644 liberr.spec || echo "restore of liberr.spec fails"
if [ $TOUCH = can ]
then
    touch -am 0814124490 liberr.spec
fi
# ============= perror.c ==============
sed 's/^X//' << 'SHAR_EOF' > perror.c &&
X/*
X * perror.c: implement the perror(3) library function.
X * Intended to allow for shared library implementation under A/UX
X *
X * John L. Coolidge, coolidge at cs.uiuc.edu                      8/14/90
X *
X * Public domain (I'd appreciate a mention if you use it, though)
X */
X
X#include <sys/types.h>
X#include <sys/uio.h>
X
X#include "shared.h"
X
Xextern char* sys_errlist[];
Xextern int sys_nerr;
X
Xextern int errno;
Xextern int strlen();
Xextern void writev();
X
Xperror(char* s)
X{
X       struct iovec errvec[4];
X       char* errmsg = sys_errlist[errno];
X       if( errno > sys_nerr ) {
X               errvec[2].iov_base = "Unknown error";
X               errvec[2].iov_len = 13;
X       }
X       else {
X               errvec[2].iov_base = (caddr_t)errmsg;
X               errvec[2].iov_len = strlen(errmsg);
X       }
X       errvec[3].iov_base = "\n";
X       errvec[3].iov_len = 1;
X       if( !s ) writev(2, errvec+2, 2);
X       else {
X               errvec[0].iov_base = (caddr_t)s;
X               errvec[0].iov_len = strlen(s);
X               errvec[1].iov_base = ": ";
X               errvec[1].iov_len = 2;
X               writev(2, errvec, 4);
X       }
X}
SHAR_EOF
chmod 0644 perror.c || echo "restore of perror.c fails"
if [ $TOUCH = can ]
then
    touch -am 0814124490 perror.c
fi
# ============= pointer.c ==============
sed 's/^X//' << 'SHAR_EOF' > pointer.c &&
X/*
X * pointer.c: Declaration of external pointers for perror.
X * Intended to allow for shared library implementation under A/UX
X *
X * John L. Coolidge, coolidge at cs.uiuc.edu                       8/14/90
X *
X * Public domain (I'd appreciate a mention if you use it, though)
X */
X
X#include "shared.h"
X
Xvoid errno=0;
Xvoid strlen=0;
Xvoid writev=0;
SHAR_EOF
chmod 0644 pointer.c || echo "restore of pointer.c fails"
if [ $TOUCH = can ]
then
    touch -am 0814124490 pointer.c
fi
# ============= shared.h ==============
sed 's/^X//' << 'SHAR_EOF' > shared.h &&
X/*
X * shared.h: Redefine external symbols for importing.
X * Intended to allow for shared library implementation under A/UX
X *
X * John L. Coolidge, coolidge at cs.uiuc.edu                       8/14/90
X *
X * Public domain (I'd appreciate a mention if you use it, though)
X */
X
X
X#define errno (*_perror_errno)
X#define strlen (*_perror_stren)
X#define writev (*_perror_writev)
SHAR_EOF
chmod 0644 shared.h || echo "restore of shared.h fails"
if [ $TOUCH = can ]
then
    touch -am 0814124490 shared.h
fi
exit 0

--------------------------------------------------------------------------
John L. Coolidge     Internet:coolidge at cs.uiuc.edu   UUCP:uiucdcs!coolidge
Of course I don't speak for the U of I (or anyone else except myself)
Copyright 1990 John L. Coolidge. Copying allowed if (and only if) attributed.
You may redistribute this article if and only if your recipients may as well.



More information about the Comp.unix.aux mailing list