summaryrefslogtreecommitdiff
path: root/setup/elflib.inc
blob: 0611c7d48dbbfeb962e50ad6947aada5ccc24800 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * These routines are used to change the contects of various structures
 * stored inside the modules
 */
/*
 *
 * This file is part of Open Sound System.
 *
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * This this source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 */

#if !defined(ELF64) && !defined(ELF32)
#  if defined(sparc) || defined(__x86_64__)
#     define ELF64
#  else
#error here
#     define ELF32
#  endif
#endif

#undef Elf_Ehdr
#undef Elf_Shdr
#undef Elf_Sym

#ifdef ELF32
#define Elf_Ehdr Elf32_Ehdr
#define Elf_Shdr Elf32_Shdr
#define Elf_Sym Elf32_Sym

static int
valid_elf_file (Elf_Ehdr * hdr)
{
  if (hdr->e_ident[0] != 0x7f)
    return 0;
  if (hdr->e_ident[1] != 'E')
    return 0;
  if (hdr->e_ident[2] != 'L')
    return 0;
  if (hdr->e_ident[3] != 'F')
    return 0;
  if (hdr->e_ident[EI_CLASS] != ELFCLASS32)
    {
      fprintf (stderr, "EI_CLASS=%x\n", hdr->e_ident[EI_CLASS]);
      return 0;
    }
/* Check for x86 (LSB) and PPC (MSB) data format */
  if ((hdr->e_ident[EI_DATA] != ELFDATA2LSB) &&
      (hdr->e_ident[EI_DATA] != ELFDATA2MSB))
    {
      fprintf (stderr, "EI_DATA=%x\n", hdr->e_ident[EI_DATA]);
      return 0;
    }
  if (hdr->e_ident[EI_VERSION] != EV_CURRENT)
    {
      fprintf (stderr, "EI_VERSION=%x (%x)\n", hdr->e_ident[EI_VERSION],
	       EV_CURRENT);
      return 0;
    }
  if (hdr->e_type != ET_REL && hdr->e_type != ET_DYN)
    {
      fprintf (stderr, "e_type=%x\n", hdr->e_type);
      return 0;
    }
/* Check for x86 and PPC machine type */
  if ((hdr->e_machine != EM_386) && (hdr->e_machine != EM_PPC)
      && (hdr->e_machine != EM_SPARC))
    {
      fprintf (stderr, "e_machine=%x\n", hdr->e_machine);
      return 0;
    }

  if (hdr->e_version != EV_CURRENT)
    {
      fprintf (stderr, "e_version=%x (%x)\n", hdr->e_version, EV_CURRENT);
      return 0;
    }
  if (hdr->e_ehsize != sizeof (*hdr))
    {
      fprintf (stderr, "e_ehsize=%x (%x)\n", hdr->e_ehsize, sizeof (*hdr));
      return 0;
    }

  return 1;
}
#else
/* Elf64 */
#define Elf_Ehdr Elf64_Ehdr
#define Elf_Shdr Elf64_Shdr
#define Elf_Sym Elf64_Sym
#define locate_symbol locate_symbol64

#define elf_read_datasym elf_read_datasym64
#define valid_elf_file valid_elf_file64

static int
valid_elf_file (Elf64_Ehdr * hdr)
{
  if (hdr->e_ident[0] != 0x7f)
    return 0;
  if (hdr->e_ident[1] != 'E')
    return 0;
  if (hdr->e_ident[2] != 'L')
    return 0;
  if (hdr->e_ident[3] != 'F')
    return 0;
  if (hdr->e_ident[EI_CLASS] != ELFCLASS64)
    return 0;
  if (hdr->e_ident[EI_VERSION] != EV_CURRENT)
    return 0;
  if (hdr->e_type != ET_REL)
    return 0;
#ifdef sparc
/* Check for Sparc machine type */
  if ((hdr->e_machine != EM_SPARCV9))
    {
      fprintf (stderr, "e_machine=%x\n", hdr->e_machine);
      return 0;
    }
#else
/* Check for AMD64 (LSB) data format */
  if (hdr->e_ident[EI_DATA] != ELFDATA2LSB)
    return 0;
/* Check for x86 and PPC machine type */
#ifndef EM_X86_64
#define EM_X86_64 0		/* Dummy */
#endif
  if ((hdr->e_machine != EM_ALPHA) && (hdr->e_machine != EM_X86_64))
    return 0;
#endif
  if (hdr->e_version != EV_CURRENT)
    return 0;
  if (hdr->e_ehsize != sizeof (*hdr))
    return 0;

  return 1;
}
#endif

