70 Chapter 3
Linker Tasks
Using Linker commands
Note that the line CFLAGS =… causes any C files to be compiled in ANSI
mode (-Aa) and causes the compiler to search for routines that are
defined in the Posix standard (-D_POSIX_SOURCE).
For details on using make refer to make(1).
Source for dynprog. Here is the source file for the dynprog
program.
dynprog.c — Example Dynamic Link and Load Program
#include <stdio.h>
#include <nlist.h>
extern void * alloc_load_space(const char * base_prog,
const char * obj_files,
const char * dest_file);
extern void * dyn_load(const char * base_prog,
unsigned int addr,
const char * obj_files,
const char * dest_file,
const char * entry_pt);
const char * base_prog = “dynprog”; /* this executable’s name
*/
const char * obj_files = “file1.o file2.o”; /* .o files to combine
*/
const char * dest_file = “dynobj.o”; /* .o file to load
*/
const char * entry_pt = “foo”; /* define entry pt name
*/
void glorp (const char *); /* prototype for local
function */
void (* foo_ptr) (); /* pointer to entry point foo
*/
void (* bar_ptr) (); /* pointer to function bar
*/
int * counter_ptr; /* pointer to variable counter [file2.c]
*/
main()
{
unsigned int addr; /* address at which to load dynobj.o */
struct nlist nl[3]; /* nlist struct to retrieve addresse */
/*
STEP 1: Allocate space for module:
*/
addr = (unsigned int) alloc_load_space(base_prog,
obj_files, dest_file);
/*
STEP 2: Load the file at the address, and get address of entry
point:
*/