aboutsummaryrefslogtreecommitdiff
path: root/unix/shlib/edsym-ssol.c
blob: 8e20c26c7af150ddd074c014cbcaf99bbffd932e (plain) (blame)
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
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <nlist.h>
#include <libelf.h>

/*
 * EDSYM -- Edit the symbol table of a process or object module which uses
 * the IRAF shared library.  (Sun Solaris version July94).
 *
 * Usage:	edsym file shimage [flags]
 *
 *	-d	debug: show symbol table being edited
 *	-k	keep: do not omit "uninteresting" symbols
 *	-n	noact: do not modify any files
 *	-t	delete symbols pointing into transfer vector
 *	-T	delete all shared image symbols, keeping only client symbols
 */


#define	VHDRSIZE	0x1c
#define	v_end		vec[4]
#define	IS_TVECT(a)	((a)>=(vshlib+VHDRSIZE)&&(a)<vshend)
#define	max(a,b)	(((a)>=(b))?(a):(b))

static	int debug = 0;			/* print symbol table */
static	int noact = 0;			/* do not modify any files */
static	int omit_tv = 0;		/* omit transfer vector symbols */
static	int omit_shsym = 0;		/* omit all shlib symbols */
static	int keep_sym = 0;		/* do not omit uninteresting symbols */

extern	char *malloc();


/* EDSYM -- Edit the symbol table of a process which uses the IRAF shared
 * library.  For each symbol found which points to a location in the shared
 * library transfer vector, add a V prefix to the symbol name, and add a
 * symbol with the old name pointing to the actual function in the shared
 * image.  This is desirable before runtime debugging of processes linked
 * with the shared library.
 */