#ifdef NEEDS_LOCATE_SYMBOL
static char *
locate_symbol (char *buffer, int len, char *symname)
{
  Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  Elf_Shdr *shdr;
  Elf_Sym *sym = NULL;
  char *strtab = NULL, *ptr;
  int symsize = 0;
  int i;

  shdr = (Elf_Shdr *) & buffer[ehdr->e_shoff];

  for (i = 0; i < ehdr->e_shnum; i++)
    {

      switch (shdr[i].sh_type)
	{
	case SHT_SYMTAB:
	  sym = (Elf_Sym *) & buffer[shdr[i].sh_offset];
	  symsize = shdr[i].sh_size / shdr[i].sh_entsize;
	  break;

	case SHT_STRTAB:
#ifndef SECOND_STRTAB
	  if (strtab != NULL)
	    break;
#endif
	  strtab = &buffer[shdr[i].sh_offset];
	  break;
	}
    }

  if (sym == NULL || strtab == NULL)
    {
      fprintf (stderr, "Missing ELF symbol information\n");
      return NULL;
    }

  for (i = 0; i < symsize; i++)
    {
      char *name = &strtab[sym[i].st_name];

      if (strcmp (name, symname) == 0)
	{
	  ptr = buffer + (sym[i].st_value + shdr[sym[i].st_shndx].sh_offset);
	  return ptr;
	}
    }
  return NULL;
}
#endif

int
elf_read_datasym (char *buffer, int blen, Elf_Sym * sym, int addr,
		  char *buf, int count)
{
  Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  Elf_Shdr *shdr;
  int i;

  shdr = (Elf_Shdr *) & buffer[ehdr->e_shoff];

  i = sym->st_shndx;
  memcpy (buf, &buffer[shdr[i].sh_offset + addr], count);

  return 1;
}

#ifdef PATCH_MODULE
static int
PATCH_MODULE (char *fname)
{
  int fd;
  int l, n;
  char *lic;

  oss_license_t *oss_license;

  char elf_file[1024 * 1024];

/*
 * Some security checks to prevent running osslic by
 * mistake. That would wipe out the license information.
 */

  if (!ok)
    {
      printf ("Incorrect osslic usage\n");
      return 0;
    }

  if (fname == NULL)
    return 0;

  if ((fd = open (fname, O_RDWR, 0)) == -1)
    {
      perror (fname);
      return 0;
    }

  if ((l = read (fd, elf_file, sizeof (elf_file))) < 1
      || l == sizeof (elf_file))
    {
      perror (fname);
      fprintf (stderr, "Bad ELF file %s\n", fname);
      return 0;
    }

  if (!valid_elf_file ((Elf_Ehdr *) elf_file))
    {
      fprintf (stderr, "OSS elflib: Invalid ELF file %s\n", fname);
      return 0;
    }

  if ((lic = locate_symbol (elf_file, l, "oss_license")) == NULL)
    {
      fprintf (stderr, "File error\n");
      close (fd);
      return 0;
    }

  oss_license = (oss_license_t *) lic;

  oss_license->license_type = license_type;
  strcpy (oss_license->person, person);
  strcpy (oss_license->organization, organization);
  strcpy (oss_license->options_string, options_string);
  strcpy (oss_license->serial, serial);
  oss_license->exp_year = exp_year;
  oss_license->exp_month = exp_mon;

  if (lseek (fd, 0L, SEEK_SET) == -1)
    {
      perror (fname);
      fprintf (stderr, "Bad seek\n");
      return 0;
    }

  if ((n = write (fd, elf_file, l)) != l)
    {
      perror (fname);
      fprintf (stderr, "Writing ELF file %s failed\n", fname);
      return 0;
    }

  close (fd);
  return 1;
}
#endif

