Shared Object Lab - Introduction to System Software | CS 0449, Lab Reports of Computer Science

Material Type: Lab; Class: INTRO TO SYSTEMS SOFTWARE; Subject: Computer Science; University: University of Pittsburgh; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 09/02/2009

koofers-user-7u0
koofers-user-7u0 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 0449 Shared Object Lab
Background
A Shared Object is an executable file that acts as a shared library of functions. Dynamic linking provides a
way for a process to call a function that is not part of its executable code. The executable code for the
function is located in a .so file, which contains one or more functions that are compiled, linked, and
stored separately from the processes that use them. Shared objects also facilitate the sharing of data
and resources. Multiple applications can simultaneously access the contents of a single copy of a shared
object in memory.
Dynamic linking differs from static linking in that it allows an executable file to include only the
information needed at run time to locate the executable code for a shared object function. In static
linking, the linker gets all of the referenced functions from the static link library and places it with your
code into your executable.
Procedure
1. Login via SSH to unixs.cis.pitt.edu
2. Create a directory in your private directory (mkdir private/lab3). Change directories to it.
3. The first step is to make a library. Let us imagine we are writing our own string library. One
function that we might want to provide is string copy. Edit a file in your favorite text editor, or
pico like:
pico mystr.c
4. Now enter the code necessary to make a string copy function:
void my_strcpy(char *dest, char *src)
{
while(*dest++ = *src++);
}
5. Save the file
6. We now need to compile this into a library. The first step is to invoke the compiler
independently of the linker. We can do this by providing the -c option to gcc. Also, since we do
not know the address in memory where the shared object will be loaded, we need to tell gcc to
create position-independent code via the -fPIC option:
pf3
pf4

Partial preview of the text

Download Shared Object Lab - Introduction to System Software | CS 0449 and more Lab Reports Computer Science in PDF only on Docsity!

CS 0449 – Shared Object Lab

Background

A Shared Object is an executable file that acts as a shared library of functions. Dynamic linking provides a way for a process to call a function that is not part of its executable code. The executable code for the function is located in a .so file, which contains one or more functions that are compiled, linked, and stored separately from the processes that use them. Shared objects also facilitate the sharing of data and resources. Multiple applications can simultaneously access the contents of a single copy of a shared object in memory.

Dynamic linking differs from static linking in that it allows an executable file to include only the information needed at run time to locate the executable code for a shared object function. In static linking, the linker gets all of the referenced functions from the static link library and places it with your code into your executable.

Procedure

  1. Login via SSH to unixs.cis.pitt.edu
  2. Create a directory in your private directory (mkdir private/lab3). Change directories to it.
  3. The first step is to make a library. Let us imagine we are writing our own string library. One function that we might want to provide is string copy. Edit a file in your favorite text editor, or pico like:

pico mystr.c

  1. Now enter the code necessary to make a string copy function:

void my_strcpy(char *dest, char src) { while(dest++ = *src++); }

  1. Save the file
  2. We now need to compile this into a library. The first step is to invoke the compiler independently of the linker. We can do this by providing the -c option to gcc. Also, since we do not know the address in memory where the shared object will be loaded, we need to tell gcc to create position-independent code via the -fPIC option:

gcc -fPIC -c mystr.c

  1. This creates a mystr.o object file. We can now invoke the linker separately, telling it to create a shared object file with the -shared flag. The -soname option allows us to specify the name of the output file, as well as the version number, which is placed after the .so extension. Finally, -lc tells the linker to also link in the functions from libc.

ld -shared -soname libmystr.so.1 -o libmystr.so.1.0 -lc mystr.o

  1. We want this to be accessible via mystr.so without any version number, so we need to make a symbolic link to the file with a new name. A symbolic name is much like a shortcut in Windows.

ln -s libmystr.so.1.0 libmystr.so

  1. Finally, we normally want to install libraries to the standard place on a UNIX or Linux system (/lib or /usr/lib) but we cannot do that on unixs. Instead, we can tell Linux that this directory is an appropriate place to look for libraries by issuing the command:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

  1. We now need to write a program to use our new function. The function to open a shared object is dlopen. Read the man page to learn a bit more about it:

man dlopen

  1. Make a file named main.c with the following contents:

#include <stdio.h> #include <dlfcn.h>

int main() { void handle; void (my_str_copy)(char *, char *); char *error; handle = dlopen("libmystr.so", RTLD_LAZY); if(!handle) { //handle == NULL printf("%s\n", dlerror()); //dlerror gives us a string with the error exit(1); } dlerror(); // Clear any existing error my_str_copy = dlsym(handle, "my_strcpy"); //lookup the function by name if ((error = dlerror()) != NULL) {

gzip USERNAME_lab3.tar cp USERNAME_lab3.tar.gz ~dsk6/incoming/449/lab