main (argc, argv)
int argc;
char *argv[];
{
        register Elf32_Phdr *phdr;
        register Elf32_Ehdr *ehdr;
        register Elf32_Shdr *shdr;
	register Elf32_Sym *sym;
	unsigned vshlib, vshend, vsize;
	char *fname, *shlib, *buf, *ip, *op;
	int version, fd, fd_sh, nsyms, arg;
	char *out, shpath[256];
	int offset, nbytes;
	struct nlist nl[3];
	unsigned ushlib[8];
	unsigned *vec, *epa;
        Elf *elf, *elf_sh;
        Elf_Scn *scn;

	/* Process arguments.  This version of edsym does not support all of
	 * the following arguments.
	 */
	if (argc < 3) {
	    fprintf (stderr, "Usage: edsym <file> <shlib> [-dkntT]\n");
	    exit (0);
	} else {
	    fname = argv[1];
	    shlib = argv[2];
	    for (arg=3;  arg < argc;  arg++) {
		if (argv[arg][0] != '-')
		    continue;
		for (ip=argv[arg]+1;  *ip;  ip++) {
		    switch (*ip) {
		    case 'd':		/* print symbols */
			debug++;
			break;
		    case 'k':		/* do not omit uninteresting symbols */
			keep_sym++;
			break;
		    case 'n':		/* do not edit object file */
			noact++;
			break;
		    case 't':		/* omit transfer vector symbols */
			omit_tv++;
			break;
		    case 'T':		/* omit all shlib symbols */
			omit_shsym++;
			break;
		    default:
			fprintf (stderr, "edsym: unknown switch -%c\n", *ip);
		    }
		}
	    }
		
	}

	/* Open the file to be edited. */
	if ((fd = open (fname, 2)) < 0) {
	    fprintf (stderr, "edsym: cannot open file %s\n", fname);
	    exit (1);
	}

	elf_version (EV_CURRENT);
	elf = elf_begin (fd, ELF_C_READ, NULL);
	if (!elf) {
	    fprintf (stderr, "edsym: not an ELF format executable\n");
	    exit (2);
	}

	/* Read file header.  */
	ehdr = elf32_getehdr (elf);
	if (!ehdr) {
	    fprintf (stderr, "edsym: cannot read file header\n");
	    exit (3);
	}

	/* Read program header for text segment, which we assume to be the
	 * first loadable program segment.
	 */
	phdr = elf32_getphdr (elf);
	if (ehdr->e_phnum <= 0 || !phdr) {
	    printf ("cannot read program header\n");
	    exit (4);
	}

	/* Get the ushlib vector from the file being edited.
	 */
	nl[0].n_name = "ushlib_";
	nl[1].n_name = NULL;
	if (nlist (fname, nl) != 0) {
	    fprintf (stderr, "cannot read name list from %s\n", fname);
	    exit (4);
	}

	offset = (unsigned)nl[0].n_value - phdr->p_vaddr + phdr->p_offset;
	lseek (fd, offset, L_SET);
	if (read (fd, (char *)ushlib, sizeof(ushlib)) != sizeof(ushlib)) {
	    fprintf (stderr, "cannot read shared image %s\n", fname);
	    exit (9);
	}

	version = ushlib[0];
	vshlib = ushlib[2];
	vshend = ushlib[3];
	vsize = vshend - vshlib;

	/* Read transfer vector from shared image.
	 */

	/* Use the correct version of the shared image. */
	for (ip=shlib, out=op=shpath;  *op = *ip++;  op++)
	    if (*op == '/')
		out = op + 1;
	if (strcmp (out, "S.e") == 0) {
	    sprintf (out, "S%d.e", version);
	    shlib = shpath;
	}

	/* Open the file to be edited. */
	if ((fd_sh = open (shlib, 0)) < 0) {
	    fprintf (stderr, "edsym: cannot open file %s\n", shlib);
	    exit (1);
	}

	elf_sh = elf_begin (fd_sh, ELF_C_READ, NULL);
	if (!elf_sh) {
	    fprintf (stderr, "edsym: not an ELF format executable\n");
	    exit (2);
	}

	/* Read file header.  */
	ehdr = elf32_getehdr (elf_sh);
	if (!ehdr) {
	    fprintf (stderr, "edsym: cannot read file header\n");
	    exit (3);
	}

	/* Read program header for text segment, which we assume to be the
	 * first loadable program segment.
	 */
	phdr = elf32_getphdr (elf_sh);
	if (ehdr->e_phnum <= 0 || !phdr) {
	    printf ("cannot read program header\n");
	    exit (4);
	}

	if (!(vec = (unsigned *) malloc (vsize))) {
	    fprintf (stderr, "out of memory\n");
	    exit (10);
	}
	offset = vshlib - phdr->p_vaddr + phdr->p_offset;
	lseek (fd_sh, offset, L_SET);
	if (read (fd_sh, vec, vsize) != vsize) {
	    fprintf (stderr,
		"cannot read transfer vector from %s\n", shlib);
	    exit (11);
	}

	/* Locate symbol table section in file to be edited. */
	scn = NULL;
	while (scn = elf_nextscn(elf,scn)) {
	    shdr = elf32_getshdr (scn);
	    if (shdr->sh_type == SHT_SYMTAB)
		break;
	}
	if (!scn) {
	    fprintf (stderr, "edsym: file %s has no symbol table\n", fname);
	    exit (12);
	}

	/* Read symbol table into memory. */
	nbytes = shdr->sh_size;
	nsyms = nbytes / sizeof(Elf32_Sym);
	if (!(buf = (char *) malloc (nbytes))) {
	    fprintf (stderr, "edsym: out of memory\n");
	    exit (13);
	}
	lseek (fd, shdr->sh_offset, 0);
	if (read (fd, buf, nbytes) != nbytes) {
	    fprintf (stderr, "edsym: cannot read symbol table\n");
	    exit (14);
	}

	/* Now edit the symbol table of the object file.
	 */
	for (sym = (Elf32_Sym *)buf;  --nsyms >= 0;  sym++) {
	    Elf32_Addr v = sym->st_value;
	    if (debug) {
		printf ("name=%d value=0x%x size=0x%x info=%o shndx=%d",
		    sym->st_name, sym->st_value, sym->st_size,
		    sym->st_info, sym->st_shndx);
	    }
	    if (sym->st_size == 0 &&
		    ELF32_ST_BIND(sym->st_info) == STB_GLOBAL && IS_TVECT(v)) {
		epa = (unsigned *)((char *)vec + (v - vshlib));
		v = (((*epa & 0x3fffff) << 10) | (*(epa+1) & 0x3ff));
		if (debug)
		    printf ("\t%x -> %x", sym->st_value, v);
		sym->st_value = (Elf32_Addr) v;
	    }
	    if (debug)
		printf ("\n");
	}

	/* Write out the edited symbol table. */
	lseek (fd, shdr->sh_offset, 0);
	if (write (fd, buf, nbytes) != nbytes) {
	    fprintf (stderr, "edsym: cannot update symbol table\n");
	    exit (7);
	}

	/* All done. */
	free (buf);
	free ((char *)vec);
	elf_end (elf_sh);
	close (fd_sh);
	elf_end (elf);
	close (fd);

	exit (0);
}