#ifdef ELF_LOAD_SYMTAB

typedef struct
{
  char name[64];
  unsigned long addr;
} oss_symbol_t;

#define MAX_MODULES 100
static oss_symbol_t module_table[MAX_MODULES];
static int n_module_table = 0;

typedef void (*elf_callback_t) (char *buffer, int blen, Elf_Sym * sym,
				char *name, int addr);
static int
list_symbols (char *buffer, int len, char *prefix, elf_callback_t callback)
{
  Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  Elf_Shdr *shdr;
  Elf_Sym *sym = NULL;
  char *strtab = NULL;
  int symsize = 0;
  int i;
  int shstrndx = -1;
  int found = 0;

  shdr = (Elf_Shdr *) & buffer[ehdr->e_shoff];
  shstrndx = ehdr->e_shstrndx;

  if (elf_verbose > 1)
    printf ("e_shstrndx=%d\n", shstrndx);

  for (i = 0; i < ehdr->e_shnum; i++)
    {
      switch (shdr[i].sh_type)
	{
	case SHT_SYMTAB:
	  sym = (Elf_Sym *) & buffer[shdr[i].sh_offset];
	  symsize = shdr[i].sh_size / shdr[i].sh_entsize;
	  if (elf_verbose > 1)
#ifdef ELF32
	    printf ("ELF symtab at 0x%x\n", shdr[i].sh_offset);
#else
	    printf ("ELF symtab at 0x%lx\n", shdr[i].sh_offset);
#endif
	  break;

	case SHT_STRTAB:
	  if (i == shstrndx)	/* Ignore section header symbol table */
	    break;
	  strtab = &buffer[shdr[i].sh_offset];
	  if (elf_verbose > 1)
#ifdef ELF32
	    printf ("ELF strtab at 0x%x (%d) \n", shdr[i].sh_offset, i);
#else
	    printf ("ELF strtab at 0x%lx (%d) \n", shdr[i].sh_offset, i);
#endif
	  break;
	}
    }

  if (sym == NULL || strtab == NULL)
    {
      fprintf (stderr, "Missing ELF symbol information\n");
      return 0;
    }

  for (i = 0; i < symsize; i++)
    {
      char *name = &strtab[sym[i].st_name];

      if (elf_verbose > 2)
	printf ("Sym %d '%s'\n", sym[i].st_name, name);

      if (strncmp (name, prefix, strlen (prefix)) == 0)
	{
	  oss_symbol_t *m;

	  if (callback != NULL)
	    {
	      found = 1;
	      callback (buffer, len, &sym[i], name, sym[i].st_value);
	      continue;
	    }

	  m = &module_table[n_module_table++];
	  strcpy (m->name, name);
	  m->addr = sym[i].st_value;
	}
    }
  return found;
}

int
ELF_LOAD_SYMTAB (char *filename, char *prefix, elf_callback_t callback)
{
  char buffer[4 * 1024 * 1024];
  int fd, len;
  Elf_Ehdr *hdr = (Elf_Ehdr *) buffer;

  if ((fd = open (filename, O_RDONLY, 0)) == -1)
    {
      perror (filename);
      return 0;
    }

  if ((len = read (fd, buffer, sizeof (buffer))) <= 0)
    {
      perror (filename);
      close (fd);
      return 0;
    }

  if (len >= sizeof (buffer))
    {
      fprintf (stderr, "%s is too large\n", filename);
      close (fd);
      return 0;
    }

  if (len < sizeof (*hdr))
    {
      fprintf (stderr, "%s is too short (%d)\n", filename, len);
      close (fd);
      return 0;
    }

  if (!valid_elf_file (hdr))
    {
      fprintf (stderr, "%s is not a valid ELF object\n", filename);
      close (fd);
      return 0;
    }

  if (!(list_symbols (buffer, len, prefix, callback)))
    {
      if (elf_verbose > 1)
	fprintf(stderr, "%s does not contain %s symbol\n", filename, prefix);
      close (fd);
      return 0;
    }

  close (fd);
  return 1;
}
#endif